Search in sources :

Example 1 with DocumentMissingException

use of org.elasticsearch.index.engine.DocumentMissingException in project jena by apache.

the class TextIndexES method deleteEntity.

/**
     * Delete the value of the entity from the existing document, if any.
     * The document itself will never get deleted. Only the value will get deleted.
     * @param entity entity whose value needs to be deleted
     */
@Override
public void deleteEntity(Entity entity) {
    String fieldToRemove = null;
    String valueToRemove = null;
    for (String field : docDef.fields()) {
        if (entity.get(field) != null) {
            fieldToRemove = field;
            if (entity.getLanguage() != null && !entity.getLanguage().isEmpty()) {
                fieldToRemove = normalizeFieldName(fieldToRemove, entity.getLanguage());
            }
            valueToRemove = (String) entity.get(field);
            break;
        }
    }
    if (fieldToRemove != null && valueToRemove != null) {
        LOGGER.debug("deleting content related to entity: " + entity.getId());
        String deleteScript = DELETE_SCRIPT.replaceAll("<fieldToRemove>", fieldToRemove);
        Map<String, Object> params = new HashMap<>();
        params.put("valueToRemove", valueToRemove);
        UpdateRequest updateRequest = new UpdateRequest(indexName, docDef.getEntityField(), entity.getId()).script(new Script(Script.DEFAULT_SCRIPT_TYPE, Script.DEFAULT_SCRIPT_LANG, deleteScript, params));
        try {
            client.update(updateRequest).get();
        } catch (Exception e) {
            if (ExceptionUtils.getRootCause(e) instanceof DocumentMissingException) {
                LOGGER.debug("Trying to delete values from a missing document. Ignoring deletion of entity: ", entity);
            } else {
                throw new TextIndexException("Unable to delete entity.", e);
            }
        }
    }
}
Also used : Script(org.elasticsearch.script.Script) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException)

Example 2 with DocumentMissingException

use of org.elasticsearch.index.engine.DocumentMissingException in project elasticsearch by elastic.

the class UpdateHelper method prepare.

/**
     * Prepares an update request by converting it into an index or delete request or an update response (no action).
     */
