Search in sources :

Example 66 with SearchRequestBuilder

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

the class SuggestSearchIT method searchSuggest.

protected Suggest searchSuggest(String suggestText, int expectShardsFailed, Map<String, SuggestionBuilder<?>> suggestions) {
    SearchRequestBuilder builder = client().prepareSearch().setSize(0);
    SuggestBuilder suggestBuilder = new SuggestBuilder();
    if (suggestText != null) {
        suggestBuilder.setGlobalText(suggestText);
    }
    for (Entry<String, SuggestionBuilder<?>> suggestion : suggestions.entrySet()) {
        suggestBuilder.addSuggestion(suggestion.getKey(), suggestion.getValue());
    }
    builder.suggest(suggestBuilder);
    SearchResponse actionGet = builder.execute().actionGet();
    assertThat(Arrays.toString(actionGet.getShardFailures()), actionGet.getFailedShards(), equalTo(expectShardsFailed));
    return actionGet.getSuggest();
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) TermSuggestionBuilder(org.elasticsearch.search.suggest.term.TermSuggestionBuilder) PhraseSuggestionBuilder(org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 67 with SearchRequestBuilder

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

the class SuggestSearchIT method testUnmappedField.

public void testUnmappedField() throws IOException, InterruptedException, ExecutionException {
    CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.analysis.analyzer.biword.tokenizer", "standard").putArray("index.analysis.analyzer.biword.filter", "shingler", "lowercase").put("index.analysis.filter.shingler.type", "shingle").put("index.analysis.filter.shingler.min_shingle_size", 2).put("index.analysis.filter.shingler.max_shingle_size", 3));
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("name").field("type", "text").startObject("fields").startObject("shingled").field("type", "text").field("analyzer", "biword").field("search_analyzer", "standard").endObject().endObject().endObject().endObject().endObject().endObject();
    assertAcked(builder.addMapping("type1", mapping));
    ensureGreen();
    indexRandom(true, client().prepareIndex("test", "type1").setSource("name", "I like iced tea"), client().prepareIndex("test", "type1").setSource("name", "I like tea."), client().prepareIndex("test", "type1").setSource("name", "I like ice cream."));
    refresh();
    PhraseSuggestionBuilder phraseSuggestion = phraseSuggestion("name.shingled").addCandidateGenerator(candidateGenerator("name").prefixLength(0).minWordLength(0).suggestMode("always").maxEdits(2)).gramSize(3);
    Suggest searchSuggest = searchSuggest("ice tea", "did_you_mean", phraseSuggestion);
    assertSuggestion(searchSuggest, 0, 0, "did_you_mean", "iced tea");
    phraseSuggestion = phraseSuggestion("nosuchField").addCandidateGenerator(candidateGenerator("name").prefixLength(0).minWordLength(0).suggestMode("always").maxEdits(2)).gramSize(3);
    {
        SearchRequestBuilder searchBuilder = client().prepareSearch().setSize(0);
        searchBuilder.suggest(new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion));
        assertThrows(searchBuilder, SearchPhaseExecutionException.class);
    }
    {
        SearchRequestBuilder searchBuilder = client().prepareSearch().setSize(0);
        searchBuilder.suggest(new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion));
        assertThrows(searchBuilder, SearchPhaseExecutionException.class);
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) PhraseSuggestionBuilder(org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 68 with SearchRequestBuilder

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

the class IndicesOptionsIntegrationIT method verify.

private static void verify(ActionRequestBuilder requestBuilder, boolean fail, long expectedCount) {
    if (fail) {
        if (requestBuilder instanceof MultiSearchRequestBuilder) {
            MultiSearchResponse multiSearchResponse = ((MultiSearchRequestBuilder) requestBuilder).get();
            assertThat(multiSearchResponse.getResponses().length, equalTo(1));
            assertThat(multiSearchResponse.getResponses()[0].isFailure(), is(true));
            assertThat(multiSearchResponse.getResponses()[0].getResponse(), nullValue());
        } else {
            try {
                requestBuilder.get();
                fail("IndexNotFoundException or IndexClosedException was expected");
            } catch (IndexNotFoundException | IndexClosedException e) {
            }
        }
    } else {
        if (requestBuilder instanceof SearchRequestBuilder) {
            SearchRequestBuilder searchRequestBuilder = (SearchRequestBuilder) requestBuilder;
            assertHitCount(searchRequestBuilder.get(), expectedCount);
        } else if (requestBuilder instanceof MultiSearchRequestBuilder) {
            MultiSearchResponse multiSearchResponse = ((MultiSearchRequestBuilder) requestBuilder).get();
            assertThat(multiSearchResponse.getResponses().length, equalTo(1));
            assertThat(multiSearchResponse.getResponses()[0].getResponse(), notNullValue());
        } else {
            requestBuilder.get();
        }
    }
}
Also used : MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) MultiSearchRequestBuilder(org.elasticsearch.action.search.MultiSearchRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) MultiSearchRequestBuilder(org.elasticsearch.action.search.MultiSearchRequestBuilder)

Example 69 with SearchRequestBuilder

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

the class GeoDistanceIT method testGeoDistanceAggregation.

public void testGeoDistanceAggregation() throws IOException {
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("name", "TestPosition").startObject("location").field("lat", src_lat).field("lon", src_lon).endObject().endObject()).get();
    refresh();
    SearchRequestBuilder search = client().prepareSearch("test");
    String name = "TestPosition";
    search.setQuery(QueryBuilders.matchAllQuery()).setTypes("type1").addAggregation(AggregationBuilders.geoDistance(name, new GeoPoint(tgt_lat, tgt_lon)).field("location").unit(DistanceUnit.MILES).addRange(0, 25000));
    // no hits please
    search.setSize(0);
    SearchResponse response = search.get();
    Aggregations aggregations = response.getAggregations();
    assertNotNull(aggregations);
    InternalGeoDistance geoDistance = aggregations.get(name);
    assertNotNull(geoDistance);
    List<? extends Range.Bucket> buckets = ((Range) geoDistance).getBuckets();
    assertNotNull("Buckets should not be null", buckets);
    assertEquals("Unexpected number of buckets", 1, buckets.size());
    assertEquals("Unexpected doc count for geo distance", 1, buckets.get(0).getDocCount());
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Aggregations(org.elasticsearch.search.aggregations.Aggregations) InternalGeoDistance(org.elasticsearch.search.aggregations.bucket.range.geodistance.InternalGeoDistance) Range(org.elasticsearch.search.aggregations.bucket.range.Range) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 70 with SearchRequestBuilder

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

the class QueryRescorerIT method testFromSize.

// #11277
public void testFromSize() throws Exception {
    Builder settings = Settings.builder();
    settings.put(SETTING_NUMBER_OF_SHARDS, 1);
    settings.put(SETTING_NUMBER_OF_REPLICAS, 0);
    assertAcked(prepareCreate("test").setSettings(settings));
    for (int i = 0; i < 5; i++) {
        client().prepareIndex("test", "type", "" + i).setSource("text", "hello world").get();
    }
    refresh();
    SearchRequestBuilder request = client().prepareSearch();
    request.setQuery(QueryBuilders.termQuery("text", "hello"));
    request.setFrom(1);
    request.setSize(4);
    request.addRescorer(RescoreBuilder.queryRescorer(QueryBuilders.matchAllQuery()), 50);
    assertEquals(4, request.get().getHits().getHits().length);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) RescoreBuilder(org.elasticsearch.search.rescore.RescoreBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) QueryRescorerBuilder(org.elasticsearch.search.rescore.QueryRescorerBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder)

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