2015-08-22 103 views
1

根據elasticsearch-groovy documentation,有一個BulkRequest的例子。我不想在一個大的語句中添加請求並執行,就像文檔中的示例一樣,我想逐步構建一個BulkRequest或一個ActionRequests(IndexRequest,DeleteRequest,...)數組,然後在一個句柄中執行整個數組塊。Elasticsearch-groovy批量請求

我試過如下:

@Grab(group='org.elasticsearch', module='elasticsearch-groovy', version='1.7.0') 

import org.elasticsearch.client.Client 
import org.elasticsearch.node.Node 
import static org.elasticsearch.node.NodeBuilder.nodeBuilder 

import org.elasticsearch.action.ActionRequest 
import org.elasticsearch.action.bulk.* 
import org.elasticsearch.action.index.IndexRequest 

Node node = nodeBuilder().settings { 
       cluster { 
        name = "lemato_cluster" 
        } 
        node { 
        client = true 
        } 
      }.node() 

// Get a usable Node Client     
Client client = node.client 

BulkRequest indexBulk = [] 

indexBulk.add new IndexRequest().with { 
    index "my_index" 
    type "my_type" 
    id "1" 
    source { 
     user = "kimchy" 
     postDate = "2013-01-30" 
     message = "trying out Elasticsearch" 
     nested { 
     details { 
      here = 123 
      timestamp = new Date() 
     } 
     } 
    } 
} 

indexBulk.add new IndexRequest().with { 
    index "my_index" 
    type "my_type" 
    id "2" 
    source { 
     user = "kimchy2" 
     postDate = "2013-02-30" 
     message = "trying out Elasticsearch for my 2nd set" 
     nested { 
     details { 
      here = 123 
      timestamp = new Date() 
     } 
     } 
    } 
} 

BulkResponse br = client.bulk(indexBulk).actionGet() 
println br.getItems() 

node.close() 

這工作得很好,但可惜只執行第一IndexRequest,第二個被丟棄。

回答

0

我認爲要做到這一點的唯一方法,就是使用BulkRequestBuilder

@Grab(group='org.elasticsearch', module='elasticsearch-groovy', version='1.7.0') 

import org.elasticsearch.client.Client 
import org.elasticsearch.node.Node 
import static org.elasticsearch.node.NodeBuilder.nodeBuilder 

import org.elasticsearch.action.ActionRequest 
import org.elasticsearch.action.bulk.* 
import org.elasticsearch.action.index.IndexRequest 

Node node = nodeBuilder().settings { 
       cluster { 
        name = "lemato_cluster" 
        } 
        node { 
        client = true 
        } 
      }.node() 

// Get a usable Node Client     
Client client = node.client 

import static org.elasticsearch.common.xcontent.XContentFactory.*; 

BulkRequestBuilder bulkRequest = client.prepareBulk(); 

// either use client#prepare, or use Requests# to directly build index/delete requests 
bulkRequest.add(client.prepareIndex("twitter", "tweet", "1") 
     .setSource{ 
      user = "kimchy" 
      postDate = new Date()   
      message = "just a post" 
     } 
    ); 

bulkRequest.add(client.prepareIndex("twitter", "tweet", "2") 
     .setSource{ 
      user = "kimchy" 
      postDate = new Date()   
      message = "another post" 
     } 
    ); 

BulkResponse bulkResponse = bulkRequest.execute().actionGet(); 
0

做的正確的方法是這樣的:

@Grab(group='org.elasticsearch', module='elasticsearch-groovy', version='1.7.0') 

import org.elasticsearch.client.Client 
import org.elasticsearch.node.Node 
import static org.elasticsearch.node.NodeBuilder.nodeBuilder 

import org.elasticsearch.action.ActionRequest 
import org.elasticsearch.action.bulk.* 
import org.elasticsearch.action.index.IndexRequest 

Node node = nodeBuilder().settings { 
       cluster { 
        name = "lemato_cluster" 
        } 
        node { 
        client = true 
        } 
      }.node() 

// Get a usable Node Client     
Client client = node.client 

BulkResponse response = client.bulk { 
    add new IndexRequest().with {   <--- call "add" and then specify the list of request to add 
    index "my_index" 
    type "my_type" 
    id "1" 
    source { 
     user = "kimchy" 
     postDate = "2013-01-30" 
     message = "trying out Elasticsearch" 
     nested { 
     details { 
      here = 123 
      timestamp = new Date() 
     } 
     } 
    } 
    }, 
    new IndexRequest().with {    <--- adding the second request 
    index "my_index" 
    type "my_type" 
    id "2" 
    source { 
     user = "kimchy2" 
     postDate = "2013-02-30" 
     message = "trying out Elasticsearch for my 2nd set" 
     nested { 
     details { 
      here = 123 
      timestamp = new Date() 
     } 
     } 
    } 
    }, 

    ... <--- add your other IndexRequest, UpdateRequest or DeleteRequest separated by commas. 

}.actionGet() 

println response.getItems() 

node.close() 
+0

感謝,但我真正想要的是逐步建立陣列。例如:'myactionRequestArray.add(new client.index ...);一些其他的代碼; myactionRequestArray.add(new client.index ...);一些其他的代碼; response = myactionRequestArray.execute()。actionGet()' – boraas