Search in sources :

Example 1 with SearchResponse

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

the class ArrayMapperTest method testEmptyArray.

@Test
public void testEmptyArray() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER).field("type", "double").field("index", "not_analyzed").endObject().endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper = mapper(INDEX, TYPE, mapping);
    // parse source with empty array
    ParsedDocument doc = mapper.parse(INDEX, TYPE, "abc", XContentFactory.jsonBuilder().startObject().array("array_field").endObject().bytes());
    assertThat(doc.dynamicMappingsUpdate() == null, is(true));
    assertThat(doc.docs().size(), is(1));
    // no lucene field generated
    assertThat(doc.docs().get(0).get("array_field"), is(nullValue()));
    // insert
    IndexResponse response = client().prepareIndex(INDEX, "type", "123").setSource("{array_field:[]}").execute().actionGet();
    assertThat(response.getVersion(), is(1L));
    client().admin().indices().prepareRefresh(INDEX).execute().actionGet();
    SearchResponse searchResponse = client().prepareSearch(INDEX).setTypes("type").setFetchSource(true).addField("array_field").setQuery("{\"term\": {\"_id\": \"123\"}}").execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), is(1L));
    assertThat(Joiner.on(',').withKeyValueSeparator(":").join(searchResponse.getHits().getAt(0).getSource()), is("array_field:[]"));
    assertThat(searchResponse.getHits().getAt(0).fields().containsKey("array_field"), is(false));
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest) Test(org.junit.Test)

Example 2 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse in project rssriver by dadoonet.

the class RssRiverAllParametersTest method test_mcapp_rivers.

/**
     * http://www.malwaredomains.com/wordpress/?feed=rss
     * http://www.darkreading.com/rss/all.xml
     */
@Test
public void test_mcapp_rivers() throws IOException, InterruptedException {
    startRiver("mcapp", getLastUpdatedId("malwaredomains"), createRiver(true, "malwaredomains", "darkreading"));
    // We wait for some documents
    existSomeDocs("mcapp", "malwaredomains");
    existSomeDocs("mcapp", "darkreading");
    SearchResponse response = client().prepareSearch("mcapp").setQuery(QueryBuilders.matchQuery("description", "domains")).addField(RssToJson.Rss.RAW + ".html").execute().actionGet();
    assertThat(response.getHits().getTotalHits(), greaterThan(0L));
    assertThat(response.getHits().getAt(0).field(RssToJson.Rss.RAW + ".html"), notNullValue());
    assertThat(response.getHits().getAt(0).field(RssToJson.Rss.RAW + ".html").getValues(), notNullValue());
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) ElasticsearchIntegrationTest(org.elasticsearch.test.ElasticsearchIntegrationTest)

Example 3 with SearchResponse

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

the class CombiIT method testSubAggregationForTopAggregationOnUnmappedField.

/**
     * Some top aggs (eg. date_/histogram) that are executed on unmapped fields, will generate an estimate count of buckets - zero.
     * when the sub aggregator is then created, it will take this estimation into account. This used to cause
     * and an ArrayIndexOutOfBoundsException...
     */
public void testSubAggregationForTopAggregationOnUnmappedField() throws Exception {
    prepareCreate("idx").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("name").field("type", "keyword").endObject().startObject("value").field("type", "integer").endObject().endObject().endObject().endObject()).execute().actionGet();
    ensureSearchable("idx");
    SubAggCollectionMode aggCollectionMode = randomFrom(SubAggCollectionMode.values());
    SearchResponse searchResponse = client().prepareSearch("idx").addAggregation(histogram("values").field("value1").interval(1).subAggregation(terms("names").field("name").collectMode(aggCollectionMode))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), Matchers.equalTo(0L));
    Histogram values = searchResponse.getAggregations().get("values");
    assertThat(values, notNullValue());
    assertThat(values.getBuckets().isEmpty(), is(true));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) SubAggCollectionMode(org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with SearchResponse

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

the class CombiIT method testMultipleAggsOnSameField_WithDifferentRequiredValueSourceType.

/**
     * Making sure that if there are multiple aggregations, working on the same field, yet require different
     * value source type, they can all still work. It used to fail as we used to cache the ValueSource by the
     * field name. If the cached value source was of type "bytes" and another aggregation on the field required to see
     * it as "numeric", it didn't work. Now we cache the Value Sources by a custom key (field name + ValueSource type)
     * so there's no conflict there.
     */
public void testMultipleAggsOnSameField_WithDifferentRequiredValueSourceType() throws Exception {
    createIndex("idx");
    IndexRequestBuilder[] builders = new IndexRequestBuilder[randomInt(30)];
    IntIntMap values = new IntIntHashMap();
    long missingValues = 0;
    for (int i = 0; i < builders.length; i++) {
        String name = "name_" + randomIntBetween(1, 10);
        if (rarely()) {
            missingValues++;
            builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("name", name).endObject());
        } else {
            int value = randomIntBetween(1, 10);
            values.put(value, values.getOrDefault(value, 0) + 1);
            builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("name", name).field("value", value).endObject());
        }
    }
    indexRandom(true, builders);
    ensureSearchable();
    SubAggCollectionMode aggCollectionMode = randomFrom(SubAggCollectionMode.values());
    SearchResponse response = client().prepareSearch("idx").addAggregation(missing("missing_values").field("value")).addAggregation(terms("values").field("value").collectMode(aggCollectionMode)).execute().actionGet();
    assertSearchResponse(response);
    Aggregations aggs = response.getAggregations();
    Missing missing = aggs.get("missing_values");
    assertNotNull(missing);
    assertThat(missing.getDocCount(), equalTo(missingValues));
    Terms terms = aggs.get("values");
    assertNotNull(terms);
    Collection<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets.size(), equalTo(values.size()));
    for (Terms.Bucket bucket : buckets) {
        values.remove(((Number) bucket.getKey()).intValue());
    }
    assertTrue(values.isEmpty());
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) IntIntMap(com.carrotsearch.hppc.IntIntMap) IntIntHashMap(com.carrotsearch.hppc.IntIntHashMap) Missing(org.elasticsearch.search.aggregations.bucket.missing.Missing) SubAggCollectionMode(org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode)

Example 5 with SearchResponse

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

the class SearchServiceTests method testClearIndexDelete.

public void testClearIndexDelete() throws ExecutionException, InterruptedException {
    createIndex("index");
    client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).get();
    SearchResponse searchResponse = client().prepareSearch("index").setSize(1).setScroll("1m").get();
    assertThat(searchResponse.getScrollId(), is(notNullValue()));
    SearchService service = getInstanceFromNode(SearchService.class);
    assertEquals(1, service.getActiveContexts());
    assertAcked(client().admin().indices().prepareDelete("index"));
    assertEquals(0, service.getActiveContexts());
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

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