Search code examples
neo4jlucenefull-text-searchfuzzy-searchneo4j-apoc

Neo4j Lucene query syntax for multi-word autosuggest fuzzy fulltext search


I'm trying to implement Lucene query for multi-word autosuggest full-text search.

For example, I'd like to be able to search for Arnold Schwarzenegger in database.

Right now I can do the fuzzy search by one word:

Name.primaryName:Shwarzengger~

which returns:

Arnold Schwarzenegger
Katherine Schwarzenegger
Patrick Schwarzenegger
Christian Schwarzenegger
Aurelia Schwarzenegger
Patrick M. Knapp Schwarzenegger
Brian Schwarzenegger
Christina Schwarzenegger

but how to do the same for a multi-word term?

For example the following query:

Name.primaryName:arn Shwarzengger~

doesn't work with the following error:

Neo.ClientError.Procedure.ProcedureCallFailed: Failed to invoke procedure apoc.index.nodes: Caused by: java.lang.NullPointerException: field must not be null

How to properly construct multi-word search term?


Solution

  • You can use the Luchene language to search:

    CALL apoc.index.nodes('Company','name:Ne* AND employees:Ma*')

    https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_add_document_to_index

    For example, break the initial query and construct a fuzzy one:

    WITH "arnold schwarzenegger" AS queryString
    WITH split(queryString, " ") AS terms,
         "primaryName" AS key
    WITH REDUCE(
           acc = "", 
           i IN range(0, size(terms) - 2) | 
           acc + key + ":" + terms[i] + " OR "
         ) + key + ":" + terms[size(terms) - 1] 
         AS query
    CALL apoc.index.nodes('Node', query) YIELD node
    RETURN node