fwind
fwind

Reputation: 1314

Schema index for words in a string

I have a large amount of nodes which have the property text containing a string.

I want to find all nodes which text contains a given string (exact match). This can be done using the CONTAINS operator.

MATCH (n)
WHERE n.text CONTAINS 'keyword'
RETURN n

Edit: I am looking for all nodes n where n.text contains the substring 'keyword'. E.g. n.text = 'This is a keyword'.

To speed up this I want to create an index for each word. Is this possible using the new Schema Indexes?

(Alternatively this could be done using a legacy index and adding each node to this index but I would prefer using a schema index)

Upvotes: 2

Views: 102

Answers (2)

David Fox
David Fox

Reputation: 116

If I understand your question correctly, a legacy index would accomplish exactly what you're looking to do. If you don't want to have to maintain the index for each node you create/delete/update, you can use auto indexing (http://jexp.de/blog/2014/03/full-text-indexing-fts-in-neo4j-2-0/).

If you're looking to only use schema indexing, another approach would be to store each keyword as a separate node. Then you could use a schema index for finding relevant keyword nodes which then map to the node they exist on. Just a thought.

Upvotes: 2

ceej
ceej

Reputation: 1893

Absolutely. Given that you are looking for an exact match you can use a schema index. Judging from your question you probably know this but to create the index you will need to assign your node a label and then create the index on that label.

CREATE INDEX ON :MyLabel(text)

Then at query time the cypher execution index will automatically use this index with the following query

MATCH (n:MyLabel { text : 'keyword' })
RETURN n

This will use the schema index to look up the node with label MyLabel and property text with value keyword. Note that this is an exact match of the complete value of the property.

To force Neo4j to use a particular index you can use index hints

MATCH (n:MyLabel)
USING INDEX n:MyLabel(text)
WHERE n.text = 'keyword'
RETURN n

EDIT

On re-reading your question I am thinking you are not actually looking for a full exact match but actually wanting an exact match on the keyword parameter within the text field. If so, then...no, you cannot yet use schema indexes. Quoting Use index with STARTS WITH in the Neo4j manual:

The similar operators ENDS WITH and CONTAINS cannot currently be solved using indexes.

Upvotes: 3

Related Questions