Hi, I’m working on a feature in which we need to search by name in our database. The search works fine for single word queries, but when I input a string with spaces in it (2 or more words), the results are not accurate, because it returns different documents which match only one of the words that I inputted. From what I have read on a similar post on this forum, it might be a tokenization issue and the person which fixed it made a fix which works for exact matches, here I need an autocomplete solution for this issue.
Hi, here is my index:
{
"mappings": {
"dynamic": true,
"fields": {
"groupTag": {
"type": "autocomplete"
},
"name": {
"type": "autocomplete"
}
}
}
}
Here is my document:
{
"_id": {
"$oid": "63e4f8a261f74736f0fcc8b6"
},
"_v": 4,
"groupTag": "T-2",
"createdAt": {
"$date": {
"$numberLong": "1675950242663"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1676022397735"
}
},
"name": "Dan test group",
}
And if I search for the name “Dan test group”, the search feature returns me other documents which contains in the name field any of the words “Dan”, “test”, “group”, and I end getting information that I didn’t search for. I need the search to return documents which name contains the entire string that I typed (“Dan test group”).
Hi Dan,
This is happening because you are using the autocomplete
field mapping, which allows you to return results which partially match your search query. If you are interested in return exact matches, you might want to consider using a string
field mapping type with the lucene.keyword analyzer. Your index definition would look something like this:
{
"mappings": {
"dynamic": true,
"fields": {
"groupTag": {
"type": "string",
"analyzer": "lucene.keyword"
},
"name": {
"type": "string",
"analyzer": "lucene.keyword"
}
}
}
}
You can learn more about exact matching in this blog post.
Thank you for help, but this solution does not help me. I need to still use autocomplete, but when I type 2 words, I need the response to contain both words. Currently it returns me different responses for each word.