Search in sources :

Example 46 with GetResponse

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

the class GeoShapeQueryBuilder method fetch.

/**
     * Fetches the Shape with the given ID in the given type and index.
     *
     * @param getRequest
     *            GetRequest containing index, type and id
     * @param path
     *            Name or path of the field in the Shape Document where the
     *            Shape itself is located
     * @return Shape with the given ID
     * @throws IOException
     *             Can be thrown while parsing the Shape Document and extracting
     *             the Shape
     */
private ShapeBuilder fetch(Client client, GetRequest getRequest, String path) throws IOException {
    if (ShapesAvailability.JTS_AVAILABLE == false) {
        throw new IllegalStateException("JTS not available");
    }
    getRequest.preference("_local");
    getRequest.operationThreaded(false);
    GetResponse response = client.get(getRequest).actionGet();
    if (!response.isExists()) {
        throw new IllegalArgumentException("Shape with ID [" + getRequest.id() + "] in type [" + getRequest.type() + "] not found");
    }
    if (response.isSourceEmpty()) {
        throw new IllegalArgumentException("Shape with ID [" + getRequest.id() + "] in type [" + getRequest.type() + "] source disabled");
    }
    String[] pathElements = path.split("\\.");
    int currentPathSlot = 0;
    // It is safe to use EMPTY here because this never uses namedObject
    try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, response.getSourceAsBytesRef())) {
        XContentParser.Token currentToken;
        while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (currentToken == XContentParser.Token.FIELD_NAME) {
                if (pathElements[currentPathSlot].equals(parser.currentName())) {
                    parser.nextToken();
                    if (++currentPathSlot == pathElements.length) {
                        return ShapeBuilder.parse(parser);
                    }
                } else {
                    parser.nextToken();
                    parser.skipChildren();
                }
            }
        }
        throw new IllegalStateException("Shape with name [" + getRequest.id() + "] found but missing " + path + " field");
    }
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 47 with GetResponse

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

the class TasksIT method testTaskStoringFailureResult.

