-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_test.py
More file actions
99 lines (81 loc) · 1.87 KB
/
query_test.py
File metadata and controls
99 lines (81 loc) · 1.87 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# -*- coding:utf-8 -*
import json
import os
import requests
from elasticsearch import Elasticsearch
import yaml
import sys
def pretty_print_msg(data):
if isinstance(data, dict):
print(json.dumps(data, indent=4))
else:
print(data)
class QuerySamples(object):
def __init__(self):
try:
file_name = sys.argv[1] if len(sys.argv) > 1 else "conf.yaml"
self.config = yaml.load(open(file_name, 'r').read())
except Exception as e:
pretty_print_msg("Error:%s" % (e,))
exit(1)
env = os.getenv(
self.config['env']['env_var'],
self.config['env']['env_default']
)
mysql_configs = self.config.get('mysql_conf', [])
for e in mysql_configs:
if e.get('env', '') == env:
self.mysql_conf = e
es_configs = self.config.get('es_conf', [])
for e in es_configs:
if e.get('env', '') == env:
self.es_conf = e
self.go_addr = self.config['go_conf']['addr'][env]
def run_es_query(self):
client = Elasticsearch(self.es_conf['addr'])
req_bodies = [
{
"index": "item_db",
"type": "order_tab",
"body": {
"query": {
"bool": {
"must": {
"match": {
"sku_list": "S00002"
},
"match": {
"seller_email": "gmail.com"
}
}
}
},
"from": 0,
"size": 2
},
},
]
for req in req_bodies:
result = client.search(
index=req['index'],
doc_type=req['type'],
body=req['body']
)
pretty_print_msg(result)
print(result["hits"]["total"])
def run_go_query(self):
print(self.go_addr)
resp = requests.post(url=self.go_addr + '/item/skus', data={'key': 'Promotion'})
pretty_print_msg(resp.json())
def run(self):
#self.run_es_query()
self.run_go_query()
pass
def init_env():
dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path)
if __name__ == '__main__':
init_env()
QuerySamples().run()
pass