# Filtered Node Similarity

This feature is in the alpha tier. For more information on feature tiers, see API Tiers.

Supported algorithm traits:

## Glossary

Directed

Directed trait. The algorithm is well-defined on a directed graph.

Directed

Directed trait. The algorithm ignores the direction of the graph.

Directed

Directed trait. The algorithm does not run on a directed graph.

Undirected

Undirected trait. The algorithm is well-defined on an undirected graph.

Undirected

Undirected trait. The algorithm ignores the undirectedness of the graph.

Heterogeneous nodes

Heterogeneous nodes fully supported. The algorithm has the ability to distinguish between nodes of different types.

Heterogeneous nodes

Heterogeneous nodes allowed. The algorithm treats all selected nodes similarly regardless of their label.

Heterogeneous relationships

Heterogeneous relationships fully supported. The algorithm has the ability to distinguish between relationships of different types.

Heterogeneous relationships

Heterogeneous relationships allowed. The algorithm treats all selected relationships similarly regardless of their type.

Weighted relationships

Weighted trait. The algorithm supports a relationship property to be used as weight, specified via the relationshipWeightProperty configuration parameter.

Weighted relationships

Weighted trait. The algorithm treats each relationship as equally important, discarding the value of any relationship weight.

## 1. Introduction

The Filtered Node Similarity algorithm is an extension to the Node Similarity algorithm. It adds support for filtering on source nodes, target nodes, or both.

## 2. Node filtering

A node filter reduces the node space for which the algorithm will produce results. Consider two similarity results: A = `(alice)-[:SIMILAR_TO]→(bob)` and B `(bob)-[:SIMILAR_TO]→(alice)`. Result A will be produced if the `(alice)` node matches the source node filter and the `(bob)` node matches the target node filter If the `(alice)` node does not match the target node filter, or the `(bob)` node does not match the source node filter, result B will not be produce.

## 3. Configuring node filters

For the standard configuration of node similarity, see Node Similarity syntax.

The source node filter is specified with the `sourceNodeFilter` configuration parameter. The target node filter is specified with the `targetNodeFilter` configuration parameter. Neither parameter is mandatory.

The node filter parameters accept one of the following:

 a single node id `sourceNodeFilter: 42` a list of node ids `sourceNodeFilter: [23, 42, 87]` a single node `MATCH (person:Person) WITH person ORDER BY person.age DESC LIMIT 1` …​ `sourceNodeFilter: n` a list of nodes `MATCH (person:Person) WHERE person.age > 35 collect(person) AS people` …​ `sourceNodeFilter: people` a single label `sourceNodeFilter: 'Person'`

## 4. Syntax

This section covers the syntax used to execute the Filtered Node Similarity algorithm in each of its execution modes. We are describing the named graph variant of the syntax. To learn more about general syntax variants, see Syntax overview.

Filtered Node Similarity syntax per mode
Run Filtered Node Similarity in stream mode on a named graph.
``````CALL gds.alpha.nodeSimilarity.filtered.stream(
graphName: String,
configuration: Map
) YIELD
node1: Integer,
node2: Integer,
similarity: Float``````
Table 2. Parameters
Name Type Default Optional Description

graphName

String

`n/a`

no

The name of a graph stored in the catalog.

configuration

Map

`{}`

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 3. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

nodeLabels

List of String

`['*']`

yes

Filter the named graph using the given node labels.

relationshipTypes

List of String

`['*']`

yes

Filter the named graph using the given relationship types.

concurrency

Integer

`4`

yes

The number of concurrent threads used for running the algorithm.

jobId

String

`Generated internally`

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

`true`

yes

If disabled the progress percentage will not be logged.

Table 4. Node Similarity specific configuration
Name Type Default Optional Description

similarityCutoff

Float

`1E-42`

yes

Lower limit for the similarity score to be present in the result. Values must be between 0 and 1.

degreeCutoff

Integer

`1`

yes

Inclusive lower bound on the node degree for a node to be considered in the comparisons. This value can not be lower than 1.

upperDegreeCutoff

Integer

`2147483647`

yes

Inclusive upper bound on the node degree for a node to be considered in the comparisons. This value can not be lower than 1.

topK

Integer

`10`

yes

Limit on the number of scores per node. The K largest results are returned. This value cannot be lower than 1.

bottomK

Integer

`10`

yes

Limit on the number of scores per node. The K smallest results are returned. This value cannot be lower than 1.

topN

Integer

`0`

yes

Global limit on the number of scores computed. The N largest total results are returned. This value cannot be negative, a value of 0 means no global limit.

