Search code examples
javascriptelasticsearchaws-elasticsearch

Search and join two indices in Elastic Search


How to join two separated indexes after matching query in elastic search?

For instance, I have two index called all_products and SourceTable. I want to combine two indexes based on product_id

{
    "_index": "all_products",
    "_type": "all_products",
    "_id": "123",
    "_score": 0.9808292,
    "_source": {
        "city": "CHENNAI",
        "product_id": "123",
        "name": "sam"
    }
},
{
    "_index": "sourcetable",
    "_type": "sourcetable",
    "_id": "232",
    "_score": 0.2876821,
    "_source": {
        "product_id": "123",
        "id": 232
    }
},

Solution

  • There is no JOIN on Elasticsearch, but you can search two or more indices for the same field at the same time.

    For example you could search the field product_id for the value 123 on both of your indices, but the result won't be a join, it will only return the documents that have product_id = 123 for each indice.