Skip to content

Commit

Permalink
fix constant_keyword field type not working
Browse files Browse the repository at this point in the history
  • Loading branch information
kkewwei committed Jul 4, 2024
1 parent 501a702 commit 157063f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
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 @@ -27,6 +27,7 @@
import java.util.Collection;

import static org.hamcrest.Matchers.containsString;
import static org.opensearch.index.mapper.ConstantKeywordFieldMapper.CONTENT_TYPE;

public class ConstantKeywordFieldMapperTests extends OpenSearchSingleNodeTestCase {

Expand Down Expand Up @@ -105,6 +106,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 157063f

Please sign in to comment.