# Search Email Address

**URL:** <https://discuss.elastic.co/t/search-email-address/59171>\
**Category:** Elasticsearch\
**Created:** [August 29, 2016, 11:13am UTC](https://discuss.elastic.co/t/search-email-address/59171 "2016-08-29T11:13:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://discuss.elastic.co/u/Lee)\
**Post date:** [August 29, 2016, 11:13am UTC](https://discuss.elastic.co/t/search-email-address/59171/1 "2016-08-29T11:13:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![thn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thn/32/8061_2.png) [@thn](https://discuss.elastic.co/u/thn)\
**Post date:** [August 29, 2016, 1:00pm UTC](https://discuss.elastic.co/t/search-email-address/59171/2 "2016-08-29T13:00:49Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-uaxurlemail-tokenizer.html)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:24pm UTC](https://discuss.elastic.co/t/search-email-address/59171/3 "2017-07-05T22:24:25Z")

</div>