@SuppressWarnings("unchecked")
protected Result prepare(ShardId shardId, UpdateRequest request, final GetResult getResult, LongSupplier nowInMillis) {
    if (!getResult.isExists()) {
        if (request.upsertRequest() == null && !request.docAsUpsert()) {
            throw new DocumentMissingException(shardId, request.type(), request.id());
        }
        IndexRequest indexRequest = request.docAsUpsert() ? request.doc() : request.upsertRequest();
        if (request.scriptedUpsert() && request.script() != null) {
            // Run the script to perform the create logic
            IndexRequest upsert = request.upsertRequest();
            Map<String, Object> upsertDoc = upsert.sourceAsMap();
            Map<String, Object> ctx = new HashMap<>(2);
            // Tell the script that this is a create and not an update
            ctx.put("op", "create");
            ctx.put("_source", upsertDoc);
            ctx.put("_now", nowInMillis.getAsLong());
            ctx = executeScript(request.script, ctx);
            //Allow the script to abort the create by setting "op" to "none"
            String scriptOpChoice = (String) ctx.get("op");
            // (the default) or "none", meaning abort upsert
            if (!"create".equals(scriptOpChoice)) {
                if (!"none".equals(scriptOpChoice)) {
                    logger.warn("Used upsert operation [{}] for script [{}], doing nothing...", scriptOpChoice, request.script.getIdOrCode());
                }
                UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
                update.setGetResult(getResult);
                return new Result(update, DocWriteResponse.Result.NOOP, upsertDoc, XContentType.JSON);
            }
            indexRequest.source((Map) ctx.get("_source"));
        }
        indexRequest.index(request.index()).type(request.type()).id(request.id()).create(true).setRefreshPolicy(request.getRefreshPolicy()).routing(request.routing()).parent(request.parent()).waitForActiveShards(request.waitForActiveShards());
        if (request.versionType() != VersionType.INTERNAL) {
            // in all but the internal versioning mode, we want to create the new document using the given version.
            indexRequest.version(request.version()).versionType(request.versionType());
        }
        return new Result(indexRequest, DocWriteResponse.Result.CREATED, null, null);
    }
    long updateVersion = getResult.getVersion();
    if (request.versionType() != VersionType.INTERNAL) {
        assert request.versionType() == VersionType.FORCE;
        // remember, match_any is excluded by the conflict test
        updateVersion = request.version();
    }
    if (getResult.internalSourceRef() == null) {
        // no source, we can't do nothing, through a failure...
        throw new DocumentSourceMissingException(shardId, request.type(), request.id());
    }
    Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true);
    String operation = null;
    final Map<String, Object> updatedSourceAsMap;
    final XContentType updateSourceContentType = sourceAndContent.v1();
    String routing = getResult.getFields().containsKey(RoutingFieldMapper.NAME) ? getResult.field(RoutingFieldMapper.NAME).getValue().toString() : null;
    String parent = getResult.getFields().containsKey(ParentFieldMapper.NAME) ? getResult.field(ParentFieldMapper.NAME).getValue().toString() : null;
    if (request.script() == null && request.doc() != null) {
        IndexRequest indexRequest = request.doc();
        updatedSourceAsMap = sourceAndContent.v2();
        if (indexRequest.routing() != null) {
            routing = indexRequest.routing();
        }
        if (indexRequest.parent() != null) {
            parent = indexRequest.parent();
        }
        boolean noop = !XContentHelper.update(updatedSourceAsMap, indexRequest.sourceAsMap(), request.detectNoop());
        // cases where users repopulating multi-fields or adding synonyms, etc.
        if (request.detectNoop() && noop) {
            operation = "none";
        }
    } else {
        Map<String, Object> ctx = new HashMap<>(16);
        ctx.put("_index", getResult.getIndex());
        ctx.put("_type", getResult.getType());
        ctx.put("_id", getResult.getId());
        ctx.put("_version", getResult.getVersion());
        ctx.put("_routing", routing);
        ctx.put("_parent", parent);
        ctx.put("_source", sourceAndContent.v2());
        ctx.put("_now", nowInMillis.getAsLong());
        ctx = executeScript(request.script, ctx);
        operation = (String) ctx.get("op");
        updatedSourceAsMap = (Map<String, Object>) ctx.get("_source");
    }
    if (operation == null || "index".equals(operation)) {
        final IndexRequest indexRequest = Requests.indexRequest(request.index()).type(request.type()).id(request.id()).routing(routing).parent(parent).source(updatedSourceAsMap, updateSourceContentType).version(updateVersion).versionType(request.versionType()).waitForActiveShards(request.waitForActiveShards()).setRefreshPolicy(request.getRefreshPolicy());
        return new Result(indexRequest, DocWriteResponse.Result.UPDATED, updatedSourceAsMap, updateSourceContentType);
    } else if ("delete".equals(operation)) {
        DeleteRequest deleteRequest = Requests.deleteRequest(request.index()).type(request.type()).id(request.id()).routing(routing).parent(parent).version(updateVersion).versionType(request.versionType()).waitForActiveShards(request.waitForActiveShards()).setRefreshPolicy(request.getRefreshPolicy());
        return new Result(deleteRequest, DocWriteResponse.Result.DELETED, updatedSourceAsMap, updateSourceContentType);
    } else if ("none".equals(operation)) {
        UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
        update.setGetResult(extractGetResult(request, request.index(), getResult.getVersion(), updatedSourceAsMap, updateSourceContentType, getResult.internalSourceRef()));
        return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType);
    } else {
        logger.warn("Used update operation [{}] for script [{}], doing nothing...", operation, request.script.getIdOrCode());
        UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
        return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType);
    }
}
Also used : DocumentSourceMissingException(org.elasticsearch.index.engine.DocumentSourceMissingException) HashMap(java.util.HashMap) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetResult(org.elasticsearch.index.get.GetResult) XContentType(org.elasticsearch.common.xcontent.XContentType) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) HashMap(java.util.HashMap) Map(java.util.Map) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 3 with DocumentMissingException

