라이브러리 설치
pip install elasticsearch
python에서 elasticsearch 연결
from elasticsearch import Elasticsearch
es = Elasticsearch(['http://localhost:8080'], http_auth=('elastic', ''))
es = Elasticsearch('[엘라스틱_서버_IP_주소]:9200')
create
es.index(
index="myIndex",
doc_type="_doc",
body={"conent":"내용"}
)
mapping.json 예시
----------------------------------------------------
{
"mappings" : {
"properties" : {
"@timestamp" : {
"type" : "date"
},
"content" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
----------------------------------------------------
with open('mapping.json', 'r') as f:
mapping = json.load(f)
es.indices.create(index="myIndex", body=mapping)
es.index(
index="myIndex",
doc_type="_doc",
body={"conent":"내용"}
)
read
es.search(index="myIndex", body={"conent":"내용"})
update
es.update(index='myIndex', id=1, body = {"content":"수정값"})
delete
es.delete('myIndex', id=1)
reference