MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Mongo Search Autocomplete Type

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",
        },
      },
    },

https://www.w3schools.com/charsets/ref_utf_symbols.asp

Best
Pavel

Hi Pavel, thanks so much for the reply.

Unfortunately that is not working for me :disappointed:

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

hmm that also doesn’t work for me :thinking:

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.

@Pavel_Duchovny A friendly bump :blush:

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

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

Thanks, just posted it as an idea.

@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

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.

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!

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!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.