Search in sources :

Example 56 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GeoShapeQueryTests method testNullShape.

public void testNullShape() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "geo_shape").endObject().endObject().endObject().endObject().string();
    client().admin().indices().prepareCreate("test").addMapping("type1", mapping, XContentType.JSON).execute().actionGet();
    ensureGreen();
    client().prepareIndex("test", "type1", "aNullshape").setSource("{\"location\": null}", XContentType.JSON).setRefreshPolicy(IMMEDIATE).get();
    GetResponse result = client().prepareGet("test", "type1", "aNullshape").execute().actionGet();
    assertThat(result.getField("location"), nullValue());
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 57 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class ReindexVersioningTests method assertDest.

private void assertDest(String fooValue, int version) {
    GetResponse get = client().prepareGet("dest", "test", "test").get();
    assertEquals(fooValue, get.getSource().get("foo"));
    assertEquals(version, get.getVersion());
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse)

Example 58 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class UpdateByQueryWhileModifyingTests method testUpdateWhileReindexing.

public void testUpdateWhileReindexing() throws Exception {
    AtomicReference<String> value = new AtomicReference<>(randomSimpleString(random()));
    indexRandom(true, client().prepareIndex("test", "test", "test").setSource("test", value.get()));
    AtomicReference<Exception> failure = new AtomicReference<>();
    AtomicBoolean keepUpdating = new AtomicBoolean(true);
    Thread updater = new Thread(() -> {
        while (keepUpdating.get()) {
            try {
                BulkByScrollResponse response = updateByQuery().source("test").refresh(true).abortOnVersionConflict(false).get();
                assertThat(response, matcher().updated(either(equalTo(0L)).or(equalTo(1L))).versionConflicts(either(equalTo(0L)).or(equalTo(1L))));
            } catch (Exception e) {
                failure.set(e);
            }
        }
    });
    updater.start();
    try {
        for (int i = 0; i < MAX_MUTATIONS; i++) {
            GetResponse get = client().prepareGet("test", "test", "test").get();
            assertEquals(value.get(), get.getSource().get("test"));
            value.set(randomSimpleString(random()));
            IndexRequestBuilder index = client().prepareIndex("test", "test", "test").setSource("test", value.get()).setRefreshPolicy(IMMEDIATE);
            /*
                 * Update by query increments the version number so concurrent
                 * indexes might get version conflict exceptions so we just
                 * blindly retry.
                 */
            int attempts = 0;
            while (true) {
                attempts++;
                try {
                    index.setVersion(get.getVersion()).get();
                    break;
                } catch (VersionConflictEngineException e) {
                    if (attempts >= MAX_ATTEMPTS) {
                        throw new RuntimeException("Failed to index after [" + MAX_ATTEMPTS + "] attempts. Too many version conflicts!");
                    }
                    logger.info("Caught expected version conflict trying to perform mutation number [{}] with version [{}] " + "on attempt [{}]. Retrying.", i, get.getVersion(), attempts);
                    get = client().prepareGet("test", "test", "test").get();
                }
            }
        }
    } finally {
        keepUpdating.set(false);
        updater.join(TimeUnit.SECONDS.toMillis(10));
        if (failure.get() != null) {
            throw new RuntimeException(failure.get());
        }
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString) GetResponse(org.elasticsearch.action.get.GetResponse) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) BulkByScrollResponse(org.elasticsearch.action.bulk.byscroll.BulkByScrollResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException)

Example 59 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class DocumentActionsIT method testBulk.

