Desired behavior:
Assuming I have two documents with these descrirption
field values:
The Lazy Fox Jumps Over
The Lazy Boy is my favorite pastime
and I search:
Lazy F
I'd like the result to be the document with:
The Lazy Fox Jumps Over
and NOT bring up the one with
The Lazy Boy is my favorite pastime
Initially I had a regular phrase
search which failed brought me the Lazy Fox
document, but also the Lazy Boy...
record, because it had the word Lazy
in it.
Then I tried autocomplete
:
{
"mappings": {
"dynamic": false,
"fields": {
"description": [
{
"type": "string"
},
{
"tokenization": "nGram",
"type": "autocomplete"
}
]
}
}
}
and search:
{
"$search":{
"autocomplete":{
"query": querySplit,
"path":"description"
},
"index":"my_test_index"
}
}
That improved things, for partial string search, but it also gets me those two documents where the word lazy
exists, instead of the one I was aiming for.
SO: Is there any mapping that would be able to search for exact substrings, case insensitive?
The search logic should work as this sample python works, the simplest of partial string search, even across words:
s = "This is a Lazy Fox"
f = "This is a Lazy Boy"
print(s.find('Lazy F') > -1)
print(f.find('Lazy F') > -1)
output:
True
False
Thanks!
You can set up a text index and chain up text search and a regex search.
db.collection.find({
"$text": {
"$search": "Lazy F",
"$caseSensitive": false
},
"description": {
"$regex": "Lazy F",
"$options": "i"
}
})