Skip to content

Commit

Permalink
fix constant_keyword field type (#14807) (#14969)
Browse files Browse the repository at this point in the history
Signed-off-by: kkewwei <[email protected]>

Signed-off-by: kkewwei <[email protected]>

test

Signed-off-by: Daniel (dB.) Doubrovkine <[email protected]>
Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
(cherry picked from commit cb74371)

Signed-off-by: Andrew Ross <[email protected]>
Co-authored-by: kkewwei <[email protected]>
Co-authored-by: Andrew Ross <[email protected]>
  • Loading branch information
3 people authored Jul 25, 2024
1 parent fc729b6 commit f84a26e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions release-notes/opensearch.release-notes-2.16.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@
- Create new IndexInput for multi part upload ([#14888](https://github.com/opensearch-project/OpenSearch/pull/14888))
- Fix searchable snapshot failure with scripted fields ([#14411](https://github.com/opensearch-project/OpenSearch/pull/14411))
- Fix the visit of inner query for NestedQueryBuilder ([#14739](https://github.com/opensearch-project/OpenSearch/pull/14739))
- Fix constant_keyword field type used when creating index ([#14807](https://github.com/opensearch-project/OpenSearch/pull/14807))
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
# The test setup includes:
# - Create index with constant_keyword field type
# - Check mapping
# - Index two example documents
# - Search
# - Delete Index when connection is teardown

"Mappings and Supported queries":
- skip:
version: " - 2.99.99"
reason: "fixed in 3.0.0"

# Create index with constant_keyword field type
- do:
indices.create:
index: test
body:
mappings:
properties:
genre:
type: "constant_keyword"
value: "1"

# Index document
- do:
index:
index: test
id: 1
body: {
"genre": "1"
}

- do:
index:
index: test
id: 2
body: {
"genre": 1
}

- do:
indices.refresh:
index: test

# Check mapping
- do:
indices.get_mapping:
index: test
- is_true: test.mappings
- match: { test.mappings.properties.genre.type: constant_keyword }
- length: { test.mappings.properties.genre: 2 }

# Verify Document Count
- do:
search:
body: {
query: {
match_all: {}
}
}

- length: { hits.hits: 2 }
- match: { hits.hits.0._source.genre: "1" }
- match: { hits.hits.1._source.genre: 1 }

# Delete Index when connection is teardown
- do:
indices.delete:
index: test
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ private static ConstantKeywordFieldMapper toType(FieldMapper in) {
*/
public static class Builder extends ParametrizedFieldMapper.Builder {

private final Parameter<String> value;
private final Parameter<String> value = Parameter.stringParam(valuePropertyName, false, m -> toType(m).value, null);

public Builder(String name, String value) {
super(name);
this.value = Parameter.stringParam(valuePropertyName, false, m -> toType(m).value, value);
this.value.setValue(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public void testMissingDefaultIndexMapper() throws Exception {
assertThat(e.getMessage(), containsString("Field [field] is missing required parameter [value]"));
}

public void testBuilderToXContent() throws IOException {
ConstantKeywordFieldMapper.Builder builder = new ConstantKeywordFieldMapper.Builder("name", "value1");
XContentBuilder xContentBuilder = JsonXContent.contentBuilder().startObject();
builder.toXContent(xContentBuilder, false);
xContentBuilder.endObject();
assertEquals("{\"value\":\"value1\"}", xContentBuilder.toString());
}

private final SourceToParse source(CheckedConsumer<XContentBuilder, IOException> build) throws IOException {
XContentBuilder builder = JsonXContent.contentBuilder().startObject();
build.accept(builder);
Expand Down

0 comments on commit f84a26e

Please sign in to comment.