# JOIN two indexes using ElasticSearch Node.js client

**URL:** <https://discuss.elastic.co/t/join-two-indexes-using-elasticsearch-node-js-client/313283>\
**Category:** Elasticsearch\
**Tags:** docker, language-clients\
**Created:** [August 30, 2022, 7:27pm UTC](https://discuss.elastic.co/t/join-two-indexes-using-elasticsearch-node-js-client/313283 "2022-08-30T19:27:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sadiki-dev](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sadiki-dev](https://discuss.elastic.co/u/sadiki-dev)\
**Post date:** [August 30, 2022, 7:27pm UTC](https://discuss.elastic.co/t/join-two-indexes-using-elasticsearch-node-js-client/313283/1 "2022-08-30T19:27:58Z")

</div>

I have two indexes (two collections) , products index and reviews index. The two is linked by productId. The structure of both are :

```json
// products.json
[
    {
        "id": 1,
        "name": "IPhone 11",
        "quantity_stock": 100,
        "description": "description of IPhone 11",
        "manufacturing": {
            "id": 416665497,
            "entreprise": "Apple",
            "country": "USA",
            "address": {
                "zipCode": "10000",
                "state": "New York",
                "street": "Johny 10"
            }
        }
    },
    {
        "id": 2,
        "name": "Samsung 10",
        "quantity_stock": 130,
        "description": "description of Samsung 10",
        "manufacturing": {
            "id": 4545465497,
            "entreprise": "Samsung",
            "country": "Koea",
            "address": {
                "zipCode": "20000",
                "state": "Tokyo",
                "street": "yong ki 645"
            }
        }
    },
    {
        "id": 3,
        "name": "Redmi Note 10",
        "quantity_stock": 80,
        "description": "description of Redmi Note 10",
        "manufacturing": {
            "id": 4545465497,
            "entreprise": "Samsung",
            "country": "China",
            "address": {
                "zipCode": "14000",
                "state": "Pekin",
                "street": "yong ki 10"
            }
        }
    }
]

```

```json
// reviews.json
[
    {
        "id": 1,
        "rating": 8,
        "productId": 1,
        "comment": "It's an excelent product"
    },
    {
        "id": 2,
        "rating": 1,
        "productId": 1,
        "comment": "It's a bad product"
    },
    {
        "id": 3,
        "rating": 6,
        "productId": 2,
        "comment": "It's a good product"
    }
]

```

I want to join the two indexes (products and reviews) to have all products linked with their own reviews.

```json
// 👉 Input for One product
{
        "id": 1,
        "name": "IPhone 11",
        "quantity_stock": 100,
        "description": "description of IPhone 11",
        "manufacturing": {
            "id": 416665497,
            "entreprise": "Apple",
            "country": "USA",
            "address": {
                "zipCode": "10000",
                "state": "New York",
                "street": "Johny 10"
            }
        }
    }

```

```json
// 👉 Desired Output :
{
        "id": 1,
        "name": "IPhone 11",
        "quantity_stock": 100,
        "description": "description of IPhone 11",
        "manufacturing": {
            "id": 416665497,
            "entreprise": "Apple",
            "country": "USA",
            "address": {
                "zipCode": "10000",
                "state": "New York",
                "street": "Johny 10"
            }
        },
// Add This 👇
        "reviews": [
            {
                "id": 1,
                "rating": 8,
                "productId": 1,
                "comment": "It's an excelent product"
            },
            {
                "id": 2,
                "rating": 1,
                "productId": 1,
                "comment": "It's a bad product"
            }
        ]
    }

```

How I can do a join between two indexes, something like $lookup in MongoDB ?  
Please who has any idea or reference to solve this issue.

Kind regards.

---

<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:** [August 30, 2022, 9:36pm UTC](https://discuss.elastic.co/t/join-two-indexes-using-elasticsearch-node-js-client/313283/2 "2022-08-30T21:36:31Z")

</div>

You cannot do this with an Elasticsearch query. You will need to manage the two queries and merging in your code.

---

<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:** [September 27, 2022, 9:36pm UTC](https://discuss.elastic.co/t/join-two-indexes-using-elasticsearch-node-js-client/313283/3 "2022-09-27T21:36:43Z")

</div>

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