PYTHON 에서 ELASTICSEARCH CURD하기

code_able·2023년 10월 18일
0

라이브러리 설치

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

profile
할수 있다! code able

0개의 댓글