# Combining records that have the same id with a query

**URL:** <https://discuss.elastic.co/t/combining-records-that-have-the-same-id-with-a-query/324479>\
**Category:** Elasticsearch\
**Created:** [February 1, 2023, 9:15pm UTC](https://discuss.elastic.co/t/combining-records-that-have-the-same-id-with-a-query/324479 "2023-02-01T21:15:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chachew](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@chachew](https://discuss.elastic.co/u/chachew)\
**Post date:** [February 1, 2023, 9:15pm UTC](https://discuss.elastic.co/t/combining-records-that-have-the-same-id-with-a-query/324479/1 "2023-02-01T21:15:46Z")

</div>

I have records for call logs that i want to query and combine into 1 result per record instead of 2. This is for logging of calls from one person to another. The person that initiates the call has the 'initiator' flag set to true and the receiver has that flag set to false.

I can query for each of the people separately and that works fine, the problem is when i want to get a record of the complete transaction between the two, i get back both sets of records, while i only want one record with data from both combined.

Below are my records in ES with a sample of the output that i want, that i cant figure out how to write a query for.

```auto
{ ... other records }
// Outbound record
{
  "id": 211,
  "initiator" : true,
  "source_name": "John Doe",
  "destination_name": "",
  "type": 0
}

// Inbound record
{
  "id": 211,
  "initiator" : false,
  "source_name": "",
  "destination_name": "Jane Smith",
  "type": 4
}
{ ... other records }

----- Desired output
{
  "id": 211,
  "initiator" : true,
  "source_name": "John Doe",
  "destination_name": "Jane Smith",
  "type": 0
}

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [February 1, 2023, 9:18pm UTC](https://discuss.elastic.co/t/combining-records-that-have-the-same-id-with-a-query/324479/2 "2023-02-01T21:18:25Z")

</div>

Welcome to our community! 😃

Will [Collapse search results | Elasticsearch Guide [8.6] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.6/collapse-search-results.html) do what you want?

---

<div class="post-metadata">

**Author:** ![chachew](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@chachew](https://discuss.elastic.co/u/chachew)\
**Post date:** [February 1, 2023, 9:24pm UTC](https://discuss.elastic.co/t/combining-records-that-have-the-same-id-with-a-query/324479/3 "2023-02-01T21:24:45Z")

</div>

Ive run across that in the docs but im not entirely sure how to use it, the documentation its not very clear to me. My results could change based on the 'initiator' and 'type' fields.

If 'initiator' == true and 'type' == 0 then i want the result output one way, if 'initiator' == true and 'type' == 4 then the results should be slightly different. Can collapse do that?

---

<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:** [March 1, 2023, 9:25pm UTC](https://discuss.elastic.co/t/combining-records-that-have-the-same-id-with-a-query/324479/4 "2023-03-01T21:25:12Z")

</div>

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