Filtered KNearest Neighbors
This feature is in the alpha tier. For more information on feature tiers, see API Tiers.
1. Introduction
The Filtered KNearest Neighbors algorithm extends our popular KNearest Neighbors algorithm with filtering on source nodes, target nodes or both.
1.1. Types of Filtering
We are in a world of source nodes, target nodes and the relationship between them that hold a similarity score or distance.
Just like for the KNearest Neighbors algorithm, output with filtering are new relationships between nodes and their knearest neighbors. Similarity scores are expressed via relationship properties.
Filtered KNearest Neighbors in addition give you control over nodes on either end of the relationships, saving you from having to filter a big result set on your own, and enabling better control over output volumes.
1.1.1. Source node filtering
For some use cases you will want to restrict the set of nodes that can act as source nodes; or the type of node that can act as source node. This is source node filtering. You want the best scoring relationships that originate from these particular nodes or this particular type of node.
A source node filter can be in either of these forms:

A set of nodes

A label

A set of nodes and a label
1.1.2. Target node filtering
Just like for source nodes, you sometimes want to restrict the set of nodes or type of node that can act as target node, i.e. target node filtering. The best scoring relationships for a given source node where the target node is from a set, or of a type.
Just like for the source node filter, a target nodes filter can be in either of these forms:

A set of nodes

A label

A set of nodes and a label
1.1.3. Seeding for target node filtering
A further use case for target node filtering is that you absolutely want to produce k results. You want to fill a fixed size bucket with relationships, you hope that there are enough high scoring relationships found by the KNearest Neighbors algorithm, but as an insurance policy we can seed your result set with arbitrary relationships to "guarantee" a full bucket of k results.
Just like the KNearest Neighbors algorithm is not guaranteed to find k results, the Filtered KNearest Neighbors algorithm is not strictly guaranteed to find k results either. But you will increase your odds massively if you employ seeing. In fact, with seeding, the only time you would not get k results is when there are not k target nodes in your graph.
Now, the quality of the arbitrary padding results is unknown. How does that square with the similarityCutoff
parameter? Here we have chosen semantics where seeding overrides similarity cutoff, and you risk getting results where the similarity score is below the cutoff  but guaranteeing that at least there are k of them.
Seeding is a boolean property you switch on or off (default).
You can mix and match source node filtering, target node filtering and seeding to achieve your goals. 
1.1.4. Configuring filters and seeding
You should consult KNearest Neighbors configuration for the standard configuration options.
The source node filter to use is specified with the sourceNodeFilter
configuration parameter. It is not mandatory.
This parameter accepts one of:
a single node id 

a list of node ids 

a single node 

a list of nodes 

a single label 

The target node filter to use are specified with the targetNodeFilter
configuration parameter. It is not mandatory.
This parameter accepts one of:
a single node id 

a list of node ids 

a single node 

a list of nodes 

a single label 

Seeding can be enabled with the seedTargetNodes
configuration parameter. It defaults to false
.
2. Syntax
This section covers the syntax used to execute the Filtered KNearest Neighbors 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.
CALL gds.alpha.knn.filtered.stream(
graphName: String,
configuration: Map
) YIELD
node1: Integer,
node2: Integer,
similarity: Float
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

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

Boolean 

yes 
If disabled the progress percentage will not be logged. 
Name  Type  Default  Optional  Description 

nodeProperties 
String or Map or List of Strings / Maps 

no 
The node properties to use for similarity computation along with their selected similarity metrics. Accepts a single property key, a Map of property keys to metrics, or a List of property keys and/or Maps, as above. See Node properties and metrics configuration for details. 
topK 
Integer 

yes 
The number of neighbors to find for each node. The Knearest neighbors are returned. This value cannot be lower than 1. 
sampleRate 
Float 

yes 
Sample rate to limit the number of comparisons per node. Value must be between 0 (exclusive) and 1 (inclusive). 
deltaThreshold 
Float 

yes 
Value as a percentage to determine when to stop early. If fewer updates than the configured value happen, the algorithm stops. Value must be between 0 (exclusive) and 1 (inclusive). 
Integer 

yes 
Hard limit to stop the algorithm after that many iterations. 

randomJoins 
Integer 

yes 
The number of random attempts per node to connect new node neighbors based on random selection, for each iteration. 
String 

yes 
The method used to sample the first 

