Java Code Examples for org.elasticsearch.action.DocWriteResponse.Result#UPDATED

The following examples show how to use org.elasticsearch.action.DocWriteResponse.Result#UPDATED . 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: 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);
    }
}
 
Example 2
Source File: FessEsClient.java    From fess with Apache License 2.0 5 votes vote down vote up
public boolean update(final String index, final String id, final String field, final Object value) {
    try {
        final Result result =
                client.prepareUpdate().setIndex(index).setId(id).setDoc(field, value).execute()
                        .actionGet(ComponentUtil.getFessConfig().getIndexIndexTimeout()).getResult();
        return result == Result.CREATED || result == Result.UPDATED;
    } catch (final ElasticsearchException e) {
        throw new FessEsClientException("Failed to set " + value + " to " + field + " for doc " + id, e);
    }
}
 
Example 3
Source File: FessEsClient.java    From fess with Apache License 2.0 5 votes vote down vote up
public boolean store(final String index, final Object obj) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    @SuppressWarnings("unchecked")
    final Map<String, Object> source = obj instanceof Map ? (Map<String, Object>) obj : BeanUtil.copyBeanToNewMap(obj);
    final String id = (String) source.remove(fessConfig.getIndexFieldId());
    source.remove(fessConfig.getIndexFieldVersion());
    final Number seqNo = (Number) source.remove(fessConfig.getIndexFieldSeqNo());
    final Number primaryTerm = (Number) source.remove(fessConfig.getIndexFieldPrimaryTerm());
    IndexResponse response;
    try {
        if (id == null) {
            // TODO throw Exception in next release
            // create
            response =
                    client.prepareIndex().setIndex(index).setSource(new DocMap(source)).setRefreshPolicy(RefreshPolicy.IMMEDIATE)
                            .setOpType(OpType.CREATE).execute().actionGet(fessConfig.getIndexIndexTimeout());
        } else {
            // create or update
            final IndexRequestBuilder builder =
                    client.prepareIndex().setIndex(index).setId(id).setSource(new DocMap(source))
                            .setRefreshPolicy(RefreshPolicy.IMMEDIATE).setOpType(OpType.INDEX);
            if (seqNo != null) {
                builder.setIfSeqNo(seqNo.longValue());
            }
            if (primaryTerm != null) {
                builder.setIfPrimaryTerm(primaryTerm.longValue());
            }
            response = builder.execute().actionGet(fessConfig.getIndexIndexTimeout());
        }
        final Result result = response.getResult();
        return result == Result.CREATED || result == Result.UPDATED;
    } catch (final ElasticsearchException e) {
        throw new FessEsClientException("Failed to store: " + obj, e);
    }
}