Filtered K-Nearest Neighbors

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.

Introduction

The Filtered K-Nearest Neighbors algorithm extends our popular K-Nearest Neighbors algorithm with filtering on source nodes, target nodes or both.

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 K-Nearest Neighbors algorithm, output with filtering are new relationships between nodes and their k-nearest neighbors. Similarity scores are expressed via relationship properties.

Filtered K-Nearest 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.

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

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

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 K-Nearest 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 K-Nearest Neighbors algorithm is not guaranteed to find k results, the Filtered K-Nearest 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.

Configuring filters and seeding

You should consult K-Nearest 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:

Table 1. sourceNodeFilter syntax

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'

The target node filter to use are specified with the targetNodeFilter configuration parameter. It is not mandatory.

This parameter accepts one of:

Table 2. targetNodeFilter syntax

a single node id

targetNodeFilter: 117

a list of node ids

targetNodeFilter: [256, 512]

a single node

MATCH (person:Person) WITH person ORDER BY person.age ASC LIMIT 1 …​ targetNodeFilter: n

a list of nodes

MATCH (person:Person) WHERE person.age < 35 collect(person) AS people …​ targetNodeFilter: people

a single label

targetNodeFilter: 'Person'

Seeding can be enabled with the seedTargetNodes configuration parameter. It defaults to false.

Syntax

This section covers the syntax used to execute the Filtered K-Nearest 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.

Filtered K-Nearest Neighbors syntax per mode
Run Filtered K-Nearest Neighbors in stream mode on a named graph.
CALL gds.knn.filtered.stream(
  graphName: String,
  configuration: Map
) YIELD
  node1: Integer,
  node2: Integer,
  similarity: Float
Table 3. 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 4. Configuration
Name Type Default Optional Description

nodeLabels

List of String

['*']

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

['*']

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

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.

nodeProperties

String or Map or List of Strings / Maps

n/a

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

10

yes

The number of neighbors to find for each node. The K-nearest neighbors are returned. This value cannot be lower than 1.

sampleRate

Float

0.5

yes

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

deltaThreshold

Float

0.001

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).

maxIterations

Integer

100

yes

Hard limit to stop the algorithm after that many iterations.

randomJoins

Integer

10

yes

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

initialSampler

String

"uniform"

yes

The method used to sample the first k random neighbors for each node. "uniform" and "randomWalk", both case-insensitive, are valid inputs.

randomSeed

Integer

n/a

yes

The seed value to control the randomness of the algorithm. Note that concurrency must be set to 1 when setting this parameter.

similarityCutoff

Float

0

yes

Filter out from the list of K-nearest neighbors nodes with similarity below this threshold.

perturbationRate

Float

0

yes

The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity.

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.

seedTargetNodes

Boolean

false

yes

Enable seeding of target nodes.

Table 5. 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 K-Nearest Neighbors in stats mode on a named graph.
CALL gds.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
Table 6. 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 7. Configuration
Name Type Default Optional Description

nodeLabels

List of String

['*']

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

['*']

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

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.

nodeProperties

String or Map or List of Strings / Maps

n/a

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

10

yes

The number of neighbors to find for each node. The K-nearest neighbors are returned. This value cannot be lower than 1.

sampleRate

Float

0.5

yes

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

deltaThreshold

Float

0.001

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).

maxIterations

Integer

100

yes

Hard limit to stop the algorithm after that many iterations.

randomJoins

Integer

10

yes

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

initialSampler

String

"uniform"

yes

The method used to sample the first k random neighbors for each node. "uniform" and "randomWalk", both case-insensitive, are valid inputs.

randomSeed

Integer

n/a

yes

The seed value to control the randomness of the algorithm. Note that concurrency must be set to 1 when setting this parameter.

similarityCutoff

Float

0

yes

Filter out from the list of K-nearest neighbors nodes with similarity below this threshold.

perturbationRate

Float

0

yes

The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity.

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.

seedTargetNodes

Boolean

false

yes

Enable seeding of target nodes.

Table 8. Results
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.

Run K-Nearest Neighbors in mutate mode on a graph stored in the catalog.
CALL gds.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
Table 9. 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 10. Configuration
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.

nodeProperties

String or Map or List of Strings / Maps

n/a

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

10

yes

The number of neighbors to find for each node. The K-nearest neighbors are returned. This value cannot be lower than 1.

sampleRate

Float

0.5

yes

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

deltaThreshold

Float

0.001

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).

maxIterations

Integer

100

yes

Hard limit to stop the algorithm after that many iterations.

randomJoins

Integer

10

yes

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

initialSampler

String

"uniform"

yes

The method used to sample the first k random neighbors for each node. "uniform" and "randomWalk", both case-insensitive, are valid inputs.

