Using Collapse in elastic low level client with Hibernate Search

Using Collapse in elastic low level client with Hibernate Search.
How to use Collapse in Java Code as it is working in query search in browser with Collapse on field but with elastic low level client with Hibernate Search, how to do it in java code.

I think this is a question you need to ask in Hibernate Search forums.

Here we can just tell how you can use the High Level Rest Client or the Low Level Rest Client.
But to answer your question related to "how to do collapse with the Low Level client", here is an answer.

The documentation mentions:

GET /my-index-000001/_search
  "query": {
    "match": {
      "message": "GET /search"
  "collapse": {
    "field": ""                
  "sort": [ "http.response.bytes" ],  
  "from": 10                          

You can translate that to the LowLevel client with:

RestClient client = RestClient.builder(HttpHost.create("http://localhost:9200")).build();
Request request = new Request("GET",  "/my-index-000001/_search");
request.setJsonEntity("{\n" +
        "  \"query\": {\n" +
        "    \"match\": {\n" +
        "      \"message\": \"GET /search\"\n" +
        "    }\n" +
        "  },\n" +
        "  \"collapse\": {\n" +
        "    \"field\": \"\"                \n" +
        "  },\n" +
        "  \"sort\": [ \"http.response.bytes\" ],  \n" +
        "  \"from\": 10                          \n" +
Response response = client.performRequest(request);

Then you will need to parse the response.
You can use Jackson for that.

JsonNode tree = new ObjectMapper().readTree(response.getEntity().getContent());


1 Like

Thank you for answering!

Is there any JAVA classes example like CollapseBuilder and its usage which can be used with Low Level Client or can be used only as string like you have set in request.setJsonEntity( .....) . I mean is there any way with Java classes whihc can be used with low level client for Collapse Builder

If you want to use the CollapseBuilder classes, you should use the HLCLient instead.

Is there anyway to get the count after collapsed result. After few records are removed still the total count gives all values and not left after removed results. How to update the total_hit also in same response.

I don't understand the question.

Could you provide 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 is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

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