Java Code Examples for org.elasticsearch.action.update.UpdateResponse#getResult()

The following examples show how to use org.elasticsearch.action.update.UpdateResponse#getResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticsearchTransactionManager.java    From jstarcraft-core with Apache License 2.0 7 votes vote down vote up
@Override
protected void unlock(TransactionDefinition definition) {
    // 尝试解锁
    String key = definition.getName();
    Long value = definition.getMost().toEpochMilli();
    Map<String, Object> document = new HashMap<>();
    document.put(NAME, key);
    document.put(MOST, value);
    document.put(NOW, Instant.now().toEpochMilli());
    UpdateRequest request = new UpdateRequest().index(index).type(type)

            .id(key)

            .script(new Script(ScriptType.INLINE, SCRIPT, UNLOCK_SCRIPT, document));
    try {
        UpdateResponse response = elastic.update(request, RequestOptions.DEFAULT);
        if (response.getResult() == DocWriteResponse.Result.NOOP) {
            throw new TransactionUnlockException();
        }
    } catch (Exception exception) {
        throw new TransactionUnlockException(exception);
    }
}
 
Example 2
Source File: ESNestedSearchService.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 更新操作
 *
 * @param esObject es通用更新请求参数
 * @return <code>true</code> 保存或更新成功; 否则更新失败
 */
public SearchBaseResult<Boolean> update(UpdateESObject esObject) {
    final UpdateRequestBuilder updateRequest = esObject.nestedUpdate() ? getNestedListUpdateRequest(esObject)
            : getUpdateRequest(esObject);
    SearchLogger.log(updateRequest);
    try {
        updateRequest.setDetectNoop(false);
        if (esObject.isRefresh()) {
            updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        }
        UpdateResponse updateResponse = updateRequest.execute().get();
        SearchLogger.log(updateResponse);
        final DocWriteResponse.Result result = updateResponse.getResult();
        return SearchBaseResult.success(DocWriteResponse.Result.UPDATED == result, Boolean.class);
    } catch (Exception ex) {
        SearchLogger.error("update", ex);
        final String message = ex.getMessage();
        if (message != null && message.contains("document missing")) {
            return SearchBaseResult.faild(ESErrorCode.DOC_NOT_EXIST_ERROR_CODE, "更新文档不存在");
        }
        return SearchBaseResult.faild(ESErrorCode.ELASTIC_ERROR_CODE, "esMsg:" + message);
    }
}
 
Example 3
Source File: ElasticsearchTransactionManager.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
protected void lock(TransactionDefinition definition) {
    // 尝试加锁
    String key = definition.getName();
    Long value = definition.getMost().toEpochMilli();
    Map<String, Object> document = new HashMap<>();
    document.put(NAME, key);
    document.put(MOST, value);
    document.put(NOW, Instant.now().toEpochMilli());
    UpdateRequest request = new UpdateRequest().index(index).type(type)

            .id(key)

            .script(new Script(ScriptType.INLINE, SCRIPT, LOCK_SCRIPT, document))

            .upsert(document)

            .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    try {
        UpdateResponse response = elastic.update(request, RequestOptions.DEFAULT);
        if (response.getResult() == DocWriteResponse.Result.NOOP) {
            throw new TransactionLockException();
        }
    } catch (Exception exception) {
        throw new TransactionLockException(exception);
    }

}
 
Example 4
Source File: CommonWebpageDAO.java    From Gather-Platform with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 更新网页
 *
 * @param webpage 网页
 * @return
 */
public boolean update(Webpage webpage) throws ExecutionException, InterruptedException {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index(INDEX_NAME);
    updateRequest.type(TYPE_NAME);
    updateRequest.id(webpage.getId());
    updateRequest.doc(gson.toJson(webpage));
    UpdateResponse response = client.update(updateRequest).get();
    return response.getResult() == UpdateResponse.Result.UPDATED;
}
 
Example 5
Source File: CommonWebpageDAO.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 更新网页
 *
 * @param webpage 网页
 * @return
 */
public boolean update(Webpage webpage) throws ExecutionException, InterruptedException {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index(INDEX_NAME);
    updateRequest.type(TYPE_NAME);
    updateRequest.id(webpage.getId());
    updateRequest.doc(gson.toJson(webpage));
    UpdateResponse response = client.update(updateRequest).get();
    return response.getResult() == UpdateResponse.Result.UPDATED;
}
 
Example 6
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean tagUntag(Project prj, String documentId, String rootDocument, Script untagScript) throws IOException {
    UpdateRequest update = new UpdateRequest(prj.getId(), esCfg.indexType, documentId).routing(rootDocument);
    update.script(untagScript);
    update.setRefreshPolicy(esCfg.refreshPolicy);
    UpdateResponse updateResponse = client.update(update);
    return updateResponse.status() == RestStatus.OK && updateResponse.getResult() == DocWriteResponse.Result.UPDATED;
}
 
Example 7
Source File: ElasticsearchLockProvider.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@Override
@NonNull
public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) {
    try {
        Map<String, Object> lockObject = lockObject(lockConfiguration.getName(),
            lockConfiguration.getLockAtMostUntil(),
            now());
        UpdateRequest ur = new UpdateRequest()
            .index(index)
            .type(type)
            .id(lockConfiguration.getName())
            .script(new Script(ScriptType.INLINE,
                "painless",
                UPDATE_SCRIPT,
                lockObject)
            )
            .upsert(lockObject)
            .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        UpdateResponse res = highLevelClient.update(ur, RequestOptions.DEFAULT);
        if (res.getResult() != DocWriteResponse.Result.NOOP) {
            return Optional.of(new ElasticsearchSimpleLock(lockConfiguration));
        } else {
            return Optional.empty();
        }
    } catch (IOException | ElasticsearchException e) {
        if (e instanceof ElasticsearchException && ((ElasticsearchException) e).status() == RestStatus.CONFLICT) {
            return Optional.empty();
        } else {
            throw new LockException("Unexpected exception occurred", e);
        }
    }
}
 
Example 8
Source File: SearchHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
public boolean update(final String id, final Consumer<UpdateRequestBuilder> builderLambda) {
    try {
        final FessConfig fessConfig = ComponentUtil.getFessConfig();
        final UpdateRequestBuilder builder =
                ComponentUtil.getFessEsClient().prepareUpdate().setIndex(fessConfig.getIndexDocumentUpdateIndex()).setId(id);
        builderLambda.accept(builder);
        final UpdateResponse response = builder.execute().actionGet(fessConfig.getIndexIndexTimeout());
        return response.getResult() == Result.CREATED || response.getResult() == Result.UPDATED;
    } catch (final ElasticsearchException e) {
        throw new FessEsClientException("Failed to update doc  " + id, e);
    }
}