Java Code Examples for org.elasticsearch.action.index.IndexRequest#id()

The following examples show how to use org.elasticsearch.action.index.IndexRequest#id() . 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: BookingDaoESImp.java    From blue-marlin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean lockBooking()
{
    try
    {
        IndexRequest indexRequest = new IndexRequest(this.bookingsIndex, ES_TYPE);
        indexRequest.id(LOCK_BOOKING_ID);
        indexRequest.create(true);
        indexRequest.source(new HashMap());
        esclient.index(indexRequest, WriteRequest.RefreshPolicy.WAIT_UNTIL.getValue());
    }
    catch (Exception e)
    {
        return false;
    }
    return true;
}
 
Example 2
Source File: TransportShardBulkAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private WriteResult<IndexResponse> shardIndexOperation(BulkShardRequest request, IndexRequest indexRequest, MetaData metaData,
                                        IndexShard indexShard, boolean processed) throws Throwable {
    indexShard.checkDiskSpace(fsService);
    // validate, if routing is required, that we got routing
    MappingMetaData mappingMd = metaData.index(request.index()).mappingOrDefault(indexRequest.type());
    if (mappingMd != null && mappingMd.routing().required()) {
        if (indexRequest.routing() == null) {
            throw new RoutingMissingException(request.index(), indexRequest.type(), indexRequest.id());
        }
    }

    if (!processed) {
        indexRequest.process(metaData, mappingMd, allowIdGeneration, request.index());
    }

    return TransportIndexAction.executeIndexRequestOnPrimary(request, indexRequest, indexShard, mappingUpdatedAction);
}
 
Example 3
Source File: ElasticsearchClientTransport.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean indexData(IndexMetadata indexMetadata, String data) {
  IndexRequest indexRequest = new IndexRequest(indexMetadata.getName(), TYPE);
  if (indexMetadata.getId() != null && !indexMetadata.getId().isEmpty()) {
    indexRequest.id(indexMetadata.getId());
  }
  indexRequest.source(data, XContentType.JSON);
  indexRequest.routing(indexMetadata.getRouting());

  try {
    IndexResponse indexResponse = client.index(indexRequest).get();
    return indexResponse.status().equals(RestStatus.CREATED) || indexResponse.status().equals(RestStatus.OK);
  } catch (InterruptedException | ExecutionException e) {
    log.error("Error indexing '#{}' to '{}'.", indexMetadata.getId(), indexMetadata.getName(), e);
  }

  return false;
}
 
Example 4
Source File: ElasticsearchClientRest.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean indexData(IndexMetadata indexMetadata, String data) {
  IndexRequest indexRequest = new IndexRequest(indexMetadata.getName(), TYPE);
  if (indexMetadata.getId() != null && !indexMetadata.getId().isEmpty()) {
    indexRequest.id(indexMetadata.getId());
  }
  indexRequest.source(data, XContentType.JSON);
  indexRequest.routing(indexMetadata.getRouting());

  try {
    IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
    return indexResponse.status().equals(RestStatus.CREATED) || indexResponse.status().equals(RestStatus.OK);
  } catch (IOException e) {
    log.error("Could not index '#{}' to index '{}'.", indexMetadata.getRouting(), indexMetadata.getName(), e);
  }
  return false;
}
 
Example 5
Source File: ElasticsearchClientRest.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean updateIndex(IndexMetadata indexMetadata, String data) {
  UpdateRequest updateRequest = new UpdateRequest(indexMetadata.getName(), TYPE, indexMetadata.getId());
  updateRequest.doc(data, XContentType.JSON);
  updateRequest.routing(indexMetadata.getId());

  IndexRequest indexRequest = new IndexRequest(indexMetadata.getName(), TYPE);
  if (indexMetadata.getId() != null && !indexMetadata.getId().isEmpty()) {
    indexRequest.id(indexMetadata.getId());
  }
  indexRequest.source(data, XContentType.JSON);

  updateRequest.upsert(indexRequest);

  try {
    UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
    return updateResponse.status().equals(RestStatus.OK);
  } catch (IOException e) {
    log.error("Error updating index '{}'.", indexMetadata.getName(), e);
  }
  return false;
}
 
