如何使用Elasticsearch聚合返回唯一文档计数

15

我遇到一个问题,就是在elasticsearch中仅使用嵌套字段的术语聚合无法返回唯一文档的计数。

以下是我们模型的示例:

{
    ...,
    "location" : [
        {"city" : "new york", "state" : "ny"},
        {"city" : "woodbury", "state" : "ny"},
        ...
    ],
    ...
}

我想在state字段上进行聚合,但由于文档中出现了两次“ny”,所以该文档将在“ny”存储桶中计算两次。

因此,我想知道是否有一种方法可以获取不同文档的计数。

映射:

people = {
  :properties => {
    :location => {
      :type => 'nested',
      :properties => {
        :city => {
          :type => 'string',
          :index => 'not_analyzed',
        },
        :state => {
          :type => 'string',
          :index => 'not_analyzed',
        },
      }
    },
    :last_name => {
      :type => 'string',
      :index => 'not_analyzed'
    }
  }
}

查询非常简单:

curl -XGET 'http://localhost:9200/people/_search?pretty&search_type=count' -d '{
  "query" : {
    "bool" : {
      "must" : [
        {"term" : {"last_name" : "smith"}}
      ]
    }
  },
  "aggs" : {
    "location" : {
      "nested" : {
        "path" : "location"
      },
      "aggs" : {
        "state" : {
          "terms" : {"field" : "location.state", "size" : 10}
        }
      }
    }
  }
}'

响应:

{
  "took" : 104,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1248513,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "location" : {
      "doc_count" : 2107012,
      "state" : {
        "buckets" : [ {
          "key" : 6,
          "key_as_string" : "6",
          "doc_count" : 214754
        }, {
          "key" : 12,
          "key_as_string" : "12",
          "doc_count" : 168887
        }, {
          "key" : 48,
          "key_as_string" : "48",
          "doc_count" : 101333
        } ]
      }
    }
  }
}

doc_count比hit中的总数要大得多,因此必定存在重复项。

谢谢!


请发布您正在使用的索引映射和查询,否则我无法帮助您。 - Andrei Stefan
@AndreiStefan 我已更新映射和查询。谢谢! - milodky
1个回答

22

我认为你需要一个reverse_nested聚合,因为你想要基于嵌套值进行聚合,但实际上是计算根文档而不是嵌套的文档。

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "last_name": "smith"
          }
        }
      ]
    }
  },
  "aggs": {
    "location": {
      "nested": {
        "path": "location"
      },
      "aggs": {
        "state": {
          "terms": {
            "field": "location.state",
            "size": 10
          },
          "aggs": {
            "top_reverse_nested": {
              "reverse_nested": {}
            }
          }
        }
      }
    }
  }
}

因此,您将看到类似于以下内容:

"aggregations": {
      "location": {
         "doc_count": 6,
         "state": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "ny",
                  "doc_count": 4,
                  "top_reverse_nested": {
                     "doc_count": 2
                  }
               },
               {
                  "key": "ca",
                  "doc_count": 2,
                  "top_reverse_nested": {
                     "doc_count": 2
                  }
               }
            ]
         }
      }
   }

你要查找的内容在top_reverse_nested部分下。 需要注意的一点是:如果我没记错的话,"doc_count": 6是指嵌套文档的数量,因此不要被这些数字所迷惑,以为你正在计算根文档的数量,实际上应该是嵌套文档的数量。因此,对于一个有三个匹配的嵌套文档的文档来说,计数应该是3而不是1。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接