Enabling Legacy Automatic Full Text Search on Neo4j 3.x

solar-elastic-lucene

Neo4j 3.x has made inroads toward Full Text Search capabilities using Cypher keywords “STARTS WITH”, “ENDS WITH” and “CONTAINS”. However this search capability is limited to a single Schema Index and can be a problem when you need a very flexible search interface. To search across multiple models you can do this trick:

MATCH (n:Person) WHERE n.name CONTAINS {search_term} RETURN n
UNION
MATCH (n:Place) WHERE n.name CONTAINS {search_term} RETURN n
UNION
MATCH (n:Thing) WHERE n.name CONTAINS {search_term} RETURN n

This works out ok when dealing with just a few, but let’s say we have to search across 10 models? That’s ten separate search queries… wouldn’t it be nice if you could just search one index and avoid the overhead? Well yes Virginia, there really is a Santa Claus. The badly named “Legacy Indexes”, specifically the “node_auto_index”. To enable this feature you have to edit your neo4j.conf file and add these lines:

# Set this parameter equal to true to enable it
dbms.auto_index.nodes.enabled=true
# A list of node property names (comma separated) that will be indexed by default.
dbms.auto_index.nodes.keys=name

Now once you restart the server, the index won’t be created just yet. You first have to set an indexable property and then the index will be created. So for example we can create a node with a name property.

CREATE (p:Place {name:"Chicago"})

Now if we start the neo4j shell ( /bin/neo4j-shell ) we can run this command to see our node auto index.

index --get-config node_auto_index

The result we get is:

{
    "provider": "lucene",
    "type": "exact"
}

So great, the index was created, but it’s “exact” not what we want… so we need to issue this command to change it:

index --set-config node_auto_index type fulltext

This command leads to a very helpful message:

INDEX CONFIGURATION CHANGED, INDEX DATA MAY BE INVALID

So what do we do here? Well, we can simply reset the name property of each type of node we want to index to itself.

MATCH (p:Person) SET p.name = p.name

Now we are able to perform a full text search query using the “legacy” START clause:

START n=node:node_auto_index("name:*m*") RETURN n;

That’s it. One single index to rule them all.

Tagged , , , , ,

3 thoughts on “Enabling Legacy Automatic Full Text Search on Neo4j 3.x

  1. […] Automatic Full Text Search on Neo4j3.x […]

  2. […] Full Text Search Guide […]

  3. […] Enabling Legacy Automatic Full Text Search on Neo4j 3.X, by Max De Marzi […]

Leave a comment