Example 6
Source File: ElasticsearchWriterBase.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
protected Pair<BulkRequest, FutureCallbackHolder> prepareBatch(Batch<Object> batch, WriteCallback callback) {
  BulkRequest bulkRequest = new BulkRequest();
  final StringBuilder stringBuilder = new StringBuilder();
  for (Object record : batch.getRecords()) {
    try {
      byte[] serializedBytes = this.serializer.serializeToJson(record);
      log.debug("serialized record: {}", serializedBytes);
      IndexRequest indexRequest = new IndexRequest(this.indexName, this.indexType)
          .source(serializedBytes, 0, serializedBytes.length, XContentType.JSON);
      if (this.idMappingEnabled) {
        String id = this.typeMapper.getValue(this.idFieldName, record);
        indexRequest.id(id);
        stringBuilder.append(";").append(id);
      }
      bulkRequest.add(indexRequest);
    }
    catch (Exception e) {
      log.error("Encountered exception {}", e);
    }
  }
  FutureCallbackHolder futureCallbackHolder = new FutureCallbackHolder(callback,
      exception -> log.error("Batch: {} failed on ids; {} with exception {}", batch.getId(),
          stringBuilder.toString(), exception),
      this.malformedDocPolicy);
  return new Pair(bulkRequest, futureCallbackHolder);
}
 
Example 7
Source File: IndexAnomalyDetectorActionHandler.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
private void indexAnomalyDetector(String detectorId) throws IOException {
    AnomalyDetector detector = new AnomalyDetector(
        anomalyDetector.getDetectorId(),
        anomalyDetector.getVersion(),
        anomalyDetector.getName(),
        anomalyDetector.getDescription(),
        anomalyDetector.getTimeField(),
        anomalyDetector.getIndices(),
        anomalyDetector.getFeatureAttributes(),
        anomalyDetector.getFilterQuery(),
        anomalyDetector.getDetectionInterval(),
        anomalyDetector.getWindowDelay(),
        anomalyDetector.getUiMetadata(),
        anomalyDetector.getSchemaVersion(),
        Instant.now()
    );
    IndexRequest indexRequest = new IndexRequest(ANOMALY_DETECTORS_INDEX)
        .setRefreshPolicy(refreshPolicy)
        .source(detector.toXContent(channel.newBuilder(), XCONTENT_WITH_TYPE))
        .setIfSeqNo(seqNo)
        .setIfPrimaryTerm(primaryTerm)
        .timeout(requestTimeout);
    if (detectorId != null) {
        indexRequest.id(detectorId);
    }
    client.index(indexRequest, indexAnomalyDetectorResponse());
}
 
Example 8
Source File: ElasticSearchService.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
/**
 * 添加文章数据
 */
public void addArticle(Article article) throws IOException {
    IndexRequest indexRequest = new IndexRequest(BLOG_INDEX, ARTICLE_TYPE);
    indexRequest.id(article.getId().toString());
    indexRequest.source(JSON.toJSONString(article), XContentType.JSON);
    IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

    logger.info("添加数据 | {}", JSON.toJSONString(indexResponse));
}
 
Example 9
Source File: DataServiceImpl.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public boolean saveData(EventData eventData) throws IOException {
    IndexRequest indexRequest = new IndexRequest(INDEX_NAME);
    indexRequest.id(eventData.getId().getId());
    indexRequest.source(Utils.createContent(eventData));
    IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
    return RestStatus.CREATED.equals(indexResponse.status());
}
 
Example 10
Source File: EsUtil.java    From demo-project with MIT License 5 votes vote down vote up
/**
 * Description: 插入/更新一条记录
 *
 * @param index  index
 * @param entity 对象
 * @author fanxb
 * @date 2019/7/24 15:02
 */
