Searches can be performed on a Full-text search index in the following ways:
- Specific attribute (complete match)
- Logical operator (AND / OR)
- Single-character wildcard (?)
- Multi-character wildcard (*)
- Fuzzy search (~)
For the full description of full-text search in Neo4j, see the official documentation available here: Full-text search index.
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.
Creating a Full-text search index:
We will be using an example search index created on the titles for the Movie nodes in the default Movies graph:
CREATE FULLTEXT INDEX movieNamesIndex FOR (m:Movie) ON EACH [m.title]
Performing searches
Specific attribute (complete match):
Search for movies that have the word “matrix” in their title.
CALL db.index.fulltext.queryNodes("movieNamesIndex", "title:matrix")
YIELD node, score
RETURN node.title as title, score
Results:
Logical operator:
There are two logical operators available, “OR” and “AND”.
Search for movies that have the words "the" OR "matrix" in their title.
CALL db.index.fulltext.queryNodes("movieNamesIndex", "title:'the' OR 'matrix'")
YIELD node, score
RETURN node.title as title, score
Results:
Single-character wildcard:
The single-character wildcard operator ?
looks for terms that match that with the single character replaced.
CALL db.index.fulltext.queryNodes("movieNamesIndex", "title:ma?") YIELD node, score
RETURN node.title as title, score
Results:
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 ma*x
. To prevent extremely slow wildcard queries, a term should not start with the wildcard. Bad example: *trix
.
CALL db.index.fulltext.queryNodes("movieNamesIndex", "title:ma*") YIELD node, score
RETURN node.title as title, score
Results:
Fuzzy search:
Fuzzy search ~
works by using mathematical formulae that calculate the similarity between two words.
CALL db.index.fulltext.queryNodes("movieNamesIndex", "title:ma~") YIELD node, score
RETURN node.title as title, score
Results:
Comments
0 comments
Please sign in to leave a comment.