同一请求中的 Elasticsearch 组、过滤器和聚合

发布于 2025-01-13 00:55:59 字数 2693 浏览 7 评论 0原文

我有一个用例,用于在 Elasticsearch 中按两个单独的字段进行过滤和分组,同时聚合其余字段中的值。寻求帮助制定 Elasticsearch 查询。这是一个例子。每个文档都有一个 locationIdcategoryproductIdvisitCountpurchaseCount 和 <代码>样本计数。我想查看每个位置中每个类别的 visitCountpurchaseCountsampleCount 总和。请注意,productId 在所有条目中都是唯一的。我尝试阅读 Elasticsearch 文档,但找不到好的资源来学习如何进行分组、过滤和聚合。请注意,这是一个网站用例,我们在带有页面的表格中显示这些数据。由于位置和类别的数量,很可能会有多个组超出一页。请帮助制定 Elasticsearch 查询。

示例文档:

[{
    "locationId": 12345,
    "category": "Food",
    "productId": "JKHNG98",
    "visitCount": 10,
    "purchaseCount": 9,
    "sampleCount": 7
}, {
    "locationId": 12345,
    "category": "Food",
    "productId": "HJUSY68",
    "visitCount": 1,
    "purchaseCount": 15,
    "sampleCount": 7
}, {
    "locationId": 12345,
    "category": "Entertainment",
    "productId": "KGUJKHG78",
    "visitCount": 20,
    "purchaseCount": 15,
    "sampleCount": 10
}, {
    "locationId": 12345,
    "category": "Entertainment",
    "productId": "67912HYK",
    "visitCount": 5,
    "purchaseCount": 15,
    "sampleCount": 10
}, {
    "locationId": 54321,
    "category": "Food",
    "productId": "9823HYKN",
    "visitCount": 15,
    "purchaseCount": 12,
    "sampleCount": 5
}, {
    "locationId": 54321,
    "category": "Food",
    "productId": "KJHKJSAHD22",
    "visitCount": 55,
    "purchaseCount": 12,
    "sampleCount": 5
}, {
    "locationId": 54321,
    "category": "Entertainment",
    "productId": "SDJFHSF788",
    "visitCount": 45,
    "purchaseCount": 44,
    "sampleCount": 23
}, {
    "locationId": 54321,
    "category": "Entertainment",
    "productId": "2131286JH",
    "visitCount": 80,
    "purchaseCount": 44,
    "sampleCount": 23
}]

输入可以是多个位置 ID,但始终只有 1 个类别。

过滤器输入类别“食品”的预期结果:

[{
    "locationId": 12345,
    "category": "Food",
    "sumOfVisitCount": 11,
    "sumOfPurchaseCount": 24,
    "sumOfSampleCount": 14
},{
    "locationId": 54321,
    "category": "Food",
    "sumOfVisitCount": 15,
    "sumOfPurchaseCount": 12,
    "sumOfSampleCount": 5
}]

位置“12345”的过滤器输入的预期结果:

[{
    "locationId": 12345,
    "category": "Food",
    "sumOfVisitCount": 11,
    "sumOfPurchaseCount": 24,
    "sumOfSampleCount": 14
}, {
    "locationId": 12345,
    "category": "Entertainment"
    "sumOfVisitCount": 25,
    "sumOfPurchaseCount": 30,
    "sumOfSampleCount": 20
}]

位置“12345”和类别“食品”的过滤器输入的预期结果:

[{
    "locationId": 12345,
    "category": "Food",
    "sumOfVisitCount": 11,
    "sumOfPurchaseCount": 24,
    "sumOfSampleCount": 14
}]

I have a use case to filter and group by two separate fields in Elasticsearch, while aggregating values in rest of the fields. Looking for help formulating a query for Elasticsearch. Here is an example. Each document has a locationId, category, productId, visitCount, purchaseCount and sampleCount. I want to view sums of visitCount, purchaseCount and sampleCount for each category within each location. Note that productId is unique across all entries. I have tried reading up Elasticsearch documentation but could not find a good source to learn how I can do grouping, filtering and aggregation all together. Please note that this is for a website use case where we show this data in a table with pages. Due to the amount of locations and categories, it is likely that there will be several groups that will go beyond 1 page. Please help formulate a query for Elasticsearch.

