Search in sources :

Example 21 with Global

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));
}
Also used : Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Example 22 with Global

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));
}
Also used : Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 23 with Global

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");
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)23 Global (org.elasticsearch.search.aggregations.bucket.global.Global)23 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)3 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)3 GeoPoint (org.elasticsearch.common.geo.GeoPoint)2 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)2 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)2 PercentileRanks (org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks)2 Percentiles (org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles)2 Stats (org.elasticsearch.search.aggregations.metrics.stats.Stats)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 Script (org.elasticsearch.script.Script)1 GlobalBuilder (org.elasticsearch.search.aggregations.bucket.global.GlobalBuilder)1 Missing (org.elasticsearch.search.aggregations.bucket.missing.Missing)1 TermsBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder)1