|
5 | 5 | #include <cstring>
|
6 | 6 | #include <cassert>
|
7 | 7 | #include <locale>
|
| 8 | +#include <vector> |
8 | 9 |
|
9 | 10 | ElasticSearch::ElasticSearch(const std::string& node, bool readOnly): _http(node, true), _readOnly(readOnly) {
|
10 | 11 |
|
@@ -309,7 +310,7 @@ long ElasticSearch::search(const std::string& index, const std::string& type, co
|
309 | 310 | return result.getValue("hits").getObject().getValue("total").getLong();
|
310 | 311 | }
|
311 | 312 |
|
312 |
| - /// Delete given type (and all documents, mappings) |
| 313 | +/// Delete given type (and all documents, mappings) |
313 | 314 | bool ElasticSearch::deleteType(const std::string& index, const std::string& type){
|
314 | 315 | std::ostringstream uri;
|
315 | 316 | uri << index << "/" << type;
|
@@ -339,3 +340,89 @@ void ElasticSearch::refresh(const std::string& index){
|
339 | 340 | Json::Object msg;
|
340 | 341 | _http.get(oss.str().c_str(), 0, &msg);
|
341 | 342 | }
|
| 343 | + |
| 344 | +// Bulk API of ES. |
| 345 | +bool ElasticSearch::bulk(const char* data, Json::Object& jResult) { |
| 346 | + if(_readOnly) |
| 347 | + return false; |
| 348 | + |
| 349 | + return (200 == _http.post("/_bulk", data, &jResult)); |
| 350 | +} |
| 351 | + |
| 352 | +BulkBuilder::BulkBuilder() {} |
| 353 | + |
| 354 | +void BulkBuilder::createCommand(const std::string &op, const std::string &index, const std::string &type, const std::string &id = "") { |
| 355 | + Json::Object command; |
| 356 | + Json::Object commandParams; |
| 357 | + |
| 358 | + if (id != "") { |
| 359 | + commandParams.addMemberByKey("_id", id); |
| 360 | + } |
| 361 | + |
| 362 | + commandParams.addMemberByKey("_index", index); |
| 363 | + commandParams.addMemberByKey("_type", type); |
| 364 | + |
| 365 | + command.addMemberByKey(op, commandParams); |
| 366 | + operations.push_back(command); |
| 367 | +} |
| 368 | + |
| 369 | +void BulkBuilder::index(const std::string &index, const std::string &type, const std::string &id, const Json::Object &fields) { |
| 370 | + createCommand("index", index, type, id); |
| 371 | + operations.push_back(fields); |
| 372 | +} |
| 373 | + |
| 374 | +void BulkBuilder::create(const std::string &index, const std::string &type, const std::string &id, const Json::Object &fields) { |
| 375 | + createCommand("create", index, type, id); |
| 376 | + operations.push_back(fields); |
| 377 | +} |
| 378 | + |
| 379 | +void BulkBuilder::index(const std::string &index, const std::string &type, const Json::Object &fields) { |
| 380 | + createCommand("index", index, type); |
| 381 | + operations.push_back(fields); |
| 382 | +} |
| 383 | + |
| 384 | +void BulkBuilder::create(const std::string &index, const std::string &type, const Json::Object &fields) { |
| 385 | + createCommand("create", index, type); |
| 386 | + operations.push_back(fields); |
| 387 | +} |
| 388 | + |
| 389 | +void BulkBuilder::update(const std::string &index, const std::string &type, const std::string &id, const Json::Object &fields) { |
| 390 | + createCommand("update", index, type, id); |
| 391 | + |
| 392 | + Json::Object updateFields; |
| 393 | + updateFields.addMemberByKey("doc", fields); |
| 394 | + |
| 395 | + operations.push_back(updateFields); |
| 396 | +} |
| 397 | + |
| 398 | +void BulkBuilder::del(const std::string &index, const std::string &type, const std::string &id) { |
| 399 | + createCommand("delete", index, type, id); |
| 400 | +} |
| 401 | + |
| 402 | +void BulkBuilder::upsert(const std::string &index, const std::string &type, const std::string &id, const Json::Object &fields) { |
| 403 | + createCommand("update", index, type, id); |
| 404 | + |
| 405 | + Json::Object updateFields; |
| 406 | + updateFields.addMemberByKey("doc", fields); |
| 407 | + updateFields.addMemberByKey("doc_as_upsert", true); |
| 408 | + |
| 409 | + operations.push_back(updateFields); |
| 410 | +} |
| 411 | + |
| 412 | +std::string BulkBuilder::str() { |
| 413 | + std::stringstream json; |
| 414 | + |
| 415 | + for(auto &operation : operations) { |
| 416 | + json << operation.str() << std::endl; |
| 417 | + } |
| 418 | + |
| 419 | + return json.str(); |
| 420 | +} |
| 421 | + |
| 422 | +void BulkBuilder::clear() { |
| 423 | + operations.clear(); |
| 424 | +} |
| 425 | + |
| 426 | +bool BulkBuilder::isEmpty() { |
| 427 | + return operations.empty(); |
| 428 | +} |
0 commit comments