python + elasticsearch + 조회/삽입/생성
ELK/elasticsearch2019. 2. 3. 16:46
from elasticsearch import Elasticsearch
import pprint as ppr
import json
class ElaAPI:
es = Elasticsearch(hosts="192.168.240.129", port=9200) # 객체 생성
@classmethod
def srvHealthCheck(cls):
health = cls.es.cluster.health()
print (health)
@classmethod
def allIndex(cls):
# Elasticsearch에 있는 모든 Index 조회
print (cls.es.cat.indices())
@classmethod
def dataInsert(cls):
# ===============
# 데이터 삽입
# ===============
with open("../json_doc_make/tst.json", "r", encoding="utf-8") as fjson:
data = json.loads(fjson.read())
for n, i in enumerate(data):
doc = {"cont" :i['cont'],
"mnagnnm":i["mnagnnm"],
"post" :i["post"],
"rgdt" :i["rgdt"],
"rgter" :i["rgter"],
"tel" :i["tel"],
"title" :i["title"]}
res = cls.es.index(index="today19020301", doc_type="today", id=n+1, body=doc)
print (res)
@classmethod
def searchAll(cls, indx=None):
# ===============
# 데이터 조회 [전체]
# ===============
res = cls.es.search(
index = "today19020301", doc_type = "today",
body = {
"query":{"match_all":{}}
}
)
print (json.dumps(res, ensure_ascii=False, indent=4))
@classmethod
def searchFilter(cls):
# ===============
# 데이터 조회 []
# ===============
res = cls.es.search(
index = "today19020301", doc_type = "today",
body = {
"query": {"match":{"post":"산림교육문화과"}}
}
)
ppr.pprint(res)
@classmethod
def createIndex(cls):
# ===============
# 인덱스 생성
# ===============
cls.es.indices.create(
index = "today19020301",
body = {
"settings": {
"number_of_shards": 5
},
"mappings": {
"today":{
"properties": {
"cont": {"type": "text"},
"mnagnnm": {"type": "text"},
"post": {"type": "text"},
"rgdt": {"type": "text"},
"rgter": {"type": "text"},
"tel": {"type": "text"},
"title": {"type": "text"}
}
}
}
}
)
ElaAPI.allIndex()
# ElaAPI.srvHealthCheck()
# ElaAPI.createIndex()
# ElaAPI.dataInsert()
# ElaAPI.searchAll()
# ElaAPI.searchFilter()
'ELK > elasticsearch' 카테고리의 다른 글
python + elasticsearch + 현재 정리 중 (0) | 2019.02.19 |
---|---|
python + elasticsearch (0) | 2019.02.16 |
search java api (0) | 2019.01.29 |
python + elasticsearch api (0) | 2019.01.29 |
java api elasticsearch aggregation (0) | 2019.01.28 |