Sample documents:

[{
    "locationId": 12345,
    "category": "Food",
    "productId": "JKHNG98",
    "visitCount": 10,
    "purchaseCount": 9,
    "sampleCount": 7
}, {
    "locationId": 12345,
    "category": "Food",
    "productId": "HJUSY68",
    "visitCount": 1,
    "purchaseCount": 15,
    "sampleCount": 7
}, {
    "locationId": 12345,
    "category": "Entertainment",
    "productId": "KGUJKHG78",
    "visitCount": 20,
    "purchaseCount": 15,
    "sampleCount": 10
}, {
    "locationId": 12345,
    "category": "Entertainment",
    "productId": "67912HYK",
    "visitCount": 5,
    "purchaseCount": 15,
    "sampleCount": 10
}, {
    "locationId": 54321,
    "category": "Food",
    "productId": "9823HYKN",
    "visitCount": 15,
    "purchaseCount": 12,
    "sampleCount": 5
}, {
    "locationId": 54321,
    "category": "Food",
    "productId": "KJHKJSAHD22",
    "visitCount": 55,
    "purchaseCount": 12,
    "sampleCount": 5
}, {
    "locationId": 54321,
    "category": "Entertainment",
    "productId": "SDJFHSF788",
    "visitCount": 45,
    "purchaseCount": 44,
    "sampleCount": 23
}, {
    "locationId": 54321,
    "category": "Entertainment",
    "productId": "2131286JH",
    "visitCount": 80,
    "purchaseCount": 44,
    "sampleCount": 23
}]

Input can be multiple location IDs but always just 1 category.

Expected result for filter input category "Food":

[{
    "locationId": 12345,
    "category": "Food",
    "sumOfVisitCount": 11,
    "sumOfPurchaseCount": 24,
    "sumOfSampleCount": 14
},{
    "locationId": 54321,
    "category": "Food",
    "sumOfVisitCount": 15,
    "sumOfPurchaseCount": 12,
    "sumOfSampleCount": 5
}]

Expected result for filter input of location "12345":

[{
    "locationId": 12345,
    "category": "Food",
    "sumOfVisitCount": 11,
    "sumOfPurchaseCount": 24,
    "sumOfSampleCount": 14
}, {
    "locationId": 12345,
    "category": "Entertainment"
    "sumOfVisitCount": 25,
    "sumOfPurchaseCount": 30,
    "sumOfSampleCount": 20
}]

Expected result for filter input of location "12345" and category "Food":

[{
    "locationId": 12345,
    "category": "Food",
    "sumOfVisitCount": 11,
    "sumOfPurchaseCount": 24,
    "sumOfSampleCount": 14
}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖风昔人 2025-01-20 00:56:00

您需要结合使用查询和聚合来获得所需的结果。以下查询使用:

过滤器输入类别“食品”的搜索查询

  1. 查询部分使用术语查询 匹配类别等于 Food
  2. 使用术语聚合 ,根据 locationId 创建存储桶,并查找 sampleCountviewCountpurchaseCount 的总和, <一使用 href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html" rel="nofollow noreferrer">求和聚合
POST idxtest/_search
{
  "size": 0,
  "query": {
    "term": {
      "category.keyword": "Food"
    }
  },
  "aggs": {
    "NAME": {
      "terms": {
        "field": "locationId"
      },
      "aggs": {
        "sumOfPurchaseCount": {
          "sum": {
            "field": "purchaseCount"
          }
        },
        "sumOfVisitCount": {
          "sum": {
            "field": "visitCount"
          }
        },
        "sumOfSampleCount": {
          "sum": {
            "field": "sampleCount"
          }
        }
      }
    }
  }
}

搜索结果:

"aggregations" : {
    "NAME" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 12345,
          "doc_count" : 2,
          "sumOfSampleCount" : {
            "value" : 14.0
          },
          "sumOfVisitCount" : {
            "value" : 11.0
          },
          "sumOfPurchaseCount" : {
            "value" : 24.0
          }
        },
        {
          "key" : 54321,
          "doc_count" : 2,
          "sumOfSampleCount" : {
            "value" : 10.0
          },
          "sumOfVisitCount" : {
            "value" : 70.0
          },
          "sumOfPurchaseCount" : {
            "value" : 24.0
          }
        }
      ]
    }

