org.elasticsearch.client.Client.prepareIndex()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(274)

本文整理了Java中org.elasticsearch.client.Client.prepareIndex()方法的一些代码示例,展示了Client.prepareIndex()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Client.prepareIndex()方法的具体详情如下:
包路径:org.elasticsearch.client.Client
类名称:Client
方法名:prepareIndex

Client.prepareIndex介绍

[英]Index a document associated with a given index and type.

The id is optional, if it is not provided, one will be generated automatically.
[中]索引与给定索引和类型关联的文档。
id是可选的,如果未提供,将自动生成一个id。

代码示例

代码示例来源:origin: apache/usergrid

public void doOperation( final Client client, final BulkRequestBuilder bulkRequest ) {
  IndexRequestBuilder builder = client.prepareIndex( writeAlias, IndexingUtils.ES_ENTITY_TYPE, documentId ).setSource( data );
  bulkRequest.add( builder );
}

代码示例来源:origin: richardwilly98/elasticsearch-river-mongodb

private void logStatistics(long duration) {
    if (definition.isStoreStatistics()) {
      long totalDocuments = deletedDocuments.get() + insertedDocuments.get();
      logger.trace("Indexed {} documents: {} insertions, {} updates, {} deletions", totalDocuments, insertedDocuments.get(),
          updatedDocuments.get(), deletedDocuments.get());
      Map<String, Object> source = new HashMap<String, Object>();
      Map<String, Object> statistics = Maps.newHashMap();
      statistics.put("duration", duration);
      statistics.put("date", new Date());
      statistics.put("index", index);
      statistics.put("type", type);
      statistics.put("documents.inserted", insertedDocuments.get());
      statistics.put("documents.updated", updatedDocuments.get());
      statistics.put("documents.deleted", deletedDocuments.get());
      statistics.put("documents.total", documentCount.get());
      source.put("statistics", statistics);
      client.prepareIndex(definition.getStatisticsIndexName(), definition.getStatisticsTypeName()).setSource(source).get();
    }
  }
}

代码示例来源:origin: richardwilly98/elasticsearch-river-mongodb

public static void setRiverStatus(Client client, String riverName, Status status) {
  logger.info("setRiverStatus called with {} - {}", riverName, status);
  XContentBuilder xb;
  try {
    xb = jsonBuilder().startObject().startObject(MongoDBRiver.TYPE).field(MongoDBRiver.STATUS_FIELD, status).endObject()
        .endObject();
    client.prepareIndex("_river", riverName, MongoDBRiver.STATUS_ID).setSource(xb).get();
  } catch (IOException ioEx) {
    logger.error("setRiverStatus failed for river {}", ioEx, riverName);
  }
}

代码示例来源:origin: brianfrankcooper/YCSB

/**
 * Insert a record in the database. Any field/value pairs in the specified
 * values HashMap will be written into the record with the specified record
 * key.
 *
 * @param table
 *          The name of the table
 * @param key
 *          The record key of the record to insert.
 * @param values
 *          A HashMap of field/value pairs to insert in the record
 * @return Zero on success, a non-zero error code on error. See this class's
 *         description for a discussion of error codes.
 */
@Override
public Status insert(String table, String key, Map<String, ByteIterator> values) {
 try {
  final XContentBuilder doc = jsonBuilder().startObject();
  for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
   doc.field(entry.getKey(), entry.getValue());
  }
  doc.endObject();
  client.prepareIndex(indexKey, table, key).setSource(doc).execute().actionGet();
  return Status.OK;
 } catch (Exception e) {
  e.printStackTrace();
  return Status.ERROR;
 }
}

代码示例来源:origin: apache/nifi

@Override
  public void process(final InputStream in) throws IOException {
    String json = IOUtils.toString(in, charset)
        .replace("\r\n", " ").replace('\n', ' ').replace('\r', ' ');
    if (indexOp.equalsIgnoreCase("index")) {
      bulk.add(esClient.get().prepareIndex(index, docType, id)
          .setSource(json.getBytes(charset)));
    } else if (indexOp.equalsIgnoreCase("upsert")) {
      bulk.add(esClient.get().prepareUpdate(index, docType, id)
          .setDoc(json.getBytes(charset))
          .setDocAsUpsert(true));
    } else if (indexOp.equalsIgnoreCase("update")) {
      bulk.add(esClient.get().prepareUpdate(index, docType, id)
          .setDoc(json.getBytes(charset)));
    } else {
      throw new IOException("Index operation: " + indexOp + " not supported.");
    }
  }
});

