弹性搜索返回对象与总和

发布于 2025-02-03 02:01:44 字数 2066 浏览 3 评论 0原文

我试图通过通过弹性搜索产生的收入来获取前100位客人的清单。为此,我使用术语sum congregation。但是,它确实返回正确的值,我想用聚合返回整个guest对象。

这是我的查询:

GET reservations/_search
{
  "size": 0, 
  "aggs": {
    "top_revenue": {
      "terms": {
        "field": "total",
        "size": 100,
        "order": {
          "top_revenue_hits": "desc"
        }
      },
      "aggs": {
        "top_revenue_sum": {
          "sum": {
            "field": "total"
          }
        }
      }
    }
  }
}

这返回了前100位客人的列表,但只有他们所花费的数量:

{
  "aggregations" : {
    "top_revenue" : {
      "doc_count_error_upper_bound" : -1,
      "sum_other_doc_count" : 498,
      "buckets" : [
        {
          "key" : 934.9500122070312,
          "doc_count" : 8,
          "top_revenue_hits" : {
          "value" : 7479.60009765625
          }
        },
        {
          "key" : 922.0,
          "doc_count" : 6,
          "top_revenue_hits" : {
          "value" : 5532.0
          }
        },
        ...
      ]
    }
  }
}

我如何获取查询以返回整个guests对象,而不仅仅是总和金额。

当我运行获取Reservations/_search时,它会返回:

{
  "hits": [
    {
      "_index": "reservations",
      "_id": "1334620",
      "_score": 1.0,
      "_source": {
        "id": "1334620",
        "total": 110.8,
        "payment": "unpaid",
        "contact": {
          "name": "John Doe",
          "email": "[email protected]"
        }
      }
    },
    ... other reservations
  ]
}

我想让它返回使用sum gentregation。

我尝试使用top_hits汇总,使用_Source它确实返回了整个guest> Guest对象,但并未显示所花费的总金额。并且在将_source添加到sum聚合时会出现错误。

我可以用sum聚合返回整个guest对象,还是这不是正确的方法?

I am trying to get a list of the top 100 guests by revenue generated with Elastic Search. To do this I am using a terms and a sum aggregation. However it does return the correct values, I wan to return the entire guest object with the aggregation.

This is my query:

GET reservations/_search
{
  "size": 0, 
  "aggs": {
    "top_revenue": {
      "terms": {
        "field": "total",
        "size": 100,
        "order": {
          "top_revenue_hits": "desc"
        }
      },
      "aggs": {
        "top_revenue_sum": {
          "sum": {
            "field": "total"
          }
        }
      }
    }
  }
}

This returns a list of the top 100 guests but only the amount they spent:

{
  "aggregations" : {
    "top_revenue" : {
      "doc_count_error_upper_bound" : -1,
      "sum_other_doc_count" : 498,
      "buckets" : [
        {
          "key" : 934.9500122070312,
          "doc_count" : 8,
          "top_revenue_hits" : {
          "value" : 7479.60009765625
          }
        },
        {
          "key" : 922.0,
          "doc_count" : 6,
          "top_revenue_hits" : {
          "value" : 5532.0
          }
        },
        ...
      ]
    }
  }
}

How can I get the query to return the entire guests object, not only the sum amount.

When I run GET reservations/_search it returns:

{
  "hits": [
    {
      "_index": "reservations",
      "_id": "1334620",
      "_score": 1.0,
      "_source": {
        "id": "1334620",
        "total": 110.8,
        "payment": "unpaid",
        "contact": {
          "name": "John Doe",
          "email": "[email protected]"
        }
      }
    },
    ... other reservations
  ]
}

I want to get this to return with the sum aggregation.

I have tried to use a top_hits aggregation, using _source it does return the entire guest object but it does not show the total amount spent. And when adding _source to the sum aggregation it gives an error.

Can I return the entire guest object with a sum aggregation or is this not the correct way?

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

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

发布评论

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

评论(1

西瑶 2025-02-10 02:01:44

我假设contact.name在映射中是关键字。以下查询应该为您工作。

{
  "size": 0,
  "aggs": {
    "guests": {
      "terms": {
        "field": "contact.name",
        "size": 100
      },
      "aggs": {
        "sum_total": {
          "sum": {
            "field": "total"
          }
        },
        "sortBy": {
          "bucket_sort": {
            "sort": [
              { "sum_total": { "order": "desc" } } 
            ]
          }
        },
        "guest": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}

I assumed that contact.name is keyword in the mapping. Following query should work for you.

{
  "size": 0,
  "aggs": {
    "guests": {
      "terms": {
        "field": "contact.name",
        "size": 100
      },
      "aggs": {
        "sum_total": {
          "sum": {
            "field": "total"
          }
        },
        "sortBy": {
          "bucket_sort": {
            "sort": [
              { "sum_total": { "order": "desc" } } 
            ]
          }
        },
        "guest": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文