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

The following examples show how to use org.elasticsearch.action.index.IndexRequestBuilder#setRouting() . 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: 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 2
Source File: IndexingComponent.java    From elasticsearch-reindex-tool with Apache License 2.0 6 votes vote down vote up
public Optional<BulkResult> indexData(ElasticDataPointer targetDataPointer, SearchHit[] hits) {
  BulkRequestBuilder bulkRequest = createBulkRequestBuilder();

  for (SearchHit hit : hits) {
    Map<String, Object> source = hit.getSource();

    IndexRequestBuilder requestBuilder = prepareIndex(targetDataPointer.getIndexName(), targetDataPointer
        .getTypeName(), hit.getId(), source, hit.getIndex());
    if (hit.getFields().get("_ttl") != null) {
      requestBuilder.setTTL(hit.getFields().get("_ttl").value());
    }
    if (hit.getFields().get("_routing") != null) {
      requestBuilder.setRouting(hit.getFields().get("_routing").value());
    }
    requestBuilder.setSource(source);
    bulkRequest.add(requestBuilder);
  }
  return executeBulk(hits.length, bulkRequest);
}
 
Example 3
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを登録する.
 * @param index インデックス名
 * @param type タイプ名
 * @param id ドキュメントのid
 * @param routingId routingId
 * @param data データ
 * @param opType 操作タイプ
 * @param version version番号
 * @return 非同期応答
 */
public ActionFuture<IndexResponse> asyncIndex(String index,
        String type,
        String id,
        String routingId,
        Map<String, Object> data,
        OpType opType,
        long version) {
    IndexRequestBuilder req = esTransportClient.prepareIndex(index, type, id).setSource(data).setOpType(opType)
            .setConsistencyLevel(WriteConsistencyLevel.DEFAULT).setRefresh(true);
    if (routingFlag) {
        req = req.setRouting(routingId);
    }
    if (version > -1) {
        req.setVersion(version);
    }

    ActionFuture<IndexResponse> ret = req.execute();
    EsRequestLogInfo logInfo = new EsRequestLogInfo(index, type, id, routingId, data, opType.toString(),
            version);
    this.fireEvent(Event.afterCreate, logInfo);

    return ret;
}
 
Example 4
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * ルーティングIDに関係なくバルクでドキュメントを登録.
 * @param index インデックス名
 * @param bulkMap バルクドキュメント
 * @return ES応答
 */
public DcBulkResponse asyncBulkCreate(
        String index, Map<String, List<EsBulkRequest>> bulkMap) {
    BulkRequestBuilder bulkRequest = esTransportClient.prepareBulk();
    // ルーティングIDごとにバルク登録を行うと効率が悪いため、引数で渡されたEsBulkRequestは全て一括登録する。
    // また、バルク登録後にactionGet()すると同期実行となるため、ここでは実行しない。
    // このため、execute()のレスポンスを返却し、呼び出し側でactionGet()してからレスポンスチェック、リフレッシュすること。
    for (Entry<String, List<EsBulkRequest>> ents : bulkMap.entrySet()) {
        for (EsBulkRequest data : ents.getValue()) {
            IndexRequestBuilder req = esTransportClient.
                    prepareIndex(index, data.getType(), data.getId()).setSource(data.getSource());
            if (routingFlag) {
                req = req.setRouting(ents.getKey());
            }
            bulkRequest.add(req);
        }
    }
    DcBulkResponse response = DcBulkResponseImpl.getInstance(bulkRequest.execute().actionGet());
    return response;
}
 
Example 5
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを登録する.
 * @param index インデックス名
 * @param type タイプ名
 * @param id ドキュメントのid
 * @param routingId routingId
 * @param data データ
 * @param opType 操作タイプ
 * @param version version番号
 * @return 非同期応答
 */
public ActionFuture<IndexResponse> asyncIndex(String index,
        String type,
        String id,
        String routingId,
        Map<String, Object> data,
        OpType opType,
        long version) {
    IndexRequestBuilder req = esTransportClient.prepareIndex(index, type, id).setSource(data).setOpType(opType)
            .setConsistencyLevel(WriteConsistencyLevel.DEFAULT).setRefresh(true);
    if (routingFlag) {
        req = req.setRouting(routingId);
    }
    if (version > -1) {
        req.setVersion(version);
    }

    ActionFuture<IndexResponse> ret = req.execute();
    EsRequestLogInfo logInfo = new EsRequestLogInfo(index, type, id, routingId, data, opType.toString(),
            version);
    this.fireEvent(Event.afterCreate, logInfo);

    return ret;
}
 
Example 6
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * ルーティングIDに関係なくバルクでドキュメントを登録.
 * @param index インデックス名
 * @param bulkMap バルクドキュメント
 * @return ES応答
 */
