Multi Level Nested Queries

I have a multi-level nested document. I want to query based on multiple nested queries with all the conditions must match.

Example

Document 1

{
	"publishing_rule": {
		"publishing_orders": [{
				"transporters": [{
					"fteid": "81"
				}],
				"active": false
			},
			{
				"transporters": [{
					"fteid": "82"
				}],
				"active": true
			}
		]
	}
}

Document 2

{
	"publishing_rule": {
		"publishing_orders": [{
				"transporters": [{
					"fteid": "81"
				}],
				"active": true
			},
			{
				"transporters": [{
					"fteid": "82"
				}],
				"active": false
			}
		]
	}
}

I want to fetch all the documents which match below condition

publishing_rule.publishing_orders.active = true 

AND 

publishing_rule.publishing_orders.transporters.fteid = '81'

Both active and transporters.fteid should be part of same object.

I have tried creating below mapping

{
  "mappings": {
    "_doc": {
      "properties": {
        "publishing_rule.publishing_orders": {
          "type": "nested",
          "properties": {
            "transporters": {
              "type": "nested"
            }
          }
        }
      }
    }
  }
}

And used below query

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "publishing_rule.publishing_orders",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "publishing_rule.publishing_orders.active": true
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "publishing_rule.publishing_orders.transporters",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "publishing_rule.publishing_orders.transporters.fteid": "81"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Also tried below query.

{
  "query": {
    "nested": {
      "path": "publishing_rule.publishing_orders",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "publishing_rule.publishing_orders.active": true
              }
            },
            {
              "nested": {
                "path": "publishing_rule.publishing_orders.transporters",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "publishing_rule.publishing_orders.transporters.fteid": "81"
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

But I am not getting expected result. The query returning both the documents.

I am expecting only Document 2 in the result.

@Onkar_Kore please, post the result of this request GET /your_index_name/_mapping.

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