位置“12345”和类别“食品”的过滤器输入的搜索查询

POST idxtest/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "category.keyword": "Food"
          }
        },
        {
          "term": {
            "locationId": 12345
          }
        }
      ]
    }
  },
  "aggs": {
    "NAME": {
      "terms": {
        "field": "locationId"
      },
      "aggs": {
        "sumOfPurchaseCount": {
          "sum": {
            "field": "purchaseCount"
          }
        },
        "sumOfVisitCount": {
          "sum": {
            "field": "visitCount"
          }
        },
        "sumOfSampleCount": {
          "sum": {
            "field": "sampleCount"
          }
        }
      }
    }
  }
}

搜索结果:

"aggregations" : {
    "NAME" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 12345,
          "doc_count" : 2,
          "sumOfSampleCount" : {
            "value" : 14.0
          },
          "sumOfVisitCount" : {
            "value" : 11.0
          },
          "sumOfPurchaseCount" : {
            "value" : 24.0
          }
        }
      ]
    }
  }

You need to use a combination of query and aggregation to achieve your required result. Below query uses :

Search Query for filter input category "Food"

  1. Query part uses term query to match the documents that have category equal to Food
  2. Terms aggregation is used, to create buckets based on locationId, and in order to find the sum of sampleCount, viewCount and purchaseCount, sum aggregation is used
POST idxtest/_search
{
  "size": 0,
  "query": {
    "term": {
      "category.keyword": "Food"
    }
  },
  "aggs": {
    "NAME": {
      "terms": {
        "field": "locationId"
      },
      "aggs": {
        "sumOfPurchaseCount": {
          "sum": {
            "field": "purchaseCount"
          }
        },
        "sumOfVisitCount": {
          "sum": {
            "field": "visitCount"
          }
        },
        "sumOfSampleCount": {
          "sum": {
            "field": "sampleCount"
          }
        }
      }
    }
  }
}

Search Result:

"aggregations" : {
    "NAME" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 12345,
          "doc_count" : 2,
          "sumOfSampleCount" : {
            "value" : 14.0
          },
          "sumOfVisitCount" : {
            "value" : 11.0
          },
          "sumOfPurchaseCount" : {
            "value" : 24.0
          }
        },
        {
          "key" : 54321,
          "doc_count" : 2,
          "sumOfSampleCount" : {
            "value" : 10.0
          },
          "sumOfVisitCount" : {
            "value" : 70.0
          },
          "sumOfPurchaseCount" : {
            "value" : 24.0
          }
        }
      ]
    }

Search Query for filter input of location "12345" and category "Food"

POST idxtest/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "category.keyword": "Food"
          }
        },
        {
          "term": {
            "locationId": 12345
          }
        }
      ]
    }
  },
  "aggs": {
    "NAME": {
      "terms": {
        "field": "locationId"
      },
      "aggs": {
        "sumOfPurchaseCount": {
          "sum": {
            "field": "purchaseCount"
          }
        },
        "sumOfVisitCount": {
          "sum": {
            "field": "visitCount"
          }
        },
        "sumOfSampleCount": {
          "sum": {
            "field": "sampleCount"
          }
        }
      }
    }
  }
}

Search Result :

"aggregations" : {
    "NAME" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 12345,
          "doc_count" : 2,
          "sumOfSampleCount" : {
            "value" : 14.0
          },
          "sumOfVisitCount" : {
            "value" : 11.0
          },
          "sumOfPurchaseCount" : {
            "value" : 24.0
          }
        }
      ]
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文