randomSeed

Integer

n/a

yes

The seed value to control the randomness of the algorithm. Note that concurrency must be set to 1 when setting this parameter.

similarityCutoff

Float

0

yes

Filter out from the list of K-nearest neighbors nodes with similarity below this threshold.

perturbationRate

Float

0

yes

The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity.

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.

seedTargetNodes

Boolean

false

yes

Enable seeding of target nodes.

Table 11. Results
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.

Run K-Nearest Neighbors in write mode on a graph stored in the catalog.
CALL gds.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
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. Configuration
Name Type Default Optional Description

nodeLabels

List of String

['*']

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

['*']

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

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.

nodeProperties

String or Map or List of Strings / Maps

n/a

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

10

yes

The number of neighbors to find for each node. The K-nearest neighbors are returned. This value cannot be lower than 1.

sampleRate

Float

0.5

yes

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

deltaThreshold

Float

0.001

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).

maxIterations

Integer

100

yes

Hard limit to stop the algorithm after that many iterations.

randomJoins

Integer

10

yes

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

initialSampler

String

"uniform"

yes

The method used to sample the first k random neighbors for each node. "uniform" and "randomWalk", both case-insensitive, are valid inputs.

randomSeed

Integer

n/a

yes

The seed value to control the randomness of the algorithm. Note that concurrency must be set to 1 when setting this parameter.

similarityCutoff

Float

0

yes

Filter out from the list of K-nearest neighbors nodes with similarity below this threshold.

perturbationRate

Float

0

yes

The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity.

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.

seedTargetNodes

Boolean

false

yes

Enable seeding of target nodes.

Table 14. Results
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.

Examples

All the examples below should be run in an empty database.

The examples use Cypher projections as the norm. Native projections will be deprecated in a future release.

In this section we will show examples of running the Filtered K-Nearest 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.

Visualization of the example graph
CREATE (alice:Person:Vegan {name: 'Alice', age: 24})
CREATE (bob:Person:Vegan {name: 'Bob', age: 73})
CREATE (carol:Person {name: 'Carol', age: 24})
CREATE (dave:Person:Vegan {name: 'Dave', age: 48})
CREATE (eve:Person:Vegan {name: 'Eve', age: 67});

In the example, we want to use the Filtered K-Nearest Neighbors algorithm to compare people based on their age.

The following statement will project the graph and store it in the graph catalog.
MATCH (p:Person|Vegan)
RETURN gds.graph.project(
  'myGraph',
  p,
  null,
  {
    sourceNodeLabels: labels(p),
    targetNodeLabels: [],
    sourceNodeProperties: p { .age },
    targetNodeProperties: {}
  }
)

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 stats 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.knn.filtered.stream.estimate('myGraph', {
    topK: 1,
    nodeProperties: ['age'],
    sourceNodeFilter: 'Vegan'
})
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
Table 15. Results
nodeCount relationshipCount bytesMin bytesMax requiredMemory

5

0

2256

3312

"[2256 Bytes ... 3312 Bytes]"

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 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, filter on source nodes, and stream results:
CALL gds.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
Table 16. Results
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.

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 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, seeding the target node set. It will then filter for target nodes and stream results:
CALL gds.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
Table 17. Results
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.

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 algorithm and return the result in form of statistical and measurement values:
CALL gds.knn.filtered.stats('myGraph', {
    topK: 1,
    concurrency: 1,
    randomSeed: 42,
    nodeProperties: ['age'],
    sourceNodeFilter: 'Vegan'
})
YIELD nodesCompared, similarityPairs
Table 18. Results
nodesCompared similarityPairs

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.knn.filtered.mutate('myGraph', {
    mutateRelationshipType: 'SIMILAR',
    mutateProperty: 'score',
    topK: 1,
    randomSeed: 42,
    concurrency: 1,
    nodeProperties: ['age'],
    sourceNodeFilter: 'Vegan'
})
YIELD nodesCompared, relationshipsWritten
Table 19. Results
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 a → b is topK for a and symmetrically b → a is topK for b, it appears as though an undirected relationship is produced. However, they are just two directed relationships that have been independently produced.

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.

The following will run the algorithm, and write back results:
CALL gds.knn.filtered.write('myGraph', {
    writeRelationshipType: 'SIMILAR',
    writeProperty: 'score',
    topK: 1,
    randomSeed: 42,
    concurrency: 1,
    nodeProperties: ['age'],
    sourceNodeFilter: 'Vegan'
})
YIELD nodesCompared, relationshipsWritten
Table 20. Results
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 a → b is topK for a and symmetrically b → a is topK for b, it appears as though an undirected relationship is written. However, they are just two directed relationships that have been independently written.