代码示例来源:origin: brianfrankcooper/YCSB

client.prepareIndex(indexKey, table, key).setSource(response.getSource()).execute().actionGet();

代码示例来源:origin: loklak/loklak_server

IndexResponse r = elasticsearchClient.prepareIndex(indexName, typeName, id).setSource(jsonMap)
  .setVersion(version == null ? 1 : version.longValue())
  .setVersionType(version == null ? VersionType.FORCE : VersionType.EXTERNAL)

代码示例来源:origin: prestodb/presto

@Override
public void addResults(QueryStatusInfo statusInfo, QueryData data)
{
  if (types.get() == null && statusInfo.getColumns() != null) {
    types.set(getTypes(statusInfo.getColumns()));
  }
  if (data.getData() == null) {
    return;
  }
  checkState(types.get() != null, "Type information is missing");
  List<Column> columns = statusInfo.getColumns();
  for (List<Object> fields : data.getData()) {
    try {
      XContentBuilder dataBuilder = jsonBuilder().startObject();
      for (int i = 0; i < fields.size(); i++) {
        Type type = types.get().get(i);
        Object value = convertValue(fields.get(i), type);
        dataBuilder.field(columns.get(i).getName(), value);
      }
      dataBuilder.endObject();
      client.prepareIndex(tableName, "doc")
          .setSource(dataBuilder.string(), JSON)
          .get();
    }
    catch (IOException e) {
      throw new UncheckedIOException("Error loading data into Elasticsearch index: " + tableName, e);
    }
  }
}

代码示例来源:origin: apache/usergrid

/**
 * Tests writing a document to a new index to ensure it's working correctly. See this post:
 * http://s.apache.org/index-missing-exception
 */
private void testNewIndex() {
  // create the document, this ensures the index is ready
  // Immediately create a document and remove it to ensure the entire cluster is ready
  // to receive documents. Occasionally we see errors.
  // See this post: http://s.apache.org/index-missing-exception
  if (logger.isTraceEnabled()) {
    logger.trace("Testing new index name: read {} write {}", alias.getReadAlias(), alias.getWriteAlias());
  }
  final RetryOperation retryOperation = () -> {
    final String tempId = UUIDGenerator.newTimeUUID().toString();
    esProvider.getClient().prepareIndex( alias.getWriteAlias(), VERIFY_TYPE, tempId )
      .setSource(DEFAULT_PAYLOAD).get();
    if (logger.isTraceEnabled()) {
      logger.trace("Successfully created new document with docId {} in index read {} write {} and type {}",
        tempId, alias.getReadAlias(), alias.getWriteAlias(), VERIFY_TYPE);
    }
    // delete all types, this way if we miss one it will get cleaned up
    esProvider.getClient().prepareDelete( alias.getWriteAlias(), VERIFY_TYPE, tempId).get();
    if (logger.isTraceEnabled()) {
      logger.trace("Successfully deleted  documents in read {} write {} and type {} with id {}",
        alias.getReadAlias(), alias.getWriteAlias(), VERIFY_TYPE, tempId);
    }
    return true;
  };
  doInRetry(retryOperation);
}

代码示例来源:origin: alibaba/jstorm

@Override
public void execute(Tuple tuple) {
 try {
  String index = mapper.getIndex(tuple);
  String type = mapper.getType(tuple);
  String id = mapper.getId(tuple);
  String source = mapper.getSource(tuple);
  OpType opType = mapper.getOpType();
  client.prepareIndex(index, type).setId(id).setSource(source)
    .setOpType(opType).execute();
  collector.ack(tuple);
 } catch (Exception e) {
  collector.fail(tuple);
 }
}

代码示例来源:origin: apache/flume

@Override
public void addEvent(Event event, IndexNameBuilder indexNameBuilder,
  String indexType, long ttlMs) throws Exception {
 if (bulkRequestBuilder == null) {
  bulkRequestBuilder = client.prepareBulk();
 }
 IndexRequestBuilder indexRequestBuilder = null;
 if (indexRequestBuilderFactory == null) {
  indexRequestBuilder = client
    .prepareIndex(indexNameBuilder.getIndexName(event), indexType)
    .setSource(serializer.getContentBuilder(event).bytes());
 } else {
  indexRequestBuilder = indexRequestBuilderFactory.createIndexRequest(
    client, indexNameBuilder.getIndexPrefix(event), indexType, event);
 }
 if (ttlMs > 0) {
  indexRequestBuilder.setTTL(ttlMs);
 }
 bulkRequestBuilder.add(indexRequestBuilder);
}

