【问题标题】:Checking if field exists under an elasticsearch nested aggregation检查弹性搜索嵌套聚合下是否存在字段
【发布时间】:2015-07-12 10:05:52
【问题描述】:

尝试执行 ES 查询时,我在尝试对数组中的对象进行嵌套过滤时遇到了问题。我们的数据结构已经改变:

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

到:

 "_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":"....."
        }
    ]
 }

此嵌套过滤器适用于具有新数据结构的索引:

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

但是,当遍历具有较旧数据结构的索引时,它会失败,因此无法添加该功能。理想情况下,我能够找到这两种数据结构,但此时我会接受“优雅的失败”,即不返回结构旧的结果。

我尝试在“custom_data.key”字段上添加“exists”过滤器,并在“custom_data.className”字段的“not”中添加“exists”,但我不断收到“SearchParseException[[events_2015-07 -01][0]: from[-1],size[-1]: Parse Failure [Failed to parse source"

【问题讨论】:

    标签: database elasticsearch nested nosql


    【解决方案1】:

    有一个indices filter(和查询),您可以使用它来根据它所针对的索引执行条件过滤器(和查询)。

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

    使用它,您应该能够从旧映射过渡到新映射。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 2018-08-22
      • 2021-08-16
      相关资源
      最近更新 更多