Tyler_Bell
(Tyler Bell)
October 19, 2020, 3:23am
1
Hey guys, how do I get search autocomplete to work with email addresses?
Here’s my search index.
{
"mappings": {
"dynamic": false,
"fields": {
"email": {
"type": "autocomplete"
}
}
}
}
If I have a document say,
{
_id: “123”
email: “kate@example.com ”
}
and do a search w/ this ag
{
$search: {
autocomplete: {
path: "email",
query: "ka",
},
},
},
it will successfully return the doc above.
Even if the query is “kat” or “kate”, the search will return the document.
However, I soon as I add the “@” (kate@) the document is not returned. I’m assuming this has something to do with “@” being a special character.
So how can I improve the search so autocomplete works on emails?
Thanks!
Hi @Tyler_Bell ,
I suggest you try an encoding url representation like %40
instead of symbol @
:
{
$search: {
autocomplete: {
path: "email",
query: "kate%40",
},
},
},
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Best
Pavel
Tyler_Bell
(Tyler Bell)
October 19, 2020, 6:43pm
3
Hi Pavel, thanks so much for the reply.
Unfortunately that is not working for me
I tried
$search: {
autocomplete: {
path: "email",
query: "kate%40",
},
},
Is there anything else I can try?
Thanks!
Hi @Tyler_Bell ,
Using HTML codes worked for me, for @
I used @
value
$search: {
autocomplete: {
path: "email",
query: "kate@",
},
}
Please let me know if this worked for you.
Best regards,
Pavel
Tyler_Bell
(Tyler Bell)
October 20, 2020, 6:33pm
5
Pavel_Duchovny:
@
hmm that also doesn’t work for me
Here’s my index
"mappings": {
"dynamic": false,
"fields": {
"email": {
"type": "autocomplete"
}
}
}
}
and here’s my query
{
$search: {
autocomplete: {
path: "email",
query: "kate@"
},
},
},
Querying “ka”, “kat”, and “kate” all work, but as soon as I add the “@
”, there are no results.
Tyler_Bell
(Tyler Bell)
October 22, 2020, 1:35pm
7
@Pavel_Duchovny A friendly bump
Hi @Tyler_Bell ,
Apperantly autocomplete index does not tokenize @ so you can’t use autocomplete for this search…
You would want to use a new text index with text operation and not autocomplete ,since autocomplete is only designed to work with autocomplete indexes.
Best
Pavel
Tyler_Bell
(Tyler Bell)
October 24, 2020, 8:48pm
9
Thanks Pavel.
Do you know if there are any plans of allowing autocomplete to work with emails? It seems like emails would be a common use case.
Thanks!
Hi @Tyler_Bell ,
Yes we have plans to introduce an email tokenizer.
You can place a comment on https://feedback.mongodb.com …
Thanks
Pavel
Tyler_Bell
(Tyler Bell)
October 25, 2020, 4:14pm
11
Thanks, just posted it as an idea.
Marcus
(Marcus Eagan)
October 25, 2020, 5:41pm
12
@Tyler_Bell One alternative that could work for you Tyler would be to create a custom analyzer with a keyword tokenizer and autocomplete field. It’s important to note that your query operator would need to change from autocomplete to text.
Consider the following three documents:
{ "_id" : 1, "email" : "kate@example.com" }
{ "_id" : 2, "email" : "harshad@example.com" }
{ "_id" : 3, "email" : "missing" }
The following index definition:
{
"analyzer": "emailAutocomplete",
"mappings": {
"dynamic": true
},
"analyzers": [
{
"charFilters": [],
"name": "emailAutocomplete",
"tokenFilters": [
{
"maxGram": 10,
"minGram": 1,
"type": "nGram"
}
],
"tokenizer": {
"type": "keyword"
}
}
]
}
The following query:
db.email_search.aggregate([
... {
... $search: {
... index: "KeywordTokenizer_Autocomplete_Analyzer",
... text: {
... path: "email",
... query: "@",
... }
... }
... }
... ])
{ "_id" : 1, "email" : "kate@example.com" }
{ "_id" : 2, "email" : "harshad@example.com" }
Thanks to these Harshad and @Pavel_Duchovny for the investigation. I am only relaying the message. Hopefully that helps you.
In production, I would recommend considering/testing a higher value for minGram
depending on your corpus and use case because such a low minGram
could quickly expand the size of your index.
1 Like
Marcus
(Marcus Eagan)
October 25, 2020, 6:21pm
13
Another gotcha to consider is that the name of the analyzer should be different from your previous index, otherwise you need to delete the index and create it again.
Tyler_Bell
(Tyler Bell)
October 25, 2020, 6:29pm
14
Awesome, thank you! I will give this a try!
A completely non related question, but is there an ETA on being able to use the search
stage after other stages? idea here
Thanks for your help!
Marcus
(Marcus Eagan)
October 27, 2020, 7:39am
15
There currently is no ETA for $search
as a later stage. Such a featrue would require significant changes, so we are doing our best to support a variety of use cases and scenarios to support the community. If you have specific questions you can ask them here or in the feedback portal.
Thanks again!
system
(system)
Closed
November 1, 2020, 7:39am
16
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.