首页 > 解决方案 > 如何在 Low Level Rest Client API 中编写 Match_all 搜索查询

问题描述

我需要一些帮助,编写一个正确的 Match_all 搜索查询来匹配我在 Elasticsearch 中的索引中的所有内容。我正在使用 Elasticsearch 6.3.1。和 Java 8。

我想在 Java Low Level Rest Client API 中翻译这个查询。

GET try1/_search
{
  "query": {
    "match_all": {}
  }
}

我在下面尝试了类似的方法,但它没有从索引中给我任何东西。我现在知道在哪里放置我的索引名称以在下面搜索,

SearchRequestBuilder sr = new SearchRequestBuilder(client, SearchAction.INSTANCE)
                            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                            .setQuery(QueryBuilders.matchAllQuery());

上面的代码返回给我这个,这不是索引内容,

{"query":{"match_all":{"boost":1.0}}}

我也试过这个,但没有奏效,波纹管,

SearchRequest searchRequest  = new SearchRequest("try1");
                    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
                    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
                    searchRequest.source(searchSourceBuilder);

结果是,

{searchType=QUERY_THEN_FETCH, indices=[try1], indicesOptions=IndicesOptions[id=38, ignore_unavailable=false, allow_no_indices=true, expand_wildcards_open=true, expand_wildcards_closed=false, allow_aliases_to_multiple_indices=true, forbid_closed_indices=true, ignore_aliases=false], types=[], routing='null', preference='null', requestCache=null, scroll=null, maxConcurrentShardRequests=0, batchedReduceSize=512, preFilterShardSize=128, allowPartialSearchResults=null, source={"query":{"match_all":{"boost":1.0}}}}

标签: elasticsearch

解决方案


I have resolved it using the RestClient API in Low Level client like bellow ,

´´´´
RestClient restClient = RestClient.builder(
                            new HttpHost("localhost", 9200, "http")).build();

Response response1 = restClient.performRequest("GET","/try1/_doc/1");//here is the //secret
RequestLine requestLine = response1.getRequestLine();
HttpHost host = response1.getHost();
int statusCode = response1.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString( response1.getEntity());
System.out.println("result is : " + responseBody);
´´´´

Result is ,

´´´´
result is : {"_index":"try1","_type":"_doc","_id":"1","_version":4,"found":true,"_source":{"my_id":"6","gender":"Ahoiii"}}
´´´´

推荐阅读