Elasticsearch可以通過(guò)http報(bào)文和json語(yǔ)句來(lái)進(jìn)行增刪查改,可以通過(guò)libcurl構(gòu)造語(yǔ)句,去發(fā)送到es集群進(jìn)行操作。參考
https://blog.csdn.net/xsdxs/article/details/72849796
https://stackoverflow.com/questions/25887453/how-to-use-libcurl-in-c-to-send-post-to-elasticsearch
實(shí)現(xiàn)一個(gè)簡(jiǎn)便的esclient()函數(shù),方便讀寫

運(yùn)行結(jié)果:
圖1是查詢單條語(yǔ)句的結(jié)果
圖2是批量查詢的結(jié)果
圖3是以上程序運(yùn)行前的圖
圖4是以上程序運(yùn)行后的圖




附錄:以上源碼
```
????#include
????#include
????#include
????#include
????#include
????using namespace std;
????size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
????{
? ????? strncat((char *)stream, (char *)ptr, size*nmemb);
? ? ????return size * nmemb;
????}
????int esclient(const string &_http, const string &_json, const string &_command)
????{
????????????CURL *curl;
????????????CURLcode res;
????????????struct curl_slist* headers = NULL;????????
????????????curl = curl_easy_init();
????????????curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
????????????curl_easy_setopt(curl, CURLOPT_URL, _http.c_str());
????????????headers=curl_slist_append(headers, "Content-Type:application/json");
????????????curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
????????????char out[40960]={0};????????
????????????curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
????????????curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);
????????????if(_command=="delete")
????????????{
????????????????????curl_easy_setopt(curl,? CURLOPT_CUSTOMREQUEST, "DELETE");
????????????}
????????????else if(_command=="get")
????????????{????????????????
????????????????????curl_easy_setopt(curl,? CURLOPT_CUSTOMREQUEST, "GET");????????????????
????????????}
????????????else
????????????{
????????????????????curl_easy_setopt(curl,? CURLOPT_CUSTOMREQUEST, "POST");
????????????}
????????????if(!_json.empty())
????????????{
????????????????????curl_easy_setopt(curl,CURLOPT_POSTFIELDS,_json.c_str());
????????????????????curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,_json.length());
????????????}
????????????res = curl_easy_perform(curl);
???????????printf("%s\n",out);
????????????curl_easy_cleanup(curl);
????????????return res;
????}
????int main(void)
????{
????????????esclient("http://172.31.100.114:9200/customer/external/1","","get");????????//查詢單條數(shù)據(jù)
????????????esclient("http://172.31.100.114:9200/bank/_search","","search");????????//批量查詢數(shù)據(jù),但是只返回10條,如需其他要求,加json語(yǔ)句
????????????esclient("http://172.31.100.114:9200/customer/external/3/"," { \"name\": \"hhh\" }","add");????????????????//增加一條數(shù)據(jù)
????????????esclient("http://172.31.100.114:9200/customer/external/2/_updata/"," {\"doc\": { \"name\": \"eee\" }}","updata");????????//更新某條數(shù)據(jù)
????????????esclient("http://172.31.100.114:9200/ccc","","delete");????????????????//刪除索引
????????????esclient("http://172.31.100.114:9200/customer/external/_delete_by_query"," {\"query\":{ \"match\":{\"name\": \"hhh\" }}}","");????????//匹配刪除數(shù)據(jù)
????}
```