Java Code Examples for org.elasticsearch.action.index.IndexRequestBuilder#setId()

The following examples show how to use org.elasticsearch.action.index.IndexRequestBuilder#setId() . 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: ESNestedSearchService.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 获取新增请求
 *
 * @param esObject 更新请求参数
 * @return UpdateRequestBuilder 更新请求
 */
private IndexRequestBuilder getIndexRequest(SaveESObject esObject) {
    String dataId = getId(esObject.getUkMap());

    byte[] dataBytes = null;
    try {
        dataBytes = OBJECT_MAPPER.writeValueAsBytes(esObject.getDataMap());
    } catch (JsonProcessingException e) {
        // never hapened
        SearchLogger.error("", e);
    }

    IndexRequestBuilder indexRequestBuilder = transportClient.prepareIndex().setIndex(esObject.getIndexName())
            .setType(esObject.getTypeName());
    if (StringUtils.isNotBlank(dataId)) {
        indexRequestBuilder.setId(dataId);
    }
    // TODO 替换
    // indexRequestBuilder.setSource(esObject.getDataMap());
    indexRequestBuilder.setSource(dataBytes, XContentType.JSON);
    return indexRequestBuilder;
}
 
Example 2
Source File: DefaultElasticSearchService.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
private void populateIndexRequestBuilder(IndexRequestBuilder builder, IndexOptions options) {
    if (options != null) {
        if (options.getId() != null) builder.setId(options.getId());
        if (options.getRouting() != null) builder.setRouting(options.getRouting());
        if (options.getParent() != null) builder.setParent(options.getParent());
        if (options.getOpType() != null)
            builder.setOpType(DocWriteRequest.OpType.valueOf(options.getOpType().name()));
        if (options.getWaitForActiveShard() != null)
            builder.setWaitForActiveShards(options.getWaitForActiveShard());
        if (options.getRefreshPolicy() != null)
            builder.setRefreshPolicy(WriteRequest.RefreshPolicy.valueOf(options.getRefreshPolicy().name()));
        if (options.getVersion() != null) builder.setVersion(options.getVersion());
        if (options.getVersionType() != null) builder.setVersionType(options.getVersionType());
        if (options.getTimeout() != null) builder.setTimeout(options.getTimeout());
    }
}
 
Example 3
Source File: ElasticsearchDocumentWriter.java    From CogStack-Pipeline with Apache License 2.0 5 votes vote down vote up
@Override
public final void write(List<? extends Document> documents) throws Exception {
    BulkRequestBuilder bulkRequest = client.prepareBulk();

    for (Document doc : documents) {
        XContentParser parser = null;
        parser = XContentFactory.xContent(XContentType.JSON)
                .createParser(NamedXContentRegistry.EMPTY,
                        DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
                        doc.getOutputData().getBytes());
        parser.close();
        XContentBuilder builder = jsonBuilder().copyCurrentStructure(parser);


        IndexRequestBuilder request = client.prepareIndex(
                indexName,
                typeName).setSource(
                builder);
        request.setId(doc.getPrimaryKeyFieldValue());
        bulkRequest.add(request);
    }
    //check that no nonessential processes failed
    if (documents.size() != 0) {
        BulkResponse response;
        response = bulkRequest.execute().actionGet(timeout);
        getResponses(response);
    }
}
 
