Search in sources :

Example 91 with SearchResponse

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

the class SimpleVersioningIT method testSimpleVersioningWithFlush.

public void testSimpleVersioningWithFlush() throws Exception {
    createIndex("test");
    ensureGreen();
    IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(1L));
    client().admin().indices().prepareFlush().execute().actionGet();
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_2").setVersion(1).execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(2L));
    client().admin().indices().prepareFlush().execute().actionGet();
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareIndex("test", "type", "1").setCreate(true).setSource("field1", "value1_1").execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(1).execute(), VersionConflictEngineException.class);
    client().admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 10; i++) {
        assertThat(client().prepareGet("test", "type", "1").execute().actionGet().getVersion(), equalTo(2L));
    }
    for (int i = 0; i < 10; i++) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setVersion(true).execute().actionGet();
        assertThat(searchResponse.getHits().getAt(0).getVersion(), equalTo(2L));
    }
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 92 with SearchResponse

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

the class SimpleVersioningIT method testInternalVersioning.

public void testInternalVersioning() throws Exception {
    createIndex("test");
    ensureGreen();
    IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(1L));
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_2").setVersion(1).execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(2L));
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareIndex("test", "type", "1").setCreate(true).setSource("field1", "value1_1").execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(1).execute(), VersionConflictEngineException.class);
    client().admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 10; i++) {
        assertThat(client().prepareGet("test", "type", "1").execute().actionGet().getVersion(), equalTo(2L));
    }
    // search with versioning
    for (int i = 0; i < 10; i++) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setVersion(true).execute().actionGet();
        assertThat(searchResponse.getHits().getAt(0).getVersion(), equalTo(2L));
    }
    // search without versioning
    for (int i = 0; i < 10; i++) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).execute().actionGet();
        assertThat(searchResponse.getHits().getAt(0).getVersion(), equalTo(Versions.NOT_FOUND));
    }
    DeleteResponse deleteResponse = client().prepareDelete("test", "type", "1").setVersion(2).execute().actionGet();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(3L));
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(2).execute(), VersionConflictEngineException.class);
    // This is intricate - the object was deleted but a delete transaction was with the right version. We add another one
    // and thus the transaction is increased.
    deleteResponse = client().prepareDelete("test", "type", "1").setVersion(3).execute().actionGet();
    assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(4L));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 93 with SearchResponse

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

the class MoreExpressionTests method testSparseField.

public void testSparseField() throws Exception {
    ElasticsearchAssertions.assertAcked(prepareCreate("test").addMapping("doc", "x", "type=long", "y", "type=long"));
    ensureGreen("test");
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("x", 4), client().prepareIndex("test", "doc", "2").setSource("y", 2));
    SearchResponse rsp = buildRequest("doc['x'] + 1").get();
    ElasticsearchAssertions.assertSearchResponse(rsp);
    SearchHits hits = rsp.getHits();
    assertEquals(2, rsp.getHits().getTotalHits());
    assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(1.0, hits.getAt(1).field("foo").getValue(), 0.0D);
}
Also used : SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 94 with SearchResponse

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

the class MoreExpressionTests method testPipelineAggregationScript.

// test to make sure expressions are allowed to be used for reduce in pipeline aggregations
public void testPipelineAggregationScript() throws Exception {
    createIndex("agg_index");
    ensureGreen("agg_index");
    indexRandom(true, client().prepareIndex("agg_index", "doc", "1").setSource("one", 1.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "2").setSource("one", 2.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "3").setSource("one", 3.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "4").setSource("one", 4.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "5").setSource("one", 5.0, "two", 2.0, "three", 3.0, "four", 4.0));
    SearchResponse response = client().prepareSearch("agg_index").addAggregation(histogram("histogram").field("one").interval(2).subAggregation(sum("twoSum").field("two")).subAggregation(sum("threeSum").field("three")).subAggregation(sum("fourSum").field("four")).subAggregation(bucketScript("totalSum", new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value0 + _value1 + _value2", Collections.emptyMap()), "twoSum", "threeSum", "fourSum"))).execute().actionGet();
    Histogram histogram = response.getAggregations().get("histogram");
    assertThat(histogram, notNullValue());
    assertThat(histogram.getName(), equalTo("histogram"));
    List<Histogram.Bucket> buckets = histogram.getBuckets();
    for (int bucketCount = 0; bucketCount < buckets.size(); ++bucketCount) {
        Histogram.Bucket bucket = buckets.get(bucketCount);
        if (bucket.getDocCount() == 1) {
            SimpleValue seriesArithmetic = bucket.getAggregations().get("totalSum");
            assertThat(seriesArithmetic, notNullValue());
            double seriesArithmeticValue = seriesArithmetic.value();
            assertEquals(9.0, seriesArithmeticValue, 0.001);
        } else if (bucket.getDocCount() == 2) {
            SimpleValue seriesArithmetic = bucket.getAggregations().get("totalSum");
            assertThat(seriesArithmetic, notNullValue());
            double seriesArithmeticValue = seriesArithmetic.value();
            assertEquals(18.0, seriesArithmeticValue, 0.001);
        } else {
            fail("Incorrect number of documents in a bucket in the histogram.");
        }
    }
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) CompiledScript(org.elasticsearch.script.CompiledScript) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue)

Example 95 with SearchResponse

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

the class MoreExpressionTests method testBoolean.

public void testBoolean() throws Exception {
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("vip").field("type", "boolean");
    xContentBuilder.endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").addMapping("type1", xContentBuilder));
    ensureGreen();
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("price", 1.0, "vip", true), client().prepareIndex("test", "doc", "2").setSource("price", 2.0, "vip", false), client().prepareIndex("test", "doc", "3").setSource("price", 2.0, "vip", false));
    // access .value
    SearchResponse rsp = buildRequest("doc['vip'].value").get();
    assertSearchResponse(rsp);
    assertEquals(3, rsp.getHits().getTotalHits());
    assertEquals(1.0D, rsp.getHits().getAt(0).field("foo").getValue(), 1.0D);
    assertEquals(0.0D, rsp.getHits().getAt(1).field("foo").getValue(), 1.0D);
    assertEquals(0.0D, rsp.getHits().getAt(2).field("foo").getValue(), 1.0D);
    // access .empty
    rsp = buildRequest("doc['vip'].empty ? 1 : 0").get();
    assertSearchResponse(rsp);
    assertEquals(3, rsp.getHits().getTotalHits());
    assertEquals(0.0D, rsp.getHits().getAt(0).field("foo").getValue(), 1.0D);
    assertEquals(0.0D, rsp.getHits().getAt(1).field("foo").getValue(), 1.0D);
    assertEquals(1.0D, rsp.getHits().getAt(2).field("foo").getValue(), 1.0D);
    // ternary operator
    // vip's have a 50% discount
    rsp = buildRequest("doc['vip'] ? doc['price']/2 : doc['price']").get();
    assertSearchResponse(rsp);
    assertEquals(3, rsp.getHits().getTotalHits());
    assertEquals(0.5D, rsp.getHits().getAt(0).field("foo").getValue(), 1.0D);
    assertEquals(2.0D, rsp.getHits().getAt(1).field("foo").getValue(), 1.0D);
    assertEquals(2.0D, rsp.getHits().getAt(2).field("foo").getValue(), 1.0D);
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1457 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)145 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)131 SearchHit (org.elasticsearch.search.SearchHit)114 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)61 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