Skip to content

Commit

Permalink
Add a test around JSON fields and source filtering. (#35399)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtibshirani committed Mar 28, 2019
1 parent 8041e23 commit 91bc7e1
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.test.ESIntegTestCase;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.IsEqual.equalTo;
Expand Down Expand Up @@ -95,4 +99,35 @@ public void testSourceWithWildcardFiltering() {
assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(1));
assertThat((String) response.getHits().getAt(0).getSourceAsMap().get("field"), equalTo("value"));
}

public void testSourceFilteringWithJsonField() {
prepareCreate("test").addMapping("_doc", "headers", "type=json").get();
ensureGreen();

Map<String, Object> headers = new HashMap<>();
headers.put("content-type", "application/json");
headers.put("origin", "https://www.elastic.co");
Map<String, Object> source = Collections.singletonMap("headers", headers);

client().prepareIndex("test", "_doc", "1").setSource(source).get();
refresh();

SearchResponse response = client().prepareSearch("test").setFetchSource(true).get();
assertThat(response.getHits().getAt(0).getSourceAsMap(), equalTo(source));

// Check 'include' filtering.
response = client().prepareSearch("test").setFetchSource("headers", null).get();
assertThat(response.getHits().getAt(0).getSourceAsMap(), equalTo(source));

response = client().prepareSearch("test").setFetchSource("headers.content-type", null).get();
Map<String, Object> filteredSource = Collections.singletonMap("headers",
Collections.singletonMap("content-type", "application/json"));
assertThat(response.getHits().getAt(0).getSourceAsMap(), equalTo(filteredSource));

// Check 'exclude' filtering.
response = client().prepareSearch("test").setFetchSource(null, "headers.content-type").get();
filteredSource = Collections.singletonMap("headers",
Collections.singletonMap("origin", "https://www.elastic.co"));
assertThat(response.getHits().getAt(0).getSourceAsMap(), equalTo(filteredSource));
}
}

0 comments on commit 91bc7e1

Please sign in to comment.