Elastic query to fetch multiple docs from multiple indexes based on input(multiple param)

Hi Team,

Need an elastic query to fetch multiple docs from multiple indexes based on input.
If I have given two fields as input, It needs verify both indexes and fetch the docs, or if at least one match should fetch that doc.

example:

   GET my_index1,my_index2/_search
{
     "query": {
            "bool":{
               "filter": [
                     { "match": {"index1field" : "001"}},
                     { "match": {"index2field" : "USERD"}}
                  ]
             }
            }
   }

Note: assume "index1field" : "001" belongs to my_index1 and "index2field" : "USERD" belongs to my_index2

Here I am expecting out put with two docs.
If at least one match it needs fetch one record like OR.

GET my_index1,my_index2/_search?q=index1field : 001

--> It is ok for one input parameter but what about multiple param to get the multi data ?

Any one please provide elastic query (kibana) for this ?

Like this:

GET /my_index1,my_index2/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": {
              "match": {
                "index1field": "001"
              }
            }
          }
        },
        {
          "bool": {
            "must": {
              "match": {
                "index2field": "USERD"
              }
            }
          }
        }
      ]
    }
  }
}

Thank you very much iti.

is there any possibility with one bool instead of 3 ?

Bool is more powerful, you can have a lot more conditions (nested).

But if you like,

 GET /my_index1,my_index2/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "index1field.keyword": "001"
              }
            },
            {
              "term": {
                "index2field.keyword": "USERD"
              }
            }
          ]
        }
      }
    }

Thank you iti.

But the above query fetching only one document even it satisfies the two conditions.(I've tried with out keyword also).

I am expecting two docs if it satisfies the two condition.
Can you please help into this ?

Works fine on my server.
Could you show me the mapping and data?

Thank you iti, It's working fine after correcting my data.

and It's working fine with multi bool also as you suggested earlier like below

GET /my_index1,my_index2/_search
{
  "query": {
    "bool": {
      "should": [
        {
      "bool":{
          "must":[
            {"match":{
            "index1field.keyword": "001"
            }
            },
            {"range":{
            "exp_tms":{
              "gte":"now"
          }
        }
        }]}},
        {
          "bool": {
            "must": {
              "match": {
                "index2field.keyword": "USERD"
              }
            }
          }
        }
      ]
    }
  }
}

Is this not possible in single bool,
Why because we are java client to elastic, we have an ready made functionality for one bool.
otherwise I need implement java code for multi bool to prepares elastic query(uri)

If you have complex query, multiple BOOLs is not avoidable. Just wondering how you implement the client. If you use low level REST client. It takes any query you send just like Kibana.

Take a look at this example:

yes iti, we are using above code to interact elastic with java.
Thank you !!!!

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