Search in sources :

Example 26 with UpdateRequest

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

the class RequestUtils method createBulkRequest.

public static BulkRequestBuilder createBulkRequest(final Client client, final StreamInput streamInput, final String index) throws IOException {
    final BulkRequestBuilder builder = client.prepareBulk();
    final BulkRequest request = builder.request();
    request.readFrom(streamInput);
    if (index != null) {
        request.requests().stream().forEach(req -> {
            if (req instanceof DeleteRequest) {
                ((DeleteRequest) req).index(index);
            } else if (req instanceof DeleteByQueryRequest) {
                ((DeleteByQueryRequest) req).indices(index);
            } else if (req instanceof IndexRequest) {
                ((IndexRequest) req).index(index);
            } else if (req instanceof UpdateRequest) {
                ((UpdateRequest) req).index(index);
            } else if (req instanceof UpdateByQueryRequest) {
                ((UpdateByQueryRequest) req).indices(index);
            } else {
                throw new ElasticsearchException("Unsupported request in bulk: " + req);
            }
        });
    }
    return builder;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest)

Example 27 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project vertexium by visallo.

the class Elasticsearch5SearchIndex method bulkUpdate.

private void bulkUpdate(Graph graph, Iterable<UpdateRequest> updateRequests) {
    int totalCount = 0;
    Map<String, BulkRequest> bulkRequests = new HashMap<>();
    for (UpdateRequest updateRequest : updateRequests) {
        BulkRequest bulkRequest = bulkRequests.computeIfAbsent(updateRequest.index(), k -> new BulkRequest());
        bulkRequest.add(updateRequest);
        if (bulkRequest.numberOfActions() >= MAX_BATCH_COUNT || bulkRequest.estimatedSizeInBytes() > MAX_BATCH_SIZE) {
            LOGGER.debug("adding elements... %d (est size %d)", bulkRequest.numberOfActions(), bulkRequest.estimatedSizeInBytes());
            totalCount += bulkRequest.numberOfActions();
            doBulkRequest(bulkRequest);
            bulkRequest = new BulkRequest();
            bulkRequests.put(updateRequest.index(), bulkRequest);
        }
    }
    for (BulkRequest bulkRequest : bulkRequests.values()) {
        if (bulkRequest.numberOfActions() > 0) {
            LOGGER.debug("adding elements... %d (est size %d)", bulkRequest.numberOfActions(), bulkRequest.estimatedSizeInBytes());
            totalCount += bulkRequest.numberOfActions();
            doBulkRequest(bulkRequest);
        }
    }
    LOGGER.debug("added %d elements", totalCount);
    if (getConfig().isAutoFlush()) {
        flush(graph);
    }
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest)

Example 28 with UpdateRequest

use of org.elasticsearch.action.update.UpdateRequest in project main by JohnPeng739.

the class ElasticAccessorRest method save.

/**
 * {@inheritDoc}
 *
 * @see ElasticAccessor#save(Base)
 */
@Override
public <T extends Base> T save(T t) throws UserInterfaceDalErrorException {
    try {
        Class<T> clazz = (Class<T>) t.getClass();
        boolean isNew;
        if (StringUtils.isBlank(t.getId())) {
            // 新数据
            t.setId(DigestUtils.uuid());
            t.setCreatedTime(System.currentTimeMillis());
            isNew = true;
        } else {
            T check = getById(t.getId(), clazz);
            if (check != null) {
                // 修改数据
                t.setCreatedTime(check.getCreatedTime());
                if (check instanceof BaseDict) {
                    ((BaseDict) t).setCode(((BaseDict) check).getCode());
                }
                isNew = false;
            } else {
                isNew = true;
            }
        }
        t.setUpdatedTime(System.currentTimeMillis());
        t.setOperator(sessionDataStore.getCurrentUserCode());
        if (isNew) {
            IndexRequest request = new IndexRequest(index, t.getClass().getName(), t.getId());
            request.source(JSON.toJSONString(t), XContentType.JSON);
            client.index(request);
        } else {
            UpdateRequest request = new UpdateRequest(index, t.getClass().getName(), t.getId());
            request.doc(JSON.toJSONString(t), XContentType.JSON);
            client.update(request);
        }
        return getById(t.getId(), (Class<T>) t.getClass());
    } catch (IOException ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Save the data into elastic fail.", ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
    }
}
Also used : BaseDict(org.mx.dal.entity.BaseDict) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) IOException(java.io.IOException) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 29 with UpdateRequest

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

the class NormalizeFeatures method normalizeMetadataVariables.

public void normalizeMetadataVariables(ESDriver es) {
    es.createBulkProcessor();
    SearchResponse scrollResp = es.getClient().prepareSearch(indexName).setTypes(metadataType).setScroll(new TimeValue(60000)).setQuery(QueryBuilders.matchAllQuery()).setSize(100).execute().actionGet();
    while (true) {
        for (SearchHit hit : scrollResp.getHits().getHits()) {
            Map<String, Object> metadata = hit.getSource();
            Map<String, Object> updatedValues = new HashMap<>();
            // !!!important change to other normalizer class when using other metadata
            MetadataFeature normalizer = new PODAACMetadataFeature();
            normalizer.normalizeMetadataVariables(metadata, updatedValues);
            UpdateRequest ur = es.generateUpdateRequest(indexName, metadataType, hit.getId(), updatedValues);
            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 : MetadataFeature(org.apache.sdap.mudrod.recommendation.structure.MetadataFeature) PODAACMetadataFeature(org.apache.sdap.mudrod.recommendation.structure.PODAACMetadataFeature) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) PODAACMetadataFeature(org.apache.sdap.mudrod.recommendation.structure.PODAACMetadataFeature) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 30 with UpdateRequest

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

the class LinkageTriple method standardTriples.

public static void standardTriples(ESDriver es, String index, String type) throws IOException {
    es.createBulkProcessor();
    SearchResponse sr = es.getClient().prepareSearch(index).setTypes(type).setQuery(QueryBuilders.matchAllQuery()).setSize(0).addAggregation(AggregationBuilders.terms("concepts").field("concept_A").size(0)).execute().actionGet();
    Terms concepts = sr.getAggregations().get("concepts");
    for (Terms.Bucket entry : concepts.getBuckets()) {
        String concept = (String) entry.getKey();
        double maxSim = LinkageTriple.getMaxSimilarity(es, index, type, concept);
        if (maxSim == 1.0) {
            continue;
        }
        SearchResponse scrollResp = es.getClient().prepareSearch(index).setTypes(type).setScroll(new TimeValue(60000)).setQuery(QueryBuilders.termQuery("concept_A", concept)).addSort("weight", SortOrder.DESC).setSize(100).execute().actionGet();
        while (true) {
            for (SearchHit hit : scrollResp.getHits().getHits()) {
                Map<String, Object> metadata = hit.getSource();
                double sim = (double) metadata.get("weight");
                double newSim = sim / maxSim;
                UpdateRequest ur = es.generateUpdateRequest(index, type, hit.getId(), "weight", Double.parseDouble(df.format(newSim)));
                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) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse)

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