Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Enabling Legacy Automatic Full-Text Search on Neo4j 3.X

DZone's Guide to

Enabling Legacy Automatic Full-Text Search on Neo4j 3.X

Wouldn't it be great to search one index that covers multiple models? See how some buried configuration settings can be tuned to let you cover more ground in Neo4j.

· Database Zone ·
Free Resource

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper


Image title

Neo4j 3.x has made inroads toward full-text search capabilities using some Cypher keywords, like “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 use 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 models, but let’s say we have to search across 10? That’s 10 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. He comes in the form of 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.

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

Topics:
database ,neo4j ,cypher ,data model

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}