.setQuery(termQuery("stag", "111"))
.addFacet(termsFacet("facet1").field("stag").size(10).executionHint(executionHint))
.addFacet(termsFacet("facet2").field("tag").size(10).executionHint(executionHint))
.execute().actionGet();
TermsFacet facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.getTotalCount(), equalTo(2l));
assertThat(facet.getOtherCount(), equalTo(0l));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(2));
facet = searchResponse.facets().facet("facet2");
assertThat(facet.name(), equalTo("facet2"));
assertThat(facet.entries().size(), equalTo(3));
assertThat(facet.entries().get(0).term(), equalTo("yyy"));
assertThat(facet.entries().get(0).count(), equalTo(2));
// Numeric
searchResponse = client.prepareSearch()
.setQuery(termQuery("stag", "111"))
.addFacet(termsFacet("facet1").field("lstag").size(10).executionHint(executionHint))
.addFacet(termsFacet("facet2").field("ltag").size(10).executionHint(executionHint))
.addFacet(termsFacet("facet3").field("ltag").size(10).exclude(3000).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet, instanceOf(InternalLongTermsFacet.class));
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(2));
facet = searchResponse.facets().facet("facet2");
assertThat(facet, instanceOf(InternalLongTermsFacet.class));
assertThat(facet.name(), equalTo("facet2"));
assertThat(facet.entries().size(), equalTo(3));
assertThat(facet.entries().get(0).term(), equalTo("2000"));
assertThat(facet.entries().get(0).count(), equalTo(2));
assertThat(facet.entries().get(1).term(), anyOf(equalTo("1000"), equalTo("3000")));
assertThat(facet.entries().get(1).count(), equalTo(1));
assertThat(facet.entries().get(2).term(), anyOf(equalTo("1000"), equalTo("3000")));
assertThat(facet.entries().get(2).count(), equalTo(1));
facet = searchResponse.facets().facet("facet3");
assertThat(facet, instanceOf(InternalLongTermsFacet.class));
assertThat(facet.name(), equalTo("facet3"));
assertThat(facet.entries().size(), equalTo(2));
assertThat(facet.entries().get(0).term(), equalTo("2000"));
assertThat(facet.entries().get(0).count(), equalTo(2));
assertThat(facet.entries().get(1).term(), equalTo("1000"));
assertThat(facet.entries().get(1).count(), equalTo(1));
searchResponse = client.prepareSearch()
.setQuery(termQuery("stag", "111"))
.addFacet(termsFacet("facet1").field("dstag").size(10).executionHint(executionHint))
.addFacet(termsFacet("facet2").field("dtag").size(10).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet, instanceOf(InternalDoubleTermsFacet.class));
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111.1"));
assertThat(facet.entries().get(0).count(), equalTo(2));
facet = searchResponse.facets().facet("facet2");
assertThat(facet, instanceOf(InternalDoubleTermsFacet.class));
assertThat(facet.name(), equalTo("facet2"));
assertThat(facet.entries().size(), equalTo(3));
assertThat(facet.entries().get(0).term(), equalTo("2000.1"));
assertThat(facet.entries().get(0).count(), equalTo(2));
assertThat(facet.entries().get(1).term(), anyOf(equalTo("1000.1"), equalTo("3000.1")));
assertThat(facet.entries().get(1).count(), equalTo(1));
assertThat(facet.entries().get(2).term(), anyOf(equalTo("1000.1"), equalTo("3000.1")));
assertThat(facet.entries().get(2).count(), equalTo(1));
searchResponse = client.prepareSearch()
.setQuery(termQuery("stag", "111"))
.addFacet(termsFacet("facet1").field("bstag").size(10).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet, instanceOf(InternalByteTermsFacet.class));
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(2));
searchResponse = client.prepareSearch()
.setQuery(termQuery("stag", "111"))
.addFacet(termsFacet("facet1").field("istag").size(10).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet, instanceOf(InternalIntTermsFacet.class));
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(2));
searchResponse = client.prepareSearch()
.setQuery(termQuery("stag", "111"))
.addFacet(termsFacet("facet1").field("shstag").size(10).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet, instanceOf(InternalShortTermsFacet.class));
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(2));
// Test Facet Filter
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("stag").size(10).facetFilter(termFilter("tag", "xxx")).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(1));
// now with global
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("stag").size(10).facetFilter(termFilter("tag", "xxx")).global(true).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(1));
// Test Facet Filter (with a type)
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("type1.stag").size(10).facetFilter(termFilter("tag", "xxx")).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(1));
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("tag").size(10).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(3));
assertThat(facet.entries().get(0).term(), equalTo("yyy"));
assertThat(facet.entries().get(0).count(), equalTo(2));
assertThat(facet.entries().get(1).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
assertThat(facet.entries().get(1).count(), equalTo(1));
assertThat(facet.entries().get(2).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
assertThat(facet.entries().get(2).count(), equalTo(1));
// Bounded Size
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("tag").size(2).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(2));
assertThat(facet.entries().get(0).term(), equalTo("yyy"));
assertThat(facet.entries().get(0).count(), equalTo(2));
assertThat(facet.entries().get(1).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
assertThat(facet.entries().get(1).count(), equalTo(1));
// Test Exclude
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("tag").size(10).exclude("yyy").executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(2));
assertThat(facet.entries().get(0).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
assertThat(facet.entries().get(0).count(), equalTo(1));
assertThat(facet.entries().get(1).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
assertThat(facet.entries().get(1).count(), equalTo(1));
// Test Order
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("tag").size(10).order(TermsFacet.ComparatorType.TERM).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(3));
assertThat(facet.entries().get(0).term(), equalTo("xxx"));
assertThat(facet.entries().get(0).count(), equalTo(1));
assertThat(facet.entries().get(1).term(), equalTo("yyy"));
assertThat(facet.entries().get(1).count(), equalTo(2));
assertThat(facet.entries().get(2).term(), equalTo("zzz"));
assertThat(facet.entries().get(2).count(), equalTo(1));
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("tag").size(10).order(TermsFacet.ComparatorType.REVERSE_TERM).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(3));
assertThat(facet.entries().get(2).term(), equalTo("xxx"));
assertThat(facet.entries().get(2).count(), equalTo(1));
assertThat(facet.entries().get(1).term(), equalTo("yyy"));
assertThat(facet.entries().get(1).count(), equalTo(2));
assertThat(facet.entries().get(0).term(), equalTo("zzz"));
assertThat(facet.entries().get(0).count(), equalTo(1));
// Script
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("tag").size(10).script("term + param1").param("param1", "a").order(TermsFacet.ComparatorType.TERM).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(3));
assertThat(facet.entries().get(0).term(), equalTo("xxxa"));
assertThat(facet.entries().get(0).count(), equalTo(1));
assertThat(facet.entries().get(1).term(), equalTo("yyya"));
assertThat(facet.entries().get(1).count(), equalTo(2));
assertThat(facet.entries().get(2).term(), equalTo("zzza"));
assertThat(facet.entries().get(2).count(), equalTo(1));
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("tag").size(10).script("term == 'xxx' ? false : true").order(TermsFacet.ComparatorType.TERM).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(2));
assertThat(facet.entries().get(0).term(), equalTo("yyy"));
assertThat(facet.entries().get(0).count(), equalTo(2));
assertThat(facet.entries().get(1).term(), equalTo("zzz"));
assertThat(facet.entries().get(1).count(), equalTo(1));
// Fields Facets
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").fields("stag", "tag").size(10).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(4));
assertThat(facet.entries().get(0).term(), anyOf(equalTo("111"), equalTo("yyy")));
assertThat(facet.entries().get(0).count(), equalTo(2));
assertThat(facet.entries().get(1).term(), anyOf(equalTo("111"), equalTo("yyy")));
assertThat(facet.entries().get(1).count(), equalTo(2));
assertThat(facet.entries().get(2).term(), anyOf(equalTo("zzz"), equalTo("xxx")));
assertThat(facet.entries().get(2).count(), equalTo(1));
assertThat(facet.entries().get(3).term(), anyOf(equalTo("zzz"), equalTo("xxx")));
assertThat(facet.entries().get(3).count(), equalTo(1));
searchResponse = client.prepareSearch()
.setQuery(termQuery("xxx", "yyy")) // don't match anything
.addFacet(termsFacet("facet1").field("tag").size(10).allTerms(true).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(3));
assertThat(facet.entries().get(0).term(), anyOf(equalTo("xxx"), equalTo("yyy"), equalTo("zzz")));
assertThat(facet.entries().get(0).count(), equalTo(0));
assertThat(facet.entries().get(1).term(), anyOf(equalTo("xxx"), equalTo("yyy"), equalTo("zzz")));
assertThat(facet.entries().get(1).count(), equalTo(0));
assertThat(facet.entries().get(2).term(), anyOf(equalTo("xxx"), equalTo("yyy"), equalTo("zzz")));
assertThat(facet.entries().get(2).count(), equalTo(0));
// Script Field
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").scriptField("_source.stag").size(10).executionHint(executionHint))
.addFacet(termsFacet("facet2").scriptField("_source.tag").size(10).executionHint(executionHint))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(2));
facet = searchResponse.facets().facet("facet2");
assertThat(facet.name(), equalTo("facet2"));
assertThat(facet.entries().size(), equalTo(3));
assertThat(facet.entries().get(0).term(), equalTo("yyy"));
assertThat(facet.entries().get(0).count(), equalTo(2));
}
}