Search in sources :

Example 16 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class MissingValueIT method testPercentiles.

public void testPercentiles() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(percentiles("percentiles").field("long").missing(1000)).get();
    assertSearchResponse(response);
    Percentiles percentiles = response.getAggregations().get("percentiles");
    assertEquals(1000, percentiles.percentile(100), 0);
}
Also used : Percentiles(org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class MissingValueIT method testGeoCentroid.

public void testGeoCentroid() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(geoCentroid("centroid").field("location").missing("2,1")).get();
    assertSearchResponse(response);
    GeoCentroid centroid = response.getAggregations().get("centroid");
    GeoPoint point = new GeoPoint(1.5, 1.5);
    assertThat(point.lat(), closeTo(centroid.centroid().lat(), 1E-5));
    assertThat(point.lon(), closeTo(centroid.centroid().lon(), 1E-5));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoCentroid(org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroid) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 18 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class MissingValueIT method testHistogram.

public void testHistogram() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("my_histogram").field("long").interval(5).missing(7)).get();
    assertSearchResponse(response);
    Histogram histogram = response.getAggregations().get("my_histogram");
    assertEquals(2, histogram.getBuckets().size());
    assertEquals(0d, histogram.getBuckets().get(0).getKey());
    assertEquals(1, histogram.getBuckets().get(0).getDocCount());
    assertEquals(5d, histogram.getBuckets().get(1).getKey());
    assertEquals(1, histogram.getBuckets().get(1).getDocCount());
    response = client().prepareSearch("idx").addAggregation(histogram("my_histogram").field("long").interval(5).missing(3)).get();
    assertSearchResponse(response);
    histogram = response.getAggregations().get("my_histogram");
    assertEquals(1, histogram.getBuckets().size());
    assertEquals(0d, histogram.getBuckets().get(0).getKey());
    assertEquals(2, histogram.getBuckets().get(0).getDocCount());
}
Also used : AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 19 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class AliasRoutingIT method testAliasSearchRoutingWithConcreteAndAliasedIndices_issue3268.

/*
    See https://github.com/elastic/elasticsearch/pull/3268
    Searching on more than one index, if one of those is an alias with configured routing, the shards that belonged
    to the other indices (without routing) were not taken into account in PlainOperationRouting#searchShardsCount.
    That could cause returning 1, which led to forcing the QUERY_AND_FETCH mode.
    As a result, (size * number of hit shards) results were returned and no reduce phase was taking place.
     */
