根据elasticsearch-groovy documentation,有一个BulkRequest的示例。我不想像文档中的示例那样添加请求并在一个大语句中执行,而是要逐步构建BulkRequestActionRequests数组(IndexRequest,DeleteRequest等),然后在一个数组中执行整个数组。块。

我尝试了以下方法:

@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被执行,第二个IndexRequest被丢弃。

最佳答案

正确的做法是这样的:

@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()

08-05 08:49