use of org.elasticsearch.search.aggregations.bucket.global.Global in project elasticsearch by elastic.
the class TransportTwoNodesSearchIT method testSimpleFacets.
public void testSimpleFacets() throws Exception {
prepareData();
SearchSourceBuilder sourceBuilder = searchSource().query(termQuery("multi", "test")).from(0).size(20).explain(true).aggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.filter("all", termQuery("multi", "test")))).aggregation(AggregationBuilders.filter("test1", termQuery("name", "test1")));
SearchResponse searchResponse = client().search(searchRequest("test").source(sourceBuilder)).actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
Global global = searchResponse.getAggregations().get("global");
Filter all = global.getAggregations().get("all");
Filter test1 = searchResponse.getAggregations().get("test1");
assertThat(test1.getDocCount(), equalTo(1L));
assertThat(all.getDocCount(), equalTo(100L));
}
use of org.elasticsearch.search.aggregations.bucket.global.Global in project elasticsearch by elastic.
the class ChildQuerySearchIT method testScopedFacet.
public void testScopedFacet() throws Exception {
assertAcked(prepareCreate("test").addMapping("parent").addMapping("child", "_parent", "type=parent", "c_field", "type=keyword"));
ensureGreen();
// index simple data
client().prepareIndex("test", "parent", "p1").setSource("p_field", "p_value1").get();
client().prepareIndex("test", "child", "c1").setSource("c_field", "red").setParent("p1").get();
client().prepareIndex("test", "child", "c2").setSource("c_field", "yellow").setParent("p1").get();
client().prepareIndex("test", "parent", "p2").setSource("p_field", "p_value2").get();
client().prepareIndex("test", "child", "c3").setSource("c_field", "blue").setParent("p2").get();
client().prepareIndex("test", "child", "c4").setSource("c_field", "red").setParent("p2").get();
refresh();
SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow")), ScoreMode.None)).addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.filter("filter", boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow"))).subAggregation(AggregationBuilders.terms("facet1").field("c_field")))).get();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
assertThat(searchResponse.getHits().getAt(0).getId(), anyOf(equalTo("p2"), equalTo("p1")));
assertThat(searchResponse.getHits().getAt(1).getId(), anyOf(equalTo("p2"), equalTo("p1")));
Global global = searchResponse.getAggregations().get("global");
Filter filter = global.getAggregations().get("filter");
Terms termsFacet = filter.getAggregations().get("facet1");
assertThat(termsFacet.getBuckets().size(), equalTo(2));
assertThat(termsFacet.getBuckets().get(0).getKeyAsString(), equalTo("red"));
assertThat(termsFacet.getBuckets().get(0).getDocCount(), equalTo(2L));
assertThat(termsFacet.getBuckets().get(1).getKeyAsString(), equalTo("yellow"));
assertThat(termsFacet.getBuckets().get(1).getDocCount(), equalTo(1L));
}
use of org.elasticsearch.search.aggregations.bucket.global.Global in project elasticsearch by elastic.
the class IndexAliasesIT method testSearchingFilteringAliasesSingleIndex.
public void testSearchingFilteringAliasesSingleIndex() throws Exception {
logger.info("--> creating index [test]");
assertAcked(prepareCreate("test").addMapping("type1", "id", "type=text", "name", "type=text,fielddata=true"));
ensureGreen();
logger.info("--> adding filtering aliases to index [test]");
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1"));
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias2"));
assertAcked(admin().indices().prepareAliases().addAlias("test", "foos", termQuery("name", "foo")));
assertAcked(admin().indices().prepareAliases().addAlias("test", "bars", termQuery("name", "bar")));
assertAcked(admin().indices().prepareAliases().addAlias("test", "tests", termQuery("name", "test")));
logger.info("--> indexing against [test]");
client().index(indexRequest("test").type("type1").id("1").source(source("1", "foo test"), XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE)).actionGet();
client().index(indexRequest("test").type("type1").id("2").source(source("2", "bar test"), XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE)).actionGet();
client().index(indexRequest("test").type("type1").id("3").source(source("3", "baz test"), XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE)).actionGet();
client().index(indexRequest("test").type("type1").id("4").source(source("4", "something else"), XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE)).actionGet();
logger.info("--> checking single filtering alias search");
SearchResponse searchResponse = client().prepareSearch("foos").setQuery(QueryBuilders.matchAllQuery()).get();
assertHits(searchResponse.getHits(), "1");
logger.info("--> checking single filtering alias wildcard search");
searchResponse = client().prepareSearch("fo*").setQuery(QueryBuilders.matchAllQuery()).get();
assertHits(searchResponse.getHits(), "1");
searchResponse = client().prepareSearch("tests").setQuery(QueryBuilders.matchAllQuery()).get();
assertHits(searchResponse.getHits(), "1", "2", "3");
logger.info("--> checking single filtering alias search with sort");
searchResponse = client().prepareSearch("tests").setQuery(QueryBuilders.matchAllQuery()).addSort("_uid", SortOrder.ASC).get();
assertHits(searchResponse.getHits(), "1", "2", "3");
logger.info("--> checking single filtering alias search with global facets");
searchResponse = client().prepareSearch("tests").setQuery(QueryBuilders.matchQuery("name", "bar")).addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.terms("test").field("name"))).get();
assertSearchResponse(searchResponse);
Global global = searchResponse.getAggregations().get("global");
Terms terms = global.getAggregations().get("test");
assertThat(terms.getBuckets().size(), equalTo(4));
logger.info("--> checking single filtering alias search with global facets and sort");
searchResponse = client().prepareSearch("tests").setQuery(QueryBuilders.matchQuery("name", "bar")).addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.terms("test").field("name"))).addSort("_uid", SortOrder.ASC).get();
assertSearchResponse(searchResponse);
global = searchResponse.getAggregations().get("global");
terms = global.getAggregations().get("test");
assertThat(terms.getBuckets().size(), equalTo(4));
logger.info("--> checking single filtering alias search with non-global facets");
searchResponse = client().prepareSearch("tests").setQuery(QueryBuilders.matchQuery("name", "bar")).addAggregation(AggregationBuilders.terms("test").field("name")).addSort("_uid", SortOrder.ASC).get();
assertSearchResponse(searchResponse);
terms = searchResponse.getAggregations().get("test");
assertThat(terms.getBuckets().size(), equalTo(2));
searchResponse = client().prepareSearch("foos", "bars").setQuery(QueryBuilders.matchAllQuery()).get();
assertHits(searchResponse.getHits(), "1", "2");
logger.info("--> checking single non-filtering alias search");
searchResponse = client().prepareSearch("alias1").setQuery(QueryBuilders.matchAllQuery()).get();
assertHits(searchResponse.getHits(), "1", "2", "3", "4");
logger.info("--> checking non-filtering alias and filtering alias search");
searchResponse = client().prepareSearch("alias1", "foos").setQuery(QueryBuilders.matchAllQuery()).get();
assertHits(searchResponse.getHits(), "1", "2", "3", "4");
logger.info("--> checking index and filtering alias search");
searchResponse = client().prepareSearch("test", "foos").setQuery(QueryBuilders.matchAllQuery()).get();
assertHits(searchResponse.getHits(), "1", "2", "3", "4");
logger.info("--> checking index and alias wildcard search");
searchResponse = client().prepareSearch("te*", "fo*").setQuery(QueryBuilders.matchAllQuery()).get();
assertHits(searchResponse.getHits(), "1", "2", "3", "4");
}
Aggregations