# Using Collapse in elastic low level client with Hibernate Search

**URL:** <https://discuss.elastic.co/t/using-collapse-in-elastic-low-level-client-with-hibernate-search/256616>\
**Category:** Elasticsearch\
**Created:** [November 25, 2020, 8:39am UTC](https://discuss.elastic.co/t/using-collapse-in-elastic-low-level-client-with-hibernate-search/256616 "2020-11-25T08:39:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daviddavid](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@daviddavid](https://discuss.elastic.co/u/daviddavid)\
**Post date:** [November 25, 2020, 8:39am UTC](https://discuss.elastic.co/t/using-collapse-in-elastic-low-level-client-with-hibernate-search/256616/1 "2020-11-25T08:39:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 25, 2020, 9:02am UTC](https://discuss.elastic.co/t/using-collapse-in-elastic-low-level-client-with-hibernate-search/256616/2 "2020-11-25T09:02:16Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html) mentions:

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

```

You can translate that to the LowLevel client with:

```auto
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\": \"user.id\" \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.

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

```

HTH

---

<div class="post-metadata">

**Author:** ![daviddavid](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@daviddavid](https://discuss.elastic.co/u/daviddavid)\
**Post date:** [November 25, 2020, 9:08am UTC](https://discuss.elastic.co/t/using-collapse-in-elastic-low-level-client-with-hibernate-search/256616/3 "2020-11-25T09:08:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 25, 2020, 9:09am UTC](https://discuss.elastic.co/t/using-collapse-in-elastic-low-level-client-with-hibernate-search/256616/4 "2020-11-25T09:09:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daviddavid](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@daviddavid](https://discuss.elastic.co/u/daviddavid)\
**Post date:** [November 25, 2020, 11:20am UTC](https://discuss.elastic.co/t/using-collapse-in-elastic-low-level-client-with-hibernate-search/256616/5 "2020-11-25T11:20:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 25, 2020, 1:47pm UTC](https://discuss.elastic.co/t/using-collapse-in-elastic-low-level-client-with-hibernate-search/256616/6 "2020-11-25T13:47:00Z")

</div>

I don't understand the question.

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 23, 2020, 1:47pm UTC](https://discuss.elastic.co/t/using-collapse-in-elastic-low-level-client-with-hibernate-search/256616/7 "2020-12-23T13:47:02Z")

</div>

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