当前位置: 首页 > 面试题库 >

如何通过使用Elasticsearch聚合返回唯一文档的数量

秦胡媚
2023-03-14
问题内容

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

这是我们的模型的一个例子:

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

我想在状态字段上进行汇总,但是由于“ 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比命中总数大得多。因此,必须有重复项。

谢谢!


问题答案:

我认为您需要reverse_nested聚合,因为您希望基于嵌套值进行聚合,但实际上是在计算ROOT文档,而不是嵌套文档

{
  "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是NESTED文档计数,那么不要以为您正在计算根文档而对这些数字感到困惑,因为计数是嵌套的。因此,对于具有三个匹配的嵌套文档的文档,计数为3,而不是1。



 类似资料:
  • 问题内容: 我对Elasticsearch世界真的很陌生。 比方说,我有两个字段嵌套聚集:与: 这段代码可以完美地工作,并且给我这样的东西: 现在,我需要排除所有小于1000的聚合结果,而改为: 是否可以在查询正文中设置此需求?还是我必须在调用者布局中执行过滤器(在我的情况下为javascript)? 提前致谢 问题答案: 下次,M’sieur Toph’:RTFM! 我真的很傻:问了30秒后,我

  • 问题内容: 我将如何从记录中获取所有值,并使它们唯一。 记录 询问 任何帮助都会很棒。 问题答案: 您可以使用术语聚合。 搜索将返回如下内容: 聚合中的参数指定要包含在聚合结果中的最大术语数。如果需要所有结果,请将其设置为大于数据中唯一术语数的值。

  • 如何从记录中获取所有的值并使它们唯一。 记录 查询 任何帮助都会很好。

  • 问题内容: 我正在尝试计算具有唯一嵌套字段值的文档(以及文档本身)。看起来获得唯一文档有效。但是,当我尝试执行的请求时,出现如下错误: 禁止:org.elasticsearch.client.ResponseException:方法[POST],主机 [http:// localhost:9200] ,URI [/ package / _count?ignore_throttled = true&

  • 假设我有以下JSON结构,我希望按性别分组,并希望在同一字段中返回多个文档值: 现在我知道我可以做这样的事情,但是我需要把年龄和名字合并到一个字段中。

  • 但邮差回信说: 知道为什么或如何进一步调试它吗?Spring data-elasticsearch在做我不明白的事情吗? 我应该期待这样的事情: