• Download Gist
TermsStatsFacetsShouldAllowFilter.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
@Test public void testTermsStatsFacetsShouldAllowFilter() throws Exception {
try {
client.admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
client.admin().indices().prepareCreate("test").execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
 
for (int i = 0; i < 20; i++) {
client.prepareIndex("test", "type1", Integer.toString(i)).setSource("num", i % 10).execute().actionGet();
}
client.admin().indices().prepareRefresh().execute().actionGet();
 
XContentFilterBuilder filter = FilterBuilders.numericRangeFilter("range").gt(5);
 
AbstractFacetBuilder fb1 = termsStats("stats1").keyField("num").valueScript("doc.score").order(TermsStatsFacet.ComparatorType.COUNT);
fb1.facetFilter(filter);
AbstractFacetBuilder fb2 = termsStats("stats2").keyField("num").valueScript("doc.score").order(TermsStatsFacet.ComparatorType.TOTAL);
fb2.facetFilter(filter);
SearchResponse searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(fb1).addFacet(fb2).setExplain(true)
.execute().actionGet();
 
if (searchResponse.failedShards() > 0) {
logger.warn("Failed shards:");
for (ShardSearchFailure shardSearchFailure : searchResponse.shardFailures()) {
logger.warn("-> {}", shardSearchFailure);
}
}
//System.out.println("expl:"+searchResponse.hits().hits()[0].explanation().getValue());
assertThat(searchResponse.failedShards(), equalTo(0));
TermsStatsFacet facet = searchResponse.facets().facet("stats1");
assertThat(facet.entries().size(), equalTo(5));
 
facet = searchResponse.facets().facet("stats2");
assertThat(facet.entries().size(), equalTo(5));
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.