Neo4j With Scala: Full Text Search

DZone 's Guide to

Neo4j With Scala: Full Text Search

Now that we have looked at integrating Neo4j and Scala, the fifth part of this series gives us a walkthrough about how to use full text search.

· Big Data Zone ·
Free Resource

Welcome back to my series about Neo4j with Scala. Until now, we have talked and learned about the use of Neo4j with Scala and how easily we can integrate both amazing technologies.

Before starting this article, here is a quick recap:

  1. Neo4j With Scala: An Introduction
  2. Neo4j With Scala: User Defined Procedures and APOC
  3. Neo4j With Scala: Migrating Data
  4. Neo4j With Scala: Awesome Experience With Spark

ElasticSearch is a modern search and analytic engine based on Apache Lucene. ElasticSearch is a full-text search engine and is highly scalable. It allows RESTful web interface and schema-free documents. ElasticSearch is able to achieve fast search responses because it searches an index instead of searching the text directly. ElasticSearch also provides the capability to store data but we shouldn’t use it as a primary database because ES does not provide following support:

  1. Security: ES does not provide any access control and authentication.
  2. Maturity of tools: ES does not provide mature client libraries and 3rd party tools, it makes developer life much harder.
  3. Transaction: ES doesn’t provide transaction and processing for data manipulation.
  4. Long Computation: ES commands for searching data are not suited to “large” scans of data and advanced computation on the database side.
  5. Durability: ES is scalable and distributed but backup and durability is not as high as other databases. You can lose your data if you make it your primary database.

These are some concerns when using ES. Neo4j provides the way to overcome the concerns we mentioned above and also provides full text search with the 3.x version.

We can use cypher keywords, “CONTAINS”, “STARTS WITH”, “ENDS WITH” for the full text search but it is limited to the single schema. So when we try to use it with multiple schema, we have to use keyword “UNION” and implement the multiple cypher query.

MATCH (r:Restaurant) WHERE r.name STARTS WITH {search_term} RETURN count(*);
MATCH (c:City) WHERE c.name CONTAINS {search_term} RETURN count(*);
MATCH (a:Address) WHERE a.name ENDS WITH {search_term} RETURN count(*);

But this will work only for the limited schema. When you have to make full text searching on the multiples schemas, it is better to use indexes and it will be the better choice to gain it. We have to make changes these changes (which mention below) in the conf file.

# Set this parameter equal to true to enable it
# A list of node property names (comma separated) that will be indexed by default.

After making changes in the conf, we have to restart the neo4j and create node.

CREATE (c:Country {country:"india"})

Now we have almost finished with the primary setup. Now to see the whole picture we have to start the neo4j-shell and run this command:

index --get-config node_auto_index

Output :
    "provider": "lucene",
    "type": "exact"

exact.pngGreat, we are ready.

Wait! Here we find “exact” so now what can we do? We run the following command to change it:

index --set-config node_auto_index type fulltext

Output :

When we run the above cypher it leads to very specific information, so what do we do here? We can simply reset the name property of each type of node we want to index to itself.

MATCH (c:Country) SET c.country = c.country

Now we are done with our indexing work and ready to test the result. Run this command for the searching in neo4j :

START n=node:node_auto_index("country:*india*") RETURN n;


Here we see that how can we use neo4j for the full text search, with Neo4j we get the advantages of database with the searching. This is a sample example on very less amount of data but we can use it on a large amount of data effectively.

I hope it will help you to set-up full text search with Neo4j.



Automatic Full Text Search on Neo4j3.x 

text search ,keywords ,query ,neo4j ,scala

Published at DZone with permission of Anurag Srivastava , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}