diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/index/100_partial_flat_object.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/index/100_partial_flat_object.yml index 6fc2654bcfc8f..0c00ff6e4a6a8 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/index/100_partial_flat_object.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/index/100_partial_flat_object.yml @@ -506,20 +506,6 @@ teardown: - length: { hits.hits: 2 } - # Exists Query with nested dot path, use the flat_object_field_name.last_key - - do: - search: - body: { - _source: true, - query: { - "exists": { - "field": issue.labels.type - } - } - } - - - length: { hits.hits: 3 } - # Exists Query without dot path for the flat_object_field_name - do: search: @@ -613,3 +599,22 @@ teardown: - length: { hits.hits: 1 } - match: { hits.hits.0._source.issue.labels.comment: [ [ "Doe","Shipped" ],[ "John","Approved" ] ] } + +--- +"Exists query for sub field": + - skip: + version: " - 2.99.99" + reason: "exists query for sub field of flat_object field has bug before 3.0.0" + + - do: + search: + body: { + _source: true, + query: { + "exists": { + "field": issue.labels.category.type + } + } + } + + - length: { hits.hits: 3 } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/index/105_partial_flat_object_nested.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/index/105_partial_flat_object_nested.yml index 549ddbdde7bab..ccb9c3e3fc23d 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/index/105_partial_flat_object_nested.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/index/105_partial_flat_object_nested.yml @@ -506,23 +506,6 @@ teardown: - length: { hits.hits: 2 } - # Exists Query with nested dot path, use the flat_object_field_name.last_key - - do: - search: - body: { - _source: true, - query: { - nested: { - path: "issue", - query: { - "exists": { - "field": issue.labels.type - } } } - } - } - - - length: { hits.hits: 2 } - # Exists Query without dot path for the flat_object_field_name - do: search: @@ -634,3 +617,27 @@ teardown: - length: { hits.hits: 1 } - match: { hits.hits.0._source.issue.0.labels.comment: [ [ "Doe","Shipped" ],[ "John","Approved" ] ] } + +--- +"Exists query for sub field": + - skip: + version: " - 2.99.99" + reason: "exists query for sub field of flat_object field has bug before 3.0.0" + + - do: + search: + body: { + _source: true, + query: { + nested: { + path: "issue", + query: { + "exists": { + "field": issue.labels.category.type + } + } + } + } + } + + - length: { hits.hits: 2 } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/index/91_flat_object_null_value.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/index/91_flat_object_null_value.yml index 716b6fb51cb43..455eb4fb91a43 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/index/91_flat_object_null_value.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/index/91_flat_object_null_value.yml @@ -373,7 +373,7 @@ teardown: body: { _source: true, query: { - exists: { "field": "record.d" } + exists: { "field": "record.name.d.name" } }, sort: [{ order: asc}] } diff --git a/server/src/main/java/org/opensearch/index/mapper/FlatObjectFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/FlatObjectFieldMapper.java index 4425e4e5b0b39..7b1b2615f996d 100644 --- a/server/src/main/java/org/opensearch/index/mapper/FlatObjectFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/FlatObjectFieldMapper.java @@ -478,8 +478,7 @@ public Query existsQuery(QueryShardContext context) { String searchKey; String searchField; if (isSubField()) { - searchKey = this.rootFieldName; - searchField = name(); + return rangeQuery(null, null, true, true, context); } else { if (hasDocValues()) { return new FieldExistsQuery(name()); diff --git a/server/src/test/java/org/opensearch/index/mapper/FlatObjectFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/FlatObjectFieldTypeTests.java index 600f04edcd552..2ab1ad689a0b9 100644 --- a/server/src/test/java/org/opensearch/index/mapper/FlatObjectFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/FlatObjectFieldTypeTests.java @@ -418,8 +418,12 @@ public void testExistsQuery() { ft.getValueFieldType(), ft.getValueAndPathFieldType() ); - assertEquals(new TermQuery(new Term("field", "field.bar")), dynamicMappedFieldType.existsQuery(null)); - + Automaton termAutomaton = PrefixQuery.toAutomaton(new BytesRef("field.bar=")); + Automaton dvAutomaton = PrefixQuery.toAutomaton(new BytesRef("field.field.bar=")); + Query indexQuery = new AutomatonQuery(new Term("field" + VALUE_AND_PATH_SUFFIX), termAutomaton, true); + Query dvQuery = new AutomatonQuery(new Term("field" + VALUE_AND_PATH_SUFFIX), dvAutomaton, true, DOC_VALUES_REWRITE); + Query expected = new IndexOrDocValuesQuery(indexQuery, dvQuery); + assertEquals(expected, dynamicMappedFieldType.existsQuery(MOCK_QSC_ENABLE_INDEX_DOC_VALUES)); } { @@ -1176,8 +1180,8 @@ public void testRangeQuery() { ); continue; } - boolean nullLowerTerm = true;// randomBoolean(); - boolean nullUpperTerm = true;// nullLowerTerm == false || randomBoolean(); + boolean nullLowerTerm = randomBoolean(); + boolean nullUpperTerm = nullLowerTerm == false || randomBoolean(); Automaton a1 = PrefixQuery.toAutomaton(new BytesRef("field.field1=")); Automaton a2 = TermRangeQuery.toAutomaton(