about summary refs log tree commit diff
path: root/wqflask/utility/elasticsearch_tools.py
diff options
context:
space:
mode:
authorPjotr Prins2018-02-15 08:44:57 +0000
committerPjotr Prins2018-02-15 08:44:57 +0000
commitd636315ad629cb56ea3e697924160432f6132792 (patch)
tree476ed4196409a1dc918a41ca8d86400863eab875 /wqflask/utility/elasticsearch_tools.py
parent2602be69f2869de376d1b9ced6131d880e9476c2 (diff)
parent1defefd05d0eb658fb5922fc755547261a5e914a (diff)
downloadgenenetwork2-d636315ad629cb56ea3e697924160432f6132792.tar.gz
Fix conflict
Diffstat (limited to 'wqflask/utility/elasticsearch_tools.py')
-rw-r--r--wqflask/utility/elasticsearch_tools.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/wqflask/utility/elasticsearch_tools.py b/wqflask/utility/elasticsearch_tools.py
new file mode 100644
index 00000000..a964b025
--- /dev/null
+++ b/wqflask/utility/elasticsearch_tools.py
@@ -0,0 +1,46 @@
+from elasticsearch import Elasticsearch, TransportError
+import logging
+
+def get_elasticsearch_connection():
+    es = None
+    try:
+        from utility.tools import ELASTICSEARCH_HOST, ELASTICSEARCH_PORT
+
+        es = Elasticsearch([{
+            "host": ELASTICSEARCH_HOST
+            , "port": ELASTICSEARCH_PORT
+        }]) if (ELASTICSEARCH_HOST and ELASTICSEARCH_PORT) else None
+
+        es_logger = logging.getLogger("elasticsearch")
+        es_logger.setLevel(logging.INFO)
+        es_logger.addHandler(logging.NullHandler())
+    except:
+        es = None
+
+    return es
+
+def get_user_by_unique_column(es, column_name, column_value, index="users", doc_type="local"):
+    return get_item_by_unique_column(es, column_name, column_value, index=index, doc_type=doc_type)
+
+def save_user(es, user, user_id):
+    es_save_data(es, "users", "local", user, user_id)
+
+def get_item_by_unique_column(es, column_name, column_value, index, doc_type):
+    item_details = None
+    try:
+        response = es.search(
+            index = index
+            , doc_type = doc_type
+            , body = { 
+                "query": { "match": { column_name: column_value } } 
+            })
+        if len(response["hits"]["hits"]) > 0:
+            item_details = response["hits"]["hits"][0]["_source"]
+    except TransportError as te: 
+        pass
+    return item_details
+
+def es_save_data(es, index, doc_type, data_item, data_id,):
+    from time import sleep
+    es.create(index, doc_type, body=data_item, id=data_id)
+    sleep(1) # Delay 1 second to allow indexing