gpt4 book ai didi

java - 在 java 中运行按函数分组的感知查询

转载 作者:行者123 更新时间:2023-12-01 08:48:56 25 4
gpt4 key购买 nike

我有感知查询来根据查询分组获取 mimetypeGroup -

GET /_all/explore/_search
{
"size": 0,
"aggs": {
"mimeTypeGroup": {
"terms": {
"field": "mimeTypeGroup"
}
}
}
}

如何在 java 代码中执行此查询。我已在 java 代码中尝试过此操作以获得结果。我需要的结果在下面添加 -

client.prepareSearch().setIndices(getIndexNames()).addAggregation(AggregationBuilders.cardinality("mimeTypeGroup").field("mimeTypeGroup")).setTypes("explore").execute().actionGet(ES_TIMEOUT_MS)

但它给了我 json -

{
"took": 13,
"timed_out": false,
"_shards": {
"total": 81,
"successful": 81,
"failed": 0
},
"hits": {
"total": 673,
"max_score": 1,
"hits": [
{
"_index": "209cc565-d640-4146-bc78-de77a86fad1a",
"_type": "explore",
"_id": "b550f807-65a7-425f-b61c-13b22973364c",
"_score": 1,
"_source": {
"owner": "ashish",
"mimeTypeGroup": "miscellaneous",
"fileExtension": "py",
"tags": [],
"source": "",
"sourceTags": {},
"parseException": null
}
},
{
"_index": "209cc565-d640-4146-bc78-de77a86fad1a",
"_type": "explore",
"_id": "a75d8527-eb02-4145-818d-4d778a42891e",
"_score": 1,
"_source": {
"owner": "ashish",
"mimeTypeGroup": "miscellaneous",
"fileExtension": "sh",
"tags": [],
"source": "",
"sourceTags": {},
"parseException": null
}
},
{
"_index": "209cc565-d640-4146-bc78-de77a86fad1a",
"_type": "explore",
"_id": "74962cab-06d1-4c8a-879f-70ecbeae9a2f",
"_score": 1,
"_source": {
"owner": "ashish",
"mimeTypeGroup": "application",
"fileExtension": "xls",
"tags": [],
"source": "",
"sourceTags": {},
"parseException": null
}
},
{
"_index": "209cc565-d640-4146-bc78-de77a86fad1a",
"_type": "explore",
"_id": "30ab6a1d-190c-4516-89fd-86a2299e0bce",
"_score": 1,
"_source": {
"owner": "ashish",
"mimeTypeGroup": "miscellaneous",
"fileExtension": "cs",
"tags": [],
"source": "",
"sourceTags": {},
"parseException": null
}
},
{
"_index": "209cc565-d640-4146-bc78-de77a86fad1a",
"_type": "explore",
"_id": "7c9083d9-4d45-4a2a-bc11-6cb8fd7f5b9c",
"_score": 1,
"_source": {
"owner": "ashish",
"mimeTypeGroup": "application",
"fileExtension": "odp",
"tags": [],
"source": "",
"sourceTags": {},
"parseException": null
}
},
{
"_index": "209cc565-d640-4146-bc78-de77a86fad1a",
"_type": "explore",
"_id": "d98ee797-38fa-4272-a256-894c9cdad6e0",
"_score": 1,
"_source": {
"owner": "ashish",
"mimeTypeGroup": "miscellaneous",
"fileExtension": "accdb",
"tags": [],
"source": "",
"sourceTags": {},
"parseException": null
}
},
{
"_index": "209cc565-d640-4146-bc78-de77a86fad1a",
"_type": "explore",
"_id": "81c276c7-e760-4235-918f-20f1b344186f",
"_score": 1,
"_source": {
"owner": "ashish",
"mimeTypeGroup": "application",
"fileExtension": "xml",
"tags": [],
"source": "",
"sourceTags": {},
"parseException": null
}
},
{
"_index": "209cc565-d640-4146-bc78-de77a86fad1a",
"_type": "explore",
"_id": "04322cf5-3e84-4ae3-918b-e23c65abab3c",
"_score": 1,
"_source": {
"owner": "ashish",
"mimeTypeGroup": "miscellaneous",
"fileExtension": "mdb",
"tags": [],
"source": "",
"sourceTags": {},
"parseException": null
}
},
{
"_index": "209cc565-d640-4146-bc78-de77a86fad1a",
"_type": "explore",
"_id": "a91b8280-b144-4997-80d0-930608fe68cb",
"_score": 1,
"_source": {
"owner": "ashish",
"mimeTypeGroup": "miscellaneous",
"fileExtension": "sql",
"tags": [],
"source": "",
"sourceTags": {},
"parseException": null
}
},
{
"_index": "209cc565-d640-4146-bc78-de77a86fad1a",
"_type": "explore",
"_id": "0908e985-4ca0-4046-89e6-9c565941dbe0",
"_score": 1,
"_source": {
"owner": "ashish",
"mimeTypeGroup": "application",
"fileExtension": "gz",
"tags": [],
"source": "",
"sourceTags": {},
"parseException": null
}
}
]
},
"aggregations": {
"mimeTypeGroup": {
"value": 5
}
}
}

但我想要的结果是 -

{
"took": 18,
"timed_out": false,
"_shards": {
"total": 81,
"successful": 81,
"failed": 0
},
"hits": {
"total": 673,
"max_score": 0,
"hits": []
},
"aggregations": {
"mimeTypeGroup": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "miscellaneous",
"doc_count": 52
},
{
"key": "application",
"doc_count": 28
},
{
"key": "text",
"doc_count": 6
},
{
"key": "image",
"doc_count": 4
},
{
"key": "audio",
"doc_count": 2
}
]
}
}
}

最佳答案

您正在进行的聚合是基数聚合,(您需要使用术语聚合来代替)

client.prepareSearch().setIndices(getIndexNames()).addAggregation(
AggregationBuilders.cardinality("mimeTypeGroup").field("mimeTypeGroup")
).setTypes("explore").execute().actionGet(ES_TIMEOUT_MS)

使用术语聚合来获取术语聚合的响应(如 json 所示),并使用 setSize(0) 来删除搜索响应。

client.prepareSearch().setIndices(getIndexNames()).addAggregation(
AggregationBuilders.terms("mimeTypeGroup").field("mimeTypeGroup")
).setTypes("explore").setSize(0).execute().actionGet(ES_TIMEOUT_MS)

关于java - 在 java 中运行按函数分组的感知查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42508137/

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