# Need help with co-occurrence of values in nested documents across documents

**URL:** <https://discuss.elastic.co/t/need-help-with-co-occurrence-of-values-in-nested-documents-across-documents/253718>\
**Category:** Elasticsearch\
**Created:** [October 29, 2020, 3:21pm UTC](https://discuss.elastic.co/t/need-help-with-co-occurrence-of-values-in-nested-documents-across-documents/253718 "2020-10-29T15:21:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![verdict-lg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/verdict-lg/32/76310_2.png) [@verdict-lg](https://discuss.elastic.co/u/verdict-lg)\
**Post date:** [October 29, 2020, 3:21pm UTC](https://discuss.elastic.co/t/need-help-with-co-occurrence-of-values-in-nested-documents-across-documents/253718/1 "2020-10-29T15:21:10Z")

</div>

Hi,

Say this is my document and Book Meta is a nested field

```auto
{
        {
        "Book ID": 1,
        "Book Meta":
        [
            {
                "Chapter_ID" : 1,
                "Chapter_Gist": "<some-text-here>",
                "Chapter_Tag": "tag-1"
            },
            {
                "Chapter_ID" : 2,
                "Chapter_Gist": "<some-text-here>",
                "Chapter_Tag": "tag-2"
            }
        ]
    },
    {
        "Book ID": 2,
        "Book Meta":
        [
            {
                "Chapter_ID" : 1,
                "Chapter_Gist": "<some-text-here>",
                "Chapter_Tag": "tag-2"
            },
            {
                "Chapter_ID" : 2,
                "Chapter_Gist": "<some-text-here>",
                "Chapter_Tag": "tag-3"
            },
            {
                "Chapter_ID" : 3,
                "Chapter_Gist": "<some-text-here>",
                "Chapter_Tag": "tag-1"
            }
        ]
    }
}

```

In the above I need aggregations as follows:  
{tag-1, tag-2} appear in book-1 and book-2, but other 2 cooccurrences {tag-1, tag-3}, {tag-2, tag-3} appear only in book-2, so the aggregation should give me

```auto
tag-1, tag-2 : 2
tag-2, tag-3 : 1
tag-1, tag-3 : 1

```

Does combination of include\_in\_parent to get a flat field in parent and use painless script to concatenate those values and then count work OR is there any other way to do this. Any help would be great

---

<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:** [November 26, 2020, 3:21pm UTC](https://discuss.elastic.co/t/need-help-with-co-occurrence-of-values-in-nested-documents-across-documents/253718/2 "2020-11-26T15:21:12Z")

</div>

Closed on November 26, 2020, 3:21pm UTC

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