Elastic Search
ElasticSearch & Python
땅개발자
2021. 11. 9. 23:26
Elasticsearch는 python으로 접속후에 CRUD와 index, alias의 설정이 가능하다.
Python를 이용하여 접속하기
from elasticsearch import Elasticsearch
host = ["http://{ip addr}"]
es = Elasticsearch(host, scheme="http", port=9200)
Index 생성
es.indices.create(index='{index 명}')
Index 삭제
es.indices.delete(index="{삭제 Index}")
Index mapping 생성
mapping = {
"properties": {
"ISSUE_CODE": {
"type": "keyword"
},
"ISSUE_NAME": {
"type": "text"
}
}
}
es.indices.put_mapping(index="analyst_report_info_test01", body=mapping)
Index mapping 취득
es.indices.get_mapping(index="{Index명}")
Index properties 추가
mapping = {
"properties": {
"info_kbn": {"type": "keyword"}
}
}
es.indices.put_mapping(index="{Index명}", body=mapping)
Index properties 지정 갱신
update = {
"doc": {
"info_kbn": "01"
}
}
es.update(index="{Index명}", id="{갱신대상 id}", body=update)
Index properties 일괄 갱신
python에서는 일괄갱신 하는 방법을 찾지 못하였다. ( 더 찾아보면 있을듯하나... ?? )
하지만, kibana의 query를 이용하면 가능하다.
POST analyst_report_text_search_all / _update_by_query?conflicts = proceed
{
"query": {
"match_all": {}
},
"script": {
"inline": "ctx._source.info_kbn = \"01\"; "
}
}
alias 생성
es.indices.put_alias(index='{Index 명}', name='{alias 명}')
alias 갱신(Index 추가)
es.indices.update_aliases({
"actions": [
{"add": {"index": "{추가 Index명}", "alias": "{alias 명}"}},
{"add": {"index": "{추가 Index명}", "alias": "{alias 명}"}},
]
})