禁用对象中的 ElasticSearch 查询字段

Wil*_*ill 6 elasticsearch

我有一个 Elastic Search 6.8.7 集群。

我有一列包含此映射:

"event_object": { "enabled": false, "type": "object" }
Run Code Online (Sandbox Code Playgroud)

我想搜索与某些其他条件匹配的记录,并且该对象中的特定字段也具有特定值。

到目前为止,我已经尝试过对索引字段进行正常搜索的各种变体,以及对未索引字段进行过滤脚本的方法:

GET /my_index/_search
{
  "query":{
    "bool":{
      "must":{
        "query_string": {
          "query": "foo:bar"
        }
      }, 
      "filter": {
        "script": {
          "script": {
            "source": "doc[\"event_object\"][\"state\"].value == \"R\""
          }
        }
      }
    }
  },
  "terminate_after":1000, 
  "from":0,
  "size":1000
}
Run Code Online (Sandbox Code Playgroud)

这是基于谷歌搜索的自我测试的大杂烩。但我什至无法编译,更不用说运行和过滤了。

Val*_*Val 6

无法访问具有enabled: false. 来自官方文档

Elasticsearch 完全跳过对该字段内容的解析。仍然可以从字段检索 JSON _source,但无法搜索或以任何其他方式存储

因此,即使编写脚本也无济于事。

但是,有一种方法可以通过聚合中的脚本访问此禁用的数据terms(使用include参数和top_hits子聚合):

POST test/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "state": {
      "terms": {
        "script": "params._source.event_object.state",
        "size": 100,
        "include": "R"
      },
      "aggs": {
        "hits": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

你会得到这样的回应:

  "aggregations" : {
    "state" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "R",
          "doc_count" : 1,
          "hits" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "test",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_score" : 1.0,
                  "_source" : {
                    "event_object" : {
                      "state" : "R"
                    },
                    "test" : "hello"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
Run Code Online (Sandbox Code Playgroud)