Skip to content

Commit

Permalink
fix constant_keyword field type
Browse files Browse the repository at this point in the history
Signed-off-by: kkewwei <[email protected]>
  • Loading branch information
kkewwei committed Jul 15, 2024
1 parent ae73be0 commit 1b4b111
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Handle NPE in GetResult if "found" field is missing ([#14552](https://github.com/opensearch-project/OpenSearch/pull/14552))
- Refactoring FilterPath.parse by using an iterative approach ([#14200](https://github.com/opensearch-project/OpenSearch/pull/14200))
- Refactoring Grok.validatePatternBank by using an iterative approach ([#14206](https://github.com/opensearch-project/OpenSearch/pull/14206))
- Fix constant_keyword field type ([#14651](https://github.com/opensearch-project/OpenSearch/pull/14651))

### Security

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
setup:
- do:
indices.create:
index: test
body:
mappings:
properties:
genre:
type : "constant_keyword"
"value" : "Romantic comedy"

- do:
index:
index: test
id: 1
body: {
"genre": "Romantic comedy",
"name": "name1"
}

- do:
indices.refresh:
index: test

---
# Delete Index when connection is teardown
teardown:
- do:
indices.delete:
index: test

---
"Mappings":
- skip:
version: " - 2.13.99"
reason: "constant_keyword is introduced in 2.14.0"

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

---
"Supported queries":
- skip:
version: " - 2.13.99"
reason: "constant_keyword is introduced in 2.14.0 in main branch"

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

- length: { hits.hits: 1 }
- match: { hits.hits.0._source.genre: "Romantic comedy" }
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,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 1b4b111

Please sign in to comment.