Skip to content

Commit bc9e4d8

Browse files
Fix exists query for flat object (#17108)
--------- Signed-off-by: panguixin <[email protected]>
1 parent 931c1aa commit bc9e4d8

File tree

5 files changed

+53
-38
lines changed

5 files changed

+53
-38
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/index/100_partial_flat_object.yml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -506,20 +506,6 @@ teardown:
506506

507507
- length: { hits.hits: 2 }
508508

509-
# Exists Query with nested dot path, use the flat_object_field_name.last_key
510-
- do:
511-
search:
512-
body: {
513-
_source: true,
514-
query: {
515-
"exists": {
516-
"field": issue.labels.type
517-
}
518-
}
519-
}
520-
521-
- length: { hits.hits: 3 }
522-
523509
# Exists Query without dot path for the flat_object_field_name
524510
- do:
525511
search:
@@ -613,3 +599,22 @@ teardown:
613599

614600
- length: { hits.hits: 1 }
615601
- match: { hits.hits.0._source.issue.labels.comment: [ [ "Doe","Shipped" ],[ "John","Approved" ] ] }
602+
603+
---
604+
"Exists query for sub field":
605+
- skip:
606+
version: " - 2.99.99"
607+
reason: "exists query for sub field of flat_object field has bug before 3.0.0"
608+
609+
- do:
610+
search:
611+
body: {
612+
_source: true,
613+
query: {
614+
"exists": {
615+
"field": issue.labels.category.type
616+
}
617+
}
618+
}
619+
620+
- length: { hits.hits: 3 }

rest-api-spec/src/main/resources/rest-api-spec/test/index/105_partial_flat_object_nested.yml

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -506,23 +506,6 @@ teardown:
506506

507507
- length: { hits.hits: 2 }
508508

509-
# Exists Query with nested dot path, use the flat_object_field_name.last_key
510-
- do:
511-
search:
512-
body: {
513-
_source: true,
514-
query: {
515-
nested: {
516-
path: "issue",
517-
query: {
518-
"exists": {
519-
"field": issue.labels.type
520-
} } }
521-
}
522-
}
523-
524-
- length: { hits.hits: 2 }
525-
526509
# Exists Query without dot path for the flat_object_field_name
527510
- do:
528511
search:
@@ -634,3 +617,27 @@ teardown:
634617

635618
- length: { hits.hits: 1 }
636619
- match: { hits.hits.0._source.issue.0.labels.comment: [ [ "Doe","Shipped" ],[ "John","Approved" ] ] }
620+
621+
---
622+
"Exists query for sub field":
623+
- skip:
624+
version: " - 2.99.99"
625+
reason: "exists query for sub field of flat_object field has bug before 3.0.0"
626+
627+
- do:
628+
search:
629+
body: {
630+
_source: true,
631+
query: {
632+
nested: {
633+
path: "issue",
634+
query: {
635+
"exists": {
636+
"field": issue.labels.category.type
637+
}
638+
}
639+
}
640+
}
641+
}
642+
643+
- length: { hits.hits: 2 }

rest-api-spec/src/main/resources/rest-api-spec/test/index/91_flat_object_null_value.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ teardown:
373373
body: {
374374
_source: true,
375375
query: {
376-
exists: { "field": "record.d" }
376+
exists: { "field": "record.name.d.name" }
377377
},
378378
sort: [{ order: asc}]
379379
}

server/src/main/java/org/opensearch/index/mapper/FlatObjectFieldMapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,7 @@ public Query existsQuery(QueryShardContext context) {
478478
String searchKey;
479479
String searchField;
480480
if (isSubField()) {
481-
searchKey = this.rootFieldName;
482-
searchField = name();
481+
return rangeQuery(null, null, true, true, context);
483482
} else {
484483
if (hasDocValues()) {
485484
return new FieldExistsQuery(name());

server/src/test/java/org/opensearch/index/mapper/FlatObjectFieldTypeTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,12 @@ public void testExistsQuery() {
418418
ft.getValueFieldType(),
419419
ft.getValueAndPathFieldType()
420420
);
421-
assertEquals(new TermQuery(new Term("field", "field.bar")), dynamicMappedFieldType.existsQuery(null));
422-
421+
Automaton termAutomaton = PrefixQuery.toAutomaton(new BytesRef("field.bar="));
422+
Automaton dvAutomaton = PrefixQuery.toAutomaton(new BytesRef("field.field.bar="));
423+
Query indexQuery = new AutomatonQuery(new Term("field" + VALUE_AND_PATH_SUFFIX), termAutomaton, true);
424+
Query dvQuery = new AutomatonQuery(new Term("field" + VALUE_AND_PATH_SUFFIX), dvAutomaton, true, DOC_VALUES_REWRITE);
425+
Query expected = new IndexOrDocValuesQuery(indexQuery, dvQuery);
426+
assertEquals(expected, dynamicMappedFieldType.existsQuery(MOCK_QSC_ENABLE_INDEX_DOC_VALUES));
423427
}
424428

425429
{
@@ -1176,8 +1180,8 @@ public void testRangeQuery() {
11761180
);
11771181
continue;
11781182
}
1179-
boolean nullLowerTerm = true;// randomBoolean();
1180-
boolean nullUpperTerm = true;// nullLowerTerm == false || randomBoolean();
1183+
boolean nullLowerTerm = randomBoolean();
1184+
boolean nullUpperTerm = nullLowerTerm == false || randomBoolean();
11811185

11821186
Automaton a1 = PrefixQuery.toAutomaton(new BytesRef("field.field1="));
11831187
Automaton a2 = TermRangeQuery.toAutomaton(

0 commit comments

Comments
 (0)