Elasticsearch,过滤全文字符串

Elasticsearch, filter on full-text string

我刚开始使用 Elasticsearch,我必须处理同事生成的数据。我注意到每个字符串数据都是一个全文值:

{
    "countryId": {
      "type": "string"
}

但我们从来不需要进行全文搜索,因此使用过滤器搜索的精确值就可以了。唯一的问题是这些值的类型暂时无法更改,因为时间不够。

所以我的问题是:如果我对全文值进行基于过滤器的搜索,会发生什么情况?是否会像使用 match search 那样分析搜索条件,或者过滤器会忽略此值的全文类型并将其作为精确值处理,从而节省大量搜索时间,因为过滤器是真的很快吗?

我查看了文档和周围但无法得到明确的答案。

您可能已经根据经验观察到尝试此操作时会发生什么,但是为了使 term 过滤器按预期运行(与指定字段中的指定参数完全匹配),索引的映射必须将字段的 index 属性 定义为 not_analyzedterm 过滤器的官方文档是 here,但最直接相关的部分可能是:

Filters documents that have fields that contain a term (not analyzed).

因此,您的索引应该具有类似于以下定义的映射:

{"mappings" : {"the_document_type": {
  "countryId" : {"type" : "string", "index" : "not_analyzed"},
  ...
  ... Mappings for other fields in your document
  ...
}}}

鉴于上面的代码片段,包含 term 过滤器的查询要求文档与 countryId 的某些指定参数完全匹配,应该会成功。类似于以下内容:

{"query" : {"filtered" :
  "query" : {"match_all" : {}},
  "filter" : {"term" : {"countryId" : "Kingdom of Anvilania"}}
}}

Elasticsearch here 中有 string 类型(以及所有其他核心类型)的进一步文档,但关于 index 属性的具体部分是这样的:

Set to analyzed for the field to be indexed and searchable after being broken down into token using an analyzer. not_analyzed means that its still searchable, but does not go through any analysis process or broken down into tokens. no means that it won’t be searchable at all (as an individual field; it may still be included in _all). Setting to no disables include_in_all. Defaults to analyzed.