2017-02-28 93 views
0

我有意識的查詢來獲得根據查詢組mimetypeGroup -潤感通過查詢功能

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

我在Java code.I如何執行這個查詢已經在我的Java代碼嘗試這得到結果。我需要的結果是低於added-

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 
       } 
      ] 
     } 
    } 
} 

回答

1

的aggregration你做的是基數aggregration ,(您需要使用術語聚合代替)

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) 
+0

非常感謝... –