Neo4J how to call db.index.fulltext.queryNodes on a nodes subset (instead of doing the search on every node in DB)?

Viewed 502

I have the following data model:

(org:Organization {name_en:'Org name in English', 'name_fr': 'Org name in French', 'anotherProperty': 'a value'})

I configured Full-text search index on the name's properties (name_en and name_fr).

If I want to make a fuzzy search on the name, I have to write the following:

CALL db.index.fulltext.queryNodes("myOrgNameIndex", 'something~') YIELD node RETURN node

But, I always have to perform a first MATCH to get a subset of org nodes based on 'anotherProperty'. So instead of doing as first operation the fuzzy search on all org nodes of my DB (a lot...), I would like first doing a MATCH with a WHERE and then calling the full-text search index based on the results of the 'first' query.

How can I do that? Is it even possible?

Already thank you for your reply.

2 Answers

You can add a WHERE clause to the CALL , like this

CALL db.index.fulltext.queryNodes("myOrgNameIndex", 'something~') YIELD node 
    WHERE node.anotherProperty = 'foo'
RETURN node

It's a little late, but maybe in some cases, when anotherProperty is a constant, it would be a useful solution to reverse the problem.

First, add new label to desired nodes:

MATCH (org:Organization { anotherProperty : 'some value' })
SET org:SearchableOrganization

Create index on SearchableOrganization:

CREATE FULLTEXT INDEX myOrgNameIndex FOR (org:SearchableOrganization) ON EACH [org.name_en, org.name_fr]

And then do the search as previously on already reduced set :

CALL db.index.fulltext.queryNodes("myOrgNameIndex", 'something~') YIELD node RETURN node
Related