简体   繁体   English

检查弹性搜索嵌套聚合下是否存在字段

[英]Checking if field exists under an elasticsearch nested aggregation

Trying to perform an ES query, I ran into a problem while trying to do a nested filtering of objects in an array. 尝试执行ES查询时,在尝试对数组中的对象进行嵌套过滤时遇到了一个问题。 Our structure of data has changed from being: 我们的数据结构已从以下方面改变:

 "_index": "events_2015-07-08",
 "_type": "my_type",
 "_source":{
    ...
    ...
    "custom_data":{
        "className:"....."
    }
 }

to: 至:

 "_index": "events_2015-07-08",
 "_type": "my_type",
 "_source":{
    ...
    ...
    "custom_data":[   //THIS CHANGED FROM AN OBJECT TO AN ARRAY OF OBJECTS
        {
          "key":".....",
         "val":"....."
        },
        {
          "key":".....",
         "val":"....."
        }
    ]
 }

this nested filter works fine on indices that have the new data structure: 此嵌套过滤器在具有新数据结构的索引上可以正常工作:

{
     "nested": { 
         "path": "custom_data",
         "filter": {
             "bool": {
                 "must": [                                
                    {
                        "term": 
                            {
                             "custom_data.key": "className"
                            }
                    }, 
                    {
                         "term": {
                             "custom_data.val": "SOME_VALUE"
                         }
                     }
                  ]
              }
          },
          "_cache": true   
      }
 }

However, it fails when going over indices that have the older data structure, so that feature cannot be added. 但是,当遍历具有较旧数据结构的索引时,它将失败,因此无法添加该功能。 Ideally I'd be able to find both data structures but at this point i'd settle for a "graceful failure" ie just don't return results where the structure is old. 理想情况下,我将能够找到两个数据结构,但在这一点上,我会解决“异常失败”的问题,即不要在结构旧的地方返回结果。

I have tried adding an "exists" filter on the field "custom_data.key", and an "exists" within "not" on the field "custom_data.className", but I keep getting "SearchParseException[[events_2015-07-01][0]: from[-1],size[-1]: Parse Failure [Failed to parse source" 我尝试在字段“ custom_data.key”上添加“存在”过滤器,并在字段“ custom_data.className”上的“非”内部添加“存在”,但是我一直在获取“ SearchParseException [[events_2015-07-01] [0]:来自[-1],大小[-1]:解析失败[无法解析源代码“

There is an indices filter (and query) that you can use to perform conditional filters (and queries) based on the index that it is running against. 有一个indices过滤器 (和查询),您可以根据它针对其运行的索引来执行条件过滤器(和查询)。

{
  "query" : {
    "filtered" : {
      "filter" : {
        "indices" : {
          "indices" : ["old-index-1", "old-index-2"],
          "filter" : {
            "term" : {
              "className" : "SOME_VALUE"
            }
          },
          "no_match_filter" : {
            "nested" : { ... }
          }
        }
      }
    }
  }
}

Using this, you should be able to transition off of the old mapping and onto the new mapping. 使用此功能,您应该能够从旧的映射过渡到新的映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM