本文整理汇总了Java中org.elasticsearch.action.bulk.BulkRequestBuilder.execute方法的典型用法代码示例。如果您正苦于以下问题:Java BulkRequestBuilder.execute方法的具体用法?Java BulkRequestBuilder.execute怎么用?Java BulkRequestBuilder.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.action.bulk.BulkRequestBuilder
的用法示例。
在下文中一共展示了BulkRequestBuilder.execute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: indexAll
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
@Override
public <P extends ParaObject> void indexAll(String appid, List<P> objects) {
if (StringUtils.isBlank(appid) || objects == null || objects.isEmpty()) {
return;
}
BulkRequestBuilder brb = client().prepareBulk();
for (ParaObject po : objects) {
brb.add(client().prepareIndex(getIndexName(appid), getType(), po.getId()).
setSource(ElasticSearchUtils.getSourceFromParaObject(po)));
}
if (brb.numberOfActions() > 0) {
ActionListener<BulkResponse> responseHandler = ElasticSearchUtils.getBulkIndexResponseHandler();
if (isAsyncEnabled()) {
brb.execute(responseHandler);
} else {
responseHandler.onResponse(brb.execute().actionGet());
}
}
logger.debug("Search.indexAll() {}", objects.size());
}
示例2: unindexAll
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
@Override
public <P extends ParaObject> void unindexAll(String appid, List<P> objects) {
if (StringUtils.isBlank(appid) || objects == null || objects.isEmpty()) {
return;
}
BulkRequestBuilder brb = client().prepareBulk();
for (ParaObject po : objects) {
brb.add(client().prepareDelete(getIndexName(appid), getType(), po.getId()));
}
if (brb.numberOfActions() > 0) {
ActionListener<BulkResponse> responseHandler = ElasticSearchUtils.getBulkIndexResponseHandler();
if (isAsyncEnabled()) {
brb.execute(responseHandler);
} else {
responseHandler.onResponse(brb.execute().actionGet());
}
}
logger.debug("Search.unindexAll() {}", objects.size());
}
示例3: createMapping
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
/**
* Create a mapping over an index
*
* @param indexName
* @param mappingName
* @param mappingSource the data that has to be inserted in the mapping.
*/
public void createMapping(String indexName, String mappingName, ArrayList<XContentBuilder> mappingSource) {
IndicesExistsResponse existsResponse = this.client.admin().indices().prepareExists(indexName).execute()
.actionGet();
//If the index does not exists, it will be created without options
if (!existsResponse.isExists()) {
if (!createSingleIndex(indexName)) {
throw new ElasticsearchException("Failed to create " + indexName
+ " index.");
}
}
BulkRequestBuilder bulkRequest = this.client.prepareBulk();
for (int i = 0; i < mappingSource.size(); i++) {
int aux = i + 1;
IndexRequestBuilder res = this.client
.prepareIndex(indexName, mappingName, String.valueOf(aux)).setSource(mappingSource.get(i));
bulkRequest.add(res);
}
bulkRequest.execute();
}
示例4: deleteDocuments
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
@Override
public void deleteDocuments(
SearchContext searchContext, Collection<String> uids)
throws SearchException {
try {
Client client = getClient();
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
for (String uid : uids) {
DeleteRequestBuilder deleteRequestBuilder =
client.prepareDelete(
Utilities.getIndexName(searchContext),
"documents", uid);
bulkRequestBuilder.add(deleteRequestBuilder);
}
Future<BulkResponse> future = bulkRequestBuilder.execute();
BulkResponse bulkResponse = future.get();
if(_log.isDebugEnabled())
{
if(bulkResponse.hasFailures())
{
_log.debug("Errors while doing bulk delete with errors " + bulkResponse.buildFailureMessage() );
}
}
} catch (Exception e) {
throw new SearchException("Unable to delete documents " + uids, e);
}
}
示例5: executeBulkRequest
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
private void executeBulkRequest(final StreamInput streamInput, final BulkRequestBuilder builder) {
builder.execute(wrap(res -> {
processRequests(streamInput);
// continue
}, e -> {
if (senderRequestRetryCount >= 0) {
if (requestErrorCount > senderRequestRetryCount) {
logger.error("[Sender][" + index + "][" + requestErrorCount + "] Failed to process the bulk request.", e);
requestErrorCount = 0;
writeError(requestPosition, builder.request(), wrap(r -> processRequests(streamInput), ex -> {
logger.warn("Failed to store an error request.", ex);
processRequests(streamInput);
}));
} else {
if (logger.isDebugEnabled()) {
logger.debug("[Sender][" + index + "][" + requestErrorCount + "] Failed to process the bulk request.", e);
}
requestErrorCount++;
executeBulkRequest(streamInput, builder);
}
} else {
IOUtils.closeQuietly(streamInput);
retryWithError("Failed to process (" + builder.request() + ")", e);
// retry
}
}));
}
示例6: CopyIndex
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
public void CopyIndex(Client sourceclient, String sourceindex, Client targetclient, String targetindex) {
// TODO Auto-generated method stub
count = 0;
QueryBuilder qb = QueryBuilders.matchAllQuery();
try {
SearchResponse scrollResp = sourceclient.prepareSearch(sourceindex)
.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC).setScroll(new TimeValue(60000))
.setQuery(qb).setSize(500).execute().actionGet();
while (true) {
final BulkRequestBuilder bulkRequest = targetclient.prepareBulk();
for (SearchHit hit : scrollResp.getHits().getHits()) {
bulkRequest.add(targetclient.prepareIndex(targetindex, hit.getType().toString(), hit.getId())
.setSource(hit.getSourceAsString()));
count++;
}
bulkRequest.execute();
scrollResp = sourceclient.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000))
.execute().actionGet();
logger.info(count + " documents putted!!");
if (scrollResp.getHits().getHits().length == 0) {
break;
}
}
logger.info("copy index " + sourceindex + " " + count + " documents" + " to " + targetindex + " complete");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
示例7: CopyIndexByQueryDsl
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
public void CopyIndexByQueryDsl(Client sourceclient, String sourceindex, Client targetclient, String targetindex,
String DSL) {
count = 0;
// 单线程scroll,bulk写入
WrapperQueryBuilder wrapper = new WrapperQueryBuilder(DSL);
try {
SearchResponse scrollResp = sourceclient.prepareSearch(sourceindex)
.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC).setScroll(new TimeValue(60000))
.setQuery(wrapper).setSize(500).execute().actionGet();
while (true) {
final BulkRequestBuilder bulkRequest = targetclient.prepareBulk();
for (SearchHit hit : scrollResp.getHits().getHits()) {
bulkRequest.add(targetclient.prepareIndex(targetindex, hit.getType().toString(), hit.getId())
.setSource(hit.getSourceAsString()));
count++;
}
bulkRequest.execute();
scrollResp = sourceclient.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000))
.execute().actionGet();
logger.info(count + " documents putted!!");
if (scrollResp.getHits().getHits().length == 0) {
break;
}
}
logger.info("copy index " + sourceindex + " " + count + " documents" + " to " + targetindex + " complete");
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: CopyIndex
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
public void CopyIndex(Client sourceclient, String sourceindex, Client targetclient, String targetindex) {
// TODO Auto-generated method stub
count = 0;
QueryBuilder qb = QueryBuilders.matchAllQuery();
try {
SearchResponse scrollResp = sourceclient.prepareSearch(sourceindex)
.addSort(SortParseElement.DOC_FIELD_NAME, SortOrder.ASC).setScroll(new TimeValue(60000))
.setQuery(qb).setSize(500).execute().actionGet();
while (true) {
final BulkRequestBuilder bulkRequest = targetclient.prepareBulk();
for (SearchHit hit : scrollResp.getHits().getHits()) {
bulkRequest.add(targetclient.prepareIndex(targetindex, hit.getType().toString(), hit.getId())
.setSource(hit.getSourceAsString()));
count++;
}
bulkRequest.execute();
scrollResp = sourceclient.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000))
.execute().actionGet();
logger.info(count + " documents putted!!");
if (scrollResp.getHits().getHits().length == 0) {
break;
}
}
logger.info("copy index " + sourceindex + " " + count + " documents" + " to " + targetindex + " complete");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
示例9: CopyIndexByQueryDsl
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
public void CopyIndexByQueryDsl(Client sourceclient, String sourceindex, Client targetclient, String targetindex,
String DSL) {
count = 0;
// 单线程scroll,bulk写入
WrapperQueryBuilder wrapper = new WrapperQueryBuilder(DSL);
try {
SearchResponse scrollResp = sourceclient.prepareSearch(sourceindex)
.addSort(SortParseElement.DOC_FIELD_NAME, SortOrder.ASC).setScroll(new TimeValue(60000))
.setQuery(wrapper).setSize(500).execute().actionGet();
while (true) {
final BulkRequestBuilder bulkRequest = targetclient.prepareBulk();
for (SearchHit hit : scrollResp.getHits().getHits()) {
bulkRequest.add(targetclient.prepareIndex(targetindex, hit.getType().toString(), hit.getId())
.setSource(hit.getSourceAsString()));
count++;
}
bulkRequest.execute();
scrollResp = sourceclient.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000))
.execute().actionGet();
logger.info(count + " documents putted!!");
if (scrollResp.getHits().getHits().length == 0) {
break;
}
}
logger.info("copy index " + sourceindex + " " + count + " documents" + " to " + targetindex + " complete");
} catch (Exception e) {
e.printStackTrace();
}
}
示例10: executeBulk
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
/**
* Executes the bulk request
* @param bulk
*/
private static void executeBulk(BulkRequestBuilder bulk) {
log.info("Executing bulk...");
ListenableActionFuture<BulkResponse> futures = bulk.execute();
BulkResponse response = futures.actionGet();
log.info("Response: " + response.getTookInMillis());
}
示例11: sendToLocalCluster
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
private void sendToLocalCluster(final String scrollId, final SearchHit[] hits) {
// prepare bulk request
final BulkRequestBuilder bulkRequest = client.prepareBulk();
for (final SearchHit hit : hits) {
IndexRequestBuilder builder = client.prepareIndex(toIndex,
toType != null ? toType : hit.getType(), hit.getId())
.setSource(hit.getSource());
Map<String, SearchHitField> fields = hit.getFields();
if (fields != null && fields.containsKey("_parent")) {
SearchHitField parentField = fields.get("_parent");
if (parentField != null) {
String parentId = parentField.getValue();
builder.setParent(parentId);
}
}
bulkRequest.add(builder);
}
// send bulk request, if success response got, searching the next 10 results using scroll_id
// using this listener (inner class) to listen to results
bulkRequest.execute(new ActionListener<BulkResponse>() {
@Override
public void onResponse(final BulkResponse bulkResponse) {
if (bulkResponse.hasFailures()) {
throw new ReindexingException(bulkResponse
.buildFailureMessage());
}
client.prepareSearchScroll(scrollId).setScroll(scroll)
.execute(ReindexingListener.this);
}
@Override
public void onFailure(final Throwable e) {
ReindexingListener.this.onFailure(e);
}
});
}
示例12: flush
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
protected synchronized void flush() {
if (resultQueue.isEmpty()) {
return;
}
final Queue<Map<String, Object>> currentQueue = resultQueue;
resultQueue = new ConcurrentLinkedQueue<>();
final BulkRequestBuilder bulkRequest = client.prepareBulk();
for (final Map<String, Object> obj : currentQueue) {
bulkRequest.add(client.prepareIndex(index, type).setSource(obj));
}
bulkRequest.execute(new ActionListener<BulkResponse>() {
@Override
public void onResponse(final BulkResponse response) {
if (response.hasFailures()) {
logger.error("Failed to write a result on {}/{}: {}",
index, type, response.buildFailureMessage());
} else {
logger.info("Wrote {} results in {}/{}.",
currentQueue.size(), index, type);
}
}
@Override
public void onFailure(final Throwable e) {
logger.error("Failed to write a result on {}/{}.", e, index,
type);
}
});
}
示例13: sendBulkInserts
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
private void sendBulkInserts() {
if (docs.isEmpty()) { return; }
BulkRequestBuilder bulkRequestBuilder = null;
try {
synchronized (esClient) {
TransportClient client = getClient();
bulkRequestBuilder = client.prepareBulk();
Iterator<EsDocument> iter = docs.iterator();
while (iter.hasNext()) {
EsDocument doc = iter.next();
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(doc.index,doc.type,doc.id).setSource(doc.doc);
bulkRequestBuilder.add(indexRequestBuilder);
}
}
ListenableActionFuture<BulkResponse> laf = bulkRequestBuilder.execute();
if (sync) {
laf.actionGet();
}
} catch (EsServerException e) {
e.printStackTrace();
}
docs = new ArrayList<EsDocument>();
bulkInsertCount = 0;
bulkInsertSize = 0;
}
示例14: writeToNoSQL
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
@Override
public void writeToNoSQL(List<Map<String, Object>> entityList) {
BulkRequestBuilder bulkRequest = client.prepareBulk();
JSONArray array = JSONArray.fromObject(entityList);
for (int i = 0; i < array.size(); i++) {
IndexRequestBuilder builder = client.prepareIndex(index_name, index_type);
if (getPrimaryKey() != null) {
builder.setId(((JSONObject) array.get(i)).getString(getPrimaryKey()));
}
builder.setSource(array.get(i).toString());
bulkRequest.add(builder);
}
if (bulkRequest.numberOfActions() > 0) {
long t1 = System.currentTimeMillis();
ListenableActionFuture<BulkResponse> action = bulkRequest.execute();
long t2 = System.currentTimeMillis();
BulkResponse response = action.actionGet();
for (Iterator<BulkItemResponse> iterator = response.iterator(); iterator.hasNext();) {
BulkItemResponse e = (BulkItemResponse) iterator.next();
if (e.isFailed()) {
throw new FailedCommunicationException("Insertion to ES failed.");
}
}
log.info("Time taken to Write " + bulkRequest.numberOfActions() + " documents to ES :" + ((t2 - t1)) + " ms");
}
}
示例15: excuteBulkRequestAsync
import org.elasticsearch.action.bulk.BulkRequestBuilder; //导入方法依赖的package包/类
/**
* Excute bulk request async.
*
* @param bulkRequestBuilder
* the bulk request builder
* @param bulkResponseActionListener
* the bulk response action listener
*/
public void excuteBulkRequestAsync(BulkRequestBuilder bulkRequestBuilder,
ActionListener<BulkResponse> bulkResponseActionListener) {
JMLog.info(log, "excuteBulkRequestAsync", bulkRequestBuilder,
bulkResponseActionListener);
bulkRequestBuilder.execute(bulkResponseActionListener);
}