Search in sources :

Example 21 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project jena by apache.

the class TextIndexES method addEntity.

/**
     * Add an Entity to the ElasticSearch Index.
     * The entity will be added as a new document in ES, if it does not already exists.
     * If the Entity exists, then the entity will simply be updated.
     * The entity will never be replaced.
     * @param entity the entity to add
     */
@Override
public void addEntity(Entity entity) {
    LOGGER.debug("Adding/Updating the entity in ES");
    //The field that has a not null value in the current Entity instance.
    //Required, mainly for building a script for the update command.
    String fieldToAdd = null;
    String fieldValueToAdd = null;
    try {
        XContentBuilder builder = jsonBuilder().startObject();
        for (String field : docDef.fields()) {
            if (entity.get(field) != null) {
                if (entity.getLanguage() != null && !entity.getLanguage().isEmpty()) {
                    //We make sure that the field name contains all underscore and no dash (for eg. when the lang value is en-GB)
                    //The reason to do this is because the script fails with exception in case we have "-" in field name.
                    fieldToAdd = normalizeFieldName(field, entity.getLanguage());
                } else {
                    fieldToAdd = field;
                }
                fieldValueToAdd = (String) entity.get(field);
                builder = builder.field(fieldToAdd, Arrays.asList(fieldValueToAdd));
                break;
            } else {
                //We are making sure that the field is at-least added to the index.
                //This will help us tremendously when we are appending the data later in an already indexed document.
                builder = builder.field(field, Collections.emptyList());
            }
        }
        builder = builder.endObject();
        IndexRequest indexRequest = new IndexRequest(indexName, docDef.getEntityField(), entity.getId()).source(builder);
        String addUpdateScript = ADD_UPDATE_SCRIPT.replaceAll("<fieldName>", fieldToAdd);
        Map<String, Object> params = new HashMap<>();
        params.put("fieldValue", fieldValueToAdd);
        UpdateRequest upReq = new UpdateRequest(indexName, docDef.getEntityField(), entity.getId()).script(new Script(Script.DEFAULT_SCRIPT_TYPE, Script.DEFAULT_SCRIPT_LANG, addUpdateScript, params)).upsert(indexRequest);
        UpdateResponse response = client.update(upReq).get();
        LOGGER.debug("Received the following Update response : " + response + " for the following entity: " + entity);
    } catch (Exception e) {
        throw new TextIndexException("Unable to Index the Entity in ElasticSearch.", e);
    }
}
Also used : Script(org.elasticsearch.script.Script) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException)

Example 22 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest 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 23 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project incubator-sdap-mudrod by apache.

the class SessionGenerator method update.

/**
 * Method to update a Elasticsearch record/document by id, field, and value
 *
 * @param es
 * @param index  index name is Elasticsearch
 * @param type   type name
 * @param id     ID of the document that needs to be updated
 * @param field1 field of the document that needs to be updated
 * @param value1 value of the document that needs to be changed to
 * @throws ElasticsearchException
 * @throws IOException
 */
private void update(ESDriver es, String index, String type, String id, String field1, Object value1) throws IOException {
    UpdateRequest ur = new UpdateRequest(index, type, id).doc(jsonBuilder().startObject().field(field1, value1).endObject());
    es.getBulkProcessor().add(ur);
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest)

Example 24 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project incubator-sdap-mudrod by apache.

the class ESDriver method generateUpdateRequest.

