Elasticsearch 分析 API 奇怪

Elasticsearch Analyze API Oddity

我的问题很简单。假设我在索引中有一个类型映射,如下所示:

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

请注意,我将 my_custom_analyzer 指定为该类型的分析器。当我在查询中没有指定分析器的情况下搜索 body 字段时,I expect my_custom_analyzer to be used。但是,当我使用 Analyze API 查询字段时:

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

它returns 字符串的标准分析结果。当我指定分析器时,它起作用了:

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

我的问题是:当我指定字段时,为什么 Analyze API 不使用默认类型分析器?

分析器是针对每个字符串字段的。 您不能将它应用于对象或嵌套对象,并希望该对象字段下的所有字段都将继承该分析器。 正确的做法如下-

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

分析器为分析器工作的原因 API 是因为您已经为该索引声明了分析器。

如果要为特定对象下的所有字符串字段定义分析器,需要在类型模板中提及。您可以在此处获得更多相关信息 - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-root-object-type.html#_dynamic_templates