JOIN two indexes using ElasticSearch Node.js client

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

// 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"
            }
        }
    }
]
// 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.

// 👉 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"
            }
        }
    }
// 👉 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.

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

1 Like

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