Skip to content

Commit a6a7ac0

Browse files
committed
Added createIndex (with optional data for mappings etc)
1 parent cd0317e commit a6a7ac0

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/elasticsearch/elasticsearch.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ ElasticSearch::ElasticSearch(const std::string& node, bool readOnly): _http(node
1616
ElasticSearch::~ElasticSearch() {
1717
}
1818

19-
// Refresh the index.
20-
void ElasticSearch::refresh(const std::string& index){
21-
std::ostringstream oss;
22-
oss << index << "/_refresh";
23-
24-
Json::Object msg;
25-
_http.get(oss.str().c_str(), 0, &msg);
26-
}
27-
2819
// Test connection with node.
2920
bool ElasticSearch::isActive() {
3021

@@ -162,11 +153,6 @@ long unsigned int ElasticSearch::getDocumentCount(const char* index, const char*
162153
return pos;
163154
}
164155

165-
// Test if index exists
166-
bool ElasticSearch::exist(const std::string& index){
167-
return (200 == _http.head(index.c_str(), 0, 0));
168-
}
169-
170156
// Test if document exists
171157
bool ElasticSearch::exist(const std::string& index, const std::string& type, const std::string& id){
172158
std::stringstream url;
@@ -322,3 +308,22 @@ long ElasticSearch::search(const std::string& index, const std::string& type, co
322308

323309
return result.getValue("hits").getObject().getValue("total").getLong();
324310
}
311+
312+
// Test if index exists
313+
bool ElasticSearch::exist(const std::string& index){
314+
return (200 == _http.head(index.c_str(), 0, 0));
315+
}
316+
317+
// Test if index exists
318+
bool ElasticSearch::createIndex(const std::string& index, const char* data){
319+
return (200 == _http.put(index.c_str(), data, 0));
320+
}
321+
322+
// Refresh the index.
323+
void ElasticSearch::refresh(const std::string& index){
324+
std::ostringstream oss;
325+
oss << index << "/_refresh";
326+
327+
Json::Object msg;
328+
_http.get(oss.str().c_str(), 0, &msg);
329+
}

src/elasticsearch/elasticsearch.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ class ElasticSearch {
3434
/// Delete the document by index/type.
3535
bool deleteAll(const char* index, const char* type);
3636

37-
/// Test if index exists
38-
bool exist(const std::string& index);
39-
4037
/// Test if document exists
4138
bool exist(const std::string& index, const std::string& type, const std::string& id);
4239

@@ -64,9 +61,16 @@ class ElasticSearch {
6461
/// Perform a scan to get all results from a query.
6562
int fullScan(const std::string& index, const std::string& type, const std::string& query, Json::Array& resultArray, int scrollSize = 1000);
6663

64+
public:
65+
/// Test if index exists
66+
bool exist(const std::string& index);
67+
68+
/// Create index, optionally with data (settings, mappings etc)
69+
bool createIndex(const std::string& index, const char* data = NULL);
70+
6771
/// Refresh the index.
6872
void refresh(const std::string& index);
69-
73+
7074
private:
7175
/// Private constructor.
7276
ElasticSearch();

0 commit comments

Comments
 (0)