aboutsummaryrefslogtreecommitdiff
path: root/wqflask/utility/elasticsearch_tools.py
blob: c2c999eaf45a0b952bca84c7733c25bb4e4504e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from elasticsearch import Elasticsearch, TransportError
from utility.tools import ELASTICSEARCH_HOST, ELASTICSEARCH_PORT

es = Elasticsearch([{
    "host": ELASTICSEARCH_HOST
    , "port": ELASTICSEARCH_PORT
}]) 

def get_user_by_unique_column(column_name, column_value):
    user_details = None
    try:
        response = es.search(
            index = "users"
            , doc_type = "local"
            , body = { 
                "query": { "match": { column_name: column_value } } 
            })
        if len(response["hits"]["hits"]) > 0:
            user_details = response["hits"]["hits"][0]["_source"]
    except TransportError as te: 
        pass
    return user_details

def save_user(user, user_id, index="users", doc_type="local"):
    from time import sleep
    es = Elasticsearch([{
        "host": ELASTICSEARCH_HOST
        , "port": ELASTICSEARCH_PORT
    }])
    es.create(index, doc_type, body=user, id=user_id)
    sleep(1) # Delay 1 second to allow indexing