James Newton
James Newton

Reputation: 7114

Find words missing from a Neo4j database

I have a Neo4j database containing words from a dictionary. If a query is made using a word not in the dictionary (such as a mis-spelled word), I want the query to return just the missing words.

I can do the opposite. This query returns words from the database that are correctly spelled:

MATCH (w:Word)
WHERE w.spelling IN ["good","words","plus","twoo","that","are","mispleled"]
RETURN w.spelling AS word

Can I write a query that will return something like ["twoo","mispleled"]? Or do I have to use a query like the one above, and check in the application itself which words have not been matched?

Upvotes: 0

Views: 53

Answers (2)

cybersam
cybersam

Reputation: 67019

[EDITED]

This might work for you. {input_words} is a parameter that contains an array of the words you want to check.

UNWIND {input_words} AS in_word
OPTIONAL MATCH (w:Word { spelling: in_word })
RETURN REDUCE(s = [], x IN COLLECT({ w: w, in_word: in_word }) |
  CASE WHEN x.w IS NULL THEN s + x.in_word ELSE s END) AS missing_word;

For efficiency, you should first create an index on :Word(spelling):

CREATE INDEX ON :Word(spelling);

Upvotes: 0

Nicole White
Nicole White

Reputation: 7790

You can do this without OPTIONAL MATCH:

WITH ["good", "words", "plus", "twoo", "that", "are", "mispleled"] AS words
MATCH (word:Word) WHERE word.spelling IN words
WITH words, COLLECT(word.spelling) AS matched
RETURN [x IN words WHERE NOT x IN matched];

The advice about creating an index still applies.

Upvotes: 2

Related Questions