# Custom 'pattern' analyzer for the field doesn't work

**URL:** <https://discuss.elastic.co/t/custom-pattern-analyzer-for-the-field-doesnt-work/114701>\
**Category:** Elasticsearch\
**Created:** [January 9, 2018, 12:03pm UTC](https://discuss.elastic.co/t/custom-pattern-analyzer-for-the-field-doesnt-work/114701 "2018-01-09T12:03:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maria-grigorieva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maria-grigorieva/32/26385_2.png) [@maria-grigorieva](https://discuss.elastic.co/u/maria-grigorieva)\
**Post date:** [January 9, 2018, 12:03pm UTC](https://discuss.elastic.co/t/custom-pattern-analyzer-for-the-field-doesnt-work/114701/1 "2018-01-09T12:03:52Z")

</div>

Hi all,

I need a piece of wisdom concerning analyzer usage.  
I want to use "pattern" analyzer with pattern "\W|\_ " for "name" field in my index.  
So, I created the mapping file as the following:  
`{ "settings": { "analysis": { "analyzer": { "my_analyzer": { "type": "pattern", "pattern": "\\W|_ ", "lowercase": true } } } }, "my_type": { "properties": { "name": { "type": "text", "analyzer": "my_analyzer", "search_analyzer": "my_analyzer" } } } }`

Then, I created index:  
`curl -XPUT 'http://localhost:9200/test_index' --data-binary "@mapping.json"; echo`

and inserted one test document:  
`curl -XPOST 'http://localhost:9200/test_index/my_type/' -d '{ "name": "mc16_13TeV:mc16_13TeV.361022.Pythia8EvtGen_A14NNPDF23LO_jetjet_JZ2W.simul.HITS.e3668_s2997_tid09756488_00" }'`

Using 'my\_analyzer' there must be tokens like `'mc16', '13tev', '361022', 'pythia2evtgen', ....`  
But it turns out there's no such tokens and Elastic is still using 'standard' analyzer...

For example, this query:  
`curl -XGET 'http://localhost:9200/test_index/_search' -d ' { "query": { "match": { "name": { "query": "13tev" } } } }'`

returns 0 hits...

It is strange, that the analyzer doesn't work, as it is defined for the field in mapping and documentation says that this is the first place where analyzer is being searched.

I'm using ES version 5.4.1.

Probably someone have any ideas of such a strange ES behaviour?

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 9, 2018, 12:13pm UTC](https://discuss.elastic.co/t/custom-pattern-analyzer-for-the-field-doesnt-work/114701/2 "2018-01-09T12:13:44Z")

</div>

Great start! You're just missing the `mappings` section in which your field declarations should go:

```
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "type": "pattern",
          "pattern": "\\W|_ ",
          "lowercase": true
        }
      }
    }
  },
  "mappings": { <--- add this section
    "my_type": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "my_analyzer",
          "search_analyzer": "my_analyzer"
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![maria-grigorieva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maria-grigorieva/32/26385_2.png) [@maria-grigorieva](https://discuss.elastic.co/u/maria-grigorieva)\
**Post date:** [January 9, 2018, 12:17pm UTC](https://discuss.elastic.co/t/custom-pattern-analyzer-for-the-field-doesnt-work/114701/3 "2018-01-09T12:17:03Z")

</div>

Omg! Thank you )) and I'm sorry for inattention to json's structure...

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 9, 2018, 12:18pm UTC](https://discuss.elastic.co/t/custom-pattern-analyzer-for-the-field-doesnt-work/114701/4 "2018-01-09T12:18:21Z")

</div>

Cool, glad it helped 😉

---

<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:** [February 6, 2018, 12:18pm UTC](https://discuss.elastic.co/t/custom-pattern-analyzer-for-the-field-doesnt-work/114701/5 "2018-02-06T12:18:22Z")

</div>

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