public void testAliasSearchRoutingWithConcreteAndAliasedIndices_issue3268() throws Exception {
    createIndex("index", "index_2");
    ensureGreen();
    assertAcked(admin().indices().prepareAliases().addAliasAction(AliasActions.add().index("index").alias("index_1").routing("1")));
    logger.info("--> indexing on index_1 which is an alias for index with routing [1]");
    client().prepareIndex("index_1", "type1", "1").setSource("field", "value1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    logger.info("--> indexing on index_2 which is a concrete index");
    client().prepareIndex("index_2", "type2", "2").setSource("field", "value2").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    SearchResponse searchResponse = client().prepareSearch("index_*").setSearchType(SearchType.QUERY_THEN_FETCH).setSize(1).setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();
    logger.info("--> search all on index_* should find two");
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    //Let's make sure that, even though 2 docs are available, only one is returned according to the size we set in the request
    //Therefore the reduce phase has taken place, which proves that the QUERY_AND_FETCH search type wasn't erroneously forced.
    assertThat(searchResponse.getHits().getHits().length, equalTo(1));
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 20 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class GeoDistanceIT method testWithSubAggregation.

public void testWithSubAggregation() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(geoDistance("amsterdam_rings", new GeoPoint(52.3760, 4.894)).field("location").unit(DistanceUnit.KILOMETERS).addUnboundedTo(500).addRange(500, 1000).addUnboundedFrom(1000).subAggregation(terms("cities").field("city").collectMode(randomFrom(SubAggCollectionMode.values())))).execute().actionGet();
    assertSearchResponse(response);
    Range geoDist = response.getAggregations().get("amsterdam_rings");
    assertThat(geoDist, notNullValue());
    assertThat(geoDist.getName(), equalTo("amsterdam_rings"));
    List<? extends Bucket> buckets = geoDist.getBuckets();
    assertThat(geoDist.getBuckets().size(), equalTo(3));
    assertThat(geoDist.getProperty("_bucket_count"), equalTo(3));
    Object[] propertiesKeys = (Object[]) geoDist.getProperty("_key");
    Object[] propertiesDocCounts = (Object[]) geoDist.getProperty("_count");
    Object[] propertiesCities = (Object[]) geoDist.getProperty("cities");
    Range.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("*-500.0"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(0.0));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(500.0));
    assertThat(bucket.getFromAsString(), equalTo("0.0"));
    assertThat(bucket.getToAsString(), equalTo("500.0"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    Terms cities = bucket.getAggregations().get("cities");
    assertThat(cities, Matchers.notNullValue());
    Set<String> names = new HashSet<>();
    for (Terms.Bucket city : cities.getBuckets()) {
        names.add(city.getKeyAsString());
    }
    assertThat(names.contains("utrecht") && names.contains("haarlem"), is(true));
    assertThat((String) propertiesKeys[0], equalTo("*-500.0"));
    assertThat((long) propertiesDocCounts[0], equalTo(2L));
    assertThat((Terms) propertiesCities[0], sameInstance(cities));
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("500.0-1000.0"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(500.0));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(1000.0));
    assertThat(bucket.getFromAsString(), equalTo("500.0"));
    assertThat(bucket.getToAsString(), equalTo("1000.0"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    cities = bucket.getAggregations().get("cities");
    assertThat(cities, Matchers.notNullValue());
    names = new HashSet<>();
    for (Terms.Bucket city : cities.getBuckets()) {
        names.add(city.getKeyAsString());
    }
    assertThat(names.contains("berlin") && names.contains("prague"), is(true));
    assertThat((String) propertiesKeys[1], equalTo("500.0-1000.0"));
    assertThat((long) propertiesDocCounts[1], equalTo(2L));
    assertThat((Terms) propertiesCities[1], sameInstance(cities));
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("1000.0-*"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(1000.0));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(bucket.getFromAsString(), equalTo("1000.0"));
    assertThat(bucket.getToAsString(), nullValue());
    assertThat(bucket.getDocCount(), equalTo(1L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    cities = bucket.getAggregations().get("cities");
    assertThat(cities, Matchers.notNullValue());
    names = new HashSet<>();
    for (Terms.Bucket city : cities.getBuckets()) {
        names.add(city.getKeyAsString());
    }
    assertThat(names.contains("tel-aviv"), is(true));
    assertThat((String) propertiesKeys[2], equalTo("1000.0-*"));
    assertThat((long) propertiesDocCounts[2], equalTo(1L));
    assertThat((Terms) propertiesCities[2], sameInstance(cities));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.range.Range.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Range(org.elasticsearch.search.aggregations.bucket.range.Range) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) GeoPoint(org.elasticsearch.common.geo.GeoPoint) HashSet(java.util.HashSet)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1451 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)239 Script (org.elasticsearch.script.Script)223 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)208 Matchers.containsString (org.hamcrest.Matchers.containsString)156 ArrayList (java.util.ArrayList)143 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)131 SearchHit (org.elasticsearch.search.SearchHit)112 HashMap (java.util.HashMap)102 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)93 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)78 GeoPoint (org.elasticsearch.common.geo.GeoPoint)70 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)69 SearchHits (org.elasticsearch.search.SearchHits)60 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)60 DateTime (org.joda.time.DateTime)59 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)58 Range (org.elasticsearch.search.aggregations.bucket.range.Range)50