Author: humbedooh
Date: Sat Apr  2 09:52:36 2016
New Revision: 1737481

URL: http://svn.apache.org/viewvc?rev=1737481&view=rev
Log:
support custom index name (and thus multiple indices)

Modified:
    steve/trunk/pysteve/lib/backends/es.py

Modified: steve/trunk/pysteve/lib/backends/es.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/backends/es.py?rev=1737481&r1=1737480&r2=1737481&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/backends/es.py (original)
+++ steve/trunk/pysteve/lib/backends/es.py Sat Apr  2 09:52:36 2016
@@ -27,6 +27,7 @@ class ElasticSearchBackend:
     def __init__(self, config):
         " Init - get config and turn it into an ES instance"
         from elasticsearch import Elasticsearch
+        self.index = config.get("elasticsearch", "index") if 
config.has_option("elasticsearch", "index") else "steve"
         self.es = Elasticsearch([
                         {
                             'host': config.get("elasticsearch", "host"),
@@ -37,8 +38,8 @@ class ElasticSearchBackend:
                     ])
         
         # Check that we have a 'steve' index. If not, create it.
-        if not self.es.indices.exists("steve"):
-            self.es.indices.create(index = "steve", body = {
+        if not self.es.indices.exists(self.index):
+            self.es.indices.create(index = self.index, body = {
                     "settings": {
                         "number_of_shards" : 3,
                         "number_of_replicas" : 1
@@ -55,13 +56,13 @@ class ElasticSearchBackend:
         if issue and issue[0]:
             doc = "issues"
             eid = hashlib.sha224(election + "/" + issue[0]).hexdigest()
-        return self.es.exists(index="steve", doc_type=doc, id=eid)
+        return self.es.exists(index=self.index, doc_type=doc, id=eid)
         
         
     
     def get_basedata(self, election):
         "Get base data from an election"
-        res = self.es.get(index="steve", doc_type="elections", id=election)
+        res = self.es.get(index=self.index, doc_type="elections", id=election)
         if res:
             return res['_source']
         return None
@@ -74,7 +75,7 @@ class ElasticSearchBackend:
             basedata['closed'] = False
         else:
             basedata['closed'] = True
-        self.es.index(index="steve", doc_type="elections", id=election, body = 
basedata )
+        self.es.index(index=self.index, doc_type="elections", id=election, 
body = basedata )
             
     
     def issue_get(self, electionID, issueID):
@@ -82,7 +83,7 @@ class ElasticSearchBackend:
         issuedata = None
         ihash = ""
         iid = hashlib.sha224(electionID + "/" + issueID).hexdigest()
-        res = self.es.get(index="steve", doc_type="issues", id=iid)
+        res = self.es.get(index=self.index, doc_type="issues", id=iid)
         if res:
             issuedata = res['_source']
             ihash = hashlib.sha224(json.dumps(issuedata)).hexdigest()
@@ -91,7 +92,7 @@ class ElasticSearchBackend:
     
     def votes_get(self, electionID, issueID):
         "Read votes and return as a dict"
-        res = self.es.search(index="steve", doc_type="votes", q = "election:%s 
AND issue:%s" % (electionID, issueID), size = 9999)
+        res = self.es.search(index=self.index, doc_type="votes", q = 
"election:%s AND issue:%s" % (electionID, issueID), size = 9999)
         results = len(res['hits']['hits'])
         if results > 0:
             votes = {}
@@ -104,7 +105,7 @@ class ElasticSearchBackend:
     
     def votes_get_raw(self, electionID, issueID):
         "Read votes and return raw format"
-        res = self.es.search(index="steve", doc_type="votes", q = "election:%s 
AND issue:%s" % (electionID, issueID), size = 9999)
+        res = self.es.search(index=self.index, doc_type="votes", q = 
"election:%s AND issue:%s" % (electionID, issueID), size = 9999)
         results = len(res['hits']['hits'])
         if results > 0:
             votes = []
@@ -115,7 +116,7 @@ class ElasticSearchBackend:
     
     def vote_history(self, electionID, issueID):
         "Read vote history and return raw format"
-        res = self.es.search(index="steve", doc_type="vote_history", sort = 
"data.timestamp", q = "election:%s AND issue:%s" % (electionID, issueID), size 
= 9999)
+        res = self.es.search(index=self.index, doc_type="vote_history", sort = 
"data.timestamp", q = "election:%s AND issue:%s" % (electionID, issueID), size 
= 9999)
         results = len(res['hits']['hits'])
         if results > 0:
             votes = []
@@ -126,25 +127,25 @@ class ElasticSearchBackend:
     
     def election_create(self,electionID, basedata):
         "Create a new election"
-        self.es.index(index="steve", doc_type="elections", id=electionID, body 
=
+        self.es.index(index=self.index, doc_type="elections", id=electionID, 
body =
             basedata
         );
     
     def election_update(self,electionID, basedata):
         "Update an election with new data"
-        self.es.index(index = "steve", doc_type = "elections", id=electionID, 
body = basedata)
+        self.es.index(index = self.index, doc_type = "elections", 
id=electionID, body = basedata)
     
     
     def issue_update(self,electionID, issueID, issueData):
         "Update an issue with new data"
-        self.es.index(index = "steve", doc_type = "issues", 
id=hashlib.sha224(electionID + "/" + issueID).hexdigest(), body = issueData)
+        self.es.index(index = self.index, doc_type = "issues", 
id=hashlib.sha224(electionID + "/" + issueID).hexdigest(), body = issueData)
     
     
     def issue_list(self, election):
         "List all issues in an election"
         issues = []
         try:
-            res = self.es.search(index="steve", doc_type="issues", sort = 
"id", q = "election:%s" % election, size = 999, _source_include = 'id')
+            res = self.es.search(index=self.index, doc_type="issues", sort = 
"id", q = "election:%s" % election, size = 999, _source_include = 'id')
             results = len(res['hits']['hits'])
             if results > 0:
                 for entry in res['hits']['hits']:
@@ -157,7 +158,7 @@ class ElasticSearchBackend:
         "List all elections"
         elections = []
         try:
-            res = self.es.search(index="steve", doc_type="elections", sort = 
"id", q = "*", size = 9999)
+            res = self.es.search(index=self.index, doc_type="elections", sort 
= "id", q = "*", size = 9999)
             results = len(res['hits']['hits'])
             if results > 0:
                 for entry in res['hits']['hits']:
@@ -173,7 +174,7 @@ class ElasticSearchBackend:
         now = time.time()
         if vhash:
             eid = vhash
-        self.es.index(index="steve", doc_type="votes", id=eid, body =
+        self.es.index(index=self.index, doc_type="votes", id=eid, body =
             {
                 'issue': issueID,
                 'election': electionID,
@@ -185,7 +186,7 @@ class ElasticSearchBackend:
             }
         );
         # Backlog of changesets
-        self.es.index(index="steve", doc_type="vote_history", body =
+        self.es.index(index=self.index, doc_type="vote_history", body =
             {
                 'issue': issueID,
                 'election': electionID,
@@ -200,11 +201,11 @@ class ElasticSearchBackend:
         
     def issue_delete(self, electionID, issueID):
         "Deletes an issue if it exists"
-        self.es.delete(index="steve", doc_type="issues", 
id=hashlib.sha224(electionID + "/" + issueID).hexdigest());
+        self.es.delete(index=self.index, doc_type="issues", 
id=hashlib.sha224(electionID + "/" + issueID).hexdigest());
         
     def issue_create(self,electionID, issueID, data):
         "Create an issue"
-        self.es.index(index="steve", doc_type="issues", 
id=hashlib.sha224(electionID + "/" + issueID).hexdigest(), body = data);
+        self.es.index(index=self.index, doc_type="issues", 
id=hashlib.sha224(electionID + "/" + issueID).hexdigest(), body = data);
     
     
     
@@ -213,7 +214,7 @@ class ElasticSearchBackend:
         
         # First, try the raw hash as an ID
         try:
-            res = self.es.get(index="steve", doc_type="voters", id=votekey)
+            res = self.es.get(index=self.index, doc_type="voters", id=votekey)
             if res:
                 return res['_source']['uid']
         except:
@@ -221,7 +222,7 @@ class ElasticSearchBackend:
         
         # Now, look for it as hash inside the doc
         try:
-            res = self.es.search(index="steve", doc_type="voters", q = 
"election:%s" % electionID, size = 9999)
+            res = self.es.search(index=self.index, doc_type="voters", q = 
"election:%s" % electionID, size = 9999)
             results = len(res['hits']['hits'])
             if results > 0:
                 for entry in res['hits']['hits']:
@@ -234,7 +235,7 @@ class ElasticSearchBackend:
     def voter_add(self,election, PID, xhash):
         "Add a voter to the DB"
         eid = hashlib.sha224(election + ":" + PID).hexdigest()
-        self.es.index(index="steve", doc_type="voters", id=eid, body = {
+        self.es.index(index=self.index, doc_type="voters", id=eid, body = {
             'election': election,
             'hash': xhash,
             'uid': PID
@@ -254,7 +255,7 @@ class ElasticSearchBackend:
         for issue in issues:
             vhash = hashlib.sha224(xhash + issue).hexdigest()
             try:
-                self.es.delete(index="steve", doc_type="votes", id=vhash);
+                self.es.delete(index=self.index, doc_type="votes", id=vhash);
             except:
                 pass
         return True
@@ -262,13 +263,13 @@ class ElasticSearchBackend:
     def voter_remove(self,election, UID):
         "Remove the voter with the given UID"
         votehash = hashlib.sha224(election + ":" + UID).hexdigest()
-        self.es.delete(index="steve", doc_type="voters", id=votehash);
+        self.es.delete(index=self.index, doc_type="voters", id=votehash);
     
     def voter_has_voted(self,election, issue, uid):
         "Return true if the voter has voted on this issue, otherwise false"
         eid = hashlib.sha224(election + ":" + issue + ":" + uid).hexdigest()
         try:
-            return self.es.exists(index="steve", doc_type="votes", id=eid)
+            return self.es.exists(index=self.index, doc_type="votes", id=eid)
         except:
             return False
 
@@ -278,7 +279,7 @@ class ElasticSearchBackend:
         # First, get all elections
         elections = {}
         
-        res = self.es.search(index="steve", doc_type="elections", sort = "id", 
q = "*", size = 9999)
+        res = self.es.search(index=self.index, doc_type="elections", sort = 
"id", q = "*", size = 9999)
         results = len(res['hits']['hits'])
         if results > 0:
             for entry in res['hits']['hits']:
@@ -291,7 +292,7 @@ class ElasticSearchBackend:
                 
         # Then, get all ballots and note whether they still apply or not
         ballots = {}
-        res = self.es.search(index="steve", doc_type="voters", body = {
+        res = self.es.search(index=self.index, doc_type="voters", body = {
             "query": {
                 "match": {
                     "uid": UID


Reply via email to