Example 4
Source File: AbstractElasticSearchOutputOperator.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * Create {@link IndexRequestBuilder} for this tuple.
 * It calls {@link #getId(T)}, {@link #getIndexName(T)}, {@link #getType(T)}.
 *
 * @param tuple
 * @return
 */
protected IndexRequestBuilder getIndexRequestBuilder(T tuple)
{
  IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(store.client, getIndexName(tuple));
  String id = getId(tuple);
  if (id != null) {
    indexRequestBuilder.setId(id);
  }
  indexRequestBuilder.setType(getType(tuple));
  return setSource(indexRequestBuilder, tuple);
}
 
Example 5
Source File: ElasticsearchPersistWriter.java    From streams with Apache License 2.0 5 votes vote down vote up
/**
 * add based on supplied parameters.
 * @param indexName indexName
 * @param type type
 * @param id id
 * @param routing routing
 * @param ts ts
 * @param json json
 */
public void add(String indexName, String type, String id, String parent, String routing, String ts, String json) {

  // make sure that these are not null
  Objects.requireNonNull(indexName);
  Objects.requireNonNull(type);
  Objects.requireNonNull(json);

  IndexRequestBuilder indexRequestBuilder = manager.client()
      .prepareIndex(indexName, type)
      .setSource(json);

  // / They didn't specify an ID, so we will create one for them.
  if (id != null) {
    indexRequestBuilder.setId(id);
  }
  if (ts != null) {
    indexRequestBuilder.setTimestamp(ts);
  }
  if (parent != null) {
    indexRequestBuilder.setParent(parent);
  }
  if (routing != null) {
    indexRequestBuilder.setRouting(routing);
  }
  add(indexRequestBuilder.request());
}
 
Example 6
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
protected IndexRequestBuilder createInsertRequest(final EsAbstractEntity esEntity) {
    final IndexRequestBuilder builder = client.prepareIndex().setIndex(asEsIndex()).setSource(toSource(esEntity));
    final String id = esEntity.asDocMeta().id();
    if (id != null) {
        builder.setId(id);
    }
    final RequestOptionCall<IndexRequestBuilder> indexOption = esEntity.asDocMeta().indexOption();
    if (indexOption != null) {
        indexOption.callback(builder);
    }
    return builder;
}
 
Example 7
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
protected IndexRequestBuilder createInsertRequest(final EsAbstractEntity esEntity) {
    final IndexRequestBuilder builder = client.prepareIndex().setIndex(asEsIndex()).setSource(toSource(esEntity));
    final String id = esEntity.asDocMeta().id();
    if (id != null) {
        builder.setId(id);
    }
    final RequestOptionCall<IndexRequestBuilder> indexOption = esEntity.asDocMeta().indexOption();
    if (indexOption != null) {
        indexOption.callback(builder);
    }
    return builder;
}
 
Example 8
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
protected IndexRequestBuilder createInsertRequest(final EsAbstractEntity esEntity) {
    final IndexRequestBuilder builder = client.prepareIndex().setIndex(asEsIndex()).setSource(toSource(esEntity));
    final String id = esEntity.asDocMeta().id();
    if (id != null) {
        builder.setId(id);
    }
    final RequestOptionCall<IndexRequestBuilder> indexOption = esEntity.asDocMeta().indexOption();
    if (indexOption != null) {
        indexOption.callback(builder);
    }
    return builder;
}
 
Example 9
Source File: ESUpdateState.java    From sql4es with Apache License 2.0 4 votes vote down vote up
/**
 * creates a set of index requests based on a set of explicit VALUES  
 * @param insert
 * @param index
 * @return
 * @throws SQLException
 */
@SuppressWarnings("unchecked")
private int insertFromValues(String sql, Insert insert, String index, int maxRequestsPerBulk) throws SQLException {
	Heading heading = new Heading();
	QueryState state = new BasicQueryState(sql, heading, this.props);
	List<Object> values = updateParser.parse(insert, state);
	if(state.hasException()) throw state.getException();
	if(heading.hasLabel("_index") || heading.hasLabel("_type")) throw new SQLException("Not possible to set _index and _type fields");

	String[] indexAndType = this.getIndexAndType(insert.getTarget().toString(), sql, "into\\s+", "\\s+values", index);
	index = indexAndType[0];
	String type = indexAndType[1];
	
	if(values.size() % heading.getColumnCount() != 0) throw new SQLException("Number of columns does not match number of values for one of the inserts");
	
	List<IndexRequestBuilder> indexReqs = new ArrayList<IndexRequestBuilder>();
	int indexCount = 0;
	int valueIdx = 0;
	while(valueIdx < values.size()){
		HashMap<String, Object> fieldValues = new HashMap<String, Object>();
		String id = null;
		for(Column col : heading.columns()){
			Object value = values.get(valueIdx);
			valueIdx++;
			
			if(col.getColumn().equals("_id")){
				id = value.toString();
				continue;
			}
			
			if(col.getColumn().indexOf('.') == -1) {
				fieldValues.put(col.getColumn(), value);
				continue;
			}
				
			// create nested object
			Map<String, Object> map = fieldValues; 
			String[] objectDef = col.getColumn().split("\\.");
			for(int k=0; k<objectDef.length; k++){
				String key = objectDef[k];
				if(k == objectDef.length-1) map.put(key, value);
				else{
					if(!map.containsKey(key)) map.put(key, new HashMap<String, Object>());
						map = (Map<String, Object>)map.get(key);
				}
			}
		}
		
		// create index request
		IndexRequestBuilder indexReq = client.prepareIndex().setIndex(index).setType(type);
		if(id != null) indexReq.setId(id);
		indexReq.setSource(fieldValues);
		indexReqs.add(indexReq);
		if(indexReqs.size() >= maxRequestsPerBulk){
			indexCount += this.execute(indexReqs, maxRequestsPerBulk);
			indexReqs.clear();
		}
	}
	if(indexReqs.size() > 0)  indexCount += this.execute(indexReqs, maxRequestsPerBulk);
	return indexCount;
}
 
Example 10
Source File: ElasticSearchSerializerWithMapping.java    From ingestion with Apache License 2.0 3 votes vote down vote up
/**
 * Prepares an ElasticSearch {@link IndexRequestBuilder} instance
 * 
 * @param indexRequest
 *            The (empty) ElasticSearch {@link IndexRequestBuilder} to
 *            prepare
 * @param indexName
 *            Index name to use -- as per
 *            {@link #getIndexName(String, long)}
 * @param indexType
 *            Index type to use -- as configured on the sink
 * @param event
 *            Flume event to serialize and add to index request
 * @throws IOException
 *             If an error occurs e.g. during serialization
 */
private void prepareIndexRequest(IndexRequestBuilder indexRequest,
		String indexName, String indexType, Event event) throws IOException {
	BytesStream contentBuilder = getContentBuilder(event);
	indexRequest.setIndex(indexName).setType(indexType)
			.setSource(contentBuilder.bytes());
       final String _id = event.getHeaders().get("_id");
       if (_id != null) {
           indexRequest.setId(_id);
       }
}