Skip to content

Commit

Permalink
Fix exists query for flat object (opensearch-project#17108)
Browse files Browse the repository at this point in the history

---------

Signed-off-by: panguixin <panguixin@bytedance.com>
  • Loading branch information
bugmakerrrrrr authored Jan 24, 2025
1 parent 931c1aa commit bc9e4d8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 }
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 }
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ teardown:
body: {
_source: true,
query: {
exists: { "field": "record.d" }
exists: { "field": "record.name.d.name" }
},
sort: [{ order: asc}]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

{
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit bc9e4d8

Please sign in to comment.