Search in sources :

Example 21 with SearchResponse

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

the class NoMasterNodeIT method testNoMasterActionsWriteMasterBlock.

public void testNoMasterActionsWriteMasterBlock() throws Exception {
    Settings settings = Settings.builder().put("discovery.type", "zen").put("action.auto_create_index", false).put("discovery.zen.minimum_master_nodes", 2).put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "200ms").put("discovery.initial_state_timeout", "500ms").put(DiscoverySettings.NO_MASTER_BLOCK_SETTING.getKey(), "write").build();
    internalCluster().startNode(settings);
    // start a second node, create an index, and then shut it down so we have no master block
    internalCluster().startNode(settings);
    prepareCreate("test1").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).get();
    prepareCreate("test2").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 2, IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).get();
    client().admin().cluster().prepareHealth("_all").setWaitForGreenStatus().get();
    client().prepareIndex("test1", "type1", "1").setSource("field", "value1").get();
    client().prepareIndex("test2", "type1", "1").setSource("field", "value1").get();
    refresh();
    ensureSearchable("test1", "test2");
    ClusterStateResponse clusterState = client().admin().cluster().prepareState().get();
    logger.info("Cluster state:\n{}", clusterState.getState());
    internalCluster().stopRandomDataNode();
    assertTrue(awaitBusy(() -> {
        ClusterState state = client().admin().cluster().prepareState().setLocal(true).get().getState();
        return state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID);
    }));
    GetResponse getResponse = client().prepareGet("test1", "type1", "1").get();
    assertExists(getResponse);
    SearchResponse countResponse = client().prepareSearch("test1").setSize(0).get();
    assertHitCount(countResponse, 1L);
    SearchResponse searchResponse = client().prepareSearch("test1").get();
    assertHitCount(searchResponse, 1L);
    countResponse = client().prepareSearch("test2").setSize(0).get();
    assertThat(countResponse.getTotalShards(), equalTo(2));
    assertThat(countResponse.getSuccessfulShards(), equalTo(1));
    TimeValue timeout = TimeValue.timeValueMillis(200);
    long now = System.currentTimeMillis();
    try {
        client().prepareUpdate("test1", "type1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").setTimeout(timeout).get();
        fail("Expected ClusterBlockException");
    } catch (ClusterBlockException e) {
        assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
        assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
    }
    now = System.currentTimeMillis();
    try {
        client().prepareIndex("test1", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout).get();
        fail("Expected ClusterBlockException");
    } catch (ClusterBlockException e) {
        assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
        assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
    }
    internalCluster().startNode(settings);
    client().admin().cluster().prepareHealth().setWaitForGreenStatus().setWaitForNodes("2").get();
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) GetResponse(org.elasticsearch.action.get.GetResponse) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) Settings(org.elasticsearch.common.settings.Settings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 22 with SearchResponse

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

the class OldIndexBackwardsCompatibilityIT method assertIndexSanity.

void assertIndexSanity(String indexName, Version indexCreated) {
    GetIndexResponse getIndexResponse = client().admin().indices().prepareGetIndex().addIndices(indexName).get();
    assertEquals(1, getIndexResponse.indices().length);
    assertEquals(indexName, getIndexResponse.indices()[0]);
    Version actualVersionCreated = Version.indexCreated(getIndexResponse.getSettings().get(indexName));
    assertEquals(indexCreated, actualVersionCreated);
    ensureYellow(indexName);
    RecoveryResponse recoveryResponse = client().admin().indices().prepareRecoveries(indexName).setDetailed(true).setActiveOnly(false).get();
    boolean foundTranslog = false;
    for (List<RecoveryState> states : recoveryResponse.shardRecoveryStates().values()) {
        for (RecoveryState state : states) {
            if (state.getStage() == RecoveryState.Stage.DONE && state.getPrimary() && state.getRecoverySource().getType() == RecoverySource.Type.EXISTING_STORE) {
                assertFalse("more than one primary recoverd?", foundTranslog);
                assertNotEquals(0, state.getTranslog().recoveredOperations());
                foundTranslog = true;
            }
        }
    }
    assertTrue("expected translog but nothing was recovered", foundTranslog);
    IndicesSegmentResponse segmentsResponse = client().admin().indices().prepareSegments(indexName).get();
    IndexSegments segments = segmentsResponse.getIndices().get(indexName);
    int numCurrent = 0;
    int numBWC = 0;
    for (IndexShardSegments indexShardSegments : segments) {
        for (ShardSegments shardSegments : indexShardSegments) {
            for (Segment segment : shardSegments) {
                if (indexCreated.luceneVersion.equals(segment.version)) {
                    numBWC++;
                    if (Version.CURRENT.luceneVersion.equals(segment.version)) {
                        numCurrent++;
                    }
                } else if (Version.CURRENT.luceneVersion.equals(segment.version)) {
                    numCurrent++;
                } else {
                    fail("unexpected version " + segment.version);
                }
            }
        }
    }
    assertNotEquals("expected at least 1 current segment after translog recovery", 0, numCurrent);
    assertNotEquals("expected at least 1 old segment", 0, numBWC);
    SearchResponse test = client().prepareSearch(indexName).get();
    assertThat(test.getHits().getTotalHits(), greaterThanOrEqualTo(1L));
}
Also used : IndicesSegmentResponse(org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse) IndexShardSegments(org.elasticsearch.action.admin.indices.segments.IndexShardSegments) IndexSegments(org.elasticsearch.action.admin.indices.segments.IndexSegments) Segment(org.elasticsearch.index.engine.Segment) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) Version(org.elasticsearch.Version) GetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse) ShardSegments(org.elasticsearch.action.admin.indices.segments.ShardSegments) IndexShardSegments(org.elasticsearch.action.admin.indices.segments.IndexShardSegments) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 23 with SearchResponse

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

