Gabe Kopley
Gabe Kopley

Reputation: 16677

Filter neo4j nodes by subtracting nodes that are returned by index

My nodes have an array property category_ids that contains integers.

I can query for nodes whose category_ids do not match any in the list [1,2,3]:

START node(*)
WHERE NOT(ANY(x in node.category_ids WHERE x IN [1,2,3]))
RETURN node;

I can use an index (I'm calling it nodes_categories and it's a standard exact lucene index) to start with the nodes that I want to filter out:

START excluded=node:nodes_categories("category_ids:(1 2 3)")
RETURN excluded;

But how do I use my index to get the nodes I do want? IE return all the nodes minus the nodes returned by my index hit? Here's my start:

START node=node(*), excluded=node:nodes_categories("category_ids:(1 2 3)")
???
RETURN node;

Edit: neo4j version is 1.9.M02

Upvotes: 2

Views: 1711

Answers (1)

Eve Freeman
Eve Freeman

Reputation: 33155

The naive way (updated):

START node=node(*), excluded=node:nodes_categories("category_ids:(1 2 3)")
WITH collect(excluded) as excluded, node
WHERE not node in(excluded)
RETURN distinct node;

A better way would be to figure out how to query the index for just the nodes you want. I'm not sure if there is a way to do it in the lucene syntax, though. Maybe something like:

START node=node:nodes_categories('category_ids:(* NOT 1 NOT 2 NOT 3)')
return node;

Upvotes: 3

Related Questions