Search Email Address

Hello,

We are using elasticsearch with rails.

We have users that are indexed by email and name.

When the query string includes email address, the results include all the users with the same email domain.
For example, when searching: "dave@example.com", the search results are the users with these email addresses: "dave@example.com", "bob@example.com", "davey@example.com".

Our expected search result is: "dave@example.com".

We've tried to send the query string with quotes and got: "dave@example.com", "davey@example.com".

In addition, when searching "dave" we would like to get both "dave@example.com" and "davey@example.com" and when searching "dave@ex" only "dave@example.com".

This is the relevant elasticsearch code:

def as_indexed_json(options={})
    { :name => self.name,
      :email => self.email }
  end

def self.search(query, user_id, organization_id)
      __elasticsearch__.search(
          query: {
              bool:
                  { must: 
                    { query_string: {
                      query: query,
                      fields: [ :name, :email ],
                      analyzer: 'standard'
                      }
                    },
                  }
          },
          size: 10000
      )
  end

settings  analysis: {
                filter: {
                    ngram_filter: {
                        type: "nGram",
                        min_gram: 2,
                        max_gram: 20
                    }
                },
                analyzer: {
                    ngram_analyzer: {
                        tokenizer: "standard",
                        type: "custom",
                        filter: ["lowercase", "ngram_filter"]
                    }
                }
            } do
    mapping do
      indexes :name, analyzer: :ngram_analyzer
      indexes :email, analyzer: :ngram_analyzer
    end
  end

Please let me know how to get these search results.

Thank you,
Lee

To search for the exact email address among other text strings, you need to create a custom analyzer using the tokenizer below

https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-uaxurlemail-tokenizer.html