Skip to content

Commit 5221f18

Browse files
committed
Ensure that the indexed leaf values are not stored.
1 parent 78f510b commit 5221f18

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.elasticsearch.index.analysis.NamedAnalyzer;
4343
import org.elasticsearch.index.query.QueryShardContext;
4444

45-
import java.io.ByteArrayOutputStream;
4645
import java.io.IOException;
4746
import java.util.Iterator;
4847
import java.util.List;
@@ -379,7 +378,8 @@ private JsonFieldMapper(String simpleName,
379378
assert fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) <= 0;
380379

381380
this.ignoreAbove = ignoreAbove;
382-
this.fieldParser = new JsonFieldParser(fieldType.name(), keyedFieldName(), fieldType, ignoreAbove);
381+
this.fieldParser = new JsonFieldParser(fieldType.name(), keyedFieldName(),
382+
ignoreAbove, fieldType.nullValueAsString());
383383
}
384384

385385
@Override

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.index.mapper;
2121

2222
import org.apache.lucene.document.Field;
23+
import org.apache.lucene.document.StringField;
2324
import org.apache.lucene.index.IndexableField;
2425
import org.apache.lucene.util.BytesRef;
2526
import org.elasticsearch.common.xcontent.XContentParser;
@@ -39,17 +40,17 @@ public class JsonFieldParser {
3940
private final String rootFieldName;
4041
private final String keyedFieldName;
4142

42-
private final MappedFieldType fieldType;
4343
private final int ignoreAbove;
44+
private final String nullValueAsString;
4445

4546
JsonFieldParser(String rootFieldName,
4647
String keyedFieldName,
47-
MappedFieldType fieldType,
48-
int ignoreAbove) {
48+
int ignoreAbove,
49+
String nullValueAsString) {
4950
this.rootFieldName = rootFieldName;
5051
this.keyedFieldName = keyedFieldName;
51-
this.fieldType = fieldType;
5252
this.ignoreAbove = ignoreAbove;
53+
this.nullValueAsString = nullValueAsString;
5354
}
5455

5556
public List<IndexableField> parse(XContentParser parser) throws IOException {
@@ -111,9 +112,8 @@ private void parseFieldValue(XContentParser.Token token,
111112
String value = parser.text();
112113
addField(path, currentName, value, fields);
113114
} else if (token == XContentParser.Token.VALUE_NULL) {
114-
String value = fieldType.nullValueAsString();
115-
if (value != null) {
116-
addField(path, currentName, value, fields);
115+
if (nullValueAsString != null) {
116+
addField(path, currentName, nullValueAsString, fields);
117117
}
118118
} else {
119119
// Note that we throw an exception here just to be safe. We don't actually expect to reach
@@ -137,8 +137,8 @@ private void addField(ContentPath path,
137137
}
138138
String keyedValue = createKeyedValue(key, value);
139139

140-
fields.add(new Field(rootFieldName, new BytesRef(value), fieldType));
141-
fields.add(new Field(keyedFieldName, new BytesRef(keyedValue), fieldType));
140+
fields.add(new StringField(rootFieldName, new BytesRef(value), Field.Store.NO));
141+
fields.add(new StringField(keyedFieldName, new BytesRef(keyedValue), Field.Store.NO));
142142
}
143143

144144
public static String createKeyedValue(String key, String value) {

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ public void testEnableStore() throws Exception {
131131
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
132132
.startObject("type")
133133
.startObject("properties")
134-
.startObject("field")
134+
.startObject("store_and_index")
135+
.field("type", "json")
136+
.field("store", true)
137+
.endObject()
138+
.startObject("store_only")
135139
.field("type", "json")
136140
.field("index", false)
137141
.field("store", true)
@@ -144,24 +148,34 @@ public void testEnableStore() throws Exception {
144148
assertEquals(mapping, mapper.mappingSource().toString());
145149

146150
BytesReference doc = BytesReference.bytes(XContentFactory.jsonBuilder().startObject()
147-
.startObject("field")
151+
.startObject("store_only")
152+
.field("key", "value")
153+
.endObject()
154+
.startObject("store_and_index")
148155
.field("key", "value")
149156
.endObject()
150157
.endObject());
151-
152158
ParsedDocument parsedDoc = mapper.parse(SourceToParse.source("test", "type", "1", doc, XContentType.JSON));
153159

154-
IndexableField[] fields = parsedDoc.rootDoc().getFields("field");
155-
assertEquals(1, fields.length);
156-
assertTrue(fields[0].fieldType().stored());
157-
158160
// We make sure to pretty-print here, since the field is always stored in pretty-printed format.
159161
BytesReference storedValue = BytesReference.bytes(JsonXContent.contentBuilder()
160162
.prettyPrint()
161163
.startObject()
162-
.field("key", "value")
164+
.field("key", "value")
163165
.endObject());
164-
assertEquals(storedValue.toBytesRef(), fields[0].binaryValue());
166+
167+
IndexableField[] storeOnly = parsedDoc.rootDoc().getFields("store_only");
168+
assertEquals(1, storeOnly.length);
169+
170+
assertTrue(storeOnly[0].fieldType().stored());
171+
assertEquals(storedValue.toBytesRef(), storeOnly[0].binaryValue());
172+
173+
IndexableField[] storeAndIndex = parsedDoc.rootDoc().getFields("store_and_index");
174+
assertEquals(2, storeAndIndex.length);
175+
176+
assertTrue(storeAndIndex[0].fieldType().stored());
177+
assertEquals(storedValue.toBytesRef(), storeAndIndex[0].binaryValue());
178+
assertFalse(storeAndIndex[1].fieldType().stored());
165179
}
166180

167181
public void testIndexOptions() throws IOException {

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ public class JsonFieldParserTests extends ESTestCase {
4141
@Before
4242
public void setUp() throws Exception {
4343
super.setUp();
44-
45-
MappedFieldType fieldType = new RootJsonFieldType();
46-
fieldType.setName("field");
47-
parser = new JsonFieldParser("field", "field._keyed", fieldType, Integer.MAX_VALUE);
44+
parser = new JsonFieldParser("field", "field._keyed", Integer.MAX_VALUE, null);
4845
}
4946

5047
public void testTextValues() throws Exception {
@@ -222,9 +219,9 @@ public void testIgnoreAbove() throws Exception {
222219

223220
RootJsonFieldType fieldType = new RootJsonFieldType();
224221
fieldType.setName("field");
225-
JsonFieldParser ignoreAboveParser = new JsonFieldParser("field", "field._keyed", fieldType, 10);
222+
JsonFieldParser parserWithIgnoreAbove = new JsonFieldParser("field", "field._keyed", 10, null);
226223

227-
List<IndexableField> fields = ignoreAboveParser.parse(xContentParser);
224+
List<IndexableField> fields = parserWithIgnoreAbove.parse(xContentParser);
228225
assertEquals(0, fields.size());
229226
}
230227

@@ -236,13 +233,10 @@ public void testNullValues() throws Exception {
236233
assertEquals(0, fields.size());
237234

238235
xContentParser = createXContentParser(input);
236+
JsonFieldParser parserWithNullValue = new JsonFieldParser("field", "field._keyed",
237+
Integer.MAX_VALUE, "placeholder");
239238

240-
RootJsonFieldType fieldType = new RootJsonFieldType();
241-
fieldType.setName("field");
242-
fieldType.setNullValue("placeholder");
243-
JsonFieldParser nullValueParser = new JsonFieldParser("field", "field._keyed", fieldType, Integer.MAX_VALUE);
244-
245-
fields = nullValueParser.parse(xContentParser);
239+
fields = parserWithNullValue.parse(xContentParser);
246240
assertEquals(2, fields.size());
247241

248242
IndexableField field = fields.get(0);

0 commit comments

Comments
 (0)