Search in sources :

Example 81 with SearchResponse

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

the class SearchStatsIT method testOpenContexts.

public void testOpenContexts() {
    String index = "test1";
    createIndex(index);
    ensureGreen(index);
    // create shards * docs number of docs and attempt to distribute them equally
    // this distribution will not be perfect; each shard will have an integer multiple of docs (possibly zero)
    // we do this so we have a lot of pages to scroll through
    final int docs = scaledRandomIntBetween(20, 50);
    for (int s = 0; s < numAssignedShards(index); s++) {
        for (int i = 0; i < docs; i++) {
            client().prepareIndex(index, "type", Integer.toString(s * docs + i)).setSource("field", "value").setRouting(Integer.toString(s)).execute().actionGet();
        }
    }
    client().admin().indices().prepareRefresh(index).execute().actionGet();
    IndicesStatsResponse indicesStats = client().admin().indices().prepareStats(index).execute().actionGet();
    assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo(0L));
    int size = scaledRandomIntBetween(1, docs);
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
    assertSearchResponse(searchResponse);
    // refresh the stats now that scroll contexts are opened
    indicesStats = client().admin().indices().prepareStats(index).execute().actionGet();
    assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo((long) numAssignedShards(index)));
    assertThat(indicesStats.getTotal().getSearch().getTotal().getScrollCurrent(), equalTo((long) numAssignedShards(index)));
    int hits = 0;
    while (true) {
        if (searchResponse.getHits().getHits().length == 0) {
            break;
        }
        hits += searchResponse.getHits().getHits().length;
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
    }
    long expected = 0;
    // the number of queries executed is equal to at least the sum of number of pages in shard over all shards
    IndicesStatsResponse r = client().admin().indices().prepareStats(index).execute().actionGet();
    for (int s = 0; s < numAssignedShards(index); s++) {
        expected += (long) Math.ceil(r.getShards()[s].getStats().getDocs().getCount() / size);
    }
    indicesStats = client().admin().indices().prepareStats().execute().actionGet();
    Stats stats = indicesStats.getTotal().getSearch().getTotal();
    assertEquals(hits, docs * numAssignedShards(index));
    assertThat(stats.getQueryCount(), greaterThanOrEqualTo(expected));
    clearScroll(searchResponse.getScrollId());
    indicesStats = client().admin().indices().prepareStats().execute().actionGet();
    stats = indicesStats.getTotal().getSearch().getTotal();
    assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo(0L));
    assertThat(stats.getScrollCount(), equalTo((long) numAssignedShards(index)));
    assertThat(stats.getScrollTimeInMillis(), greaterThan(0L));
}
Also used : IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) Stats(org.elasticsearch.index.search.stats.SearchStats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 82 with SearchResponse

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

the class CompletionSuggestSearchIT method assertSuggestionsNotInOrder.

public void assertSuggestionsNotInOrder(String suggestString, String... suggestions) {
    String suggestionName = RandomStrings.randomAsciiOfLength(random(), 10);
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion(suggestionName, SuggestBuilders.completionSuggestion(FIELD).text(suggestString).size(10))).execute().actionGet();
    assertSuggestions(searchResponse, false, suggestionName, suggestions);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 83 with SearchResponse

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

the class CompletionSuggestSearchIT method testThatFuzzySuggesterSupportsTranspositions.

public void testThatFuzzySuggesterSupportsTranspositions() throws Exception {
    createIndexAndMapping(completionMappingBuilder);
    client().prepareIndex(INDEX, TYPE, "1").setSource(jsonBuilder().startObject().startObject(FIELD).startArray("input").value("Nirvana").endArray().endObject().endObject()).get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("Nriv", FuzzyOptions.builder().setTranspositions(false).build()).size(10))).execute().actionGet();
    assertSuggestions(searchResponse, false, "foo");
    searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("Nriv", Fuzziness.ONE).size(10))).execute().actionGet();
    assertSuggestions(searchResponse, false, "foo", "Nirvana");
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 84 with SearchResponse

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

the class SimpleSortIT method testSimpleSorts.

public void testSimpleSorts() throws Exception {
    Random random = random();
    assertAcked(prepareCreate("test").addMapping("type1", 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++) {
        builders.add(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()));
    }
    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().get();
            }
        }
    }
    refresh();
    // STRING script
    int size = 1 + random.nextInt(10);
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['str_value'].value", Collections.emptyMap());
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort(new ScriptSortBuilder(script, ScriptSortType.STRING)).get();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat(searchHit.getId(), equalTo(Integer.toString(i)));
        String expected = new String(new char[] { (char) (97 + i), (char) (97 + i) });
        assertThat(searchHit.getSortValues()[0].toString(), equalTo(expected));
    }
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("str_value", SortOrder.DESC).get();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat(searchHit.getId(), equalTo(Integer.toString(9 - i)));
        String expected = new String(new char[] { (char) (97 + (9 - i)), (char) (97 + (9 - i)) });
        assertThat(searchHit.getSortValues()[0].toString(), equalTo(expected));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    assertNoFailures(searchResponse);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Script(org.elasticsearch.script.Script) Random(java.util.Random) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 85 with SearchResponse

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

the class SourceFetchingIT method testSourceDefaultBehavior.

public void testSourceDefaultBehavior() {
    createIndex("test");
    ensureGreen();
    index("test", "type1", "1", "field", "value");
    refresh();
    SearchResponse response = client().prepareSearch("test").get();
    assertThat(response.getHits().getAt(0).getSourceAsString(), notNullValue());
    response = client().prepareSearch("test").addStoredField("bla").get();
    assertThat(response.getHits().getAt(0).getSourceAsString(), nullValue());
    response = client().prepareSearch("test").addStoredField("_source").get();
    assertThat(response.getHits().getAt(0).getSourceAsString(), notNullValue());
}
Also used : 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