public void testTaskStoringFailureResult() throws Exception {
    // we need this to get task id of the process
    registerTaskManageListeners(TestTaskPlugin.TestTaskAction.NAME);
    // Start non-blocking test task that should fail
    assertThrows(TestTaskPlugin.TestTaskAction.INSTANCE.newRequestBuilder(client()).setShouldFail(true).setShouldStoreResult(true).setShouldBlock(false), IllegalStateException.class);
    List<TaskInfo> events = findEvents(TestTaskPlugin.TestTaskAction.NAME, Tuple::v1);
    assertEquals(1, events.size());
    TaskInfo failedTaskInfo = events.get(0);
    TaskId failedTaskId = failedTaskInfo.getTaskId();
    GetResponse failedResultDoc = client().prepareGet(TaskResultsService.TASK_INDEX, TaskResultsService.TASK_TYPE, failedTaskId.toString()).get();
    assertTrue(failedResultDoc.isExists());
    Map<String, Object> source = failedResultDoc.getSource();
    @SuppressWarnings("unchecked") Map<String, Object> task = (Map<String, Object>) source.get("task");
    assertEquals(failedTaskInfo.getTaskId().getNodeId(), task.get("node"));
    assertEquals(failedTaskInfo.getAction(), task.get("action"));
    assertEquals(Long.toString(failedTaskInfo.getId()), task.get("id").toString());
    @SuppressWarnings("unchecked") Map<String, Object> error = (Map<String, Object>) source.get("error");
    assertEquals("Simulating operation failure", error.get("reason"));
    assertEquals("illegal_state_exception", error.get("type"));
    assertNull(source.get("result"));
    GetTaskResponse getResponse = expectFinishedTask(failedTaskId);
    assertNull(getResponse.getTask().getResponse());
    assertEquals(error, getResponse.getTask().getErrorAsMap());
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskId(org.elasticsearch.tasks.TaskId) GetTaskResponse(org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap) Tuple(org.elasticsearch.common.collect.Tuple)

Example 48 with GetResponse

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

the class TasksIT method testTaskStoringSuccesfulResult.

public void testTaskStoringSuccesfulResult() throws Exception {
    // Randomly create an empty index to make sure the type is created automatically
    if (randomBoolean()) {
        logger.info("creating an empty results index with custom settings");
        assertAcked(client().admin().indices().prepareCreate(TaskResultsService.TASK_INDEX));
    }
    // we need this to get task id of the process
    registerTaskManageListeners(TestTaskPlugin.TestTaskAction.NAME);
    // Start non-blocking test task
    TestTaskPlugin.TestTaskAction.INSTANCE.newRequestBuilder(client()).setShouldStoreResult(true).setShouldBlock(false).get();
    List<TaskInfo> events = findEvents(TestTaskPlugin.TestTaskAction.NAME, Tuple::v1);
    assertEquals(1, events.size());
    TaskInfo taskInfo = events.get(0);
    TaskId taskId = taskInfo.getTaskId();
    GetResponse resultDoc = client().prepareGet(TaskResultsService.TASK_INDEX, TaskResultsService.TASK_TYPE, taskId.toString()).get();
    assertTrue(resultDoc.isExists());
    Map<String, Object> source = resultDoc.getSource();
    @SuppressWarnings("unchecked") Map<String, Object> task = (Map<String, Object>) source.get("task");
    assertEquals(taskInfo.getTaskId().getNodeId(), task.get("node"));
    assertEquals(taskInfo.getAction(), task.get("action"));
    assertEquals(Long.toString(taskInfo.getId()), task.get("id").toString());
    @SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) source.get("response");
    assertEquals("0", result.get("failure_count").toString());
    assertNull(source.get("failure"));
    assertNoFailures(client().admin().indices().prepareRefresh(TaskResultsService.TASK_INDEX).get());
    SearchResponse searchResponse = client().prepareSearch(TaskResultsService.TASK_INDEX).setTypes(TaskResultsService.TASK_TYPE).setSource(SearchSourceBuilder.searchSource().query(QueryBuilders.termQuery("task.action", taskInfo.getAction()))).get();
    assertEquals(1L, searchResponse.getHits().getTotalHits());
    searchResponse = client().prepareSearch(TaskResultsService.TASK_INDEX).setTypes(TaskResultsService.TASK_TYPE).setSource(SearchSourceBuilder.searchSource().query(QueryBuilders.termQuery("task.node", taskInfo.getTaskId().getNodeId()))).get();
    assertEquals(1L, searchResponse.getHits().getTotalHits());
    GetTaskResponse getResponse = expectFinishedTask(taskId);
    assertEquals(result, getResponse.getTask().getResponseAsMap());
    assertNull(getResponse.getTask().getError());
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskId(org.elasticsearch.tasks.TaskId) GetTaskResponse(org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap) Tuple(org.elasticsearch.common.collect.Tuple) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 49 with GetResponse

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

the class BulkWithUpdatesIT method testBulkUpdateSimple.

public void testBulkUpdateSimple() throws Exception {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")));
    ensureGreen();
    BulkResponse bulkResponse = client().prepareBulk().add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("1").setSource("field", 1)).add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("2").setSource("field", 2).setCreate(true)).add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("3").setSource("field", 3)).add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("4").setSource("field", 4)).add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("5").setSource("field", 5)).execute().actionGet();
    assertThat(bulkResponse.hasFailures(), equalTo(false));
    assertThat(bulkResponse.getItems().length, equalTo(5));
    for (BulkItemResponse bulkItemResponse : bulkResponse) {
        assertThat(bulkItemResponse.getIndex(), equalTo("test"));
    }
    final Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "ctx._source.field += 1", Collections.emptyMap());
    bulkResponse = client().prepareBulk().add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("1").setScript(script)).add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("2").setScript(script).setRetryOnConflict(3)).add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("3").setDoc(jsonBuilder().startObject().field("field1", "test").endObject())).get();
    assertThat(bulkResponse.hasFailures(), equalTo(false));
    assertThat(bulkResponse.getItems().length, equalTo(3));
    for (BulkItemResponse bulkItemResponse : bulkResponse) {
        assertThat(bulkItemResponse.getIndex(), equalTo("test"));
    }
    assertThat(bulkResponse.getItems()[0].getResponse().getId(), equalTo("1"));
    assertThat(bulkResponse.getItems()[0].getResponse().getVersion(), equalTo(2L));
    assertThat(bulkResponse.getItems()[1].getResponse().getId(), equalTo("2"));
    assertThat(bulkResponse.getItems()[1].getResponse().getVersion(), equalTo(2L));
    assertThat(bulkResponse.getItems()[2].getResponse().getId(), equalTo("3"));
    assertThat(bulkResponse.getItems()[2].getResponse().getVersion(), equalTo(2L));
    GetResponse getResponse = client().prepareGet().setIndex("test").setType("type1").setId("1").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getVersion(), equalTo(2L));
    assertThat(((Number) getResponse.getSource().get("field")).longValue(), equalTo(2L));
    getResponse = client().prepareGet().setIndex("test").setType("type1").setId("2").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getVersion(), equalTo(2L));
    assertThat(((Number) getResponse.getSource().get("field")).longValue(), equalTo(3L));
    getResponse = client().prepareGet().setIndex("test").setType("type1").setId("3").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getVersion(), equalTo(2L));
    assertThat(getResponse.getSource().get("field1").toString(), equalTo("test"));
    bulkResponse = client().prepareBulk().add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("6").setScript(script).setUpsert(jsonBuilder().startObject().field("field", 0).endObject())).add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("7").setScript(script)).add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("2").setScript(script)).get();
    assertThat(bulkResponse.hasFailures(), equalTo(true));
    assertThat(bulkResponse.getItems().length, equalTo(3));
    assertThat(bulkResponse.getItems()[0].getResponse().getId(), equalTo("6"));
    assertThat(bulkResponse.getItems()[0].getResponse().getVersion(), equalTo(1L));
    assertThat(bulkResponse.getItems()[1].getResponse(), nullValue());
    assertThat(bulkResponse.getItems()[1].getFailure().getIndex(), equalTo("test"));
    assertThat(bulkResponse.getItems()[1].getFailure().getId(), equalTo("7"));
    assertThat(bulkResponse.getItems()[1].getFailure().getMessage(), containsString("document missing"));
    assertThat(bulkResponse.getItems()[2].getResponse().getId(), equalTo("2"));
    assertThat(bulkResponse.getItems()[2].getResponse().getIndex(), equalTo("test"));
    assertThat(bulkResponse.getItems()[2].getResponse().getVersion(), equalTo(3L));
    getResponse = client().prepareGet().setIndex("test").setType("type1").setId("6").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getVersion(), equalTo(1L));
    assertThat(((Number) getResponse.getSource().get("field")).longValue(), equalTo(0L));
    getResponse = client().prepareGet().setIndex("test").setType("type1").setId("7").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(false));
    getResponse = client().prepareGet().setIndex("test").setType("type1").setId("2").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getVersion(), equalTo(3L));
    assertThat(((Number) getResponse.getSource().get("field")).longValue(), equalTo(4L));
}
Also used : Script(org.elasticsearch.script.Script) Alias(org.elasticsearch.action.admin.indices.alias.Alias) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 50 with GetResponse

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

