运行 在 java 中按功能分组的意义查询

Run sense query in java for group by function

我有根据查询分组获取 mimetypeGroup 的意义查询 -

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

我如何在 java code.I 中执行此查询已在我的 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)