public UpdateRequest generateUpdateRequest(String index, String type, String id, Map<String, Object> filedValueMap) {
    UpdateRequest ur = null;
    try {
        XContentBuilder builder = jsonBuilder().startObject();
        for (Entry<String, Object> entry : filedValueMap.entrySet()) {
            String key = entry.getKey();
            builder.field(key, filedValueMap.get(key));
        }
        builder.endObject();
        ur = new UpdateRequest(index, type, id).doc(builder);
    } catch (IOException e) {
        LOG.error("Error whilst attempting to generate a new Update Request.", e);
    }
    return ur;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 25 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project elasticsearch-indexing-proxy by codelibs.

the class ProxyActionFilter method getExecutor.

@SuppressWarnings("unchecked")
private <Request extends ActionRequest, Response extends ActionResponse> Supplier<Response> getExecutor(final Task task, final String action, final Request request) {
    if (BulkAction.NAME.equals(action)) {
        final long startTime = System.nanoTime();
        int count = 0;
        final BulkRequest req = (BulkRequest) request;
        for (final DocWriteRequest<?> subReq : req.requests()) {
            if (indexingProxyService.isTargetIndex(subReq.index())) {
                count++;
            }
        }
        if (count == 0) {
            return null;
        } else if (count != req.requests().size()) {
            throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.requests().size());
        }
        return () -> {
            final List<BulkItemResponse> responseList = new ArrayList<>(req.requests().size());
            for (int i = 0; i < req.requests().size(); i++) {
                final DocWriteRequest<?> dwr = req.requests().get(i);
                if (dwr instanceof IndexRequest) {
                    final IndexRequest r = (IndexRequest) dwr;
                    final String id = r.id() == null ? INDEX_UUID : r.id();
                    final IndexResponse response = new IndexResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), true);
                    responseList.add(new BulkItemResponse(i, r.opType(), response));
                } else if (dwr instanceof UpdateRequest) {
                    final UpdateRequest r = (UpdateRequest) dwr;
                    final String id = r.id() == null ? INDEX_UUID : r.id();
                    final UpdateResponse response = new UpdateResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), Result.CREATED);
                    responseList.add(new BulkItemResponse(i, r.opType(), response));
                } else if (dwr instanceof DeleteRequest) {
                    final DeleteRequest r = (DeleteRequest) dwr;
                    final String id = r.id() == null ? INDEX_UUID : r.id();
                    final DeleteResponse response = new DeleteResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), true);
                    response.setShardInfo(new ReplicationResponse.ShardInfo(1, 1, ReplicationResponse.EMPTY));
                    responseList.add(new BulkItemResponse(i, r.opType(), response));
                } else {
                    responseList.add(new BulkItemResponse(i, dwr.opType(), new BulkItemResponse.Failure(dwr.index(), dwr.type(), dwr.id(), new ElasticsearchException("Unknown request: " + dwr))));
                }
            }
            return (Response) new BulkResponse(responseList.toArray(new BulkItemResponse[responseList.size()]), (System.nanoTime() - startTime) / 1000000);
        };
    } else if (DeleteAction.NAME.equals(action)) {
        final DeleteRequest req = (DeleteRequest) request;
        if (!indexingProxyService.isTargetIndex(req.index())) {
            return null;
        }
        return () -> {
            final String id = req.id() == null ? INDEX_UUID : req.id();
            final DeleteResponse res = new DeleteResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), true);
            res.setShardInfo(new ReplicationResponse.ShardInfo(1, 1, ReplicationResponse.EMPTY));
            return (Response) res;
        };
    } else if (DeleteByQueryAction.NAME.equals(action)) {
        final long startTime = System.nanoTime();
        int count = 0;
        final DeleteByQueryRequest req = (DeleteByQueryRequest) request;
        for (final String index : req.indices()) {
            if (indexingProxyService.isTargetIndex(index)) {
                count++;
            }
        }
        if (count == 0) {
            return null;
        } else if (count != req.indices().length) {
            throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.indices().length);
        }
        return () -> {
            return (Response) new BulkByScrollResponse(TimeValue.timeValueNanos(System.nanoTime() - startTime), new BulkByScrollTask.Status(null, 0, 0, 0, 0, 0, 0, 0, 0, 0, TimeValue.ZERO, 0, null, TimeValue.ZERO), Collections.emptyList(), Collections.emptyList(), false);
        };
    } else if (IndexAction.NAME.equals(action)) {
        final IndexRequest req = (IndexRequest) request;
        if (!indexingProxyService.isTargetIndex(req.index())) {
            return null;
        }
        return () -> {
            final String id = req.id() == null ? INDEX_UUID : req.id();
            return (Response) new IndexResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), true);
        };
    } else if (UpdateAction.NAME.equals(action)) {
        final UpdateRequest req = (UpdateRequest) request;
        if (!indexingProxyService.isTargetIndex(req.index())) {
            return null;
        }
        return () -> {
            final String id = req.id() == null ? INDEX_UUID : req.id();
            return (Response) new UpdateResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), Result.CREATED);
        };
    } else if (UpdateByQueryAction.NAME.equals(action)) {
        final long startTime = System.nanoTime();
        int count = 0;
        final UpdateByQueryRequest req = (UpdateByQueryRequest) request;
        for (final String index : req.indices()) {
            if (indexingProxyService.isTargetIndex(index)) {
                count++;
            }
        }
        if (count == 0) {
            return null;
        } else if (count != req.indices().length) {
            throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.indices().length);
        }
        return () -> {
            return (Response) new BulkByScrollResponse(TimeValue.timeValueNanos(System.nanoTime() - startTime), new BulkByScrollTask.Status(null, 0, 0, 0, 0, 0, 0, 0, 0, 0, TimeValue.ZERO, 0, null, TimeValue.ZERO), Collections.emptyList(), Collections.emptyList(), false);
        };
    }
    return null;
}
Also used : Index(org.elasticsearch.index.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) ShardId(org.elasticsearch.index.shard.ShardId) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) ArrayList(java.util.ArrayList) List(java.util.List) BulkByScrollTask(org.elasticsearch.index.reindex.BulkByScrollTask) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) ActionResponse(org.elasticsearch.action.ActionResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Aggregations

UpdateRequest (org.elasticsearch.action.update.UpdateRequest)56 IndexRequest (org.elasticsearch.action.index.IndexRequest)32 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)25 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)14 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)11 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)8 GetRequest (org.elasticsearch.action.get.GetRequest)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Script (org.elasticsearch.script.Script)6 HashMap (java.util.HashMap)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 XContentType (org.elasticsearch.common.xcontent.XContentType)5 VersionType (org.elasticsearch.index.VersionType)5 HttpEntity (org.apache.http.HttpEntity)4 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)4 TimeValue (org.elasticsearch.common.unit.TimeValue)4