# Elastic Search Query for cross document reference counts

**URL:** <https://discuss.elastic.co/t/elastic-search-query-for-cross-document-reference-counts/263061>\
**Category:** Elasticsearch\
**Created:** [February 3, 2021, 4:38am UTC](https://discuss.elastic.co/t/elastic-search-query-for-cross-document-reference-counts/263061 "2021-02-03T04:38:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mobius](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@mobius](https://discuss.elastic.co/u/mobius)\
**Post date:** [February 3, 2021, 4:38am UTC](https://discuss.elastic.co/t/elastic-search-query-for-cross-document-reference-counts/263061/1 "2021-02-03T04:38:10Z")

</div>

I have emails in the index which look like these:

```auto
    {
        "emailId" : 1,
        "to" : "a@example.com",
        "from" : "b@example.com",
        "subject" : "xyz subject"
    },
    {
        "emailId" : 2,
        "to" : "b@example.com",
        "from" : "c@example.com",
        "subject" : "abc subject"
    },
    {
        "emailId" : 3,
        "to" : "c@example.com",
        "from" : "d@example.com",
        "subject" : "some subject"
    },
    {
        "emailId" : 1,
        "to" : "b@example.com",
        "from" : "a@example.com",
        "subject" : "xyz subject"
    }

```

I would like to query the index to get a response to show on a chord diagram, which requires the data in this format:

```auto
[
    { from: "A", to: "D", value: 10 },
    { from: "B", to: "D", value: 8 },
    { from: "B", to: "E", value: 4 },
    { from: "B", to: "C", value: 2 },
    { from: "C", to: "E", value: 14 },
    { from: "E", to: "D", value: 8 },
    { from: "C", to: "A", value: 4 },
    { from: "G", to: "A", value: 7 },
    { from: "D", to: "B", value: 1 }
]

```

What's the best way to model the document and what aggregation would be the best?

Thanks!

---

<div class="post-metadata">

**Author:** ![mobius](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@mobius](https://discuss.elastic.co/u/mobius)\
**Post date:** [February 3, 2021, 10:40pm UTC](https://discuss.elastic.co/t/elastic-search-query-for-cross-document-reference-counts/263061/2 "2021-02-03T22:40:12Z")

</div>

Can anyone please help with this? Does it belong in a different forum?  
Thanks in advance!

---

<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 3, 2021, 10:40pm UTC](https://discuss.elastic.co/t/elastic-search-query-for-cross-document-reference-counts/263061/3 "2021-03-03T22:40:21Z")

</div>

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