How to reindex in ElasticSearch using the Java API

As the headline says ...

I read this article ( https://www.elastic.co/blog/changing-mapping-with-zero-downtime ) and the concept was great, but I struggled to find decent help on how to do this through JAVA API

I found this plugin: https://github.com/karussell/elasticsearch-reindex , but what I am trying to do seems to be unnecessary

+5
source share
3 answers

After doing some research at a local Starbucks, here's what I came up with:

, ( "old_index" ), ... ( "new_index" ), (, STRING vs INT , , ..).

- ( "old_index" ) ( "new_index" ). , :

1. https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html

, , . .. : " , , , .

Java API , : https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/scrolling.html

2. . . Ingest Java API: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/bulk.html#_using_bulk_processor

ho, ...

1. , ""

SearchResponse scrollResp = client.prepareSearch("old_index") // Specify index
    .setSearchType(SearchType.SCAN)
    .setScroll(new TimeValue(60000))
    .setQuery(QueryBuilders.matchAllQuery()) // Match all query
    .setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll

2. .

int BULK_ACTIONS_THRESHOLD = 1000;
int BULK_CONCURRENT_REQUESTS = 1;
BulkProcessor bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {
    @Override
    public void beforeBulk(long executionId, BulkRequest request) {
        logger.info("Bulk Going to execute new bulk composed of {} actions", request.numberOfActions());
    }

    @Override
    public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
        logger.info("Executed bulk composed of {} actions", request.numberOfActions());
    }

    @Override
    public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
        logger.warn("Error executing bulk", failure);
    }
    }).setBulkActions(BULK_ACTIONS_THRESHOLD).setConcurrentRequests(BULK_CONCURRENT_REQUESTS).setFlushInterval(TimeValue.timeValueMillis(5)).build();

3. 1 , mo-

//Scroll until no hits are returned
while (true) {
    scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
    //Break condition: No hits are returned
    if (scrollResp.getHits().getHits().length == 0) {
        logger.info("Closing the bulk processor");
        bulkProcessor.close();
        break; 
    }
    // Get results from a scan search and add it to bulk ingest
    for (SearchHit hit: scrollResp.getHits()) {
        IndexRequest request = new IndexRequest("new_index", hit.type(), hit.id());
        Map source = ((Map) ((Map) hit.getSource()));
        request.source(source);
        bulkProcessor.add(request);
   }
}

4. , , . , . , , , . : ElasticSeach JAVA API

client.admin().indices().prepareAliases().addAlias("new_index", "alias_name").get();

,

client.admin().indices().prepareAliases().removeAlias("old_index", "alias_name").execute().actionGet();
client.admin().indices().prepareDelete("old_index").execute().actionGet();
+9

ES 2.0 API . , API Java, :

  1. Maven ES
  2. :

    client = TransportClient.builder().settings(elaSettings).addPlugin(ReindexPlugin.class).build();
    
  3. api reindex

    ReindexRequestBuilder builder = ReindexAction.INSTANCE.newRequestBuilder(client).source(oldIndex).destination(newIndex);  
    builder.destination().setOpType(opType);
    builder.abortOnVersionConflict(false); builder.get();
    
+3

When using Jest, you can use Reindex.Builder (io.searchbox.indices.reindex.Reindex). As of this post, the latest Jest 5.3.2 has it.

0
source

All Articles