public void testBulk() throws Exception {
    createIndex();
    NumShards numShards = getNumShards(getConcreteIndexName());
    logger.info("-> running Cluster Health");
    ensureGreen();
    BulkResponse bulkResponse = client().prepareBulk().add(client().prepareIndex().setIndex("test").setType("type1").setId("1").setSource(source("1", "test"))).add(client().prepareIndex().setIndex("test").setType("type1").setId("2").setSource(source("2", "test")).setCreate(true)).add(client().prepareIndex().setIndex("test").setType("type1").setSource(source("3", "test"))).add(client().prepareDelete().setIndex("test").setType("type1").setId("1")).add(// failure
    client().prepareIndex().setIndex("test").setType("type1").setSource("{ xxx }", XContentType.JSON)).execute().actionGet();
    assertThat(bulkResponse.hasFailures(), equalTo(true));
    assertThat(bulkResponse.getItems().length, equalTo(5));
    assertThat(bulkResponse.getItems()[0].isFailed(), equalTo(false));
    assertThat(bulkResponse.getItems()[0].getOpType(), equalTo(OpType.INDEX));
    assertThat(bulkResponse.getItems()[0].getIndex(), equalTo(getConcreteIndexName()));
    assertThat(bulkResponse.getItems()[0].getType(), equalTo("type1"));
    assertThat(bulkResponse.getItems()[0].getId(), equalTo("1"));
    assertThat(bulkResponse.getItems()[1].isFailed(), equalTo(false));
    assertThat(bulkResponse.getItems()[1].getOpType(), equalTo(OpType.CREATE));
    assertThat(bulkResponse.getItems()[1].getIndex(), equalTo(getConcreteIndexName()));
    assertThat(bulkResponse.getItems()[1].getType(), equalTo("type1"));
    assertThat(bulkResponse.getItems()[1].getId(), equalTo("2"));
    assertThat(bulkResponse.getItems()[2].isFailed(), equalTo(false));
    assertThat(bulkResponse.getItems()[2].getOpType(), equalTo(OpType.INDEX));
    assertThat(bulkResponse.getItems()[2].getIndex(), equalTo(getConcreteIndexName()));
    assertThat(bulkResponse.getItems()[2].getType(), equalTo("type1"));
    String generatedId3 = bulkResponse.getItems()[2].getId();
    assertThat(bulkResponse.getItems()[3].isFailed(), equalTo(false));
    assertThat(bulkResponse.getItems()[3].getOpType(), equalTo(OpType.DELETE));
    assertThat(bulkResponse.getItems()[3].getIndex(), equalTo(getConcreteIndexName()));
    assertThat(bulkResponse.getItems()[3].getType(), equalTo("type1"));
    assertThat(bulkResponse.getItems()[3].getId(), equalTo("1"));
    assertThat(bulkResponse.getItems()[4].isFailed(), equalTo(true));
    assertThat(bulkResponse.getItems()[4].getOpType(), equalTo(OpType.INDEX));
    assertThat(bulkResponse.getItems()[4].getIndex(), equalTo(getConcreteIndexName()));
    assertThat(bulkResponse.getItems()[4].getType(), equalTo("type1"));
    waitForRelocation(ClusterHealthStatus.GREEN);
    RefreshResponse refreshResponse = client().admin().indices().prepareRefresh("test").execute().actionGet();
    assertNoFailures(refreshResponse);
    assertThat(refreshResponse.getSuccessfulShards(), equalTo(numShards.totalNumShards));
    for (int i = 0; i < 5; i++) {
        GetResponse getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
        assertThat("cycle #" + i, getResult.isExists(), equalTo(false));
        getResult = client().get(getRequest("test").type("type1").id("2")).actionGet();
        assertThat("cycle #" + i, getResult.getSourceAsString(), equalTo(source("2", "test").string()));
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
        getResult = client().get(getRequest("test").type("type1").id(generatedId3)).actionGet();
        assertThat("cycle #" + i, getResult.getSourceAsString(), equalTo(source("3", "test").string()));
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
    }
}
Also used : RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 60 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GatewayIndexStateIT method testSimpleOpenClose.

public void testSimpleOpenClose() throws Exception {
    logger.info("--> starting 2 nodes");
    internalCluster().startNodes(2);
    logger.info("--> creating test index");
    createIndex("test");
    NumShards test = getNumShards("test");
    logger.info("--> waiting for green status");
    ensureGreen();
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(test.numPrimaries));
    assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(test.totalNumShards));
    logger.info("--> indexing a simple document");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
    logger.info("--> closing test index...");
    client().admin().indices().prepareClose("test").get();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test"), nullValue());
    logger.info("--> verifying that the state is green");
    ensureGreen();
    logger.info("--> trying to index into a closed index ...");
    try {
        client().prepareIndex("test", "type1", "1").setSource("field1", "value1").setTimeout("1s").execute().actionGet();
        fail();
    } catch (IndexClosedException e) {
    // all is well
    }
    logger.info("--> creating another index (test2) by indexing into it");
    client().prepareIndex("test2", "type1", "1").setSource("field1", "value1").execute().actionGet();
    logger.info("--> verifying that the state is green");
    ensureGreen();
    logger.info("--> opening the first index again...");
    client().admin().indices().prepareOpen("test").execute().actionGet();
    logger.info("--> verifying that the state is green");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(test.numPrimaries));
    assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(test.totalNumShards));
    logger.info("--> trying to get the indexed document on the first index");
    GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    logger.info("--> closing test index...");
    client().admin().indices().prepareClose("test").execute().actionGet();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test"), nullValue());
    logger.info("--> restarting nodes...");
    internalCluster().fullRestart();
    logger.info("--> waiting for two nodes and green status");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test"), nullValue());
    logger.info("--> trying to index into a closed index ...");
    try {
        client().prepareIndex("test", "type1", "1").setSource("field1", "value1").setTimeout("1s").execute().actionGet();
        fail();
    } catch (IndexClosedException e) {
    // all is well
    }
    logger.info("--> opening index...");
    client().admin().indices().prepareOpen("test").execute().actionGet();
    logger.info("--> waiting for green status");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(test.numPrimaries));
    assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(test.totalNumShards));
    logger.info("--> trying to get the indexed document on the first round (before close and shutdown)");
    getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    logger.info("--> indexing a simple document");
    client().prepareIndex("test", "type1", "2").setSource("field1", "value1").execute().actionGet();
}
Also used : IndexClosedException(org.elasticsearch.indices.IndexClosedException) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) GetResponse(org.elasticsearch.action.get.GetResponse)

Aggregations

GetResponse (org.elasticsearch.action.get.GetResponse)84 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)22 Test (org.junit.Test)18 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 HashMap (java.util.HashMap)11 GetRequest (org.elasticsearch.action.get.GetRequest)11 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)10 Script (org.elasticsearch.script.Script)9 Settings (org.elasticsearch.common.settings.Settings)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IOException (java.io.IOException)7 Map (java.util.Map)7 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 CompiledScript (org.elasticsearch.script.CompiledScript)7 ExecutableScript (org.elasticsearch.script.ExecutableScript)7 SearchScript (org.elasticsearch.script.SearchScript)7 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)6 IndexResponse (org.elasticsearch.action.index.IndexResponse)5 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)5 Path (java.nio.file.Path)4