From ad0962d1a3b6e55d5250cc85986cb48673f87eac Mon Sep 17 00:00:00 2001 From: kkewwei Date: Tue, 4 Jun 2024 07:34:54 +0800 Subject: [PATCH] fix test Signed-off-by: kkewwei --- .../index/106_flat_object_with_parameter.yml | 129 +++++++++++++++++- .../xcontent/JsonToStringXContentParser.java | 4 + .../mapper/FlatObjectFieldMapperTests.java | 8 ++ 3 files changed, 139 insertions(+), 2 deletions(-) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/index/106_flat_object_with_parameter.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/index/106_flat_object_with_parameter.yml index 5736274aefb28..58a8e48439263 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/index/106_flat_object_with_parameter.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/index/106_flat_object_with_parameter.yml @@ -1,7 +1,7 @@ --- # The test setup includes: # - Create flat_object mapping for test_flat_object index to test five parameters: -# - Create test_flat_object1 index to test edge cases: +# - Create test_flat_object1,test_flat_object2 index to test edge cases: # - Index example documents # - Refresh the index so it is ready for search tests @@ -68,6 +68,18 @@ setup: } } + - do: + index: + index: test_flat_object + id: 4 + body: { + "issue": { + "labels": { + "age1":null + } + } + } + - do: indices.refresh: index: test_flat_object @@ -102,6 +114,44 @@ setup: indices.refresh: index: test_flat_object1 + # index test_flat_object2 + - do: + indices.create: + index: test_flat_object2 + body: + mappings: + properties: + issue: + properties: + labels: + type: flat_object + + - do: + index: + index: test_flat_object2 + id: 1 + body: { + "issue": { + "labels": { + "category": null + } + } + } + + - do: + index: + index: test_flat_object2 + id: 2 + body: { + "issue": { + "labels": null + } + } + + - do: + indices.refresh: + index: test_flat_object2 + --- # Delete Index when connection is teardown teardown: @@ -145,7 +195,7 @@ teardown: } } - - length: { hits.hits: 3 } + - length: { hits.hits: 4 } - do: search: @@ -158,6 +208,17 @@ teardown: - length: { hits.hits: 1 } + - do: + search: + index: test_flat_object2, + body: { + query: { + match_all: { } + } + } + + - length: { hits.hits: 2 } + # test ignore_above=4. - do: search: @@ -211,6 +272,70 @@ teardown: - length: { hits.hits: 1 } - match: { hits.hits.0._source.issue.labels: null } + - do: + search: + index: test_flat_object, + body: { + _source: true, + query: { + exists: { field: "issue.labels" } + } + } + + - length: { hits.hits: 1 } + - match: { hits.hits.0._source.issue.labels: null } + + # test null_value="abc" and subField=null. + - do: + search: + index: test_flat_object, + body: { + _source: true, + query: { + term: { issue.labels.age1: "abc" } + } + } + + - length: { hits.hits: 0 } + + # null_value="abc" doesn't work on th subField + - do: + search: + index: test_flat_object, + body: { + _source: true, + query: { + exists: { field: "issue.labels.age1" } + } + } + + - length: { hits.hits: 0 } + + # test null_value=null and subField=null. + - do: + search: + index: test_flat_object2, + body: { + _source: true, + query: { + exists: { field: "issue.labels" } + } + } + + - length: { hits.hits: 0 } + + - do: + search: + index: test_flat_object2, + body: { + _source: true, + query: { + exists: { field: "issue.labels.category" } + } + } + + - length: { hits.hits: 0 } + # test depth_limit=3. - do: search: diff --git a/server/src/main/java/org/opensearch/common/xcontent/JsonToStringXContentParser.java b/server/src/main/java/org/opensearch/common/xcontent/JsonToStringXContentParser.java index fc1c6bf822cef..567ca16330a13 100644 --- a/server/src/main/java/org/opensearch/common/xcontent/JsonToStringXContentParser.java +++ b/server/src/main/java/org/opensearch/common/xcontent/JsonToStringXContentParser.java @@ -137,6 +137,10 @@ private void parseToken(StringBuilder path, String currentFieldName, int depth) if (parseValue(parsedFields)) { this.valueList.add(parsedFields.toString()); this.valueAndPathList.add(path + EQUAL_SYMBOL + parsedFields); + } else { + // it means that the value is invalid, we should delete the key from keyList. + assert this.keyList.size() > 0; + this.keyList.remove(keyList.size() - 1); } int dotIndex = path.lastIndexOf(DOT_SYMBOL, path.length()); if (dotIndex != -1 && path.length() > currentFieldName.length()) { diff --git a/server/src/test/java/org/opensearch/index/mapper/FlatObjectFieldMapperTests.java b/server/src/test/java/org/opensearch/index/mapper/FlatObjectFieldMapperTests.java index 848dacbae79d8..eda2414714695 100644 --- a/server/src/test/java/org/opensearch/index/mapper/FlatObjectFieldMapperTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/FlatObjectFieldMapperTests.java @@ -196,12 +196,20 @@ public void testIgnoreAbove() throws IOException { } public void testNullValue() throws IOException { + // test root field is null DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); ParsedDocument doc = mapper.parse(source(b -> b.nullField("field"))); assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field")); assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field" + VALUE_SUFFIX)); assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field" + VALUE_AND_PATH_SUFFIX)); + // test subField is null + String json = XContentFactory.jsonBuilder().startObject().startObject("field").nullField("Foo").endObject().endObject().toString(); + doc = mapper.parse(source(json)); + assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field")); + assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field" + VALUE_SUFFIX)); + assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field" + VALUE_AND_PATH_SUFFIX)); + mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "flat_object").field("null_value", "bar"))); doc = mapper.parse(source(b -> {}));