【问题标题】:Elasticsearch 7.8 Nested Aggregation not returning correct dataElasticsearch 7.8 嵌套聚合未返回正确数据
【发布时间】:2020-08-16 23:24:24
【问题描述】:

我已经苦苦挣扎了一个星期,试图从 Elasticsearch 嵌套聚合索引中获取正确的数据。下面是我的索引映射和插入的两个示例文档。我想找到的是:

  1. 匹配字段 xforms.sentence.tokens.value 等于 24 的所有文档
  2. 在匹配的文档集中,对按 xforms.sentence.tokens.tag 分组的匹配进行计数,其中 xforms.sentence.tokens.value 等于 24

因此,在下面插入的文档中,我期望的输出是:

{"JJ": 1, "NN": 1}

{
  "_doc": {
    "_meta": {},
    "_source": {},
    "properties": {
      "originalText": {
        "type": "text"
      },
      "testDataId": {
        "type": "text"
      },
      "xforms": {
        "type": "nested",
        "properties": {
          "sentence": {
            "type": "nested"
          },
          "predicate": {
            "type": "nested"
          }
        }
      },
      "corpusId": {
        "type": "text"
      },
      "row": {
        "type": "text"
      },
      "batchId": {
        "type": "text"
      },
      "processor": {
        "type": "text"
      }
    }
  }
}

插入的示例文档如下:

{
    "_id": "28",
    "_source": {
        "testDataId": "5e97e9bef033448b893e485baa0fdf15",
        "originalText": "Some text with the word 24",
        "xforms": [{
            "sentence": {
                "tokens": [{
                        "lemma": "Some",
                        "index": 1,
                        "after": " ",
                        "tag": "JJ",
                        "value": "Some"
                    },
                    {
                        "lemma": "text",
                        "index": 2,
                        "after": " ",
                        "tag": "NN",
                        "value": "text"
                    },
                    {
                        "lemma": "with",
                        "index": 3,
                        "after": " ",
                        "tag": "NN",
                        "value": "with"
                    },
                    {
                        "lemma": "the",
                        "index": 4,
                        "after": "",
                        "tag": "CD",
                        "value": "the"
                    },
                    {
                        "lemma": "word",
                        "index": 5,
                        "after": " ",
                        "tag": "CC",
                        "value": "word"
                    },
                    {
                        "lemma": "24",
                        "index": 6,
                        "after": " ",
                        "tag": "JJ",
                        "value": "24"
                    }
                ],
                "type": "RAW"
            },
            "originalSentence": "Some text with the word 24 in it",
            "id": "e724611d8c024bcb8f0158b60e3df87e"
        }]
    }
},
{
    "_id": "56",
    "_source": {
        "testDataId": "5e97e9bef033448b893e485baa0fad15",
        "originalText": "24 word",
        "xforms": [{
            "sentence": {
                "tokens": [{
                        "lemma": "24",
                        "index": 1,
                        "after": " ",
                        "tag": "NN",
                        "value": "24"
                    },
                    {
                        "lemma": "word",
                        "index": 2,
                        "after": " ",
                        "tag": "JJ",
                        "value": "word"
                    }
                ],
                "type": "RAW"
            },
            "originalSentence": "24 word",
            "id": "e724611d8c024bcb8f0158b60e3d123"
        }]
    }
}

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    扩展@Gibbs 的答案,@N Kiram 您还需要将tokens 设置为nested

    {
      "xforms":{
        "type":"nested",
        "properties":{
          "sentence":{
            "type":"nested",
            "properties":{
              "tokens":{              <----
                "type":"nested"
              }
            }
          },
          "predicate":{
            "type":"nested"
          }
        }
      }
    }
    

    只有这样,您的 aggs 才会产生正确的计数:

    {
      "aggregations":{
        "xforms":{
          "doc_count":8,
          "inner":{
            "doc_count":2,
            "tag_count":{
              "doc_count_error_upper_bound":0,
              "sum_other_doc_count":0,
              "buckets":[
                {
                  "key":"JJ",
                  "doc_count":1
                },
                {
                  "key":"NN",
                  "doc_count":1
                }
              ]
            }
          }
        }
      }
    }
    

    旁注:您必须重新索引才能应用更改的映射。

    【讨论】:

    • 谢谢你成功了。关于嵌套字段的 ES 文档似乎有点偏离,因为这些示例只讨论了一层嵌套字段。第二个问题,如果我可以?在同一个索引上是否可以使用通配符执行聚合过滤器,即代替 24 我执行 24*?
    【解决方案2】:
    {
      "aggs": {
        "xforms": {
          "nested": { //Nested aggregation
            "path": "xforms.sentence"
          },
          "aggs": {
            "inner": { //Counting only within the matching doc
              "filter": {
                "bool": {
                  "filter": { //Filtering docs with value=24
                    "terms": {
                      "xforms.sentence.tokens.value": [
                        "24"
                      ]
                    }
                  }
                }
              },
            "aggs" : {
              "tag_count":{ //On filtered doc, doing terms aggregation on tag's keyword version as tag is of type text
                "terms":{
                  "field":"xforms.sentence.tokens.tag.keyword"
                }
              }
            }
            }
          }
        }
      }
    }
    

    它提供以下输出

    "aggregations": {
            "xforms": {
                "doc_count": 2,
                "inner": {
                    "doc_count": 2,
                    "tag_count": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "JJ",
                                "doc_count": 2
                            },
                            {
                                "key": "NN",
                                "doc_count": 2
                            },
                            {
                                "key": "CC",
                                "doc_count": 1
                            },
                            {
                                "key": "CD",
                                "doc_count": 1
                            }
                        ]
                    }
                }
            }
        }
    

    【讨论】:

    • 是的,但该响应不正确。因为“24”只有在计数应该是 1 而不是 2 时才被标记为“JJ”
    猜你喜欢
    • 2023-03-17
    • 2021-12-14
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2020-08-26
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多