bottomN

Integer

`0`

yes

Global limit on the number of scores computed. The N smallest total results are returned. This value cannot be negative, a value of 0 means no global limit.

relationshipWeightProperty

String

`null`

yes

Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted.

similarityMetric

String

`JACCARD`

yes

The metric used to compute similarity. Can be either `JACCARD`, `OVERLAP` or `COSINE`.

Table 5. Algorithm specific configuration
Name Type Default Optional Description

sourceNodeFilter

Integer or List of Integer or String

`n/a`

no

The source node filter to apply. Accepts a single node id, a List of node ids, or a single label.

targetNodeFilter

Integer or List of Integer or String

`n/a`

no

The target node filter to apply. Accepts a single node id, a List of node ids, or a single label.

Table 6. Results
Name Type Description

`node1`

Integer

Node ID of the first node.

`node2`

Integer

Node ID of the second node.

`similarity`

Float

Similarity score for the two nodes.

Run Node Similarity in stats mode on a named graph.
``````CALL gds.alpha.nodeSimilarity.filtered.stats(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
nodesCompared: Integer,
similarityPairs: Integer,
similarityDistribution: Map,
configuration: Map``````
Table 7. Parameters
Name Type Default Optional Description

graphName

String

`n/a`

no

The name of a graph stored in the catalog.

configuration

Map

`{}`

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 8. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

nodeLabels

List of String

`['*']`

yes

Filter the named graph using the given node labels.

relationshipTypes

List of String

`['*']`

yes

Filter the named graph using the given relationship types.

concurrency

Integer

`4`

yes

The number of concurrent threads used for running the algorithm.

jobId

String

`Generated internally`

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

`true`

yes

If disabled the progress percentage will not be logged.

Table 9. Node Similarity specific configuration
Name Type Default Optional Description

similarityCutoff

Float

`1E-42`

yes

Lower limit for the similarity score to be present in the result. Values must be between 0 and 1.

degreeCutoff

Integer

`1`

yes

Inclusive lower bound on the node degree for a node to be considered in the comparisons. This value can not be lower than 1.

upperDegreeCutoff

Integer

`2147483647`

yes

Inclusive upper bound on the node degree for a node to be considered in the comparisons. This value can not be lower than 1.

topK

Integer

`10`

yes

Limit on the number of scores per node. The K largest results are returned. This value cannot be lower than 1.

bottomK

Integer

`10`

yes

Limit on the number of scores per node. The K smallest results are returned. This value cannot be lower than 1.

topN

Integer

`0`

yes

Global limit on the number of scores computed. The N largest total results are returned. This value cannot be negative, a value of 0 means no global limit.

bottomN

Integer

`0`

yes

Global limit on the number of scores computed. The N smallest total results are returned. This value cannot be negative, a value of 0 means no global limit.

relationshipWeightProperty

String

`null`

yes

Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted.

similarityMetric

String

`JACCARD`

yes

The metric used to compute similarity. Can be either `JACCARD`, `OVERLAP` or `COSINE`.

Table 10. Algorithm specific configuration
Name Type Default Optional Description

sourceNodeFilter

Integer or List of Integer or String

`n/a`

no

The source node filter to apply. Accepts a single node id, a List of node ids, or a single label.

targetNodeFilter

Integer or List of Integer or String

`n/a`

no

The target node filter to apply. Accepts a single node id, a List of node ids, or a single label.

Table 11. Results
Name Type Description

preProcessingMillis

Integer

Milliseconds for preprocessing the data.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing component count and distribution statistics.

nodesCompared

Integer

The number of nodes for which similarity was computed.

similarityPairs

Integer

The number of similarities in the result.

similarityDistribution

Map

Map containing min, max, mean as well as p50, p75, p90, p95, p99 and p999 percentile values of the computed similarity results.

configuration

Map

The configuration used for running the algorithm.

Run Filtered Node Similarity in mutate mode on a named graph.
``````CALL gds.alpha.nodeSimilarity.filtered.mutate(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
mutateMillis: Integer,
postProcessingMillis: Integer,
relationshipsWritten: Integer,
nodesCompared: Integer,
similarityDistribution: Map,
configuration: Map``````
Table 12. Parameters
Name Type Default Optional Description

graphName

String

`n/a`

no

The name of a graph stored in the catalog.

configuration

Map

`{}`

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 13. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

mutateRelationshipType

String

`n/a`

no

The relationship type used for the new relationships written to the projected graph.

mutateProperty

String

`n/a`

no

The relationship property in the GDS graph to which the similarity score is written.

nodeLabels

List of String

`['*']`

yes

Filter the named graph using the given node labels.

relationshipTypes

List of String

`['*']`

yes

Filter the named graph using the given relationship types.

concurrency

Integer

`4`

yes

The number of concurrent threads used for running the algorithm.

jobId

String

`Generated internally`

yes

An ID that can be provided to more easily track the algorithm’s progress.

Table 14. Node Similarity specific configuration
Name Type Default Optional Description

similarityCutoff

Float

`1E-42`

yes

Lower limit for the similarity score to be present in the result. Values must be between 0 and 1.

degreeCutoff

Integer

`1`

yes

Inclusive lower bound on the node degree for a node to be considered in the comparisons. This value can not be lower than 1.

upperDegreeCutoff

Integer

`2147483647`

yes

Inclusive upper bound on the node degree for a node to be considered in the comparisons. This value can not be lower than 1.

topK

Integer

`10`

yes

Limit on the number of scores per node. The K largest results are returned. This value cannot be lower than 1.

bottomK

Integer

`10`

yes

Limit on the number of scores per node. The K smallest results are returned. This value cannot be lower than 1.

topN

Integer

`0`

yes

Global limit on the number of scores computed. The N largest total results are returned. This value cannot be negative, a value of 0 means no global limit.

bottomN

Integer

`0`

yes

Global limit on the number of scores computed. The N smallest total results are returned. This value cannot be negative, a value of 0 means no global limit.

relationshipWeightProperty

String

`null`

yes

Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted.

similarityMetric

String

`JACCARD`

yes

The metric used to compute similarity. Can be either `JACCARD`, `OVERLAP` or `COSINE`.

Table 15. Algorithm specific configuration
Name Type Default Optional Description

sourceNodeFilter

Integer or List of Integer or String

`n/a`

no

The source node filter to apply. Accepts a single node id, a List of node ids, or a single label.

targetNodeFilter

Integer or List of Integer or String

`n/a`

no

The target node filter to apply. Accepts a single node id, a List of node ids, or a single label.

Table 16. Results
Name Type Description

`preProcessingMillis`

Integer

Milliseconds for preprocessing the data.

`computeMillis`

Integer

Milliseconds for running the algorithm.

`mutateMillis`

Integer

Milliseconds for adding properties to the projected graph.

`postProcessingMillis`

Integer

Milliseconds for computing percentiles.

`nodesCompared`

Integer

The number of nodes for which similarity was computed.

`relationshipsWritten`

Integer

The number of relationships created.

`similarityDistribution`

Map

Map containing min, max, mean, stdDev and p1, p5, p10, p25, p75, p90, p95, p99, p100 percentile values of the computed similarity results.

`configuration`

Map

The configuration used for running the algorithm.

Run Filtered Node Similarity in write mode on a named graph.
``````CALL gds.alpha.nodeSimilarity.filtered.write(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
writeMillis: Integer,
postProcessingMillis: Integer,
nodesCompared: Integer,
relationshipsWritten: Integer,
similarityDistribution: Map,
configuration: Map``````
Table 17. Parameters
Name Type Default Optional Description

graphName

String

`n/a`

no

The name of a graph stored in the catalog.

configuration

Map

`{}`

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 18. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

nodeLabels

List of String

`['*']`

yes

Filter the named graph using the given node labels.

relationshipTypes

List of String

`['*']`

yes

Filter the named graph using the given relationship types.

concurrency

Integer

`4`

yes

The number of concurrent threads used for running the algorithm.

jobId

String

`Generated internally`

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

`true`

yes

If disabled the progress percentage will not be logged.

writeConcurrency

Integer

`value of 'concurrency'`

yes

The number of concurrent threads used for writing the result to Neo4j.

writeRelationshipType

String

`n/a`

no

The relationship type used to persist the computed relationships in the Neo4j database.

writeProperty

String

`n/a`

no

The relationship property in the Neo4j database to which the similarity score is written.

Table 19. Node Similarity specific configuration
Name Type Default Optional Description

similarityCutoff

Float

`1E-42`

yes

Lower limit for the similarity score to be present in the result. Values must be between 0 and 1.

degreeCutoff

Integer

`1`

yes

Inclusive lower bound on the node degree for a node to be considered in the comparisons. This value can not be lower than 1.

upperDegreeCutoff

Integer

`2147483647`

yes

Inclusive upper bound on the node degree for a node to be considered in the comparisons. This value can not be lower than 1.

topK

Integer

`10`

yes

Limit on the number of scores per node. The K largest results are returned. This value cannot be lower than 1.

bottomK

Integer

`10`

yes

Limit on the number of scores per node. The K smallest results are returned. This value cannot be lower than 1.

topN

Integer

`0`

yes

Global limit on the number of scores computed. The N largest total results are returned. This value cannot be negative, a value of 0 means no global limit.

bottomN

Integer

`0`

yes

Global limit on the number of scores computed. The N smallest total results are returned. This value cannot be negative, a value of 0 means no global limit.

relationshipWeightProperty

String

`null`

yes

Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted.

similarityMetric

String

`JACCARD`

yes

The metric used to compute similarity. Can be either `JACCARD`, `OVERLAP` or `COSINE`.

Table 20. Algorithm specific configuration
Name Type Default Optional Description

sourceNodeFilter

Integer or List of Integer or String

`n/a`

no

The source node filter to apply. Accepts a single node id, a List of node ids, or a single label.

targetNodeFilter

Integer or List of Integer or String

`n/a`

no

The target node filter to apply. Accepts a single node id, a List of node ids, or a single label.

Table 21. Results
Name Type Description

preProcessingMillis

Integer

Milliseconds for preprocessing data.

computeMillis

Integer

Milliseconds for running the algorithm.

writeMillis

Integer

Milliseconds for writing result data back to Neo4j.

postProcessingMillis

Integer

Milliseconds for computing percentiles.

nodesCompared

Integer

The number of nodes for which similarity was computed.

relationshipsWritten

Integer

The number of relationships created.

similarityDistribution

Map

Map containing min, max, mean, stdDev and p1, p5, p10, p25, p75, p90, p95, p99, p100 percentile values of the computed similarity results.

configuration

Map

The configuration used for running the algorithm.

## 5. Examples

In this section we will show examples of running the Filtered Node Similarity algorithm on a concrete graph. The intention is to illustrate what the results look like and to provide a guide in how to make use of the algorithm in a real setting. We will do this on a small knowledge graph of a handful nodes connected in a particular pattern. The example graph looks like this:

The following Cypher statement will create the example graph in the Neo4j database:
``````CREATE
(alice:Person:Singer {name: 'Alice'}),
(bob:Person:Singer {name: 'Bob'}),
(carol:Person:Singer {name: 'Carol'}),
(dave:Person {name: 'Dave'}),
(eve:Person:Singer {name: 'Eve'}),
(guitar:Instrument {name: 'Guitar'}),
(synth:Instrument {name: 'Synthesizer'}),
(bongos:Instrument {name: 'Bongos'}),
(trumpet:Instrument {name: 'Trumpet'}),

(alice)-[:LIKES]->(guitar),
(alice)-[:LIKES]->(synth),
(alice)-[:LIKES {strength: 0.5}]->(bongos),
(bob)-[:LIKES]->(guitar),
(bob)-[:LIKES]->(synth),
(carol)-[:LIKES]->(bongos),
(dave)-[:LIKES]->(guitar),
(dave)-[:LIKES]->(synth),
(dave)-[:LIKES]->(bongos);``````

This bipartite graph has two node sets, Person nodes and Instrument nodes. Some of the Person nodes are also singers. The two node sets are connected via LIKES relationships. Each relationship starts at a Person node and ends at an Instrument node.

The Filtered Node Similarity algorithm will only compute similarity for nodes that have a degree of at least 1. Eve hence shall not be included in the results as her degree is zero.

 In the examples below we will use named graphs and native projections as the norm. However, Cypher projections can also be used.
The following statement will project the graph and store it in the graph catalog.
``````CALL gds.graph.project(
'myGraph',
['Person', 'Instrument','Singer'],
{
LIKES: {
properties: {
strength: {
property: 'strength',
defaultValue: 1.0
}
}
}
}
);``````

In the following examples we will demonstrate the usage of the Filtered Node Similarity algorithm on this graph. In particular, we will apply the `sourceNodeFilter` and `targetNodeFilter` filters to limit our similarity search to strictly Person nodes that also have the Singer label.

### 5.1. Memory Estimation

First off, we will estimate the cost of running the algorithm using the `estimate` procedure. This can be done with any execution mode. We will use the `write` mode in this example. Estimating the algorithm is useful to understand the memory impact that running the algorithm on your graph will have. When you later actually run the algorithm in one of the execution modes the system will perform an estimation. If the estimation shows that there is a very high probability of the execution going over its memory limitations, the execution is prohibited. To read more about this, see Automatic estimation and execution blocking.

For more details on `estimate` in general, see Memory Estimation.

The following will estimate the memory requirements for running the algorithm:
``````CALL gds.alpha.nodeSimilarity.filtered.write.estimate('myGraph', {
writeRelationshipType: 'SIMILAR',
writeProperty: 'score',
sourceNodeFilter:'Singer',
targetNodeFilter:'Singer'
})
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory``````
Table 22. Results
nodeCount relationshipCount bytesMin bytesMax requiredMemory

9

9

2384

2600

"[2384 Bytes ... 2600 Bytes]"

### 5.2. Stream

In the `stream` execution mode, the algorithm returns the similarity score for each relationship. This allows us to inspect the results directly or post-process them in Cypher without any side effects.

For more details on the `stream` mode in general, see Stream.

The following will run the algorithm, and stream the results:
``````CALL gds.alpha.nodeSimilarity.filtered.stream('myGraph', {sourceNodeFilter:'Singer' , targetNodeFilter:'Singer' } )
YIELD node1, node2, similarity
RETURN gds.util.asNode(node1).name AS Person1, gds.util.asNode(node2).name AS Person2, similarity
ORDER BY similarity DESCENDING, Person1, Person2``````
Table 23. Results
Person1 Person2 similarity

"Alice"

"Bob"

0.6666666666666666

"Bob"

"Alice"

0.6666666666666666

"Alice"

"Carol"

0.3333333333333333

"Carol"

"Alice"

0.3333333333333333

### 5.3. Stats

In the `stats` execution mode, the algorithm returns a single row containing a summary of the algorithm result. This execution mode does not have any side effects. It can be useful for evaluating algorithm performance by inspecting the `computeMillis` return item. In the examples below we will omit returning the timings. The full signature of the procedure can be found in the syntax section.

For more details on the `stats` mode in general, see Stats.

The following will run the Node Similarity algorithm with the specified filters sand return the result in form of statistical and measurement values
``````CALL gds.alpha.nodeSimilarity.filtered.stats('myGraph', {sourceNodeFilter:'Singer' , targetNodeFilter:'Singer' } )
YIELD nodesCompared, similarityPairs``````
Table 24. Results
nodesCompared similarityPairs

3

4

### 5.4. Mutate

The `mutate` execution mode extends the `stats` mode with an important side effect: updating the named graph with a new relationship property containing the similarity score for that relationship. The name of the new property is specified using the mandatory configuration parameter `mutateProperty`. The result is a single summary row, similar to `stats`, but with some additional metrics. The `mutate` mode is especially useful when multiple algorithms are used in conjunction.

For more details on the `mutate` mode in general, see Mutate.

The following will run the algorithm, and write back results to the in-memory graph:
``````CALL gds.alpha.nodeSimilarity.filtered.mutate('myGraph',{
mutateRelationshipType: 'SIMILAR',
mutateProperty: 'score',
sourceNodeFilter:'Singer',
targetNodeFilter:'Singer'
})

YIELD nodesCompared, relationshipsWritten``````
Table 25. Results
nodesCompared relationshipsWritten

3

4

As can be seen in the results, the number of created relationships is the same as the number of rows in the streaming example.

 The relationships that are produced by the mutation are always directed, even if the input graph is undirected. If `a → b` is topK for `a` and symmetrically `b → a` is topK for `b` (or both `a → b` and `b → a` are topN), it appears as though an undirected relationship is produced. However, they are just two directed relationships that have been independently produced.

### 5.5. Write

The `write` execution mode for each pair of nodes creates a relationship with their similarity score as a property to the Neo4j database. The type of the new relationship is specified using the mandatory configuration parameter `writeRelationshipType`. The name of the new property is specified using the mandatory configuration parameter `writeProperty`. The result is a single summary row, similar to `stats`, but with some additional metrics.

For more details on the `write` mode in general, see Write.

The following will run the algorithm, and write back results:
``````CALL gds.alpha.nodeSimilarity.filtered.write('myGraph',{
writeRelationshipType: 'SIMILAR',
writeProperty: 'score',
sourceNodeFilter:'Singer',
targetNodeFilter:'Singer'
})
YIELD nodesCompared, relationshipsWritten``````
Table 26. Results
nodesCompared relationshipsWritten

3

4

As we can see from the results, the number of created relationships is equal to the number of rows in the streaming example.

 The relationships that are written are always directed, even if the input graph is undirected. If `a → b` is topK for `a` and symmetrically `b → a` is topK for `b` (or both `a → b` and `b → a` are topN), it appears as though an undirected relationship is written. However, they are just two directed relationships that have been independently written.