use of org.elasticsearch.action.index.IndexResponse in project graylog2-server by Graylog2.
the class MessagesES7IT method indexMessage.
@Override
protected boolean indexMessage(String index, Map<String, Object> source, String id) {
final IndexRequest indexRequest = new IndexRequest(index).source(source).id(id);
final IndexResponse result = this.elasticsearch.elasticsearchClient().execute((c, requestOptions) -> c.index(indexRequest, requestOptions));
return result.status().equals(RestStatus.CREATED);
}
use of org.elasticsearch.action.index.IndexResponse in project elasticsearch-opennlp-plugin by spinscale.
the class OpenNlpPluginIntegrationTest method testThatFacetingIsWorking.
@Test
public void testThatFacetingIsWorking() throws Exception {
putMapping("/test-mapping-analyzers.json");
String sampleText = copyToStringFromClasspath("/sample-text.txt");
IndexResponse indexResponse = indexElement(sampleText);
SearchResponse searchResponse = new SearchRequestBuilder(node.client()).setIndices(index).setTypes(type).setQuery(QueryBuilders.matchAllQuery()).addFacet(new TermsFacetBuilder("names").field("someFieldAnalyzed.name").order(TermsFacet.ComparatorType.TERM)).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), is(1L));
assertThat(searchResponse.getHits().getAt(0).id(), is(indexResponse.getId()));
TermsFacet termsFacet = searchResponse.getFacets().facet(TermsFacet.class, "names");
assertThat(termsFacet.getTotalCount(), is(2L));
assertThat(termsFacet.getEntries().get(0).getTerm().string(), is("Jack Nicholson"));
assertThat(termsFacet.getEntries().get(1).getTerm().string(), is("Kobe Bryant"));
}
use of org.elasticsearch.action.index.IndexResponse in project jstorm by alibaba.
the class TestIndexBolt method execute.
@Override
public void execute(Tuple tuple) {
String line = tuple.getString(0);
String[] dims = line.split("\t");
JsonObject object = new JsonObject();
object.addProperty("no", dims[1]);
object.addProperty("date", dims[2]);
IndexResponse indexResponse = client.prepareIndex("test", "test").setId(dims[0]).setSource(object.toString()).execute().actionGet();
collector.ack(tuple);
}
use of org.elasticsearch.action.index.IndexResponse in project xmall by Exrick.
the class ItemESMessageListener method onMessage.
@Override
public void onMessage(Message message) {
try {
// 从消息中取商品id
TextMessage textMessage = (TextMessage) message;
log.info("得到消息:" + textMessage.getText());
String[] text = textMessage.getText().split(",");
Long itemId = new Long(text[1]);
// 等待事务提交
Thread.sleep(1000);
// 更新索引
Settings settings = Settings.builder().put("cluster.name", ES_CLUSTER_NAME).build();
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));
if ("add".equals(text[0])) {
// 根据商品id查询商品信息
SearchItem searchItem = itemMapper.getItemById(itemId);
String image = searchItem.getProductImageBig();
if (image != null && !"".equals(image)) {
String[] strings = image.split(",");
image = strings[0];
} else {
image = "";
}
searchItem.setProductImageBig(image);
IndexResponse indexResponse = client.prepareIndex(ITEM_INDEX, ITEM_TYPE, String.valueOf(searchItem.getProductId())).setSource(jsonBuilder().startObject().field("productId", searchItem.getProductId()).field("salePrice", searchItem.getSalePrice()).field("productName", searchItem.getProductName()).field("subTitle", searchItem.getSubTitle()).field("productImageBig", searchItem.getProductImageBig()).field("categoryName", searchItem.getCategoryName()).field("cid", searchItem.getCid()).endObject()).get();
} else if ("delete".equals(text[0])) {
DeleteResponse deleteResponse = client.prepareDelete(ITEM_INDEX, ITEM_TYPE, String.valueOf(itemId)).get();
}
log.info("处理消息成功");
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.
the class TransportShardBulkAction method executeUpdateRequest.
/**
* Executes update request, delegating to a index or delete operation after translation,
* handles retries on version conflict and constructs update response
* NOTE: reassigns bulk item request at <code>requestIndex</code> for replicas to
* execute translated update request (NOOP update is an exception). NOOP updates are
* indicated by returning a <code>null</code> operation in {@link BulkItemResultHolder}
* */
private static BulkItemResultHolder executeUpdateRequest(UpdateRequest updateRequest, IndexShard primary, IndexMetaData metaData, BulkShardRequest request, int requestIndex, UpdateHelper updateHelper, LongSupplier nowInMillis, final MappingUpdatePerformer mappingUpdater) throws Exception {
Engine.Result updateOperationResult = null;
UpdateResponse updateResponse = null;
BulkItemRequest replicaRequest = request.items()[requestIndex];
int maxAttempts = updateRequest.retryOnConflict();
for (int attemptCount = 0; attemptCount <= maxAttempts; attemptCount++) {
final UpdateHelper.Result translate;
// translate update request
try {
translate = updateHelper.prepare(updateRequest, primary, nowInMillis);
} catch (Exception failure) {
// we may fail translating a update to index or delete operation
// we use index result to communicate failure while translating update request
updateOperationResult = new Engine.IndexResult(failure, updateRequest.version(), SequenceNumbersService.UNASSIGNED_SEQ_NO);
// out of retry loop
break;
}
// execute translated update request
switch(translate.getResponseResult()) {
case CREATED:
case UPDATED:
IndexRequest indexRequest = translate.action();
MappingMetaData mappingMd = metaData.mappingOrDefault(indexRequest.type());
indexRequest.process(mappingMd, request.index());
updateOperationResult = executeIndexRequestOnPrimary(indexRequest, primary, mappingUpdater);
break;
case DELETED:
DeleteRequest deleteRequest = translate.action();
updateOperationResult = executeDeleteRequestOnPrimary(deleteRequest, primary);
break;
case NOOP:
primary.noopUpdate(updateRequest.type());
break;
default:
throw new IllegalStateException("Illegal update operation " + translate.getResponseResult());
}
if (updateOperationResult == null) {
// this is a noop operation
updateResponse = translate.action();
// out of retry loop
break;
} else if (updateOperationResult.hasFailure() == false) {
// set translated update (index/delete) request for replica execution in bulk items
switch(updateOperationResult.getOperationType()) {
case INDEX:
IndexRequest updateIndexRequest = translate.action();
final IndexResponse indexResponse = new IndexResponse(primary.shardId(), updateIndexRequest.type(), updateIndexRequest.id(), updateOperationResult.getSeqNo(), updateOperationResult.getVersion(), ((Engine.IndexResult) updateOperationResult).isCreated());
BytesReference indexSourceAsBytes = updateIndexRequest.source();
updateResponse = new UpdateResponse(indexResponse.getShardInfo(), indexResponse.getShardId(), indexResponse.getType(), indexResponse.getId(), indexResponse.getSeqNo(), indexResponse.getVersion(), indexResponse.getResult());
if ((updateRequest.fetchSource() != null && updateRequest.fetchSource().fetchSource()) || (updateRequest.fields() != null && updateRequest.fields().length > 0)) {
Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(indexSourceAsBytes, true, updateIndexRequest.getContentType());
updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, request.index(), indexResponse.getVersion(), sourceAndContent.v2(), sourceAndContent.v1(), indexSourceAsBytes));
}
// set translated request as replica request
replicaRequest = new BulkItemRequest(request.items()[requestIndex].id(), updateIndexRequest);
break;
case DELETE:
DeleteRequest updateDeleteRequest = translate.action();
DeleteResponse deleteResponse = new DeleteResponse(primary.shardId(), updateDeleteRequest.type(), updateDeleteRequest.id(), updateOperationResult.getSeqNo(), updateOperationResult.getVersion(), ((Engine.DeleteResult) updateOperationResult).isFound());
updateResponse = new UpdateResponse(deleteResponse.getShardInfo(), deleteResponse.getShardId(), deleteResponse.getType(), deleteResponse.getId(), deleteResponse.getSeqNo(), deleteResponse.getVersion(), deleteResponse.getResult());
updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, request.index(), deleteResponse.getVersion(), translate.updatedSourceAsMap(), translate.updateSourceContentType(), null));
// set translated request as replica request
replicaRequest = new BulkItemRequest(request.items()[requestIndex].id(), updateDeleteRequest);
break;
}
assert updateOperationResult.getSeqNo() != SequenceNumbersService.UNASSIGNED_SEQ_NO;
// out of retry loop
break;
} else if (updateOperationResult.getFailure() instanceof VersionConflictEngineException == false) {
// out of retry loop
break;
}
}
return new BulkItemResultHolder(updateResponse, updateOperationResult, replicaRequest);
}
Aggregations