-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathelasticsearch.py
60 lines (50 loc) · 2.02 KB
/
elasticsearch.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
49
50
51
52
53
54
55
56
57
58
59
60
from elasticsearch import Elasticsearch
from config.config import Config
class ElasticsearchClient:
def __init__(self, config: Config):
self.es = Elasticsearch([config.es_host], port=config.es_port)
self.index = config.es_index
def index_es(self, doc_id, doc):
res = self.es.index(index=self.index, id=doc_id, body=doc)
print(res)
def get_id_from_index(self, doc_id):
res = self.es.get(index=self.index, id=doc_id)
return res['_source']
def match_all(self):
try:
res = self.es.search(index=self.index, body={"query": {"match_all": {}}})
j = dict()
i = 0
for obj in res['hits']['hits']:
j[i] = obj['_source']
i = i+1
return j
except:
return 500
def full_search(self, query):
res = self.es.search(index=self.index, body={"query": {"match": {"message" : {"query" : query}}}})
print("Got %d Hits:" % res['hits']['total']['value'])
for hit in res['hits']['hits']:
print("_source: "+str(hit["_source"]))
def field_search(self, field_name, field_query):
res = self.es.search(index=self.index, body={"query": {"bool": {"must": [{"term": {field_name: field_query}}]}}})
print("Got %d Hits:" % res['hits']['total']['value'])
for hit in res['hits']['hits']:
print("_source: "+str(hit["_source"]))
def populate_index(self):
rows = [
["mysql", "mysql:5.7", "none", "3306"],
["elasticsearch", "docker.elastic.co/elasticsearch/elasticsearch:7.0.0", "none", "9200"],
["kibana", "docker.elastic.co/kibana/kibana:7.0.0", "none", "5601"],
["ui", "node:lts-alpine custom", "ui", "8081"]
]
i = 0
for row in rows:
i = i+1
body = {
'name': row[0],
'image': row[1],
'build': row[2],
'port': row[3]
}
self.index_es(i, body)