-
Notifications
You must be signed in to change notification settings - Fork 0
/
es_request_handler.py
48 lines (38 loc) · 1.21 KB
/
es_request_handler.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import asyncio
import time
from datastore.main import ElasticsearchRequestHandler
es = ElasticsearchRequestHandler()
async def main():
# get single document
product_id = 10
print(f"product having id {product_id}: {await es.get(product_id)}")
# get multiple documents having stock greater than 0
query = {
"query": {
"bool": {
"must": {
"range": {
"product.stock": {
"gt": 6
}
}
}
}
}
}
print(f"products having stock > 6 :: {await es.search(query)}")
# update price of product with id = 200
resp = await es.get(product_id)
product = resp.get('_source').get('product')
print(f"product price before update {product['price']}")
product['price'] = 8.5
print(f"product price after update {product['price']}")
update = await es.update(product, product_id)
if update:
print(f"product updated successfully")
else:
print(f"product update failed")
# close ES connection
await es.close_connection()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())