public DcBulkResponse asyncBulkCreate(
        String index, Map<String, List<EsBulkRequest>> bulkMap) {
    BulkRequestBuilder bulkRequest = esTransportClient.prepareBulk();
    // ルーティングIDごとにバルク登録を行うと効率が悪いため、引数で渡されたEsBulkRequestは全て一括登録する。
    // また、バルク登録後にactionGet()すると同期実行となるため、ここでは実行しない。
    // このため、execute()のレスポンスを返却し、呼び出し側でactionGet()してからレスポンスチェック、リフレッシュすること。
    for (Entry<String, List<EsBulkRequest>> ents : bulkMap.entrySet()) {
        for (EsBulkRequest data : ents.getValue()) {
            IndexRequestBuilder req = esTransportClient.
                    prepareIndex(index, data.getType(), data.getId()).setSource(data.getSource());
            if (routingFlag) {
                req = req.setRouting(ents.getKey());
            }
            bulkRequest.add(req);
        }
    }
    DcBulkResponse response = DcBulkResponseImpl.getInstance(bulkRequest.execute().actionGet());
    return response;
}
 
Example 7
Source File: ESTemplate.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 插入数据
 *
 * @param mapping 配置对象
 * @param pkVal 主键值
 * @param esFieldData 数据Map
 */
public void insert(ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
    if (mapping.get_id() != null) {
        String parentVal = (String) esFieldData.remove("$parent_routing");
        if (mapping.isUpsert()) {
            UpdateRequestBuilder updateRequestBuilder = transportClient
                .prepareUpdate(mapping.get_index(), mapping.get_type(), pkVal.toString())
                .setDoc(esFieldData)
                .setDocAsUpsert(true);
            if (StringUtils.isNotEmpty(parentVal)) {
                updateRequestBuilder.setRouting(parentVal);
            }
            getBulk().add(updateRequestBuilder);
        } else {
            IndexRequestBuilder indexRequestBuilder = transportClient
                .prepareIndex(mapping.get_index(), mapping.get_type(), pkVal.toString())
                .setSource(esFieldData);
            if (StringUtils.isNotEmpty(parentVal)) {
                indexRequestBuilder.setRouting(parentVal);
            }
            getBulk().add(indexRequestBuilder);
        }
        commitBulk();
    } else {
        SearchResponse response = transportClient.prepareSearch(mapping.get_index())
            .setTypes(mapping.get_type())
            .setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal))
            .setSize(10000)
            .get();
        for (SearchHit hit : response.getHits()) {
            getBulk().add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(), hit.getId())
                .setDoc(esFieldData));
            commitBulk();
        }
    }

}
 
Example 8
Source File: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
protected IndexRequestBuilder completeIndexRequestBuilder(IndexRequestBuilder requestBuilder,
                                                          String resourceName, EntityContentProducer ecp,
                                                          boolean includeContent)
        throws IOException{
    return requestBuilder.setRouting(null);
}
 
Example 9
Source File: InternalEsClient.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * バルクリクエストのINDEXリクエストを作成する.
 * @param index インデックス名
 * @param routingId ルーティングID
 * @param data バルクドキュメント情報
 * @return 作成したINDEXリクエスト
 */
private IndexRequestBuilder createIndexRequest(String index, String routingId, EsBulkRequest data) {
    IndexRequestBuilder request = esTransportClient.
            prepareIndex(index, data.getType(), data.getId()).setSource(data.getSource());
    if (routingFlag) {
        request = request.setRouting(routingId);
    }
    return request;
}
 
Example 10
Source File: InternalEsClient.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * バルクリクエストのINDEXリクエストを作成する.
 * @param index インデックス名
 * @param routingId ルーティングID
 * @param data バルクドキュメント情報
 * @return 作成したINDEXリクエスト
 */
private IndexRequestBuilder createIndexRequest(String index, String routingId, EsBulkRequest data) {
    IndexRequestBuilder request = esTransportClient.
            prepareIndex(index, data.getType(), data.getId()).setSource(data.getSource());
    if (routingFlag) {
        request = request.setRouting(routingId);
    }
    return request;
}
 
Example 11
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 12
Source File: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
protected IndexRequestBuilder completeIndexRequestBuilder(IndexRequestBuilder requestBuilder,
                                                          String resourceName, EntityContentProducer ecp,
                                                          boolean includeContent)
        throws IOException{
    return requestBuilder.setRouting(null);
}
 
Example 13
Source File: SiteElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@Override
protected IndexRequestBuilder completeIndexRequestBuilder(IndexRequestBuilder requestBuilder, String resourceName,
                                                          EntityContentProducer ecp, boolean includeContent) {
    return requestBuilder.setRouting(ecp.getSiteId(resourceName));
}
 
Example 14
Source File: SiteElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@Override
protected IndexRequestBuilder completeIndexRequestBuilder(IndexRequestBuilder requestBuilder, String resourceName,
                                                          EntityContentProducer ecp, boolean includeContent) {
    return requestBuilder.setRouting(ecp.getSiteId(resourceName));
}