0
votes

I need some help please, to write a correct Match_all Search Query to match all things inside my Index in Elasticsearch. I am using Elasticsearch 6.3.1. and Java 8.

I want to translate this Query in Java Low Level Rest Client API.

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

I tried something like this below, and it gives me nothing from the Index. I do now know where to put my Index name to search below,

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

the Code above returns me this, which is not the Index Content,

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

I tried this too , and did not work, bellow ,

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

the Result is,

{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}}}}
2

2 Answers

0
votes

What it looks like you are doing is printing the query/request and never actually execute the search request. Also the SearchRequest, SearchRequestBuilder and SearchSourceBuilder classes are used with the high level client, not the low level client. What you have to do is to initiate the low level rest client object and then execute your search request with your client instance. After that you can read your results from the response.

If you would like to use the high level client, which I myself like more, I would like to refer you to this part of the documentation.

0
votes

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"}}
´´´´