Search in sources :

Example 46 with UpdateRequest

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;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 47 with UpdateRequest

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();
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 48 with UpdateRequest

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");
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) JetException(com.hazelcast.jet.JetException) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Test(org.junit.Test)

Example 49 with UpdateRequest

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");
}
Also used : HashMap(java.util.HashMap) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Test(org.junit.Test)

Example 50 with UpdateRequest

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);
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest)

Aggregations

UpdateRequest (org.elasticsearch.action.update.UpdateRequest)59 IndexRequest (org.elasticsearch.action.index.IndexRequest)33 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)26 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)15 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)12 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)9 HashMap (java.util.HashMap)8 GetRequest (org.elasticsearch.action.get.GetRequest)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Script (org.elasticsearch.script.Script)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 TimeValue (org.elasticsearch.common.unit.TimeValue)5 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)5 XContentType (org.elasticsearch.common.xcontent.XContentType)5 VersionType (org.elasticsearch.index.VersionType)5 HttpEntity (org.apache.http.HttpEntity)4