Skip to content

Commit f84a26e

Browse files
gaobinlongkkewweiandrross
authored
fix constant_keyword field type (#14807) (#14969)
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]>
1 parent fc729b6 commit f84a26e

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

release-notes/opensearch.release-notes-2.16.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@
9191
- Create new IndexInput for multi part upload ([#14888](https://github.com/opensearch-project/OpenSearch/pull/14888))
9292
- Fix searchable snapshot failure with scripted fields ([#14411](https://github.com/opensearch-project/OpenSearch/pull/14411))
9393
- Fix the visit of inner query for NestedQueryBuilder ([#14739](https://github.com/opensearch-project/OpenSearch/pull/14739))
94+
- Fix constant_keyword field type used when creating index ([#14807](https://github.com/opensearch-project/OpenSearch/pull/14807))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
# The test setup includes:
3+
# - Create index with constant_keyword field type
4+
# - Check mapping
5+
# - Index two example documents
6+
# - Search
7+
# - Delete Index when connection is teardown
8+
9+
"Mappings and Supported queries":
10+
- skip:
11+
version: " - 2.99.99"
12+
reason: "fixed in 3.0.0"
13+
14+
# Create index with constant_keyword field type
15+
- do:
16+
indices.create:
17+
index: test
18+
body:
19+
mappings:
20+
properties:
21+
genre:
22+
type: "constant_keyword"
23+
value: "1"
24+
25+
# Index document
26+
- do:
27+
index:
28+
index: test
29+
id: 1
30+
body: {
31+
"genre": "1"
32+
}
33+
34+
- do:
35+
index:
36+
index: test
37+
id: 2
38+
body: {
39+
"genre": 1
40+
}
41+
42+
- do:
43+
indices.refresh:
44+
index: test
45+
46+
# Check mapping
47+
- do:
48+
indices.get_mapping:
49+
index: test
50+
- is_true: test.mappings
51+
- match: { test.mappings.properties.genre.type: constant_keyword }
52+
- length: { test.mappings.properties.genre: 2 }
53+
54+
# Verify Document Count
55+
- do:
56+
search:
57+
body: {
58+
query: {
59+
match_all: {}
60+
}
61+
}
62+
63+
- length: { hits.hits: 2 }
64+
- match: { hits.hits.0._source.genre: "1" }
65+
- match: { hits.hits.1._source.genre: 1 }
66+
67+
# Delete Index when connection is teardown
68+
- do:
69+
indices.delete:
70+
index: test

server/src/main/java/org/opensearch/index/mapper/ConstantKeywordFieldMapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ private static ConstantKeywordFieldMapper toType(FieldMapper in) {
6868
*/
6969
public static class Builder extends ParametrizedFieldMapper.Builder {
7070

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

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

7878
@Override

server/src/test/java/org/opensearch/index/mapper/ConstantKeywordFieldMapperTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ public void testMissingDefaultIndexMapper() throws Exception {
105105
assertThat(e.getMessage(), containsString("Field [field] is missing required parameter [value]"));
106106
}
107107

108+
public void testBuilderToXContent() throws IOException {
109+
ConstantKeywordFieldMapper.Builder builder = new ConstantKeywordFieldMapper.Builder("name", "value1");
110+
XContentBuilder xContentBuilder = JsonXContent.contentBuilder().startObject();
111+
builder.toXContent(xContentBuilder, false);
112+
xContentBuilder.endObject();
113+
assertEquals("{\"value\":\"value1\"}", xContentBuilder.toString());
114+
}
115+
108116
private final SourceToParse source(CheckedConsumer<XContentBuilder, IOException> build) throws IOException {
109117
XContentBuilder builder = JsonXContent.contentBuilder().startObject();
110118
build.accept(builder);

0 commit comments

Comments
 (0)