public void insertOrUpdateOne(String index, EsEntity entity) {
    IndexRequest request = new IndexRequest(index);
    request.id(entity.getId());
    request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
    try {
        client.index(request, RequestOptions.DEFAULT);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 11
Source File: EsUtil.java    From bookmark with MIT License 5 votes vote down vote up
/**
 * Description: 插入/更新一条记录
 *
 * @param index  index
 * @param entity 对象
 * @author fanxb
 * @date 2019/7/24 15:02
 */
public void insertOrUpdateOne(String index, EsEntity entity) {
    if (!status) {
        return;
    }
    IndexRequest request = new IndexRequest(index);
    request.id(entity.getId());
    request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
    try {
        client.index(request, RequestOptions.DEFAULT);
    } catch (Exception e) {
        throw new EsException(e);
    }
}
 
Example 12
Source File: ESConnection.java    From canal with Apache License 2.0 5 votes vote down vote up
public ES7xIndexRequest(String index, String id){
    if (mode == ESClientMode.TRANSPORT) {
        indexRequestBuilder = transportClient.prepareIndex();
        indexRequestBuilder.setIndex(index);
        indexRequestBuilder.setId(id);
    } else {
        indexRequest = new IndexRequest(index);
        indexRequest.id(id);
    }
}
 
Example 13
Source File: HttpBulkAction.java    From elasticsearch-helper with Apache License 2.0 4 votes vote down vote up
@Override
protected HttpRequest createHttpRequest(URL base, BulkRequest request) {
    StringBuilder bulkContent = new StringBuilder();
    for (ActionRequest actionRequest : request.requests()) {
        if (actionRequest instanceof IndexRequest) {
            IndexRequest indexRequest = (IndexRequest) actionRequest;
            bulkContent.append("{\"").append(indexRequest.opType().lowercase()).append("\":{");
            bulkContent.append("\"_index\":\"").append(indexRequest.index()).append("\"");
            bulkContent.append(",\"_type\":\"").append(indexRequest.type()).append("\"");
            if (indexRequest.id() != null) {
                bulkContent.append(",\"_id\":\"").append(indexRequest.id()).append("\"");
            }
            if (indexRequest.routing() != null) {
                bulkContent.append(",\"_routing\":\"").append(indexRequest.routing()).append("\""); // _routing
            }
            if (indexRequest.parent() != null) {
                bulkContent.append(",\"_parent\":\"").append(indexRequest.parent()).append("\"");
            }
            if (indexRequest.timestamp() != null) {
                bulkContent.append(",\"_timestamp\":\"").append(indexRequest.timestamp()).append("\"");
            }
            // avoid _ttl <= 0 at all cost!
            if (indexRequest.ttl() != null && indexRequest.ttl().seconds() > 0) {
                bulkContent.append(",\"_ttl\":\"").append(indexRequest.ttl()).append("\"");
            }
            if (indexRequest.version() > 0) {
                bulkContent.append(",\"_version\":\"").append(indexRequest.version()).append("\"");
                if (indexRequest.versionType() != null) {
                    bulkContent.append(",\"_version_type\":\"").append(indexRequest.versionType().name()).append("\"");
                }
            }
            bulkContent.append("}}\n");
            bulkContent.append(indexRequest.source().toUtf8());
            bulkContent.append("\n");
        } else if (actionRequest instanceof DeleteRequest) {
            DeleteRequest deleteRequest = (DeleteRequest) actionRequest;
            bulkContent.append("{\"delete\":{");
            bulkContent.append("\"_index\":\"").append(deleteRequest.index()).append("\"");
            bulkContent.append(",\"_type\":\"").append(deleteRequest.type()).append("\"");
            bulkContent.append(",\"_id\":\"").append(deleteRequest.id()).append("\"");
            if (deleteRequest.routing() != null) {
                bulkContent.append(",\"_routing\":\"").append(deleteRequest.routing()).append("\""); // _routing
            }
            bulkContent.append("}}\n");
        }
    }
    return newPostRequest(base, "/_bulk", bulkContent);
}