Java Code Examples for org.elasticsearch.action.index.IndexResponse#status()

The following examples show how to use org.elasticsearch.action.index.IndexResponse#status() . 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: FactSearchManager.java    From act-platform with ISC License 6 votes vote down vote up
/**
 * 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 2
Source File: IndexAnomalyDetectorActionHandler.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
private ActionListener<IndexResponse> indexAnomalyDetectorResponse() {
    return new RestResponseListener<IndexResponse>(channel) {
        @Override
        public RestResponse buildResponse(IndexResponse response) throws Exception {
            if (response.getShardInfo().getSuccessful() < 1) {
                return new BytesRestResponse(response.status(), response.toXContent(channel.newErrorBuilder(), EMPTY_PARAMS));
            }

            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())
                .field("anomaly_detector", anomalyDetector)
                .endObject();

            BytesRestResponse restResponse = new BytesRestResponse(response.status(), builder);
            if (response.status() == RestStatus.CREATED) {
                String location = String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.AD_BASE_URI, response.getId());
                restResponse.addHeader("Location", location);
            }
            return restResponse;
        }
    };
}
 
Example 3
Source File: ElasticSearchPipline.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
@Override
public void process(ResultItems resultItems) {
    IndexResponse response = client.prepareIndex(index, type)
            .setSource(resultItems.getAll())
            .get();
    RestStatus restStatus = response.status();
    if (RestStatus.CREATED == restStatus) {
        log.info("key {} have already created!", response.getId());
    } else {
        log.error("an error occured with result id {}!", restStatus.getStatus());
    }
}