gpt4 book ai didi

python - Elasticsearch:我想计算某个特定值在特定字段中出现了多少次

转载 作者:行者123 更新时间:2023-12-02 22:54:16 24 4
gpt4 key购买 nike

我有一个简单的问题,这正成为一个难题。我想计算一个特定值在我的elasticseach索引的特定字段中出现了多少次。

假设我有product.name,我想知道“Shampoo”一词在其中出现了多少次。

我知道terms聚合提供了所有唯一值以及它们的出现次数。但是,鉴于我拥有数十亿条记录,这对我不利。我不想采取额外的步骤在keys中找到我想要的值。

最佳答案

Filter aggregation

Defines a single bucket of all the documents in the current document set context that match a specified filter. Often this will be used to narrow down the current aggregation context to a specific set of documents.



可以将类似 value_count的聚合应用于过滤器聚合中返回的存储桶,以获取文档总数。

我已经将产品视为对象类型,如果它是嵌套数据类型,则需要使用嵌套查询代替简单术语查询。

制图
{
"index92" : {
"mappings" : {
"properties" : {
"product" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
}


查询:
{
"size": 0,
"aggs": {
"shampoo": {
"filter": {
"term": {
"product.name.keyword": "shampoo"
}
},"aggs": {
"count": {
"value_count": {
"field": "product.name.keyword"
}
}
}
}
}
}

结果:
 "aggregations" : {
"shampoo" : {
"doc_count" : 2,
"count" : {
"value" : 2
}
}
}

关于python - Elasticsearch:我想计算某个特定值在特定字段中出现了多少次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60632721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com