根據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,第二個被丟棄。
感謝,但我真正想要的是逐步建立陣列。例如:'myactionRequestArray.add(new client.index ...);一些其他的代碼; myactionRequestArray.add(new client.index ...);一些其他的代碼; response = myactionRequestArray.execute()。actionGet()' – boraas