0

I have created an Index with 10000+ documents. Here is the sample from that:

{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f05",
    "_score": 13.977877,
    "_source": {
        "customer_id":10,
        "customer_name": Mike,
        "customer_phone": 1111111111,
        "customer_address": "XYZ"
    }
},
{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f71",
    "_score": 12.977861,
    "_source": {
        "customer_id":20,
        "customer_name": Angie,
        "customer_phone": 2222222222,
        "customer_address": "ABC"
    }
},
{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f62",
    "_score": 10.978777,
    "_source": {
        "customer_id":30,
        "customer_name": John,
        "customer_phone": 3333333333,
        "customer_address": "PQR"
    }
},
{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f54",
    "_score": 11.817877,
    "_source": {
        "customer_id":40,
        "customer_name": Andy,
        "customer_phone": 4444444444,
        "customer_address": "MNO"
    }
},
{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f32",
    "_score": 14.457877,
    "_source": {
        "customer_id": 50,
        "customer_name": Nick,
        "customer_phone": 5555555555,
        "customer_address": "CDE"
    }
},
{
    "_index": "index_1",
    "_type": "_doc",
    "_id": "48a454f9-71d2-41a0-9e62-08c149366f21",
    "_score": 16.487877,
    "_source": {
        "customer_id":60,
        "customer_name": Atlas,
        "customer_phone": 6666666666,
        "customer_address": "DFE"
    }
}

I want to pass multiple queries at once as list in json body and get the result also in list format: For example: -> I want to pass below 3 queries in the search condition at the same time:

1) customer_id = 10, customer_name = Mike, customer_phone = 1111111111
2) customer_id = 40, customer_name = Andy, customer_phone = 4444444444
3) customer_id = 50, customer_name = Nick, customer_phone = 5555555555

Although, I can combine these 3 queries using 'AND' and 'OR' like below:

{
    "query": {
        "query_string": {
            "query": "(customer_id: 10 AND customer_name: Mike AND customer_phone: 1111111111) OR (customer_id: 40 AND customer_name: Andy AND customer_phone: 4444444444) OR (customer_id: 50 AND customer_name: Nick AND customer_phone: 5555555555)"
        }
    }
}

Other than combining the queries as above, is there any other better way to achieve the same (like passing the queries as list).

Pallavi
  • 57
  • 7
  • This thread should help: https://stackoverflow.com/questions/34900475/elasticsearch-is-bulk-search-possible/34900621#34900621 (hint: use the Multi search API) – Val Jul 26 '22 at 13:02

1 Answers1

0

You can combine should and must query:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "customer_id": {
                    "value": 10
                  }
                }
              },
              {
                "match": {
                  "customer_name": "Mike"
                }
              },
              {
                "term": {
                  "customer_phone": {
                    "value": 1111111111
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "customer_id": {
                    "value": 50
                  }
                }
              },
              {
                "match": {
                  "customer_name": "Nick"
                }
              },
              {
                "term": {
                  "customer_phone": {
                    "value": 5555555555
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "customer_id": {
                    "value": 40
                  }
                }
              },
              {
                "match": {
                  "customer_name": "Andy"
                }
              },
              {
                "term": {
                  "customer_phone": {
                    "value": 4444444444
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rabbitbr
  • 2,991
  • 2
  • 4
  • 17