Fulltext search in Neo4j is supported by means of fulltext schema indexes. Fulltext schema indexes are created, dropped, and updated transactionally, and are automatically replicated throughout a cluster.
Below are two examples to better understand the usage of a Fulltext index using multiple node properties and multiple nodes.
Fulltext Index using multiple node properties:
Let's assume we have Movie
node in our database :
As depicted in the screenshot below Movie node has multiple properties having string values i.e. title
andtagline
Now if we want to search for Movies that contain the word comedy
in either the title or tagline property. The first operation to do is to create a fulltext search index, with the help of the following procedure :
CREATE FULLTEXT INDEX titlesAndDescriptions FOR (n:Movie) ON EACH [n.title, n.tagline];
In above cypher 'titlesAndDescriptions
' is the name of the index, then `(n:Movie)` is a node label that will be represented as documents in the titlesAndDescriptions
index and then `[n.title, n.tagline]
`are the list of properties to be replicated as document fields, note that as of now, only text properties are being replicated.
Now that our titlesAndDescriptions
index is created, we can query it and test our fulltext search queries. Let’s find all the Movies withcomedy
word either in tagline or title:
CALL db.index.fulltext.queryNodes("titlesAndDescriptions", "comedy") YIELD node, score
RETURN node.title,node.tagline, node.description, score
As expected we get 2 results in the output having comedy word either intagline
property ortitle
:
Fulltext Index using multiple nodes:
In the above example we have created and used fulltext index on multiple node properties. Now in this example we would explore how to create and use fulltext index using multiple nodes.
Let's assume we have one more node Book
with property name
so this can be added along with Movie
node using pipe symbol |
while creating fulltext index like below:
CREATE FULLTEXT INDEX titlesAndDescriptions FOR (n:Movie|Book) ON EACH [n.title, n.tagline,n.name]
It is worth noting that in the above cypher we are considering Movie, as well as Book Node and index also contains properties from Book Node.
So, if we now try to create a query that searches for comedy
text in the Movie and Book nodes then there must be 3 records:
CALL db.index.fulltext.queryNodes("titlesAndDescriptions", "comedy") YIELD node, score
RETURN node.title,node.tagline,node.name, node.description, score
This will return all the Movies and Books with text 'comedy' in their title, tagline or name and output will look like below:
For the full description of fulltext search in Neo4j, see the official documentation available here: Indexes for fulltext search
Fulltext schema indexes are powered by the Apache Lucene indexing and search library. A complete description of the Lucene query syntax can be found in the Lucene documentation.
Comments
0 comments
Please sign in to leave a comment.