use of org.elasticsearch.action.update.UpdateRequest in project api-core by ca-cwds.
the class ElasticsearchDao method bulkAdd.
/**
* Prepare an index request for bulk operations.
*
* <p>
* Upsert: update or insert.
* </p>
*
* @param mapper Jackson ObjectMapper
* @param id ES document id
* @param obj document object
* @param alias index alias
* @param docType document type
* @param upsert update if document exists, else insert new
* @return prepared IndexRequest
* @throws JsonProcessingException if unable to serialize JSON
*/
public ActionRequest bulkAdd(final ObjectMapper mapper, final String id, final Object obj, final String alias, final String docType, boolean upsert) throws JsonProcessingException {
final String json = mapper.writeValueAsString(obj);
final IndexRequest idxReq = new IndexRequest(alias, docType, id).source(json, XContentType.JSON);
return upsert ? new UpdateRequest(alias, docType, id).doc(json, XContentType.JSON).upsert(idxReq) : idxReq;
}
use of org.elasticsearch.action.update.UpdateRequest in project incubator-sdap-mudrod by apache.
the class FeatureBasedSimilarity method normalizeVariableWeight.
public void normalizeVariableWeight(ESDriver es) {
es.createBulkProcessor();
double totalWeight = 0.0;
for (String variable : variableWeights.keySet()) {
totalWeight += variableWeights.get(variable);
}
SearchResponse scrollResp = es.getClient().prepareSearch(indexName).setTypes(variableSimType).setScroll(new TimeValue(60000)).setQuery(QueryBuilders.matchAllQuery()).setSize(100).execute().actionGet();
while (true) {
for (SearchHit hit : scrollResp.getHits().getHits()) {
Map<String, Object> similarities = hit.getSource();
double totalSim = 0.0;
for (String variable : variableWeights.keySet()) {
if (similarities.containsKey(variable + "_Sim")) {
double value = (double) similarities.get(variable + "_Sim");
double weight = variableWeights.get(variable);
totalSim += weight * value;
}
}
double weight = totalSim / totalWeight;
UpdateRequest ur = es.generateUpdateRequest(indexName, variableSimType, hit.getId(), "weight", weight);
es.getBulkProcessor().add(ur);
}
scrollResp = es.getClient().prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
if (scrollResp.getHits().getHits().length == 0) {
break;
}
}
es.destroyBulkProcessor();
}
use of org.elasticsearch.action.update.UpdateRequest in project hazelcast by hazelcast.
the class CommonElasticSinksTest method given_documentNotInIndex_whenWriteToElasticSinkUpdateRequest_then_jobShouldFail.
/**
* Regression test for checking that behavior was not unexpectedly changed.
* It is possible that behavior will be changed in any of future version
* since failing job based on unsuccessful delete/update leads to problems
* when job are restarted.
*/
@Test
public void given_documentNotInIndex_whenWriteToElasticSinkUpdateRequest_then_jobShouldFail() throws Exception {
elasticClient.indices().create(new CreateIndexRequest("my-index"), RequestOptions.DEFAULT);
Sink<TestItem> elasticSink = new ElasticSinkBuilder<>().clientFn(elasticClientSupplier()).mapToRequestFn((TestItem item) -> new UpdateRequest("my-index", item.getId()).doc(item.asMap())).retries(0).build();
Pipeline p = Pipeline.create();
p.readFrom(TestSources.items(new TestItem("notExist", "Frantisek"))).writeTo(elasticSink);
assertThatThrownBy(() -> submitJob(p)).hasRootCauseInstanceOf(JetException.class).hasStackTraceContaining("document missing");
}
use of org.elasticsearch.action.update.UpdateRequest in project hazelcast by hazelcast.
the class CommonElasticSinksTest method given_documentInIndex_whenWriteToElasticSinkUpdateRequest_then_documentsInIndexUpdated.
@Test
public void given_documentInIndex_whenWriteToElasticSinkUpdateRequest_then_documentsInIndexUpdated() throws Exception {
Map<String, Object> doc = new HashMap<>();
doc.put("name", "Fra");
String id = indexDocument("my-index", doc);
Sink<TestItem> elasticSink = ElasticSinks.elastic(elasticClientSupplier(), item -> new UpdateRequest("my-index", item.getId()).doc(item.asMap()));
Pipeline p = Pipeline.create();
p.readFrom(TestSources.items(new TestItem(id, "Frantisek"))).writeTo(elasticSink);
submitJob(p);
refreshIndex();
assertSingleDocument(id, "Frantisek");
}
use of org.elasticsearch.action.update.UpdateRequest in project elasticsearch-jdbc by jprante.
the class StandardSink method update.
@Override
public void update(IndexableObject object) throws IOException {
if (clientAPI == null) {
return;
}
if (Strings.hasLength(object.index())) {
this.index = object.index();
}
if (Strings.hasLength(object.type())) {
this.type = object.type();
}
if (Strings.hasLength(object.id())) {
setId(object.id());
}
if (getId() == null) {
// skip if no doc is specified to delete
return;
}
UpdateRequest request = new UpdateRequest().index(this.index).type(this.type).id(getId()).doc(object.source());
request.docAsUpsert(true);
if (object.meta(ControlKeys._version.name()) != null) {
request.versionType(VersionType.EXTERNAL).version(Long.parseLong(object.meta(ControlKeys._version.name())));
}
if (object.meta(ControlKeys._routing.name()) != null) {
request.routing(object.meta(ControlKeys._routing.name()));
}
if (object.meta(ControlKeys._parent.name()) != null) {
request.parent(object.meta(ControlKeys._parent.name()));
}
if (logger.isTraceEnabled()) {
logger.trace("adding bulk update action {}/{}/{}", request.index(), request.type(), request.id());
}
clientAPI.bulkUpdate(request);
}
Aggregations