the class BulkWithUpdatesIT method testBulkUpdateLargerVolume.

public void testBulkUpdateLargerVolume() throws Exception {
    createIndex("test");
    ensureGreen();
    int numDocs = scaledRandomIntBetween(100, 2000);
    if (numDocs % 2 == 1) {
        // this test needs an even num of docs
        numDocs++;
    }
    final Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "ctx._source.counter += 1", Collections.emptyMap());
    BulkRequestBuilder builder = client().prepareBulk();
    for (int i = 0; i < numDocs; i++) {
        builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setFields("counter").setScript(script).setUpsert(jsonBuilder().startObject().field("counter", 1).endObject()));
    }
    BulkResponse response = builder.get();
    assertThat(response.hasFailures(), equalTo(false));
    assertThat(response.getItems().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getVersion(), equalTo(1L));
        assertThat(response.getItems()[i].getIndex(), equalTo("test"));
        assertThat(response.getItems()[i].getType(), equalTo("type1"));
        assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
        assertThat(response.getItems()[i].getResponse().getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getResponse().getVersion(), equalTo(1L));
        assertThat(((UpdateResponse) response.getItems()[i].getResponse()).getGetResult().field("counter").getValue(), equalTo(1));
        for (int j = 0; j < 5; j++) {
            GetResponse getResponse = client().prepareGet("test", "type1", Integer.toString(i)).execute().actionGet();
            assertThat(getResponse.isExists(), equalTo(true));
            assertThat(getResponse.getVersion(), equalTo(1L));
            assertThat(((Number) getResponse.getSource().get("counter")).longValue(), equalTo(1L));
        }
    }
    builder = client().prepareBulk();
    for (int i = 0; i < numDocs; i++) {
        UpdateRequestBuilder updateBuilder = client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setFields("counter");
        if (i % 2 == 0) {
            updateBuilder.setScript(script);
        } else {
            updateBuilder.setDoc(jsonBuilder().startObject().field("counter", 2).endObject());
        }
        if (i % 3 == 0) {
            updateBuilder.setRetryOnConflict(3);
        }
        builder.add(updateBuilder);
    }
    response = builder.execute().actionGet();
    assertThat(response.hasFailures(), equalTo(false));
    assertThat(response.getItems().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getVersion(), equalTo(2L));
        assertThat(response.getItems()[i].getIndex(), equalTo("test"));
        assertThat(response.getItems()[i].getType(), equalTo("type1"));
        assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
        assertThat(response.getItems()[i].getResponse().getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getResponse().getVersion(), equalTo(2L));
        assertThat(((UpdateResponse) response.getItems()[i].getResponse()).getGetResult().field("counter").getValue(), equalTo(2));
    }
    builder = client().prepareBulk();
    int maxDocs = numDocs / 2 + numDocs;
    for (int i = (numDocs / 2); i < maxDocs; i++) {
        builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setScript(script));
    }
    response = builder.execute().actionGet();
    assertThat(response.hasFailures(), equalTo(true));
    assertThat(response.getItems().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        int id = i + (numDocs / 2);
        if (i >= (numDocs / 2)) {
            assertThat(response.getItems()[i].getFailure().getId(), equalTo(Integer.toString(id)));
            assertThat(response.getItems()[i].getFailure().getMessage(), containsString("document missing"));
        } else {
            assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(id)));
            assertThat(response.getItems()[i].getVersion(), equalTo(3L));
            assertThat(response.getItems()[i].getIndex(), equalTo("test"));
            assertThat(response.getItems()[i].getType(), equalTo("type1"));
            assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
        }
    }
    builder = client().prepareBulk();
    for (int i = 0; i < numDocs; i++) {
        builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setScript(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "ctx.op = \"none\"", Collections.emptyMap())));
    }
    response = builder.execute().actionGet();
    assertThat(response.buildFailureMessage(), response.hasFailures(), equalTo(false));
    assertThat(response.getItems().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        assertThat(response.getItems()[i].getItemId(), equalTo(i));
        assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getIndex(), equalTo("test"));
        assertThat(response.getItems()[i].getType(), equalTo("type1"));
        assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
    }
    builder = client().prepareBulk();
    for (int i = 0; i < numDocs; i++) {
        builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setScript(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "ctx.op = \"delete\"", Collections.emptyMap())));
    }
    response = builder.execute().actionGet();
    assertThat(response.hasFailures(), equalTo(false));
    assertThat(response.getItems().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        assertThat(response.getItems()[i].getItemId(), equalTo(i));
        assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getIndex(), equalTo("test"));
        assertThat(response.getItems()[i].getType(), equalTo("type1"));
        assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
        for (int j = 0; j < 5; j++) {
            GetResponse getResponse = client().prepareGet("test", "type1", Integer.toString(i)).get();
            assertThat(getResponse.isExists(), equalTo(false));
        }
    }
}
Also used : Script(org.elasticsearch.script.Script) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) 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