Search code examples
elasticsearchelassandra

Elastic search - Retrieve data from multiple types


I am working with elasticsearch and I have two types which hold my data.

Now I need to retrieve data from both types by using a single query.

Please see my use case

I have two types called basic and marks and I saved document as follows

myindex/basic

{ "id": "100", "name": "Tom" }

myindex/basic

{ "id": "101", "name": "John"

}

myindex/marks

{ "id": "100", "mark": "300"

}

myindex/marks

{ "id": "101", "mark": "500" }

Now i need to get the name and mark of a student his id is 100.

Is there any possibility to get result like this.

I came to know that these type of data model is not good for nosql but here i need it as these records are replicated from RDBMS DB.

Any suggestion please and thanks in advance.


Solution

  • You can query both types in single query by listing them in url

    POST myindex/basic,marks/_search
    

    You can also filter all of them by id

    POST myindex/basic,marks/_search
    {
      "query": {
        "bool": {
          "must": [
            {"term": {
              "id": {
                "value": 100
              }
            }}
          ]
        }
      }
    }