Skip to content

Commit 1c816eb

Browse files
committed
Address code review feedback.
* Don't explicitly mention that '\0' is not allowed in keys. * Use String#indexOf in FieldTypeLookup#fieldDepth. * Construct the whitespace analyzer once per field mapper.
1 parent 7f4614d commit 1c816eb

File tree

6 files changed

+13
-21
lines changed

6 files changed

+13
-21
lines changed

docs/reference/mapping/types.asciidoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ string:: <<text,`text`>> and <<keyword,`keyword`>>
4444

4545
<<alias>>:: Defines an alias to an existing field.
4646

47-
<<embedded-json>>:: Allows an entire JSON object to be indexed as a single field.
48-
4947
<<rank-feature>>:: Record numeric feature to boost hits at query time.
5048

5149
<<rank-features>>:: Record numeric features to boost hits at query time.

docs/reference/mapping/types/embedded-json.asciidoc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ keyword-style aggregations such as `terms`. As with queries, there is no
113113
special support for numerics -- all values in the JSON object are treated as
114114
keywords. When sorting, this implies that values are compared lexicographically.
115115

116-
Finally, because of the way leaf values are stored in the index, the null
117-
character `\0` is not allowed to appear in the keys of the JSON object.
118-
119116
[[stored-fields]]
120117
==== Stored fields
121118

server/src/main/java/org/elasticsearch/index/mapper/FieldTypeLookup.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,17 @@ private static int getMaxJsonFieldDepth(CopyOnWriteHashMap<String, String> alias
131131
*/
132132
private static int fieldDepth(String field) {
133133
int numDots = 0;
134-
for (int i = 0; i < field.length(); ++i) {
135-
if (field.charAt(i) == '.') {
136-
numDots++;
134+
int dotIndex = -1;
135+
while (true) {
136+
dotIndex = field.indexOf('.', dotIndex + 1);
137+
if (dotIndex < 0) {
138+
break;
137139
}
140+
numDots++;
138141
}
139142
return numDots + 1;
140143
}
141144

142-
143145
/**
144146
* Returns the mapped field type for the given field name.
145147
*/

server/src/main/java/org/elasticsearch/index/mapper/JsonFieldMapper.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,14 @@
8989
*
9090
* the mapper will produce untokenized string fields called "json_field" with values "some value" and "true",
9191
* as well as string fields called "json_field._keyed" with values "key\0some value" and "key2.key3\0true".
92-
*
93-
* Note that \0 is a reserved separator character, and cannot be used in the keys of the JSON object
94-
* (see {@link JsonFieldParser#SEPARATOR}).
92+
* Note that \0 is used as a reserved separator character (see {@link JsonFieldParser#SEPARATOR}).
9593
*
9694
* When 'store' is enabled, a single stored field is added containing the entire JSON object in
9795
* pretty-printed format.
9896
*/
9997
public final class JsonFieldMapper extends FieldMapper {
10098

10199
public static final String CONTENT_TYPE = "embedded_json";
102-
public static final NamedAnalyzer WHITESPACE_ANALYZER = new NamedAnalyzer(
103-
"whitespace", AnalyzerScope.INDEX, new WhitespaceAnalyzer());
104100
private static final String KEYED_FIELD_SUFFIX = "._keyed";
105101

106102
private static class Defaults {
@@ -183,7 +179,8 @@ public Builder copyTo(CopyTo copyTo) {
183179
public JsonFieldMapper build(BuilderContext context) {
184180
setupFieldType(context);
185181
if (fieldType().splitQueriesOnWhitespace()) {
186-
fieldType().setSearchAnalyzer(WHITESPACE_ANALYZER);
182+
NamedAnalyzer whitespaceAnalyzer = new NamedAnalyzer("whitespace", AnalyzerScope.INDEX, new WhitespaceAnalyzer());
183+
fieldType().setSearchAnalyzer(whitespaceAnalyzer);
187184
}
188185
return new JsonFieldMapper(name, fieldType, defaultFieldType,
189186
ignoreAbove, depthLimit, context.indexSettings());

server/src/main/java/org/elasticsearch/index/mapper/JsonFieldParser.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,8 @@ private void addField(ContentPath path,
138138
}
139139

140140
String key = path.pathAsText(currentName);
141-
if (key.contains(SEPARATOR)) {
142-
throw new IllegalArgumentException("Keys in [embedded_json] fields cannot contain the reserved character \\0."
143-
+ " Offending key: [" + key + "].");
144-
}
141+
assert key.contains(SEPARATOR) == false: "Keys in JSON fields cannot contain the reserved character \\0.";
142+
145143
String keyedValue = createKeyedValue(key, value);
146144

147145
if (fieldType.indexOptions() != IndexOptions.NONE) {

server/src/test/java/org/elasticsearch/index/mapper/JsonFieldMapperTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,12 @@ public void testSplitQueriesOnWhitespace() throws IOException {
488488
mapperService.merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
489489

490490
RootJsonFieldType rootFieldType = (RootJsonFieldType) mapperService.fullName("field");
491-
assertThat(rootFieldType.searchAnalyzer(), equalTo(JsonFieldMapper.WHITESPACE_ANALYZER));
491+
assertThat(rootFieldType.searchAnalyzer().name(), equalTo("whitespace"));
492492
assertTokenStreamContents(rootFieldType.searchAnalyzer().analyzer().tokenStream("", "Hello World"),
493493
new String[] {"Hello", "World"});
494494

495495
KeyedJsonFieldType keyedFieldType = (KeyedJsonFieldType) mapperService.fullName("field.key");
496-
assertThat(keyedFieldType.searchAnalyzer(), equalTo(JsonFieldMapper.WHITESPACE_ANALYZER));
496+
assertThat(keyedFieldType.searchAnalyzer().name(), equalTo("whitespace"));
497497
assertTokenStreamContents(keyedFieldType.searchAnalyzer().analyzer().tokenStream("", "Hello World"),
498498
new String[] {"Hello", "World"});
499499
}

0 commit comments

Comments
 (0)