在java中运行弹性搜索查询

时间:2017-02-23 11:09:08

标签: java elasticsearch sense

我正在创建一个java spring示例来获取弹性搜索的结果。 我想在java sample中运行感知查询 -

GET /_all/explore/_search?search_type=count
{
  "aggs": {
    "metaDatas": {
      "terms": {
        "field": "metaData",
        "size": 1000
      }
    }
  }
} 

为此我试过 -

 String query = "{\n"

                + " \"aggs\": {\n"
                + " \"metaDatas\": {\n"
                + " \"terms\": {\n"
                + " \"field\": \"metaData\",\n"
                + "  \"size\": 1000\n"
        +"}\n"
                +"}\n"
                +"}\n"
                +"}\n";
        QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder('"' + query + '"');
        SearchRequestBuilder requestBuilder = client.prepareSearch()
                .setTypes("explore")
                .setScroll(new TimeValue(ES_TIMEOUT_MS))
                .setQuery(queryBuilder);
        requestBuilder.setFrom(0);
        requestBuilder.setSize(DEFAULT_SEARCH_PAGE_SIZE);
        SearchResponse response = requestBuilder.execute().actionGet(ES_TIMEOUT_MS);
        SearchHits hits = response.getHits();

但它有0次点击。从某种意义上说,它给出了以下结果 -

{
   "took": 32,
   "timed_out": false,
   "_shards": {
      "total": 78,
      "successful": 78,
      "failed": 0
   },
   "hits": {
      "total": 535,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "metaDatas": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "textplain",
               "doc_count": 229
            },
            {
               "key": "applicationxml",
               "doc_count": 162
            },
            {
               "key": "imagejpeg",
               "doc_count": 22
            },
            {
               "key": "textxwebmarkdown",
               "doc_count": 18
            },
            {
               "key": "textxjavaproperties",
               "doc_count": 13
            }
         ]
      }
   }
}

如何在java中获得此结果。

0 个答案:

没有答案