Skip to content

Commit

Permalink
Make sure stored JSON fields are properly decoded. (#35279)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtibshirani committed Mar 11, 2019
1 parent 5aad514 commit 62385a8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,15 @@ public void setSplitQueriesOnWhitespace(boolean splitQueriesOnWhitespace) {
this.splitQueriesOnWhitespace = splitQueriesOnWhitespace;
}

@Override
public Object valueForDisplay(Object value) {
if (value == null) {
return null;
}
BytesRef binaryValue = (BytesRef) value;
return binaryValue.utf8ToString();
}

@Override
public Query existsQuery(QueryShardContext context) {
return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ protected RootJsonFieldType createDefaultFieldType() {
return new RootJsonFieldType();
}

public void testValueForDisplay() {
RootJsonFieldType ft = createDefaultFieldType();

String fieldValue = "{ \"key\": \"value\" }";
BytesRef storedValue = new BytesRef(fieldValue);
assertEquals(fieldValue, ft.valueForDisplay(storedValue));
}

public void testTermQuery() {
RootJsonFieldType ft = createDefaultFieldType();
ft.setName("field");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1226,4 +1226,47 @@ public void testLoadMetadata() throws Exception {
assertThat(fields.get("_routing").isMetadataField(), equalTo(true));
assertThat(fields.get("_routing").getValue().toString(), equalTo("1"));
}

public void testStoredJsonField() throws Exception {
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("_doc")
.startObject("properties")
.startObject("json_field")
.field("type", "json")
.field("store", true)
.endObject()
.endObject()
.endObject()
.endObject();
assertAcked(prepareCreate("test").addMapping("_doc", mapping));
ensureGreen("test");

XContentBuilder source = XContentFactory.jsonBuilder()
.startObject()
.startObject("json_field")
.field("key", "value")
.endObject()
.endObject();
index("test", "_doc", "1", source);
refresh("test");

SearchResponse response = client().prepareSearch("test")
.addStoredField("json_field")
.get();
assertSearchResponse(response);
assertHitCount(response, 1);

Map<String, DocumentField> fields = response.getHits().getAt(0).getFields();
DocumentField field = fields.get("json_field");
assertEquals("json_field", field.getName());

// We make sure to pretty-print here, since the field is always stored in pretty-printed format.
BytesReference storedValue = BytesReference.bytes(XContentFactory.jsonBuilder()
.prettyPrint()
.startObject()
.field("key", "value")
.endObject());
assertEquals(storedValue.utf8ToString(), field.getValue());
}
}

0 comments on commit 62385a8

Please sign in to comment.