the class RestoreBackwardsCompatIT method testOldSnapshot.

private void testOldSnapshot(String version, String repo, String snapshot) throws IOException {
    logger.info("--> get snapshot and check its version");
    GetSnapshotsResponse getSnapshotsResponse = client().admin().cluster().prepareGetSnapshots(repo).setSnapshots(snapshot).get();
    assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(1));
    SnapshotInfo snapshotInfo = getSnapshotsResponse.getSnapshots().get(0);
    assertThat(snapshotInfo.version().toString(), equalTo(version));
    logger.info("--> restoring snapshot");
    RestoreSnapshotResponse response = client().admin().cluster().prepareRestoreSnapshot(repo, snapshot).setRestoreGlobalState(true).setWaitForCompletion(true).get();
    assertThat(response.status(), equalTo(RestStatus.OK));
    RestoreInfo restoreInfo = response.getRestoreInfo();
    assertThat(restoreInfo.successfulShards(), greaterThan(0));
    assertThat(restoreInfo.successfulShards(), equalTo(restoreInfo.totalShards()));
    assertThat(restoreInfo.failedShards(), equalTo(0));
    String index = restoreInfo.indices().get(0);
    logger.info("--> check search");
    SearchResponse searchResponse = client().prepareSearch(index).get();
    assertThat(searchResponse.getHits().getTotalHits(), greaterThan(1L));
    logger.info("--> check settings");
    ClusterState clusterState = client().admin().cluster().prepareState().get().getState();
    assertThat(clusterState.metaData().persistentSettings().get(FilterAllocationDecider.CLUSTER_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + "version_attr"), equalTo(version));
    logger.info("--> check templates");
    IndexTemplateMetaData template = clusterState.getMetaData().templates().get("template_" + version.toLowerCase(Locale.ROOT));
    assertThat(template, notNullValue());
    assertThat(template.patterns(), equalTo(Collections.singletonList("te*")));
    assertThat(template.settings().getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, -1), equalTo(1));
    assertThat(template.mappings().size(), equalTo(1));
    assertThat(template.mappings().get("type1").string(), anyOf(equalTo("{\"type1\":{\"_source\":{\"enabled\":false}}}"), equalTo("{\"type1\":{\"_source\":{\"enabled\":\"false\"}}}"), equalTo("{\"type1\":{\"_source\":{\"enabled\":\"0\"}}}"), equalTo("{\"type1\":{\"_source\":{\"enabled\":0}}}"), equalTo("{\"type1\":{\"_source\":{\"enabled\":\"off\"}}}"), equalTo("{\"type1\":{\"_source\":{\"enabled\":\"no\"}}}")));
    assertThat(template.aliases().size(), equalTo(3));
    assertThat(template.aliases().get("alias1"), notNullValue());
    assertThat(template.aliases().get("alias2").filter().string(), containsString(version));
    assertThat(template.aliases().get("alias2").indexRouting(), equalTo("kimchy"));
    assertThat(template.aliases().get("{index}-alias"), notNullValue());
    logger.info("--> cleanup");
    cluster().wipeIndices(restoreInfo.indices().toArray(new String[restoreInfo.indices().size()]));
    cluster().wipeTemplates();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) GetSnapshotsResponse(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) RestoreInfo(org.elasticsearch.snapshots.RestoreInfo) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) Matchers.containsString(org.hamcrest.Matchers.containsString) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 24 with SearchResponse

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

the class IpFieldBwCompatIT method testRangeAgg.

public void testRangeAgg() {
    SearchResponse response = client().prepareSearch("old_index", "new_index").addAggregation(AggregationBuilders.ipRange("ip_range").field("ip_field").addMaskRange("127.0.0.1/16").addMaskRange("::1/64")).get();
    assertNoFailures(response);
    assertEquals(3, response.getHits().getTotalHits());
    Range range = response.getAggregations().get("ip_range");
    assertEquals(2, range.getBuckets().size());
    assertEquals("::1/64", range.getBuckets().get(0).getKeyAsString());
    assertEquals(3, range.getBuckets().get(0).getDocCount());
    assertEquals("127.0.0.1/16", range.getBuckets().get(1).getKeyAsString());
    assertEquals(2, range.getBuckets().get(1).getDocCount());
}
Also used : Range(org.elasticsearch.search.aggregations.bucket.range.Range) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 25 with SearchResponse

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

the class IpFieldBwCompatIT method testSort.

public void testSort() {
    SearchResponse response = client().prepareSearch("old_index", "new_index").addSort(SortBuilders.fieldSort("ip_field")).get();
    assertNoFailures(response);
    assertEquals(3, response.getHits().getTotalHits());
    assertEquals("::1", response.getHits().getAt(0).getSortValues()[0]);
    assertEquals("127.0.0.1", response.getHits().getAt(1).getSortValues()[0]);
    assertEquals("127.0.0.1", response.getHits().getAt(2).getSortValues()[0]);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1693 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)272 SearchHit (org.elasticsearch.search.SearchHit)225 Script (org.elasticsearch.script.Script)223 ArrayList (java.util.ArrayList)212 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)134 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)132 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)94 SearchHits (org.elasticsearch.search.SearchHits)89 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)83 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)76 Test (org.junit.Test)73 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)72 GeoPoint (org.elasticsearch.common.geo.GeoPoint)70 Map (java.util.Map)67