use of org.elasticsearch.index.engine.DocumentMissingException in project elasticsearch by elastic.

the class UpdateIT method testUpdate.

public void testUpdate() throws Exception {
    createTestIndex();
    ensureGreen();
    try {
        client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).execute().actionGet();
        fail();
    } catch (DocumentMissingException e) {
    // all is well
    }
    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
    UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).execute().actionGet();
    assertThat(updateResponse.getVersion(), equalTo(2L));
    assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
    }
    Map<String, Object> params = new HashMap<>();
    params.put("inc", 3);
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "field_inc", "field", params)).execute().actionGet();
    assertThat(updateResponse.getVersion(), equalTo(3L));
    assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5"));
    }
    // check noop
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "put_values", "", Collections.singletonMap("_ctx", Collections.singletonMap("op", "none")))).execute().actionGet();
    assertThat(updateResponse.getVersion(), equalTo(3L));
    assertEquals(DocWriteResponse.Result.NOOP, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5"));
    }
    // check delete
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "put_values", "", Collections.singletonMap("_ctx", Collections.singletonMap("op", "delete")))).execute().actionGet();
    assertThat(updateResponse.getVersion(), equalTo(4L));
    assertEquals(DocWriteResponse.Result.DELETED, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.isExists(), equalTo(false));
    }
    // check fields parameter
    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).setFields("field").setFetchSource(true).execute().actionGet();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
    assertThat(updateResponse.getGetResult().field("field").getValue(), notNullValue());
    // check _source parameter
    client().prepareIndex("test", "type1", "1").setSource("field1", 1, "field2", 2).execute().actionGet();
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "field_inc", "field1", Collections.emptyMap())).setFetchSource("field1", "field2").get();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
    assertThat(updateResponse.getGetResult().field("field1"), nullValue());
    assertThat(updateResponse.getGetResult().sourceAsMap().size(), equalTo(1));
    assertThat(updateResponse.getGetResult().sourceAsMap().get("field1"), equalTo(2));
    // check updates without script
    // add new field
    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("field2", 2).endObject()).execute().actionGet();
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1"));
        assertThat(getResponse.getSourceAsMap().get("field2").toString(), equalTo("2"));
    }
    // change existing field
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("field", 3).endObject()).execute().actionGet();
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("3"));
        assertThat(getResponse.getSourceAsMap().get("field2").toString(), equalTo("2"));
    }
    // recursive map
    Map<String, Object> testMap = new HashMap<>();
    Map<String, Object> testMap2 = new HashMap<>();
    Map<String, Object> testMap3 = new HashMap<>();
    testMap3.put("commonkey", testMap);
    testMap3.put("map3", 5);
    testMap2.put("map2", 6);
    testMap.put("commonkey", testMap2);
    testMap.put("map1", 8);
    client().prepareIndex("test", "type1", "1").setSource("map", testMap).execute().actionGet();
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("map", testMap3).endObject()).execute().actionGet();
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        Map map1 = (Map) getResponse.getSourceAsMap().get("map");
        assertThat(map1.size(), equalTo(3));
        assertThat(map1.containsKey("map1"), equalTo(true));
        assertThat(map1.containsKey("map3"), equalTo(true));
        assertThat(map1.containsKey("commonkey"), equalTo(true));
        Map map2 = (Map) map1.get("commonkey");
        assertThat(map2.size(), equalTo(3));
        assertThat(map2.containsKey("map1"), equalTo(true));
        assertThat(map2.containsKey("map2"), equalTo(true));
        assertThat(map2.containsKey("commonkey"), equalTo(true));
    }
}
Also used : SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) HashMap(java.util.HashMap) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

DocumentMissingException (org.elasticsearch.index.engine.DocumentMissingException)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Script (org.elasticsearch.script.Script)2 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)1 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)1 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 DocumentSourceMissingException (org.elasticsearch.index.engine.DocumentSourceMissingException)1 GetResult (org.elasticsearch.index.get.GetResult)1 CompiledScript (org.elasticsearch.script.CompiledScript)1 ExecutableScript (org.elasticsearch.script.ExecutableScript)1 SearchScript (org.elasticsearch.script.SearchScript)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1