Nested queries: nested clause affects the other parts of complex query which is joint through the OR

We found out that the search returns different results for the queries with nested clause and without it

Technical details described into the Solution part

For example:

(
	(
		(
			kind:"osdu:wks:master-data--Well:1.1.0" 
			OR kind:"osdu:wks:master-data--Wellbore:1.1.0"
		) AND (
			nested(data.GeoContexts, (GeoPoliticalEntityID:"Malaysia")))
	) OR (
			(
				kind:"osdu:wks:work-product-component--Document:1.0.0" 
				OR kind:"petronas:raw:DST_Test_Summary:1.0.0"
			) 
	)
)

and

(
	(
		(
			kind:"osdu:wks:master-data--Well:1.1.0" 
			OR kind:"osdu:wks:master-data--Wellbore:1.1.0"
		)
	) OR (
			(
				kind:"osdu:wks:work-product-component--Document:1.0.0" 
				OR kind:"petronas:raw:DST_Test_Summary:1.0.0"
			) 
	)
)

returns different results even for common part of the query:

			(
				kind:"osdu:wks:work-product-component--Document:1.0.0" 
				OR kind:"petronas:raw:DST_Test_Summary:1.0.0"
			) 

Some of the buckets not presented into query with nested clause, but all of them presented for the one without nested

Solution: !369 (merged)

This happens because of in case of ignoreUnmapped=false the elasticsearch tries to look through all queried indices and if it face data.GeoContexts (for the provided example) field with non nested type it fails with exception:

      {
        "shard": 0,
        "index": "abc-wks-log-1.0.7",
        "node": "UarT2extRZ-ElEloWLiXCw",
        "reason": {
          "type": "exception",
          "reason": "Elasticsearch exception [type=query_shard_exception, reason=failed to create query: [nested] failed to find nested object under path [data.GeoContexts]]",
          "index_uuid": "glpROaNTSdutRHafEJKzpQ",
          "index": "abc-wks-log-1.0.7",
          "caused_by": {
            "type": "exception",
            "reason": "Elasticsearch exception [type=illegal_state_exception, reason=[nested] failed to find nested object under path [data.GeoContexts]]"
          }
        }
      }

It affects to all results from the shard where failed taken place.

Search not throws and exception but returns empty/partial result.

At the same time ignoreUnmapped=true let elastic search to ignore such exceptions and return all relevant results.

Because of ignoreUnmapped=true, potentially, we can see some issues related to the nested type incapability. But in this particular case valid search is much more important.

Edited by Yauheni Lesnikau