Skip to content

Commit a35cc3c

Browse files
committed
Merge pull request QHedgeTech#3 from frodegill/master
Grouped index functions, and added function for adding index and deleting index or type
2 parents cd0317e + bd8763b commit a35cc3c

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

src/elasticsearch/elasticsearch.cpp

Lines changed: 31 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,34 @@ 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+
/// Delete given type (and all documents, mappings)
313+
bool ElasticSearch::deleteType(const std::string& index, const std::string& type){
314+
std::ostringstream uri;
315+
uri << index << "/" << type;
316+
return (200 == _http.remove(uri.str().c_str(), 0, 0));
317+
}
318+
319+
// Test if index exists
320+
bool ElasticSearch::exist(const std::string& index){
321+
return (200 == _http.head(index.c_str(), 0, 0));
322+
}
323+
324+
// Create index, optionally with data (settings, mappings etc)
325+
bool ElasticSearch::createIndex(const std::string& index, const char* data){
326+
return (200 == _http.put(index.c_str(), data, 0));
327+
}
328+
329+
// Delete given index (and all types, documents, mappings)
330+
bool ElasticSearch::deleteIndex(const std::string& index){
331+
return (200 == _http.remove(index.c_str(), 0, 0));
332+
}
333+
334+
// Refresh the index.
335+
void ElasticSearch::refresh(const std::string& index){
336+
std::ostringstream oss;
337+
oss << index << "/_refresh";
338+
339+
Json::Object msg;
340+
_http.get(oss.str().c_str(), 0, &msg);
341+
}

src/elasticsearch/elasticsearch.h

Lines changed: 15 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,23 @@ 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+
/// Delete given type (and all documents, mappings)
66+
bool deleteType(const std::string& index, const std::string& type);
67+
68+
public:
69+
/// Test if index exists
70+
bool exist(const std::string& index);
71+
72+
/// Create index, optionally with data (settings, mappings etc)
73+
bool createIndex(const std::string& index, const char* data = NULL);
74+
75+
/// Delete given index (and all types, documents, mappings)
76+
bool deleteIndex(const std::string& index);
77+
6778
/// Refresh the index.
6879
void refresh(const std::string& index);
69-
80+
7081
private:
7182
/// Private constructor.
7283
ElasticSearch();

0 commit comments

Comments
 (0)