Search in sources :

Example 46 with SearchResponse

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

the class SimpleQueryStringIT method testSimpleQueryStringUsesFieldAnalyzer.

public void testSimpleQueryStringUsesFieldAnalyzer() throws Exception {
    client().prepareIndex("test", "type1", "1").setSource("foo", 123, "bar", "abc").get();
    client().prepareIndex("test", "type1", "2").setSource("foo", 234, "bar", "bcd").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("123").field("foo").field("bar")).get();
    assertHitCount(searchResponse, 1L);
    assertSearchHits(searchResponse, "1");
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 47 with SearchResponse

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

the class FieldSortIT method testIssue2986.

public void testIssue2986() {
    assertAcked(client().admin().indices().prepareCreate("test").addMapping("type", "field1", "type=keyword").get());
    client().prepareIndex("test", "post", "1").setSource("{\"field1\":\"value1\"}", XContentType.JSON).execute().actionGet();
    client().prepareIndex("test", "post", "2").setSource("{\"field1\":\"value2\"}", XContentType.JSON).execute().actionGet();
    client().prepareIndex("test", "post", "3").setSource("{\"field1\":\"value3\"}", XContentType.JSON).execute().actionGet();
    refresh();
    SearchResponse result = client().prepareSearch("test").setQuery(matchAllQuery()).setTrackScores(true).addSort("field1", SortOrder.ASC).execute().actionGet();
    for (SearchHit hit : result.getHits()) {
        assertFalse(Float.isNaN(hit.getScore()));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 48 with SearchResponse

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

the class FieldSortIT method testIssue2991.

public void testIssue2991() {
    for (int i = 1; i < 4; i++) {
        try {
            client().admin().indices().prepareDelete("test").execute().actionGet();
        } catch (Exception e) {
        // ignore
        }
        assertAcked(client().admin().indices().prepareCreate("test").addMapping("type", "tag", "type=keyword").get());
        ensureGreen();
        client().prepareIndex("test", "type", "1").setSource("tag", "alpha").execute().actionGet();
        refresh();
        client().prepareIndex("test", "type", "3").setSource("tag", "gamma").execute().actionGet();
        refresh();
        client().prepareIndex("test", "type", "4").setSource("tag", "delta").execute().actionGet();
        refresh();
        client().prepareIndex("test", "type", "2").setSource("tag", "beta").execute().actionGet();
        refresh();
        SearchResponse resp = client().prepareSearch("test").setSize(2).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("tag").order(SortOrder.ASC)).execute().actionGet();
        assertHitCount(resp, 4);
        assertThat(resp.getHits().getHits().length, equalTo(2));
        assertFirstHit(resp, hasId("1"));
        assertSecondHit(resp, hasId("2"));
        resp = client().prepareSearch("test").setSize(2).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("tag").order(SortOrder.DESC)).execute().actionGet();
        assertHitCount(resp, 4);
        assertThat(resp.getHits().getHits().length, equalTo(2));
        assertFirstHit(resp, hasId("3"));
        assertSecondHit(resp, hasId("4"));
    }
}
Also used : SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 49 with SearchResponse

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

the class FieldSortIT method testSimpleSorts.

public void testSimpleSorts() throws Exception {
    Random random = random();
    assertAcked(prepareCreate("test").addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("str_value").field("type", "keyword").endObject().startObject("boolean_value").field("type", "boolean").endObject().startObject("byte_value").field("type", "byte").endObject().startObject("short_value").field("type", "short").endObject().startObject("integer_value").field("type", "integer").endObject().startObject("long_value").field("type", "long").endObject().startObject("float_value").field("type", "float").endObject().startObject("double_value").field("type", "double").endObject().endObject().endObject().endObject()));
    ensureGreen();
    List<IndexRequestBuilder> builders = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        IndexRequestBuilder builder = client().prepareIndex("test", "type1", Integer.toString(i)).setSource(jsonBuilder().startObject().field("str_value", new String(new char[] { (char) (97 + i), (char) (97 + i) })).field("boolean_value", true).field("byte_value", i).field("short_value", i).field("integer_value", i).field("long_value", i).field("float_value", 0.1 * i).field("double_value", 0.1 * i).endObject());
        builders.add(builder);
    }
    Collections.shuffle(builders, random);
    for (IndexRequestBuilder builder : builders) {
        builder.execute().actionGet();
        if (random.nextBoolean()) {
            if (random.nextInt(5) != 0) {
                refresh();
            } else {
                client().admin().indices().prepareFlush().execute().actionGet();
            }
        }
    }
    refresh();
    // STRING
    int size = 1 + random.nextInt(10);
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("str_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(searchResponse.getHits().getAt(i).getSortValues()[0].toString(), equalTo(new String(new char[] { (char) (97 + i), (char) (97 + i) })));
    }
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("str_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(searchResponse.getHits().getAt(i).getSortValues()[0].toString(), equalTo(new String(new char[] { (char) (97 + (9 - i)), (char) (97 + (9 - i)) })));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // BYTE
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("byte_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).byteValue(), equalTo((byte) i));
    }
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("byte_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).byteValue(), equalTo((byte) (9 - i)));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // SHORT
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("short_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).shortValue(), equalTo((short) i));
    }
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("short_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).shortValue(), equalTo((short) (9 - i)));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // INTEGER
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("integer_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).intValue(), equalTo(i));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("integer_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).intValue(), equalTo((9 - i)));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // LONG
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("long_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).longValue(), equalTo((long) i));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("long_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10L);
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).longValue(), equalTo((long) (9 - i)));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // FLOAT
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("float_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue(), closeTo(0.1d * i, 0.000001d));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("float_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue(), closeTo(0.1d * (9 - i), 0.000001d));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // DOUBLE
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("double_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue(), closeTo(0.1d * i, 0.000001d));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("double_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue(), closeTo(0.1d * (9 - i), 0.000001d));
    }
    assertNoFailures(searchResponse);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Random(java.util.Random) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 50 with SearchResponse

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

the class SimpleQueryStringIT method testEmptySimpleQueryStringWithAnalysis.

public void testEmptySimpleQueryStringWithAnalysis() throws Exception {
    // https://github.com/elastic/elasticsearch/issues/18202
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("body").field("type", "text").field("analyzer", "stop").endObject().endObject().endObject().endObject().string();
    CreateIndexRequestBuilder mappingRequest = client().admin().indices().prepareCreate("test1").addMapping("type1", mapping, XContentType.JSON);
    mappingRequest.execute().actionGet();
    indexRandom(true, client().prepareIndex("test1", "type1", "1").setSource("body", "Some Text"));
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("the*").field("body")).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 0L);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

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