0

I am using elasticsearch-6.4.3

Let's say I have a sample document in the form:

{
"segment_details": [{
  "departure_airport_code": "DEL",
  "departure_time": "2019-10-10 03:00:00"
}, 
{
"departure_airport_code": "BOM",
"departure_time": "2019-10-11 23:00:00" 
}]
}

I wrote a query to get all the documents in which any of the elements of the segment_details has departure_airport_code of given code. The below query is working fine.

GET flight-cache_layers/_doc/_search
{
  "query": {
    "nested": {
      "path": "segment_details",
      "query": {
        "bool": {
          "must": [
            { "match": { 
              "segment_details.departure_airport_code": code }}
          ]
        }
      }
    }
  }
}

I want to write a query in which I want to check if any of the element of the segment_details contains any of the departure_airport_code in the given codes list.

GET flight-cache_layers/_doc/_search
{
  "query": {
    "nested": {
      "path": "segment_details",
      "query": {
        "bool": {
          "must": [
            { "match": { 
              "segment_details.departure_airport_code" IN codes }}  # something like this.
          ]
        }
      }
    }
  }
}
Bhawan
  • 2,441
  • 3
  • 22
  • 47

1 Answers1

1

Nested Aggregation You can use a should clause where a text should match any of these values.

"query": {
    "nested": {
      "path": "segment_details",
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "FIELD": "TEXT"  --> replace field with your field and text with value
              }
            },
             {
              "match": {
                "FIELD": "TEXT"
              }
            }
          ]
        }
      }
    }
  }

If you are searching for exact words, you can use terms query

"query": {
    "nested": {
      "path": "segment_details",
      "query": {
        "bool": {
          "must": [
            {
              "terms": {
                "FIELD": [ --> replace field with your field and value array with values, you might need to use field.keyword as per your mapping 
                  "VALUE1",
                  "VALUE2"
                ]
              }
            }
          ]
        }
      }
    }
  }
jaspreet chahal
  • 8,817
  • 2
  • 11
  • 29
  • segment_details is an array of objects. And departure_airport_code is a field in that object. The above solution working if departure_airport_code is directly nested inside segment_details like this: {"segment_details":{"departure_airport_code": "DEL"}} – Bhawan Oct 03 '19 at 10:33
  • is segment_details of nested type – jaspreet chahal Oct 03 '19 at 10:46
  • Yes, it is of nested type. – Bhawan Oct 03 '19 at 10:47