【问题标题】:multi value nested field Aggregation in elasticsearchelasticsearch中的多值嵌套字段聚合
【发布时间】:2018-06-01 10:49:24
【问题描述】:

在elasticsearch 5.6中对多值和嵌套字段使用聚合时遇到了一个非常特殊的问题,我的索引映射如下:

{
"my_index": {
  "mappings": {
    "my_type": {
      "properties": {
        "my_field": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
             }
           },
           "country": {
             "type": "text",
             "fields": {
               "keyword": {
                 "type": "keyword",
                 "ignore_above": 256
               }
             }
           },
         }
       }
     }
   }
 }
}

我的数据是这样的:

"my_field": [
  {"name": "apple", "country": "USA"},
  {"name": "alibaba", "country": "CHINA"}
]

要求是:我得到一个查询词,例如apple,我用这个查询词搜索归档的name,最后,我想聚合 countryname 是查询词 apple。我的查询如下所示:

{"query": {
"nested": {"path": "my_field", "query": {"bool": {"should": [{"match": {"my_field.name.keyword": "apple"}}]}}}},
 "aggs": {"m_agg": {"nested": {"path": "my_field"},
                    "aggs": {"m1_agg": {"terms": {"field": "my_field.country.keyword"}}}}}}

所以输入是apple,期望结果是

"aggregations" : {
"m_agg" : {
  "doc_count" : 1,
  "m1_agg" : {
    "doc_count_error_upper_bound" : 0,
    "sum_other_doc_count" : 0,
    "buckets" : [
      {
        "key" : "USA",
        "doc_count" : 1
      }
    ]
  }
}
}

但弹性搜索返回结果:

"aggregations" : {
"m_agg" : {
  "doc_count" : 2,
  "m1_agg" : {
    "doc_count_error_upper_bound" : 0,
    "sum_other_doc_count" : 0,
    "buckets" : [
      {
        "key" : "USA",
        "doc_count" : 1
      },
      {
        "key" : "CHINA",
        "doc_count" : 1
      }
    ]
  }
}
}

如何更改查询 DSL,以获得预期结果?

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-nested


    【解决方案1】:

    在嵌套字段的情况下,查询部分不会影响聚合部分。

    要解决它,试试这个:

    {
      "size": 0,
      "aggregations": {
        "nested_agg": {
          "nested": {
            "path": "name"
          },
          "aggregations": {
            "bool_agg": {
              "filter": {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "my_field.name.keyword": "apple"
                      }
                    }
                  ]
                }
              },
              "aggregations": {
                "m_agg": {
                  "nested": {
                    "path": "my_field"
                  },
                  "aggregations": {
                    "m1_agg": {
                      "terms": {
                        "field": "my_field.country.keyword"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    Nested AggregationFilter Aggregation

    【讨论】:

      【解决方案2】:

      如果您需要对特定过滤值应用聚合。您必须在聚合中使用过滤器。

      在 Elastic 中,单独编写的查询/过滤器和聚合将分别产生,而不相互依赖。

      【讨论】:

        猜你喜欢
        • 2015-07-30
        • 2023-04-03
        • 1970-01-01
        • 2017-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-21
        • 2018-10-05
        相关资源
        最近更新 更多