【问题标题】:Elasticsearch nested cardinality aggregationElasticsearch 嵌套基数聚合
【发布时间】:2018-02-15 16:18:30
【问题描述】:

我有一个嵌套模式的映射,我正在尝试对嵌套字段进行聚合并按 docid 计数排序。

select name, count(distinct docid) as uniqueid from table
group by name
order by uniqueid desc 

以上是我想要做的。

{
   "size": 0,
   "aggs": {
      "samples": {
         "nested": {
            "path": "sample"
         },
         "aggs": {
            "sample": {
               "terms": {
                  "field": "sample.name",
        "order": {
                     "DocCounts": "desc"
                  }
               },
               "aggs": {
                  "DocCounts": {
                     "cardinality": {
                        "field": "docid"              
                     }
                  }
               }
            }
         }
      }
   }
}

但结果我没有得到预期的输出

结果:

"buckets": [
               {
                  "key": "xxxxx",
                  "doc_count": 173256,
                  "DocCounts": {
                     "value": 0
                  }
               },
               {
                  "key": "yyyyy",
                  "doc_count": 63,
                  "DocCounts": {
                     "value": 0
                  }
               }
]

我收到了DocCounts = 0。这不是预期的。我的查询出了什么问题。

【问题讨论】:

    标签: elasticsearch aggregation


    【解决方案1】:

    我认为你最后嵌套的aggregation 太多了。尝试摆脱它:

    {
      "size": 0,
      "aggs": {
        "samples": {
          "nested": {
            "path": "sample"
          },
          "aggs": {
            "sample": {
              "terms": {
                "field": "sample.name",
                "order": {
                  "DocCounts": "desc"
                }
              },
              "DocCounts": {
                "cardinality": {
                  "field": "docid"
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 它会返回错误。可能像这样找到了两个聚合类型定义。我已经试过了
    • "field": "sample.docid" 有没有可能解决您的问题?
    • 我不这么认为
    • 我不需要/使用有序设置,但嵌套聚合的其余部分工作,我只需要"field": "sample.docid" 就像提到的@core
    【解决方案2】:

    一般来说,在对嵌套类型按上层范围的值进行聚合时,我们观察到在存储文档时需要将上层范围的值放入/复制嵌套类型。

    那么在您的情况下,聚合将如下所示:

    "aggs": {
      "DocCounts": {
        "cardinality": {
           "field": "sample.docid"              
        }
      }
    }
    

    至少在 1.7 版的 Elasticsearch 上它可以在这种情况下工作。

    【讨论】:

      【解决方案3】:

      您可以在 DocCounts 的基数聚合之上使用反向嵌套聚合。这是因为在应用嵌套聚合时,查询会针对嵌套文档运行。因此,要访问嵌套文档中父文档的任何字段,可以使用反向嵌套聚合。更多信息请查看ES Reference

      您的基数查询将如下所示:

      "aggs": {
          "internal_DocCounts": {
              "reverse_nested": { },
              "DocCounts": {
                  "cardinality": {
                      "field": "docid"
                  }
              }
          }
      }
      

      响应将如下所示:

        "buckets": [
        {
          "key": "xxxxx",
          "doc_count": 173256,
          "internal_DocCounts": {
            "doc_count": 173256,
            "DocCounts": {
              "value": <some_value>
            }
          }
        },
        {
          "key": "yyyyy",
          "doc_count": 63,
          "internal_DocCounts": {
            "doc_count": 63,
            "DocCounts": {
              "value": <some_value>
            }
          }
        },
        .....
      

      检查这个similar thread

      【讨论】:

      • 谢谢..改进了答案
      猜你喜欢
      • 2021-12-14
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 2018-01-30
      • 2015-07-30
      相关资源
      最近更新 更多