randomSeed 
Integer 

yes 
The seed value to control the randomness of the algorithm.
Note that 
similarityCutoff 
Float 

yes 
Filter out from the list of Knearest neighbors nodes with similarity below this threshold. 
perturbationRate 
Float 

yes 
The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity. 
Name  Type  Default  Optional  Description 

sourceNodeFilter 
Integer or List of Integer or String 

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 

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

yes 
Enable seeding of target nodes. 
Name  Type  Description 


Integer 
Node ID of the first node. 

Integer 
Node ID of the second node. 

Float 
Similarity score for the two nodes. 
CALL gds.alpha.knn.filtered.stats(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
nodesCompared: Integer,
ranIterations: Integer,
didConverge: Boolean,
nodePairsConsidered: Integer,
similarityPairs: Integer,
similarityDistribution: Map,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

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

Boolean 

yes 
If disabled the progress percentage will not be logged. 
Name  Type  Default  Optional  Description 

nodeProperties 
String or Map or List of Strings / Maps 

no 
The node properties to use for similarity computation along with their selected similarity metrics. Accepts a single property key, a Map of property keys to metrics, or a List of property keys and/or Maps, as above. See Node properties and metrics configuration for details. 
topK 
Integer 

yes 
The number of neighbors to find for each node. The Knearest neighbors are returned. This value cannot be lower than 1. 
sampleRate 
Float 

yes 
Sample rate to limit the number of comparisons per node. Value must be between 0 (exclusive) and 1 (inclusive). 
deltaThreshold 
Float 

yes 
Value as a percentage to determine when to stop early. If fewer updates than the configured value happen, the algorithm stops. Value must be between 0 (exclusive) and 1 (inclusive). 
Integer 

yes 
Hard limit to stop the algorithm after that many iterations. 

randomJoins 
Integer 

yes 
The number of random attempts per node to connect new node neighbors based on random selection, for each iteration. 
String 

yes 
The method used to sample the first 

randomSeed 
Integer 

yes 
The seed value to control the randomness of the algorithm.
Note that 
similarityCutoff 
Float 

yes 
Filter out from the list of Knearest neighbors nodes with similarity below this threshold. 
perturbationRate 
Float 

yes 
The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity. 
Name  Type  Default  Optional  Description 

sourceNodeFilter 
Integer or List of Integer or String 

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 

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

yes 
Enable seeding of target nodes. 
Name  Type  Description 

ranIterations 
Integer 
Number of iterations run. 
didConverge 
Boolean 
Indicates if the algorithm converged. 
nodePairsConsidered 
Integer 
The number of similarity computations. 
preProcessingMillis 
Integer 
Milliseconds for preprocessing the data. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
postProcessingMillis 
Integer 
Milliseconds for computing similarity value 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. 
CALL gds.alpha.knn.filtered.mutate(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
mutateMillis: Integer,
postProcessingMillis: Integer,
relationshipsWritten: Integer,
nodesCompared: Integer,
ranIterations: Integer,
didConverge: Boolean,
nodePairsConsidered: Integer,
similarityDistribution: Map,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

mutateRelationshipType 
String 

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

no 
The relationship property in the GDS graph to which the similarity score is written. 
List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

yes 
An ID that can be provided to more easily track the algorithm’s progress. 
Name  Type  Default  Optional  Description 

nodeProperties 
String or Map or List of Strings / Maps 

no 
The node properties to use for similarity computation along with their selected similarity metrics. Accepts a single property key, a Map of property keys to metrics, or a List of property keys and/or Maps, as above. See Node properties and metrics configuration for details. 
topK 
Integer 

yes 
The number of neighbors to find for each node. The Knearest neighbors are returned. This value cannot be lower than 1. 
sampleRate 
Float 

yes 
Sample rate to limit the number of comparisons per node. Value must be between 0 (exclusive) and 1 (inclusive). 
deltaThreshold 
Float 

yes 
Value as a percentage to determine when to stop early. If fewer updates than the configured value happen, the algorithm stops. Value must be between 0 (exclusive) and 1 (inclusive). 
Integer 

yes 
Hard limit to stop the algorithm after that many iterations. 

randomJoins 
Integer 

yes 
The number of random attempts per node to connect new node neighbors based on random selection, for each iteration. 
String 

yes 
The method used to sample the first 

randomSeed 
Integer 

yes 
The seed value to control the randomness of the algorithm.
Note that 
similarityCutoff 
Float 

yes 
Filter out from the list of Knearest neighbors nodes with similarity below this threshold. 
perturbationRate 
Float 

yes 
The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity. 
Name  Type  Default  Optional  Description 

sourceNodeFilter 
Integer or List of Integer or String 

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 

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

yes 
Enable seeding of target nodes. 
Name  Type  Description 

ranIterations 
Integer 
Number of iterations run. 
didConverge 
Boolean 
Indicates if the algorithm converged. 
nodePairsConsidered 
Integer 
The number of similarity computations. 
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 similarity value distribution statistics. 
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. 
CALL gds.alpha.knn.filtered.write(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
writeMillis: Integer,
postProcessingMillis: Integer,
nodesCompared: Integer,
ranIterations: Integer,
didConverge: Boolean,
nodePairsConsidered: Integer,
relationshipsWritten: Integer,
similarityDistribution: Map,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

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

Boolean 

yes 
If disabled the progress percentage will not be logged. 

Integer 

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

writeRelationshipType 
String 

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

no 
The relationship property in the Neo4j database to which the similarity score is written. 
Name  Type  Default  Optional  Description 

nodeProperties 
String or Map or List of Strings / Maps 

no 
The node properties to use for similarity computation along with their selected similarity metrics. Accepts a single property key, a Map of property keys to metrics, or a List of property keys and/or Maps, as above. See Node properties and metrics configuration for details. 
topK 
Integer 

yes 
The number of neighbors to find for each node. The Knearest neighbors are returned. This value cannot be lower than 1. 
sampleRate 
Float 

yes 
Sample rate to limit the number of comparisons per node. Value must be between 0 (exclusive) and 1 (inclusive). 
deltaThreshold 
Float 

yes 
Value as a percentage to determine when to stop early. If fewer updates than the configured value happen, the algorithm stops. Value must be between 0 (exclusive) and 1 (inclusive). 
Integer 

yes 
Hard limit to stop the algorithm after that many iterations. 

randomJoins 
Integer 

yes 
The number of random attempts per node to connect new node neighbors based on random selection, for each iteration. 
String 

yes 
The method used to sample the first 

randomSeed 
Integer 

yes 
The seed value to control the randomness of the algorithm.
Note that 
similarityCutoff 
Float 

yes 
Filter out from the list of Knearest neighbors nodes with similarity below this threshold. 
perturbationRate 
Float 

yes 
The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity. 
Name  Type  Default  Optional  Description 

sourceNodeFilter 
Integer or List of Integer or String 

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 

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

yes 
Enable seeding of target nodes. 
Name  Type  Description 

ranIterations 
Integer 
Number of iterations run. 
didConverge 
Boolean 
Indicates if the algorithm converged. 
nodePairsConsidered 
Integer 
The number of similarity computations. 
preProcessingMillis 
Integer 
Milliseconds for preprocessing the data. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
writeMillis 
Integer 
Milliseconds for writing result data back to Neo4j. 
postProcessingMillis 
Integer 
Milliseconds for computing similarity value distribution statistics. 
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. 
3. Examples
In this section we will show examples of running the Filtered KNearest Neighbors algorithm on a concrete graph.
Recall that KNN can run on a graph of only nodes, without any relationships.
Consider the following graph of five disconnected Person nodes, some of whom are Vegan.
CREATE (alice:Person:Vegan {name: 'Alice', age: 24, lotteryNumbers: [1, 3], embedding: [1.0, 3.0]})
CREATE (bob:Person:Vegan {name: 'Bob', age: 73, lotteryNumbers: [1, 2, 3], embedding: [2.1, 1.6]})
CREATE (carol:Person {name: 'Carol', age: 24, lotteryNumbers: [3], embedding: [1.5, 3.1]})
CREATE (dave:Person:Vegan {name: 'Dave', age: 48, lotteryNumbers: [2, 4], embedding: [0.6, 0.2]})
CREATE (eve:Person:Vegan {name: 'Eve', age: 67, lotteryNumbers: [1, 5], embedding: [1.8, 2.7]});
In the example, we want to use the Filtered KNearest Neighbors algorithm to compare people based on either their age or a combination on all provided properties.
In the examples below we will use named graphs and native projections as the norm. However, Cypher projections can also be used. 
CALL gds.graph.project(
'myGraph',
{
Person: {
properties: ['age','lotteryNumbers','embedding']
},
Vegan: {
properties: ['age']
}
},
'*'
);
3.1. Filtering source nodes
In the stream
execution mode, the algorithm returns the similarity score for each relationship.
This allows us to inspect the results directly or postprocess them in Cypher without any side effects.
For more details on the stream
mode in general, see Stream.
CALL gds.alpha.knn.filtered.stream('myGraph', {
topK: 1,
nodeProperties: ['age'],
sourceNodeFilter: 'Vegan',
// The following parameters are set to produce a deterministic result
randomSeed: 1337,
concurrency: 1,
sampleRate: 1.0,
deltaThreshold: 0.0
})
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
Person1  Person2  similarity 

"Alice" 
"Carol" 
1.0 
"Bob" 
"Eve" 
0.14285714285714285 
"Eve" 
"Bob" 
0.14285714285714285 
"Dave" 
"Eve" 
0.05 
We use default values for the procedure configuration parameter for most parameters.
The randomSeed
and concurrency
is set to produce the same result on every invocation.
The topK
parameter is set to 1 to only return the single nearest neighbor for every node.
Notice that because Carol is not Vegan, she is not included in the result set  she was filtered out by the source node filter.
3.2. Filtering and seeding target nodes
In the stream
execution mode, the algorithm returns the similarity score for each relationship.
This allows us to inspect the results directly or postprocess them in Cypher without any side effects.
For more details on the stream
mode in general, see Stream.
CALL gds.alpha.knn.filtered.stream('myGraph', {
topK: 1,
nodeProperties: ['age'],
targetNodeFilter: 'Vegan',
seedTargetNodes: true,
similarityCutoff: 0.3,
// The following parameters are set to produce a deterministic result
randomSeed: 1337,
concurrency: 1,
sampleRate: 1.0,
deltaThreshold: 0.0
})
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
Person1  Person2  similarity 

"Carol" 
"Alice" 
1.0 
"Bob" 
"Eve" 
0.14285714285714285 
"Eve" 
"Bob" 
0.14285714285714285 
"Dave" 
"Eve" 
0.05 
"Alice" 
"Dave" 
0.04 
Here we filter for target nodes with label Vegan, and set a similarity cutoff to ensure good quality results. Normally that would mean fewer results. But we also enable seeding, which is what you do when you want to guarantee that for every node we output k neighbours. In this case seeding overrides similarity cutoff, and you see in the output that each source node has 1 result, even if they score rather poorly. We happen to know that Alice scores very highly with Carol on age similarity under normal circumstances. However, because Carol is not Vegan, she is not included in the result set  she was filtered out by the target node filter  and instead Alice is matched with Dave.
3.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.
CALL gds.alpha.knn.filtered.stats('myGraph', {
topK: 1,
concurrency: 1,
randomSeed: 42,
nodeProperties: ['age'],
sourceNodeFilter: 'Vegan'
})
YIELD nodesCompared, similarityPairs
nodesCompared  similarityPairs 

5 
4 
3.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.
CALL gds.alpha.knn.filtered.mutate('myGraph', {
mutateRelationshipType: 'SIMILAR',
mutateProperty: 'score',
topK: 1,
randomSeed: 42,
concurrency: 1,
nodeProperties: ['age'],
sourceNodeFilter: 'Vegan'
})
YIELD nodesCompared, relationshipsWritten
nodesCompared  relationshipsWritten 

5 
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 produced by the mutation are always directed, even if the input graph is undirected.
If for example 
3.5. Write
The write
execution mode extends the stats
mode with an important side effect: for each pair of nodes we create a relationship with the similarity score as a property to the Neo4j database.
The type of the new relationship is specified using the mandatory configuration parameter writeRelationshipType
.
Each new relationship stores the similarity score between the two nodes it represents.
The relationship property key is set 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.
CALL gds.alpha.knn.filtered.write('myGraph', {
writeRelationshipType: 'SIMILAR',
writeProperty: 'score',
topK: 1,
randomSeed: 42,
concurrency: 1,
nodeProperties: ['age'],
sourceNodeFilter: 'Vegan'
})
YIELD nodesCompared, relationshipsWritten
nodesCompared  relationshipsWritten 

5 
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 for example 
Was this page helpful?