Fitering nested documents


(PM) #1

I have a set of documents with the following structure of a document. The mapping have been defined as nested for specific elemnets e.g. el2array and el3

{
          "el1": "val1"
          "el2array": [
            {
              "apn": "name1",
              "VN": "7.0"
            }
            {
              "apn": "name2",
              "VN": "1.7"
            }
          ],
          "el3": {
            "mk": "LG",
            "mo": "5X",
          }
}

where el3 and el2array are nested.

I would like to run a es query which can satisfy a query to get a set of documents which has an el2array.apn=name1 and the VN of the same apn (name1) can have VN=7.0 or 8.0 i.e get all documents with data as below.

        {
          "apn": "name1",
          "VN": "7.0"
        }
        
        or
        
        {
          "apn": "name1",
          "VN": "8.0"
        }

I tried with a query like below and it does return documents with el2array.apn=name1 and all documents with el2array.VN=7.0 or 8.0 that may or may not be associated with el2array.apn=name1. The query returns documents like below which is wrong.

{
          "el2array": [
            {
              "apn": "name1",
              "VN": "10.0"    // note: VN is no 7.0 or 8.0
            }
            {
              "apn": "name2",
              "VN": "8.0"   // another element in the array has 8.0
            }
          ]
}

Below is the query that I am using. Could someone suggest as to what would be the correct query / filter ?

{
  "query" : {
    "filtered" : {
      "query" : {
        "bool" : { }
      },
      "filter" : {
        "nested" : {
          "filter" : {
            "and" : {
              "filters" : [ 
              	
              	{"term" : {"el2array.apn" :"name1"}}, 
                {"or": [{"term" : {"el2array.VN" : "7.0"} },{"term" : {"el2array.VN" : "8.0"} }] }
              
              ]
            }
          },
          "path" : "el2array"
        }
      }
    }
  }
}

(system) #2