2

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.

Jamsheer
  • 3,673
  • 3
  • 29
  • 57
  • I'm not really familiar with this, but did you tried joining queries? https://www.elastic.co/guide/en/elasticsearch/reference/6.2/joining-queries.html Maybe this helps you somehow. – Piotr Pradzynski Mar 07 '18 at 11:39
  • @PiotrPradzynski it is the answer – aclowkay Mar 07 '18 at 12:35
  • I strongly suggest denormalizing your data: https://stackoverflow.com/questions/36915428/how-to-setup-elasticsearch-index-structure-with-multiple-entity-bindings/36982705#36982705 – Val Mar 07 '18 at 13:05
  • Thank you All, In my case, i need to query data from both types with a single request – Jamsheer Mar 07 '18 at 13:07

1 Answers1

1

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
          }
        }}
      ]
    }
  }
}
pkhlop
  • 1,824
  • 3
  • 18
  • 23