Dataframe 具有某些字段和某些条件的弹性搜索中的查询?
我有关于产品的数据,其中有一些字段_id、Shop、ProductVerion。。。。它在弹性搜索中建立了索引。我想用商店搜索产品的最高产品版本 例: 版本可能是相同的 现在,我想得到以下产品:Dataframe 具有某些字段和某些条件的弹性搜索中的查询?,dataframe,apache-spark,elasticsearch,Dataframe,Apache Spark,elasticsearch,我有关于产品的数据,其中有一些字段_id、Shop、ProductVerion。。。。它在弹性搜索中建立了索引。我想用商店搜索产品的最高产品版本 例: 版本可能是相同的 现在,我想得到以下产品: Shop Amazon and ProducVersion 333 or Shop Ebay and ProductVersion 444 or Shop Alibaba and ProductVersion 444. 但我不知道。 请帮帮我 如果您提供可复制的示例会更好,但看起来您需要使用:和: 我
Shop Amazon and ProducVersion 333
or Shop Ebay and ProductVersion 444
or Shop Alibaba and ProductVersion 444.
但我不知道。
请帮帮我 如果您提供可复制的示例会更好,但看起来您需要使用:和:
我用一些示例文档进行了尝试。我将版本字段保留为数字字段 这些是我尝试使用的示例文档
[
{
"_index": "test",
"_type": "doc",
"_id": "12334",
"_score": 1,
"_source": {
"shopName": "amazon",
"version": 341
}
},
{
"_index": "test",
"_type": "doc",
"_id": "123",
"_score": 1,
"_source": {
"shopName": "amazon",
"version": 3412
}
},
{
"_index": "test",
"_type": "doc",
"_id": "1233",
"_score": 1,
"_source": {
"shopName": "amazon",
"version": 341
}
},
{
"_index": "test",
"_type": "doc",
"_id": "1238",
"_score": 1,
"_source": {
"shopName": "alibaba",
"version": 34120
}
},
{
"_index": "test",
"_type": "doc",
"_id": "1239",
"_score": 1,
"_source": {
"shopName": "alibaba",
"version": 3414
}
},
{
"_index": "test",
"_type": "doc",
"_id": "123910",
"_score": 1,
"_source": {
"shopName": "alibaba",
"version": 124
}
}
]
正如@demas所指定的,我继续使用术语聚合和热门搜索聚合
indexName/_search
{
"size": 0,
"aggs": {
"shop": {
"terms": {
"field": "shopName.keyword"
},
"aggs": {
"product": {
"top_hits": {
"size": 1,
"sort": [
{
"version": {
"order": "desc"
}
}
]
}
}
}
}
}
}
这将为您提供包含每个商店的最高产品版本号的文档,如下所示
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 0,
"hits": []
},
"aggregations": {
"shop": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "alibaba",
"doc_count": 3,
"product": {
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "1238",
"_score": null,
"_source": {
"shopName": "alibaba",
"version": 34120
},
"sort": [
34120
]
}
]
}
}
},
{
"key": "amazon",
"doc_count": 3,
"product": {
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "123",
"_score": null,
"_source": {
"shopName": "amazon",
"version": 3412
},
"sort": [
3412
]
}
]
}
}
}
]
}
}
}
如果您向我们展示您的映射会更容易。请向我们提供一些样本文档作为json元素以及相关字段的映射。请提供您的elasticsearch版本和您使用的映射的详细信息/example Document我在这里没有看到提及的版本!谢谢你的回答!我不知道答案是否正确,我看不到显示的结果数量。嗨,我只使用聚合来获取详细信息。它拥有每个shopName版本号最高的产品的文档。您还可以通过检查聚合结果中的total字段来查看每个shopName的可用文档数。我已经测试了此查询,但它似乎仍然采用较低的版本。如何检查聚合结果中的total字段??您可以通过检查每个shopname聚合结果的total字段来查看它。你能提供你的索引映射吗。产品版本号有可能以文本格式保存。这可能是您获得较低产品版本结果的原因。
indexName/_search
{
"size": 0,
"aggs": {
"shop": {
"terms": {
"field": "shopName.keyword"
},
"aggs": {
"product": {
"top_hits": {
"size": 1,
"sort": [
{
"version": {
"order": "desc"
}
}
]
}
}
}
}
}
}
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 0,
"hits": []
},
"aggregations": {
"shop": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "alibaba",
"doc_count": 3,
"product": {
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "1238",
"_score": null,
"_source": {
"shopName": "alibaba",
"version": 34120
},
"sort": [
34120
]
}
]
}
}
},
{
"key": "amazon",
"doc_count": 3,
"product": {
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "123",
"_score": null,
"_source": {
"shopName": "amazon",
"version": 3412
},
"sort": [
3412
]
}
]
}
}
}
]
}
}
}