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 with the word 'comedy' 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 will 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 the 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 the 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.
Additional search options:
The search example shown above is an example of what can be called searching for a Specific attribute.
Searches can also be performed using Single-character wildcard (?), Multi-character wildcard(*) or fuzzy search(~)
Single-character wildcard:
The single-character wildcard operator ?
looks for terms that match that with the single character replaced.
CALL db.index.fulltext.queryNodes("titlesAndDescriptions", "title:th?") YIELD node, score
RETURN node.title as title, score
Multi-character wildcard:
The multi-character wildcard *
operator looks for zero or more characters. You can also put the operator in the middle of the term like dre*am
. To prevent extremely slow wildcard queries, a term should not start with the wildcard. Bad example: *dream
.
CALL db.index.fulltext.queryNodes("titlesAndDescriptions", "title:drea*") YIELD node, score
RETURN node.title as title, score
Fuzzy search:
Fuzzy search ~
works by using mathematical formulae that calculate the similarity between two words.
CALL db.index.fulltext.queryNodes("titlesAndDescriptions", "title:dream~") YIELD node, score
RETURN node.title as title, score
Comments
0 comments
Please sign in to leave a comment.