代码示例来源:origin: loklak/loklak_server

if (be.getId() == null) continue;
bulkRequest.add(
    elasticsearchClient.prepareIndex(indexName, be.getType(), be.getId()).setSource(be.getJsonMap())
      .setVersion(be.getVersion() == null ? 1 : be.getVersion().longValue())
      .setVersionType(be.getVersion() == null ? VersionType.FORCE : VersionType.EXTERNAL));

代码示例来源:origin: larsga/Duke

private void addToIndex(String id, Map<String, Object> json) {
  this.bulkRequest.add(this.client.prepareIndex(this.indexName,
      this.indexType, id).setSource(json));
  this.bulkRequestCounter++;
  this.flushIndex(false);
}

代码示例来源:origin: komoot/photon

public void create(PhotonDoc doc) {
  try {
    this.bulkRequest.add(this.esClient.prepareIndex("photon", "place").setSource(Utils.convert(doc, this.languages)).setId(String.valueOf(doc.getPlaceId())));
  } catch (IOException e) {
    log.error(String.format("creation of new doc [%s] failed", doc), e);
  }
}

代码示例来源:origin: yacy/yacy_grid_mcp

/**
 * Write a json document into the search index.
 * Writing using a XContentBuilder is the most efficient way to add content to elasticsearch
 * 
 * @param jsonMap
 *            the json document to be indexed in elasticsearch
 * @param id
 *            the unique identifier of a document
 * @param indexName
 *            the name of the index
 * @param typeName
 *            the type of the index
 */
public IndexResponse writeSource(String indexName, XContentBuilder json, String id, String typeName, long version, VersionType versionType) {
  // put this to the index
  IndexResponse r = elasticsearchClient.prepareIndex(indexName, typeName, id).setSource(json)
    .setVersion(version).setVersionType(versionType).execute()
    .actionGet();
  // documentation about the versioning is available at
  // https://www.elastic.co/blog/elasticsearch-versioning-support
  // TODO: error handling
  return r;
}

代码示例来源:origin: komoot/photon

@Override
public void add(PhotonDoc doc) {
  try {
    this.bulkRequest.add(this.esClient.prepareIndex(indexName, indexType).
        setSource(Utils.convert(doc, languages)).setId(doc.getUid()));
  } catch (IOException e) {
    log.error("could not bulk add document " + doc.getUid(), e);
    return;
  }
  this.documentCount += 1;
  if (this.documentCount > 0 && this.documentCount % 10000 == 0) {
    this.saveDocuments();
  }
}

代码示例来源:origin: Impetus/Kundera

.prepareIndex(metadata.getSchema().toLowerCase(), entityClazz.getSimpleName(), id.toString())
    .setSource(json).execute();
IndexResponse response = listenableActionFuture.actionGet();

代码示例来源:origin: yacy/yacy_grid_mcp

if (be.id == null) continue;
bulkRequest.add(
    elasticsearchClient.prepareIndex(indexName, be.type, be.id).setSource(be.jsonMap)
      .setVersion(1)
      .setCreate(false) // enforces OpType.INDEX

代码示例来源:origin: org.elasticsearch/elasticsearch

private void doStoreResult(TaskResult taskResult, ActionListener<Void> listener) {
  IndexRequestBuilder index = client.prepareIndex(TASK_INDEX, TASK_TYPE, taskResult.getTask().getTaskId().toString());
  try (XContentBuilder builder = XContentFactory.contentBuilder(Requests.INDEX_CONTENT_TYPE)) {
    taskResult.toXContent(builder, ToXContent.EMPTY_PARAMS);
    index.setSource(builder);
  } catch (IOException e) {
    throw new ElasticsearchException("Couldn't convert task result to XContent for [{}]", e, taskResult.getTask());
  }
  doStoreResult(STORE_BACKOFF_POLICY.iterator(), index, listener);
}

代码示例来源:origin: com.floragunn/search-guard

protected void index(final AuditMessage msg) {
  client.prepareIndex(securityConfigurationIndex, "audit").setSource(msg.auditInfo).execute(new ActionListener<IndexResponse>() {
    @Override
    public void onResponse(final IndexResponse response) {
      log.trace("write audit message {}", msg);
    }
    @Override
    public void onFailure(final Throwable e) {
      log.error("Unable to write audit log due to {}", e, e.toString());
    }
  });
}

相关文章