Search in sources :

Example 86 with SearchResponse

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

the class CompletionSuggestSearchIT method testTextAndGlobalText.

/**
     * test that suggestion works if prefix is either provided via {@link CompletionSuggestionBuilder#text(String)} or
     * {@link SuggestBuilder#setGlobalText(String)}
     */
public void testTextAndGlobalText() throws Exception {
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 1; i <= numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i).endObject().endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder noText = SuggestBuilders.completionSuggestion(FIELD);
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", noText).setGlobalText("sugg")).execute().actionGet();
    assertSuggestions(searchResponse, "foo", "suggestion10", "suggestion9", "suggestion8", "suggestion7", "suggestion6");
    CompletionSuggestionBuilder withText = SuggestBuilders.completionSuggestion(FIELD).text("sugg");
    searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", withText)).execute().actionGet();
    assertSuggestions(searchResponse, "foo", "suggestion10", "suggestion9", "suggestion8", "suggestion7", "suggestion6");
    // test that suggestion text takes precedence over global text
    searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", withText).setGlobalText("bogus")).execute().actionGet();
    assertSuggestions(searchResponse, "foo", "suggestion10", "suggestion9", "suggestion8", "suggestion7", "suggestion6");
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) ArrayList(java.util.ArrayList) CollectionUtils.iterableAsArrayList(org.elasticsearch.common.util.CollectionUtils.iterableAsArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 87 with SearchResponse

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

the class CompletionSuggestSearchIT method testSuggestDocumentSourceFiltering.

public void testSuggestDocumentSourceFiltering() throws Exception {
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
    createIndexAndMapping(mapping);
    int numDocs = randomIntBetween(10, 100);
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 1; i <= numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i).endObject().field("a", "include").field("b", "exclude").endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").size(numDocs);
    SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", prefix)).setFetchSource("a", "b").get();
    CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("foo");
    CompletionSuggestion.Entry options = completionSuggestion.getEntries().get(0);
    assertThat(options.getOptions().size(), equalTo(numDocs));
    int id = numDocs;
    for (CompletionSuggestion.Entry.Option option : options) {
        assertThat(option.getText().toString(), equalTo("suggestion" + id));
        assertSearchHit(option.getHit(), hasId("" + id));
        assertSearchHit(option.getHit(), hasScore((id)));
        assertNotNull(option.getHit().getSourceAsMap());
        Set<String> sourceFields = option.getHit().getSourceAsMap().keySet();
        assertThat(sourceFields, contains("a"));
        assertThat(sourceFields, not(contains("b")));
        id--;
    }
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) ArrayList(java.util.ArrayList) CollectionUtils.iterableAsArrayList(org.elasticsearch.common.util.CollectionUtils.iterableAsArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)

Example 88 with SearchResponse

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

the class SimilarityIT method testCustomBM25Similarity.

public void testCustomBM25Similarity() throws Exception {
    try {
        client().admin().indices().prepareDelete("test").execute().actionGet();
    } catch (Exception e) {
    // ignore
    }
    client().admin().indices().prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("similarity", "custom").field("type", "text").endObject().startObject("field2").field("similarity", "classic").field("type", "text").endObject().endObject().endObject().endObject()).setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).put("similarity.custom.type", "BM25").put("similarity.custom.k1", 2.0f).put("similarity.custom.b", 0.5f)).execute().actionGet();
    client().prepareIndex("test", "type1", "1").setSource("field1", "the quick brown fox jumped over the lazy dog", "field2", "the quick brown fox jumped over the lazy dog").setRefreshPolicy(IMMEDIATE).execute().actionGet();
    SearchResponse bm25SearchResponse = client().prepareSearch().setQuery(matchQuery("field1", "quick brown fox")).execute().actionGet();
    assertThat(bm25SearchResponse.getHits().getTotalHits(), equalTo(1L));
    float bm25Score = bm25SearchResponse.getHits().getHits()[0].getScore();
    SearchResponse defaultSearchResponse = client().prepareSearch().setQuery(matchQuery("field2", "quick brown fox")).execute().actionGet();
    assertThat(defaultSearchResponse.getHits().getTotalHits(), equalTo(1L));
    float defaultScore = defaultSearchResponse.getHits().getHits()[0].getScore();
    assertThat(bm25Score, not(equalTo(defaultScore)));
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 89 with SearchResponse

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

the class SharedSignificantTermsTestMethods method checkSignificantTermsAggregationCorrect.

private static void checkSignificantTermsAggregationCorrect(ESIntegTestCase testCase) {
    SearchResponse response = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD))).execute().actionGet();
    assertSearchResponse(response);
    StringTerms classes = response.getAggregations().get("class");
    Assert.assertThat(classes.getBuckets().size(), equalTo(2));
    for (Terms.Bucket classBucket : classes.getBuckets()) {
        Map<String, Aggregation> aggs = classBucket.getAggregations().asMap();
        Assert.assertTrue(aggs.containsKey("sig_terms"));
        SignificantTerms agg = (SignificantTerms) aggs.get("sig_terms");
        Assert.assertThat(agg.getBuckets().size(), equalTo(1));
        SignificantTerms.Bucket sigBucket = agg.iterator().next();
        String term = sigBucket.getKeyAsString();
        String classTerm = classBucket.getKeyAsString();
        Assert.assertTrue(term.equals(classTerm));
    }
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) AggregationBuilders.significantTerms(org.elasticsearch.search.aggregations.AggregationBuilders.significantTerms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 90 with SearchResponse

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

the class SharedClusterSnapshotRestoreIT method testDataFileFailureDuringRestore.

public void testDataFileFailureDuringRestore() throws Exception {
    Path repositoryLocation = randomRepoPath();
    Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", repositoryLocation)));
    prepareCreate("test-idx").setSettings(Settings.builder().put("index.allocation.max_retries", Integer.MAX_VALUE)).get();
    ensureGreen();
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
    }
    refresh();
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().state(), equalTo(SnapshotState.SUCCESS));
    assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(createSnapshotResponse.getSnapshotInfo().successfulShards()));
    logger.info("-->  update repository with mock version");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("mock").setSettings(Settings.builder().put("location", repositoryLocation).put("random", randomAsciiOfLength(10)).put("random_data_file_io_exception_rate", 0.3)));
    // Test restore after index deletion
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> restore index after deletion");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    SearchResponse countResponse = client.prepareSearch("test-idx").setSize(0).get();
    assertThat(countResponse.getHits().getTotalHits(), equalTo(100L));
    logger.info("--> total number of simulated failures during restore: [{}]", getFailureCount("test-repo"));
}
Also used : Path(java.nio.file.Path) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.elasticsearch.client.Client) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) 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