Search in sources :

Example 61 with SearchRequestBuilder

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

the class SearchScrollIT method testDeepScrollingDoesNotBlowUp.

/**
     * Tests that we use an optimization shrinking the batch to the size of the shard. Thus the Integer.MAX_VALUE window doesn't OOM us.
     */
public void testDeepScrollingDoesNotBlowUp() throws Exception {
    client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).execute().get();
    /*
         * Disable the max result window setting for this test because it'll reject the search's unreasonable batch size. We want
         * unreasonable batch sizes to just OOM.
         */
    client().admin().indices().prepareUpdateSettings("index").setSettings(Settings.builder().put(IndexSettings.MAX_RESULT_WINDOW_SETTING.getKey(), Integer.MAX_VALUE)).get();
    for (SearchType searchType : SearchType.values()) {
        SearchRequestBuilder builder = client().prepareSearch("index").setSearchType(searchType).setQuery(QueryBuilders.matchAllQuery()).setSize(Integer.MAX_VALUE).setScroll("1m");
        SearchResponse response = builder.execute().actionGet();
        try {
            ElasticsearchAssertions.assertHitCount(response, 1L);
        } finally {
            String scrollId = response.getScrollId();
            if (scrollId != null) {
                clearScroll(scrollId);
            }
        }
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchType(org.elasticsearch.action.search.SearchType) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 62 with SearchRequestBuilder

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

the class SearchAfterIT method assertSearchFromWithSortValues.

private void assertSearchFromWithSortValues(String indexName, String typeName, List<List> documents, int reqSize) throws Exception {
    int numFields = documents.get(0).size();
    {
        createIndexMappingsFromObjectType(indexName, typeName, documents.get(0));
        List<IndexRequestBuilder> requests = new ArrayList<>();
        for (int i = 0; i < documents.size(); i++) {
            XContentBuilder builder = jsonBuilder();
            assertThat(documents.get(i).size(), Matchers.equalTo(numFields));
            builder.startObject();
            for (int j = 0; j < numFields; j++) {
                builder.field("field" + Integer.toString(j), documents.get(i).get(j));
            }
            builder.endObject();
            requests.add(client().prepareIndex(INDEX_NAME, TYPE_NAME, Integer.toString(i)).setSource(builder));
        }
        indexRandom(true, requests);
    }
    Collections.sort(documents, LST_COMPARATOR);
    int offset = 0;
    Object[] sortValues = null;
    while (offset < documents.size()) {
        SearchRequestBuilder req = client().prepareSearch(indexName);
        for (int i = 0; i < documents.get(0).size(); i++) {
            req.addSort("field" + Integer.toString(i), SortOrder.ASC);
        }
        req.setQuery(matchAllQuery()).setSize(reqSize);
        if (sortValues != null) {
            req.searchAfter(sortValues);
        }
        SearchResponse searchResponse = req.get();
        for (SearchHit hit : searchResponse.getHits()) {
            List toCompare = convertSortValues(documents.get(offset++));
            assertThat(LST_COMPARATOR.compare(toCompare, Arrays.asList(hit.getSortValues())), equalTo(0));
        }
        sortValues = searchResponse.getHits().getHits()[searchResponse.getHits().getHits().length - 1].getSortValues();
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) List(java.util.List) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 63 with SearchRequestBuilder

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

the class MoreExpressionTests method testStringSpecialValueVariable.

public void testStringSpecialValueVariable() throws Exception {
    // i.e. expression script for term aggregations, which is not allowed
    assertAcked(client().admin().indices().prepareCreate("test").addMapping("doc", "text", "type=keyword").get());
    ensureGreen("test");
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("text", "hello"), client().prepareIndex("test", "doc", "2").setSource("text", "goodbye"), client().prepareIndex("test", "doc", "3").setSource("text", "hello"));
    SearchRequestBuilder req = client().prepareSearch().setIndices("test");
    req.setQuery(QueryBuilders.matchAllQuery()).addAggregation(AggregationBuilders.terms("term_agg").field("text").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value", Collections.emptyMap())));
    String message;
    try {
        // shards that don't have docs with the "text" field will not fail,
        // so we may or may not get a total failure
        SearchResponse rsp = req.get();
        // at least the shards containing the docs should have failed
        assertThat(rsp.getShardFailures().length, greaterThan(0));
        message = rsp.getShardFailures()[0].reason();
    } catch (SearchPhaseExecutionException e) {
        message = e.toString();
    }
    assertThat(message + "should have contained ScriptException", message.contains("ScriptException"), equalTo(true));
    assertThat(message + "should have contained text variable error", message.contains("text variable"), equalTo(true));
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) CompiledScript(org.elasticsearch.script.CompiledScript) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 64 with SearchRequestBuilder

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

the class CustomSuggesterSearchIT method testThatCustomSuggestersCanBeRegisteredAndWork.

public void testThatCustomSuggestersCanBeRegisteredAndWork() throws Exception {
    createIndex("test");
    client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject().field("name", "arbitrary content").endObject()).setRefreshPolicy(IMMEDIATE).get();
    String randomText = randomAsciiOfLength(10);
    String randomField = randomAsciiOfLength(10);
    String randomSuffix = randomAsciiOfLength(10);
    SuggestBuilder suggestBuilder = new SuggestBuilder();
    suggestBuilder.addSuggestion("someName", new CustomSuggestionBuilder(randomField, randomSuffix).text(randomText));
    SearchRequestBuilder searchRequestBuilder = client().prepareSearch("test").setTypes("test").setFrom(0).setSize(1).suggest(suggestBuilder);
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    // TODO: infer type once JI-9019884 is fixed
    // TODO: see also JDK-8039214
    List<Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> suggestions = CollectionUtils.<Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>iterableAsArrayList(searchResponse.getSuggest().getSuggestion("someName"));
    assertThat(suggestions, hasSize(2));
    assertThat(suggestions.get(0).getText().string(), is(String.format(Locale.ROOT, "%s-%s-%s-12", randomText, randomField, randomSuffix)));
    assertThat(suggestions.get(1).getText().string(), is(String.format(Locale.ROOT, "%s-%s-%s-123", randomText, randomField, randomSuffix)));
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 65 with SearchRequestBuilder

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

the class SuggestSearchIT method testShardFailures.

// see #3469
public void testShardFailures() throws IOException, InterruptedException {
    CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.analysis.analyzer.suggest.tokenizer", "standard").putArray("index.analysis.analyzer.suggest.filter", "standard", "lowercase", "shingler").put("index.analysis.filter.shingler.type", "shingle").put("index.analysis.filter.shingler.min_shingle_size", 2).put("index.analysis.filter.shingler.max_shingle_size", 5).put("index.analysis.filter.shingler.output_unigrams", true));
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type2").startObject("properties").startObject("name").field("type", "text").field("analyzer", "suggest").endObject().endObject().endObject().endObject();
    assertAcked(builder.addMapping("type2", mapping));
    ensureGreen();
    index("test", "type2", "1", "foo", "bar");
    index("test", "type2", "2", "foo", "bar");
    index("test", "type2", "3", "foo", "bar");
    index("test", "type2", "4", "foo", "bar");
    index("test", "type2", "5", "foo", "bar");
    index("test", "type2", "1", "name", "Just testing the suggestions api");
    index("test", "type2", "2", "name", "An other title about equal length");
    // Note that the last document has to have about the same length as the other or cutoff rechecking will remove the useful suggestion
    refresh();
    // When searching on a shard with a non existing mapping, we should fail
    SearchRequestBuilder request = client().prepareSearch().setSize(0).suggest(new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion("fielddoesnotexist").maxErrors(5.0f)));
    assertThrows(request, SearchPhaseExecutionException.class);
    // When searching on a shard which does not hold yet any document of an existing type, we should not fail
    SearchResponse searchResponse = client().prepareSearch().setSize(0).suggest(new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion("name").maxErrors(5.0f))).get();
    ElasticsearchAssertions.assertNoFailures(searchResponse);
    ElasticsearchAssertions.assertSuggestion(searchResponse.getSuggest(), 0, 0, "did_you_mean", "testing suggestions");
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)114 SearchResponse (org.elasticsearch.action.search.SearchResponse)66 SearchHit (org.elasticsearch.search.SearchHit)25 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)19 SearchHits (org.elasticsearch.search.SearchHits)19 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)13 Client (org.elasticsearch.client.Client)13 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)12 Test (org.junit.Test)12 SearchRequest (org.elasticsearch.action.search.SearchRequest)11 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)10 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)10 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)9 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)9 Map (java.util.Map)8 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)8 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)8 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)8 List (java.util.List)7