# Search analyzer not work

**URL:** <https://discuss.elastic.co/t/search-analyzer-not-work/188776>\
**Category:** Elasticsearch\
**Created:** [July 3, 2019, 4:31pm UTC](https://discuss.elastic.co/t/search-analyzer-not-work/188776 "2019-07-03T16:31:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jinlian\_Chen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jinlian_chen/32/44738_2.png) [@Jinlian\_Chen](https://discuss.elastic.co/u/Jinlian_Chen)\
**Post date:** [July 3, 2019, 4:31pm UTC](https://discuss.elastic.co/t/search-analyzer-not-work/188776/1 "2019-07-03T16:31:53Z")

</div>

Hi, I've set a custom analyzer, but it seems not work when doing search.

ElasticSearch version: 7.0.1

Create index:

```
PUT /analyze_email
{
    "settings": {
        "number_of_shards": "1",
        "number_of_replicas": "0",
        "analysis": {
            "filter": {
                "email": {
                    "type": "pattern_capture",
                    "preserve_original": "true",
                    "patterns": [
                        "([^@]+)",
                        "(\\p{L}+)",
                        "(\\d+)",
                        "@(.+)",
                        "(@)"
                    ]
                }
            },
            "analyzer": {
                "email_analyzer": {
                    "tokenizer": "uax_url_email",
                    "filter": [
                        "lowercase",
                        "email",
                        "unique"
                    ]
                }
            },
            "normalizer": {
                "lowercase_normalizer": {
                    "type": "custom",
                    "filter": [
                        "lowercase"
                    ]
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "email_address": {
                "type": "text",
                "analyzer": "email_analyzer",
                "norms": "false",
                "search_analyzer": "email_analyzer", 
                "fields": {
                    "raw": {
                        "type": "keyword",
                        "normalizer": "lowercase_normalizer"
                    }
                }
            }
        }
    }
}

```

Test email\_analyzer, it works.

```
GET analyze_email/_analyze?pretty
{
  "field": "email_address",
  "text": "jinliantest@gmail.com"
}

```

Result:

```
{
  "tokens" : [
    {
      "token" : "jinliantest@gmail.com",
      "start_offset" : 0,
      "end_offset" : 21,
      "type" : "<EMAIL>",
      "position" : 0
    },
    {
      "token" : "jinliantest",
      "start_offset" : 0,
      "end_offset" : 21,
      "type" : "<EMAIL>",
      "position" : 0
    },
    {
      "token" : "@",
      "start_offset" : 0,
      "end_offset" : 21,
      "type" : "<EMAIL>",
      "position" : 0
    },
    {
      "token" : "gmail.com",
      "start_offset" : 0,
      "end_offset" : 21,
      "type" : "<EMAIL>",
      "position" : 0
    },
    {
      "token" : "gmail",
      "start_offset" : 0,
      "end_offset" : 21,
      "type" : "<EMAIL>",
      "position" : 0
    },
    {
      "token" : "com",
      "start_offset" : 0,
      "end_offset" : 21,
      "type" : "<EMAIL>",
      "position" : 0
    }
  ]
}

```

I put a doc to the index.

```
PUT analyze_email/_doc/1
{
  "email_address": "jinliantest@gmail.com"
}

```

While I did the search by "@", it didn't work, it should return doc 1?

```
GET analyze_email/_search
{
  "query": {
    "match": {"email_address": "@"}
  }
}

```

Result:

```
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  }
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 3, 2019, 6:16pm UTC](https://discuss.elastic.co/t/search-analyzer-not-work/188776/2 "2019-07-03T18:16:45Z")

</div>

Thanks for trying to format your post. much appreciated.  
Please just don't use the citation icon but the code icon `</>`. I updated your post.

Just try:

```auto
GET analyze_email/_analyze
{
  "analyzer": "email_analyzer",
  "text": ["@"]
}

```

You will get:

```auto
{
  "tokens" : []
}

```

Which explains why you can't find anything then.

Note that this works though:

```auto
GET analyze_email/_search
{
  "query": {
    "term": {
      "email_address": "@"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Jinlian\_Chen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jinlian_chen/32/44738_2.png) [@Jinlian\_Chen](https://discuss.elastic.co/u/Jinlian_Chen)\
**Post date:** [July 4, 2019, 3:48pm UTC](https://discuss.elastic.co/t/search-analyzer-not-work/188776/3 "2019-07-04T15:48:04Z")

</div>

> [@Jinlian\_Chen](#):
>
> match

That's because the match query will analyze the search value to a list of tokens?  
Will the analyzer work if email\_address is a list?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 4, 2019, 4:03pm UTC](https://discuss.elastic.co/t/search-analyzer-not-work/188776/4 "2019-07-04T16:03:32Z")

</div>

> [@Jinlian\_Chen](#):
>
> That's because the match query will analyze the search value to a list of tokens?

Yes.

> [@Jinlian\_Chen](#):
>
> Will the analyzer work if email\_address is a list?

It does not change anything in the behavior.

---

<div class="post-metadata">

**Author:** ![Jinlian\_Chen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jinlian_chen/32/44738_2.png) [@Jinlian\_Chen](https://discuss.elastic.co/u/Jinlian_Chen)\
**Post date:** [July 5, 2019, 2:58am UTC](https://discuss.elastic.co/t/search-analyzer-not-work/188776/5 "2019-07-05T02:58:39Z")

</div>

I tried this request, it didn't have token "@"

```
GET analyze_email/_analyze?pretty
{
  "field": "email_address",
  "text": "com@s.s"
}

```

Result:

```
{
  "tokens" : [
    {
      "token" : "com",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "s.s",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "s",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "<ALPHANUM>",
      "position" : 1
    }
  ]
}

```

Is this because I didn't define the patterns correctly? or caused by 'uax\_url\_email'?  
Expected tokens: com, s.s, s, @, com@s.s

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 5, 2019, 6:55am UTC](https://discuss.elastic.co/t/search-analyzer-not-work/188776/6 "2019-07-05T06:55:56Z")

</div>

I don't know. I'm not good at regex. May be it's the pattern capture. Did you try to debug how the analyzer transforms your text?

See [https://www.elastic.co/guide/en/elasticsearch/reference/current/\_explain\_analyze.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/_explain_analyze.html)

---

<div class="post-metadata">

**Author:** ![Jinlian\_Chen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jinlian_chen/32/44738_2.png) [@Jinlian\_Chen](https://discuss.elastic.co/u/Jinlian_Chen)\
**Post date:** [July 5, 2019, 8:25am UTC](https://discuss.elastic.co/t/search-analyzer-not-work/188776/7 "2019-07-05T08:25:45Z")

</div>

Thanks.  
I debug the analyzer.  
It's because the tokenizer `uax_url_email` drop some terms. I should try other way.

```
GET _analyze
{
  "tokenizer" : "uax_url_email",
  "text" : "com@s.s",
  "explain" : true
}

```

Response:

```
{
  "detail" : {
    "custom_analyzer" : true,
    "charfilters" : [],
    "tokenizer" : {
      "name" : "uax_url_email",
      "tokens" : [
        {
          "token" : "com",
          "start_offset" : 0,
          "end_offset" : 3,
          "type" : "<ALPHANUM>",
          "position" : 0,
          "bytes" : "[63 6f 6d]",
          "positionLength" : 1,
          "termFrequency" : 1
        },
        {
          "token" : "s.s",
          "start_offset" : 4,
          "end_offset" : 7,
          "type" : "<ALPHANUM>",
          "position" : 1,
          "bytes" : "[73 2e 73]",
          "positionLength" : 1,
          "termFrequency" : 1
        }
      ]
    },
    "tokenfilters" : []
  }
}
```

---

<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:** [August 2, 2019, 8:25am UTC](https://discuss.elastic.co/t/search-analyzer-not-work/188776/8 "2019-08-02T08:25:46Z")

</div>

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