# Join two indices query where there is 1-to-many relationship

**URL:** <https://discuss.elastic.co/t/join-two-indices-query-where-there-is-1-to-many-relationship/318306>\
**Category:** Elasticsearch\
**Created:** [November 7, 2022, 8:58am UTC](https://discuss.elastic.co/t/join-two-indices-query-where-there-is-1-to-many-relationship/318306 "2022-11-07T08:58:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![areobode](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@areobode](https://discuss.elastic.co/u/areobode)\
**Post date:** [November 7, 2022, 8:58am UTC](https://discuss.elastic.co/t/join-two-indices-query-where-there-is-1-to-many-relationship/318306/1 "2022-11-07T08:58:34Z")

</div>

I have two indices; authors and articles.  
Since one author writes several articles, there are 1-to-many relationship between them.  
The article index has title, vote, and authorId attributes.

Here is an example index.

```auto
PUT /test_authors/
{
  "mappings": {
    "properties": {
       "name": {"type": "keyword"},
    }
  }
}

PUT /test_articles/
{
  "mappings":{
    "properties": {
        "title": {"type": "keyword"},
        "vote": {"type": "long"},
        "authorId": {"type": "long"},
    }
  }
}

```

sample documents

```auto
// sample authors
POST /test_authors/_bulk
{"index":{}}
{"name": "Mick"}
{"index":{}}
{"name": "Tim"}

// sample articles
POST /test_articles/_bulk
{"index":{}}
{"title": "About car", "vote": 5, "authorId": 1}
{"index":{}}
{"title": "About sport", "vote": 2, "authorId": 1}
{"index":{}}
{"title": "About computer", "vote": 1, "authorId": 2}

```

What I want to do is to search authors, one of whose articles has more than 3 votes.  
In the above example, expected result should be only Mick(authorId=1).

In the world of rds, I can do it easily through by single query.

```auto
SELECT authors.name FROM authors
LEFT JOIN articles
ON articles.authorId = authors.id
WHERE articles.vote >=3

```

Can I realize like this kind of thing in elasticsearch?  
If not, how can I do this?  
Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [November 10, 2022, 9:58am UTC](https://discuss.elastic.co/t/join-two-indices-query-where-there-is-1-to-many-relationship/318306/2 "2022-11-10T09:58:57Z")

</div>

Elasticsearch does not support joins so the most common workaround is to denormalise your data and store the relevant authour data on each arcticle document.

---

<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 8, 2022, 9:59am UTC](https://discuss.elastic.co/t/join-two-indices-query-where-there-is-1-to-many-relationship/318306/3 "2022-12-08T09:59:14Z")

</div>

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