Haney
Haney

Reputation: 34922

Elasticsearch Analyze API Oddity

My question is fairly simple. Say that I have a type mapping in an index that looks like this:

"mappings" : {
  "post" : {
    "analyzer" : "my_custom_analyzer",
    "properties" : {
      "body" : {
        "type" : "string",
        "store" : true
      }
    }
  }
}

Note that I specified my_custom_analyzer as the analyzer for the type. When I search the body field without specifying an analyzer in the query, I expect my_custom_analyzer to be used. However, when I use the Analyze API to query the field:

curl http://localhost:9200/myindex/_analyze?field=post.body&text=test

It returns standard analysis results for string. When I specify the analyzer it works:

curl http://localhost:9200/myindex/_analyze?analyzer=my_custom_analyzer&text=test

My question is: why doesn't the Analyze API use the default type analyzer when I specify a field?

Upvotes: 2

Views: 522

Answers (1)

Vineeth Mohan
Vineeth Mohan

Reputation: 19283

Analyzer is per string field. You cant apply it over an object or nested object and hope all the fields under that object field will inherit that analyzer. The right approach is as follows -

"mappings" : {
  "post" : {
    "properties" : {
      "body" : {
        "type" : "string",
        "analyzer" : "my_custom_analyzer",
        "store" : true
      }
    }
  }
}

The reason the analyzer worked for analyzer API is because you have declared analyzer for that index.

If you want to define analyzer for all the string fields under a particular object ,you need to mention that in the type template. You can get more information about that here - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-root-object-type.html#_dynamic_templates

Upvotes: 2

Related Questions