如何在低级Rest Client API中编写Match_all搜索查询

时间:2019-07-09 11:50:27

标签: elasticsearch

我需要一些帮助,以编写正确的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,索引= [try1],indexesOptions = IndicesOptions [id = 38,ignore_unavailable = false,allow_no_indices = true,expand_wildcards_open = true,expand_wildcards_closed = false,allow_aliases_to_multiple_indices = true,forless_indices = true,for ,类型= [],路由=“空”,首选项=“空”,requestCache = null,滚动= null,maxConcurrentShardRequests = 0,batchedReduceSize = 512,preFilterShardSize = 128,allowPartialSearchResults = null,source = {“ query”:{ “ match_all”:{“ boost”:1.0}}}}

2 个答案:

答案 0 :(得分:0)

您正在执行的操作是打印查询/请求,而从不实际执行搜索请求。 SearchRequest,SearchRequestBuilder和SearchSourceBuilder类也与高级客户端一起使用,而不与低级客户端一起使用。您需要做的是initiate the low level rest client对象,然后将execute your search request与客户实例一起使用。之后,您可以read your results from the response

如果您想使用我自己更喜欢的高级客户端,我想带您参考this part of the documentation

答案 1 :(得分:0)

我已经在低级客户端(如波纹管)中使用RestClient API解决了该问题,

´´´´
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 : {"_index":"try1","_type":"_doc","_id":"1","_version":4,"found":true,"_source":{"my_id":"6","gender":"Ahoiii"}}
´´´´