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