# Collapse with multi value

**URL:** <https://discuss.elastic.co/t/collapse-with-multi-value/270437>\
**Category:** Elasticsearch\
**Created:** [April 17, 2021, 3:08am UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437 "2021-04-17T03:08:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![arcturuscom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arcturuscom/32/65123_2.png) [@arcturuscom](https://discuss.elastic.co/u/arcturuscom)\
**Post date:** [April 17, 2021, 3:08am UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/1 "2021-04-17T03:08:10Z")

</div>

Hello,  
i tried to collapse multivalued field and its doesnt work  
e.g:

```auto
"field": [id1, id2, id3] 

```

i'm asking if there's a workaround to solve this issue.  
any suggestions please !!

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 17, 2021, 3:58am UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/2 "2021-04-17T03:58:40Z")

</div>

Hi @arcturuscom

Just to help us understand.

Can you show what the desired input and result you want? (What do you want it to look like)

What did you try that failed?

How or what did the fail result in / error?

---

<div class="post-metadata">

**Author:** ![arcturuscom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arcturuscom/32/65123_2.png) [@arcturuscom](https://discuss.elastic.co/u/arcturuscom)\
**Post date:** [April 17, 2021, 8:35pm UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/3 "2021-04-17T20:35:18Z")

</div>

this is what i get

```auto
      {
        "shard" : 0,
        "index" : "whitespace-autres-1",
        "node" : "-pvtzg-jSm2tXGNwadcRlw",
        "reason" : {
          "type" : "illegal_state_exception",
          "reason" : "failed to collapse 120067, the collapse field must be single valued"
        }
      },

```

my quer is simple :

```auto
POST legan_test/_search?explain
{
  "from": 0,
  "size": 10,
  "collapse": {
    "field": "combiningIds.keyword"
  }
}

```

the combingIds mapping is :

```auto
            "combiningIds" : {
              "type" : "text",
              "norms" : false,
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },

```

this field is multivalue

```auto
            "combiningIds" : [
              "1234567",
              "09747",
              "119999888
            ]

```

we process some rdf files, each document contains a list of combiningIds we retrieve them and we index them in Elasticsearch  
we consider that 2 documents should be collapsed if there's atleast one element shared between them.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 18, 2021, 1:11am UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/4 "2021-04-18T01:11:16Z")

</div>

Ok now I understand... Exactly as your title stated 🙂

Unfortunately as I understand it and as the documentation [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/collapse-search-results.html) states

_The field used for collapsing must be a single valued keyword or numeric field with doc\_values activated_

Collapse does not work with arrays of keyword values. It can not chose a value out of the array.

That is exactly the error you are getting, which make sense because your field `combiningIds.keyword` is an array.

---

<div class="post-metadata">

**Author:** ![arcturuscom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arcturuscom/32/65123_2.png) [@arcturuscom](https://discuss.elastic.co/u/arcturuscom)\
**Post date:** [April 18, 2021, 3:45am UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/5 "2021-04-18T03:45:00Z")

</div>

Thanks @stephenb  
Do you have any suggestion or workaround?  
I'm thinking about each element in the table on a separate document but i don't think that's a good idea in terms of disk space  
Any hints please

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 18, 2021, 2:29pm UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/6 "2021-04-18T14:29:50Z")

</div>

Can you provide 5 or 6 sample docs and the desired results perhaps we can take a look...

The array might work with a different approach and yes de-normalization can be an option but there could be many docs.

How many documents do you have?

I am more interested in what the result you are looking for, even if collapse "worked" I am unclear what the expected result was.

---

<div class="post-metadata">

**Author:** ![arcturuscom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arcturuscom/32/65123_2.png) [@arcturuscom](https://discuss.elastic.co/u/arcturuscom)\
**Post date:** [April 18, 2021, 5:51pm UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/7 "2021-04-18T17:51:36Z")

</div>

> de-normalization can be an option but there could be many docs.

this is true. we have millions of documents

as i mentioned, my main use case is to collapse documents that they are in relationship between them instead of flat them in search result. we want to keep places for some other relevant documents.

giving an example, a document could have 5 version, and i want to display only the last version ( i know how to do this ) and collapse the old version instead of make them available in the search result. we want to avoid this because we want to increase our relevancy and make a place to the other relevant document instead of displaying 5 different version of the same document.

let consider in this example that combiningIds field like a versions field that contains something like

```auto
versions: [1,2,3,4,5]

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 18, 2021, 9:44pm UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/8 "2021-04-18T21:44:19Z")

</div>

Apologies but I do not have a solution for you ... I cannot tell what you want the output to be.

Perhaps someone else will.

I think without some sample simplified docs and what you want the output to be it will be difficult for anyone to suggest a solution.

---

<div class="post-metadata">

**Author:** ![arcturuscom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arcturuscom/32/65123_2.png) [@arcturuscom](https://discuss.elastic.co/u/arcturuscom)\
**Post date:** [April 18, 2021, 10:23pm UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/9 "2021-04-18T22:23:07Z")

</div>

![combining](https://us1.discourse-cdn.com/elastic/original/3X/d/3/d33aebb14c30d69b56f3f5eaa92269fc91bb2f33.png)  
nothing interesting in my document  
they contains some fields like title, text, category, date and combiningIds.

we consider that a document is in relationship with another if both sharing atleast one combiningId.

the screenshot above visualize the relationship between the documents.  
let say that i want to collapse document id:2 and 3 with id:1 or document 2 with 4

---

<div class="post-metadata">

**Author:** ![Jason\_Slater](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jason_slater/32/63484_2.png) [@Jason\_Slater](https://discuss.elastic.co/u/Jason_Slater)\
**Post date:** [April 18, 2021, 10:49pm UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/10 "2021-04-18T22:49:56Z")

</div>

Could you accomplish what you want with Elasticsearch Graph API?

> **[Graph: Explore Connections in Elasticsearch Data | Elastic](https://www.elastic.co/what-is/elasticsearch-graph)**
>
> Graph is an API- and UI-driven tool that helps you surface relevant relationships in your data while leveraging Elasticsearch features like distributed query execution, real-time data availability, and indexing at any scale.

> **[Graph explore API | Elasticsearch Guide \[7.12\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/graph-explore-api.html)**

---

<div class="post-metadata">

**Author:** ![arcturuscom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arcturuscom/32/65123_2.png) [@arcturuscom](https://discuss.elastic.co/u/arcturuscom)\
**Post date:** [April 18, 2021, 11:36pm UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/11 "2021-04-18T23:36:43Z")

</div>

Thnaks @Jason_Slater it's sounds interessting

---

<div class="post-metadata">

**Author:** ![Jason\_Slater](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jason_slater/32/63484_2.png) [@Jason\_Slater](https://discuss.elastic.co/u/Jason_Slater)\
**Post date:** [April 19, 2021, 5:46am UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/12 "2021-04-19T05:46:24Z")

</div>

if you have some data, such as this example:

```
{"some_id":123,"some_field":["id1","id2","id3"]}
{"some_id":456,"some_field":["id2","id3","id4"]}
{"some_id":789,"some_field":["id3","id4","id5"]}

```

where you want to find connections by the values in "some\_field"

I ingested this sample as NLJSON into "some-index" and created an index pattern for it.

In Graph, if I query for some\_id: 123, the connections are apparent:

 ![Screen Shot 2021-04-18 at 10.57.12 PM](https://us1.discourse-cdn.com/elastic/original/3X/d/8/d8c8da262a46d688dc0fff9b99585342ed5057a7.png)

For reference, my settings are here:

 ![Screen Shot 2021-04-18 at 10.58.23 PM](https://us1.discourse-cdn.com/elastic/original/3X/b/3/b370f9830675d250aca44153d46b75ba5dc8fa82.png)

Clicking on "Inspect" will reveal the API query and response behind the graph.

 ![Screen Shot 2021-04-18 at 10.41.15 PM](https://us1.discourse-cdn.com/elastic/original/3X/9/9/999c4f2a57ade75981bb395d685a633eb8b57406.png)

Hope this helps, or at least provides some food for thought.

---

<div class="post-metadata">

**Author:** ![arcturuscom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arcturuscom/32/65123_2.png) [@arcturuscom](https://discuss.elastic.co/u/arcturuscom)\
**Post date:** [April 19, 2021, 5:38pm UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/13 "2021-04-19T17:38:17Z")

</div>

@Jason_Slater very interesting, i'm curious to know if it's possible to display reponse in format :

```auto
hits : [
  {
      some_id: 123,
      inner_hits: [
          {some_id: 456}, {some_id: 789}
     ]  
  }
]

```

i want to display somehting similar to this :

 ![UI_result](https://us1.discourse-cdn.com/elastic/original/3X/1/d/1d11ef878986cd517449dbcc1723d4fc468c9ea7.png)

PS: document 456, 789 shouldn't be appeared on the other search cards as they are already collapsed in the first

---

<div class="post-metadata">

**Author:** ![wn1099](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wn1099/32/82228_2.png) [@wn1099](https://discuss.elastic.co/u/wn1099)\
**Post date:** [April 21, 2021, 11:15am UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/14 "2021-04-21T11:15:16Z")

</div>

Just a random thought. If you create a hash based on multi-value field and then apply collapse on that hashed-field?

---

<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:** [May 19, 2021, 11:15am UTC](https://discuss.elastic.co/t/collapse-with-multi-value/270437/15 "2021-05-19T11:15:30Z")

</div>

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