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);
}
}
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);
}
}
}
}
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);
}
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;
}
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;
}
Aggregations