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


(Maria Grigorieva) #1

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?


(Val Crettaz) #2

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

(Maria Grigorieva) #3

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


(Val Crettaz) #4

Cool, glad it helped :wink:


(system) #5

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