How to write Match_all Search Query in Low Level Rest Client API

Hello folks,
i need some help please , to write a correct Search Query to match all things inside my Index in Elasticsearch .

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

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

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

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

the Code above returns me this ,

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

Can Anybody please help me to write the Search Query to match_all in the right way using Low Level Client Rest API ???

thx.

Are you sure you want to use the low level client and not the high level one?

yes i want to give the Low Level Client a try , i use actually the TransportClient , to Creat Index and send Data to Elasticsearch , is there any Option , to use my TransportClient in order to write the Match_all Search Query in a right Way???
I am not using the RestClient , is there any different between both????

I Used the High Level Client too like this bellow , and still even did not give me the Content of the Index ,

try (RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(HttpHost.create("http://localhost:9200")))) {
            SearchRequest searchRequest = new SearchRequest("try1");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            searchRequest.source(searchSourceBuilder);
            System.out.println("searchrequest is : " + searchRequest);
}

The Result of this High Level Client Query 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}}}}

Nothing seems to work correctly . Cause i need the Content of the Index under "_source".

Any Suggestion i will be thankfull.

I tried again with the RestClient API insead of TransportClient like bellow,

     RestClient restClient = RestClient.builder(
                            new HttpHost("localhost", 9200, "http")).build();
                    RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
                    Response response1 = restClient.performRequest("GET","try1");//here the IndexName
                    RequestLine requestLine = response1.getRequestLine();
                    HttpHost host = response1.getHost();
                    int statusCode = response1.getStatusLine().getStatusCode();
                    //org.apache.http.Header[] headers = response1.getHeaders();
                    String responseBody = EntityUtils.toString( response1.getEntity());
                    System.out.println("result is : " + responseBody);

The Result seems to be better, cause it returns the Typs of all Fields of my Index , like bellow ,

{"try1":{"aliases":{},"mappings":{"_doc":{"properties":{"my_id":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"field":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"gender":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}}},"settings":{"index":{"creation_date":"1561710794108","number_of_shards":"5","number_of_replicas":"1","uuid":"IWI-z1wPRXiUt3N3RINEYw","version":{"created":"6030199"},"provided_name":"try1"}}}}

I just need the Values inseatd of the Typs of Fields . Is there any Option to do that ??

thx

So i have resolved it like bellow , but still have a littel Question.

     RestClient restClient = RestClient.builder(
                            new HttpHost("localhost", 9200, "http")).build();
                    //RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
                   // Map<String, String> params = Collections.singletonMap("gender", "Ahoiii");

           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();
                    //org.apache.http.Header[] headers = response1.getHeaders();
                    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"}}

I need to write this

Response response1 = restClient.performRequest("GET","/try1/_doc/1"); 

correctly to give me all Entity with their different "_id's" , not just the "_doc" with "_id =1 ".
Cause in my Index there are more than one _id .
Can you please help me do this??

thx.

Could you express what you want to do with a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Then when we know exactly what you want to do, we can translate that to Java code.

I just want to translet the Match_all Search Query using either TransportClient in Low Level Rest Client, or RestClient in Low Level Rest Client.
I want just the same Result wich this Query gives , but on my Programm Console using one of the above Low Level Clients.
This Search query will give me all Content of my Index , that mean every Entity and every Field with its Value too .

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

That is all the Idea about.
thx.

Easiest way:

try (RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(HttpHost.create("http://localhost:9200")))) {
    SearchResponse response = client.search(new SearchRequest("test"), RequestOptions.DEFAULT);
} catch (Exception e) {
    e.printStackTrace(System.err);
}

Or:

try (RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(HttpHost.create("http://localhost:9200")))) {
    SearchResponse response = client.search(new SearchRequest("test")
            .source(new SearchSourceBuilder().query(
                    QueryBuilders.matchAllQuery()
                    )
            ), RequestOptions.DEFAULT);
} catch (Exception e) {
    e.printStackTrace(System.err);
}

i tried both , they did not work , and throw me this Failure,

faild Elasticsearch exception [type=illegal_argument_exception, reason=request [/test/_search] contains unrecognized parameters: [ccs_minimize_roundtrips], [ignore_throttled]]

what is the Reason of that ?
thx

Because you are not using the same version for the client and the server most likely.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.