Java Code Examples for org.elasticsearch.action.index.IndexResponse#getResult()
The following examples show how to use
org.elasticsearch.action.index.IndexResponse#getResult() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexAnomalyDetectorJobActionHandler.java From anomaly-detection with Apache License 2.0 | 6 votes |
private void onIndexAnomalyDetectorJobResponse(IndexResponse response, AnomalyDetectorFunction function) throws IOException { if (response == null || (response.getResult() != CREATED && response.getResult() != UPDATED)) { channel.sendResponse(new BytesRestResponse(response.status(), response.toXContent(channel.newErrorBuilder(), EMPTY_PARAMS))); return; } if (function != null) { function.execute(); } else { XContentBuilder builder = channel .newBuilder() .startObject() .field(RestHandlerUtils._ID, response.getId()) .field(RestHandlerUtils._VERSION, response.getVersion()) .field(RestHandlerUtils._SEQ_NO, response.getSeqNo()) .field(RestHandlerUtils._PRIMARY_TERM, response.getPrimaryTerm()) .endObject(); channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder)); } }
Example 2
Source File: FactSearchManager.java From act-platform with ISC License | 6 votes |
/** * Index a Fact into ElasticSearch. * * @param fact Fact to index * @return Indexed Fact */ public FactDocument indexFact(FactDocument fact) { if (fact == null || fact.getId() == null) return null; IndexResponse response; try { IndexRequest request = new IndexRequest(INDEX_NAME, TYPE_NAME, fact.getId().toString()) .setRefreshPolicy(isTestEnvironment ? WriteRequest.RefreshPolicy.IMMEDIATE : WriteRequest.RefreshPolicy.NONE) .source(FACT_DOCUMENT_WRITER.writeValueAsBytes(fact), XContentType.JSON); response = clientFactory.getClient().index(request, RequestOptions.DEFAULT); } catch (ElasticsearchException | IOException ex) { throw logAndExit(ex, String.format("Could not perform request to index Fact with id = %s.", fact.getId())); } if (response.status() != RestStatus.OK && response.status() != RestStatus.CREATED) { LOGGER.warning("Could not index Fact with id = %s.", fact.getId()); } else if (response.getResult() == DocWriteResponse.Result.CREATED) { LOGGER.info("Successfully indexed Fact with id = %s.", fact.getId()); } else if (response.getResult() == DocWriteResponse.Result.UPDATED) { LOGGER.info("Successfully re-indexed existing Fact with id = %s.", fact.getId()); } return fact; }
Example 3
Source File: ESPipeline.java From Gather-Platform with GNU General Public License v3.0 | 5 votes |
@Override public void process(ResultItems resultItems, Task task) { Iterator i$ = resultItems.getAll().entrySet().iterator(); try { XContentBuilder xContentBuilder = jsonBuilder().startObject(); while (i$.hasNext()) { Map.Entry entry = (Map.Entry) i$.next(); xContentBuilder.field((String) entry.getKey(), entry.getValue()); } String json = xContentBuilder.endObject().string(); IndexResponse response = null; if (StringUtils.isNotBlank(resultItems.get("id"))) { response = client .prepareIndex(INDEX_NAME, TYPE_NAME, resultItems.get("id")) .setSource(json).get(); } else { response = client .prepareIndex(INDEX_NAME, TYPE_NAME) .setSource(json).get(); } if (response.getResult() != IndexResponse.Result.CREATED) LOG.error("索引失败,可能重复创建,resultItem:" + resultItems); } catch (IOException e) { LOG.error("索引出错," + e.getLocalizedMessage()); e.printStackTrace(); } }
Example 4
Source File: ESPipeline.java From spider with GNU General Public License v3.0 | 5 votes |
@Override public void process(ResultItems resultItems, Task task) { Iterator i$ = resultItems.getAll().entrySet().iterator(); try { XContentBuilder xContentBuilder = jsonBuilder().startObject(); while (i$.hasNext()) { Map.Entry entry = (Map.Entry) i$.next(); xContentBuilder.field((String) entry.getKey(), entry.getValue()); } String json = xContentBuilder.endObject().string(); IndexResponse response = null; if (StringUtils.isNotBlank(resultItems.get("id"))) { response = client .prepareIndex(INDEX_NAME, TYPE_NAME, resultItems.get("id")) .setSource(json).get(); } else { response = client .prepareIndex(INDEX_NAME, TYPE_NAME) .setSource(json).get(); } if (response.getResult() != IndexResponse.Result.CREATED) LOG.error("索引失败,可能重复创建,resultItem:" + resultItems); } catch (IOException e) { LOG.error("索引出错," + e.getLocalizedMessage()); e.printStackTrace(); } }
Example 5
Source File: ElasticsearchIntegrationTest.java From openshift-elasticsearch-plugin with Apache License 2.0 | 5 votes |
protected void givenDocumentIsIndexed(String index, String type, String id, XContentBuilder content) throws Exception { ThreadContext threadContext = esNode1.client().threadPool().getThreadContext(); try (StoredContext cxt = threadContext.stashContext()) { threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true"); IndexResponse response = esNode1.client().prepareIndex(index, type, id) .setSource(content) .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .execute() .get(); if(!Result.CREATED.equals(response.getResult())){ throw new RuntimeException("Test setup failed trying to index a document. Exp. CREATED but was: " + response.getResult()); } } }
Example 6
Source File: ElasticsearchClusterRunner.java From elasticsearch-cluster-runner with Apache License 2.0 | 5 votes |
@Deprecated public IndexResponse insert(final String index, final String type, final String id, final BuilderCallback<IndexRequestBuilder> builder) { final IndexResponse actionGet = builder.apply(client().prepareIndex(index, type, id)).execute().actionGet(); if (actionGet.getResult() != Result.CREATED) { onFailure("Failed to insert " + id + " into " + index + "/" + type + ".", actionGet); } return actionGet; }
Example 7
Source File: ElasticsearchClusterRunner.java From elasticsearch-cluster-runner with Apache License 2.0 | 5 votes |
public IndexResponse insert(final String index, final String id, final BuilderCallback<IndexRequestBuilder> builder) { final IndexResponse actionGet = builder.apply(client().prepareIndex().setIndex(index).setId(id)).execute().actionGet(); if (actionGet.getResult() != Result.CREATED) { onFailure("Failed to insert " + id + " into " + index + ".", actionGet); } return actionGet; }
Example 8
Source File: EsAbstractBehavior.java From fess with Apache License 2.0 | 5 votes |
@Override protected int delegateInsert(final Entity entity, final InsertOption<? extends ConditionBean> option) { final EsAbstractEntity esEntity = (EsAbstractEntity) entity; IndexRequestBuilder builder = createInsertRequest(esEntity); final IndexResponse response = builder.execute().actionGet(indexTimeout); esEntity.asDocMeta().id(response.getId()); return response.getResult() == Result.CREATED ? 1 : 0; }
Example 9
Source File: EsAbstractBehavior.java From fess with Apache License 2.0 | 5 votes |
@Override protected int delegateInsert(final Entity entity, final InsertOption<? extends ConditionBean> option) { final EsAbstractEntity esEntity = (EsAbstractEntity) entity; IndexRequestBuilder builder = createInsertRequest(esEntity); final IndexResponse response = builder.execute().actionGet(indexTimeout); esEntity.asDocMeta().id(response.getId()); return response.getResult() == Result.CREATED ? 1 : 0; }
Example 10
Source File: FessEsClient.java From fess with Apache License 2.0 | 5 votes |
public boolean store(final String index, final Object obj) { final FessConfig fessConfig = ComponentUtil.getFessConfig(); @SuppressWarnings("unchecked") final Map<String, Object> source = obj instanceof Map ? (Map<String, Object>) obj : BeanUtil.copyBeanToNewMap(obj); final String id = (String) source.remove(fessConfig.getIndexFieldId()); source.remove(fessConfig.getIndexFieldVersion()); final Number seqNo = (Number) source.remove(fessConfig.getIndexFieldSeqNo()); final Number primaryTerm = (Number) source.remove(fessConfig.getIndexFieldPrimaryTerm()); IndexResponse response; try { if (id == null) { // TODO throw Exception in next release // create response = client.prepareIndex().setIndex(index).setSource(new DocMap(source)).setRefreshPolicy(RefreshPolicy.IMMEDIATE) .setOpType(OpType.CREATE).execute().actionGet(fessConfig.getIndexIndexTimeout()); } else { // create or update final IndexRequestBuilder builder = client.prepareIndex().setIndex(index).setId(id).setSource(new DocMap(source)) .setRefreshPolicy(RefreshPolicy.IMMEDIATE).setOpType(OpType.INDEX); if (seqNo != null) { builder.setIfSeqNo(seqNo.longValue()); } if (primaryTerm != null) { builder.setIfPrimaryTerm(primaryTerm.longValue()); } response = builder.execute().actionGet(fessConfig.getIndexIndexTimeout()); } final Result result = response.getResult(); return result == Result.CREATED || result == Result.UPDATED; } catch (final ElasticsearchException e) { throw new FessEsClientException("Failed to store: " + obj, e); } }
Example 11
Source File: EsAbstractBehavior.java From fess with Apache License 2.0 | 5 votes |
@Override protected int delegateInsert(final Entity entity, final InsertOption<? extends ConditionBean> option) { final EsAbstractEntity esEntity = (EsAbstractEntity) entity; IndexRequestBuilder builder = createInsertRequest(esEntity); final IndexResponse response = builder.execute().actionGet(indexTimeout); esEntity.asDocMeta().id(response.getId()); return response.getResult() == Result.CREATED ? 1 : 0; }
Example 12
Source File: EsHighLevelRestTest1.java From java-study with Apache License 2.0 | 4 votes |
private static void insert() throws IOException { String index = "test1"; String type = "_doc"; // 唯一编号 String id = "1"; IndexRequest request = new IndexRequest(index, type, id); /* * 第一种方式,通过jsonString进行创建 */ // json String jsonString = "{" + "\"uid\":\"1234\","+ "\"phone\":\"12345678909\","+ "\"msgcode\":\"1\"," + "\"sendtime\":\"2019-03-14 01:57:04\"," + "\"message\":\"xuwujing study Elasticsearch\"" + "}"; request.source(jsonString, XContentType.JSON); /* * 第二种方式,通过map创建,,会自动转换成json的数据 */ Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("uid", 1234); jsonMap.put("phone", 12345678909L); jsonMap.put("msgcode", 1); jsonMap.put("sendtime", "2019-03-14 01:57:04"); jsonMap.put("message", "xuwujing study Elasticsearch"); request.source(jsonMap); /* * 第三种方式 : 通过XContentBuilder对象进行创建 */ XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("uid", 1234); builder.field("phone", 12345678909L); builder.field("msgcode", 1); builder.timeField("sendtime", "2019-03-14 01:57:04"); builder.field("message", "xuwujing study Elasticsearch"); } builder.endObject(); request.source(builder); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); //如果是200则表示成功,否则就是失败 if(200 == indexResponse.status().getStatus()){ } // 对响应结果进行处理 String index1 = indexResponse.getIndex(); String type1 = indexResponse.getType(); String id1 = indexResponse.getId(); long version = indexResponse.getVersion(); // 如果是新增/修改的话的话 if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) { } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) { } ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo(); if (shardInfo.getTotal() != shardInfo.getSuccessful()) { } if (shardInfo.getFailed() > 0) { for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) { String reason = failure.reason(); } } }