Matching Exact Values with ArangoSearch
If you want to find strictly equal values, then the Analyzer is what you need. It will not apply any transformations. It is a no-operation Analyzer that passes everything through unaltered.
You can index and search strings with the identity
Analyzer for exact matching, that is case-sensitive, with accented characters as-is, and only if the entire string is equal (not matching substrings).
Dataset: IMDB movie dataset
View definition:
AQL queries:
FOR doc IN imdb
SEARCH ANALYZER(doc.title == "The Matrix", "identity")
RETURN doc.title
It is not necessary to set the Analyzer context with the ANALYZER()
function here, because the default Analyzer is identity
anyway. The following query will return the exact same results:
SEARCH doc.title == "The Matrix"
RETURN doc.title
However, being explicit about the Analyzer context helps with clarity and it also makes it easier to adjust queries if you want to use something other than the identity
Analyzers. A common pitfall is to index a field with a certain Analyzer, but forgetting to set this Analyzer as context in the query. The consequence is usually an empty result, because there is nothing in the View index for the implicitly requested Analyzer. Or the field happens to be indexed with the identity
Analyzer as well, but there are no or different matches because of different Analyzer pre-processing between the indexed data and the search terms.
You can search for several alternatives by combining multiple conditions with logical OR
s, but you can also use the IN
operator for the same purpose. The advantage of the operator is that it scales better with the number of alternatives and allows you to use a single query for a varying amount of strings that you want to match.
Match multiple exact movie titles using OR
:
Result |
---|
The Matrix |
The Matrix Reloaded |
Match multiple exact movie titles using IN
:
FOR doc IN imdb
SEARCH ANALYZER(doc.title IN ["The Matrix", "The Matrix Reloaded"], "identity")
RETURN doc.title
FOR doc IN imdb
RETURN doc.title
Bind parameters:
Result |
---|
The Matrix Revisited |
The Matrix |
The Matrix Reloaded |
The Matrix Revolutions |
The Matrix Trilogy |
Searching for exact values does not end with one or many equality conditions, but extends to negations as well. You can search for inequality with the !=
operator to return everything from the View index but documents that do not fulfill the criterion. This is also works with multiple alternatives using the NOT IN
operator.
Match movies that do not have the title The Matrix
:
FOR doc IN imdb
SEARCH ANALYZER(doc.title != "The Matrix", "identity")
Note that this includes documents that do not even have a title attribute, with the effect of returning many null
values in the result.
Match movies that neither have the title The Matrix
nor The Matrix Reloaded
. Post-filter the results to exclude implicit null
s:
FOR doc IN imdb
SEARCH ANALYZER(doc.title NOT IN ["The Matrix", "The Matrix Reloaded"], "identity")
RETURN doc.title
Result |
---|
Ploning |
Code Rush |
Ghost in the Shell 2.0 |
Christmas in Wonderland |
Hadashi no Gen |
The Magician |
… |