Search in sources :

Example 86 with SearchResponse

use of org.graylog.shaded.elasticsearch7.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 87 with SearchResponse

use of org.graylog.shaded.elasticsearch7.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 88 with SearchResponse

use of org.graylog.shaded.elasticsearch7.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)

Example 89 with SearchResponse

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

the class SimpleVersioningIT method testSimpleVersioningWithFlush.

public void testSimpleVersioningWithFlush() throws Exception {
    createIndex("test");
    ensureGreen();
    IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(1L));
    client().admin().indices().prepareFlush().execute().actionGet();
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_2").setVersion(1).execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(2L));
    client().admin().indices().prepareFlush().execute().actionGet();
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareIndex("test", "type", "1").setCreate(true).setSource("field1", "value1_1").execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(1).execute(), VersionConflictEngineException.class);
    client().admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 10; i++) {
        assertThat(client().prepareGet("test", "type", "1").execute().actionGet().getVersion(), equalTo(2L));
    }
    for (int i = 0; i < 10; i++) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setVersion(true).execute().actionGet();
        assertThat(searchResponse.getHits().getAt(0).getVersion(), equalTo(2L));
    }
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 90 with SearchResponse

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

the class SimpleVersioningIT method testInternalVersioning.

public void testInternalVersioning() throws Exception {
    createIndex("test");
    ensureGreen();
    IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(1L));
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_2").setVersion(1).execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(2L));
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareIndex("test", "type", "1").setCreate(true).setSource("field1", "value1_1").execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(1).execute(), VersionConflictEngineException.class);
    client().admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 10; i++) {
        assertThat(client().prepareGet("test", "type", "1").execute().actionGet().getVersion(), equalTo(2L));
    }
    // search with versioning
    for (int i = 0; i < 10; i++) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setVersion(true).execute().actionGet();
        assertThat(searchResponse.getHits().getAt(0).getVersion(), equalTo(2L));
    }
    // search without versioning
    for (int i = 0; i < 10; i++) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).execute().actionGet();
        assertThat(searchResponse.getHits().getAt(0).getVersion(), equalTo(Versions.NOT_FOUND));
    }
    DeleteResponse deleteResponse = client().prepareDelete("test", "type", "1").setVersion(2).execute().actionGet();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(3L));
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(2).execute(), VersionConflictEngineException.class);
    // This is intricate - the object was deleted but a delete transaction was with the right version. We add another one
    // and thus the transaction is increased.
    deleteResponse = client().prepareDelete("test", "type", "1").setVersion(3).execute().actionGet();
    assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(4L));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1722 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)275 SearchHit (org.elasticsearch.search.SearchHit)227 Script (org.elasticsearch.script.Script)225 ArrayList (java.util.ArrayList)217 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)210 Matchers.containsString (org.hamcrest.Matchers.containsString)156 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)143 HashMap (java.util.HashMap)139 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)132 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)97 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)95 SearchHits (org.elasticsearch.search.SearchHits)92 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)84 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)82 Test (org.junit.Test)75 Map (java.util.Map)72 SearchRequest (org.elasticsearch.action.search.SearchRequest)70