python es example

查询最热的url python 代码

example.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
def ip(kwargs):
d1 = kwargs.get('start_day')
d2 = kwargs.get('end_day')
startday = d1.strftime(DATE_FORMAT)
endday = d2.strftime(DATE_FORMAT)
domain = kwargs.get('domain', None)
domains = kwargs.get('domains', None)
query = {}
if domain:
query['match_phrase_prefix'] = {'request': domain}
else:
queries = []
for d in domains:
queries.append({"match_phrase_prefix": {"request": d}})
query['dis_max'] = {
'queries': queries
}
body = {
"query": query,
"filter": {
"range": {
"dateTime": {
"from": "%s 00:00:00" % (startday), "to": "%s 23:59:59" % (endday)
}
}
},
"facets": {
"requestCount": {
"terms": {
"field": "ip",
"size": 10
}
}
}
}
result = {}
try:
r = es.search(index="spark", doc_type="docs", search_type="count", body=body)
facets = r.get('facets')
requestCount = facets.get('requestCount')
terms = requestCount.get('terms')
for c in terms:
result[c.get('term')] = c.get('count')
except NotFoundError as e:
return result
return result

记录一次