ComputerLanguage_Program/PYTHON
[PYTHON] python으로 Elasticsearch 연동
pbj0812
2019. 12. 12. 22:51
1. elasticsearch 모듈 설치
- 버전에 맞는 pip를 사용하여 설치
pip install elasticsearch
2. elasticsearch 실행
- 콘솔 추가 및 elasticsearch를 실행
elasticsearch
3. python 작업
1) 라이브러리 불러오기
from elasticsearch import Elasticsearch
from elasticsearch import helpers
2) elasticsearch 연결
- elasticsearch의 주소 입력(로컬에 그냥 깔기만 했다면 127.0.0.1:9200)
es = Elasticsearch('http://127.0.0.1:9200')
es.info()
3) 함수 생성
- 만약, 동일한 이름의 인덱스가 있을 경우(exists) 삭제하고(delete) 생성(create)
def make_index(es, index_name):
if es.indices.exists(index=index_name):
es.indices.delete(index=index_name)
es.indices.create(index=index_name)
4) 데이터 준비
index_name = 'goods'
doc1 = {'goods_name': '삼성 노트북 9', 'price': 1000000}
doc2 = {'goods_name': '엘지 노트북 그램', 'price': 2000000}
doc3 = {'goods_name': '애플 맥북 프로', 'price': 3000000}
doc4 = {'goods_name': '맥북', 'price': 100000}
doc5 = {'goods_name': '맥복', 'price': 100000}
5) 함수 실행
make_index(es, index_name)
6) 값 입력
- 테스트를 위해 여러 방식으로 입력
es.index(index=index_name, doc_type='string', body=doc1)
es.index(index=index_name, doc_type='string', body=doc2)
es.index(index=index_name, doc_type='string', body=doc3)
es.index(index=index_name, doc_type='string', body=doc4)
es.index(index=index_name, doc_type='string', body=doc5)
es.index(index=index_name, doc_type='string', body=doc4)
es.index(index=index_name, doc_type='string', body=doc4)
7) 새로고침
- elasticsearch의 자동 새로고침의 시간은 1초 정도 소요
- 따라서 코드에 아래 명령어를 입력하지 않았을 경우 검색을 하지 못할 가능성도 존재
es.indices.refresh(index=index_name)
8) 검색
- 검색 rawdata : {'_index': 'goods', '_type': 'string', '_id': 'RFJb-m4BtZiSNET_64uo', '_score': 0.87138504, '_source': {'goods_name': '맥북', 'price': 100000}}
results = es.search(index=index_name, body={'from':0, 'size':100, 'query':{'match':{'goods_name':'맥북'}}})
for result in results['hits']['hits']:
print('score:', result['_score'], 'source:', result['_source'])
9) 결과
- 아래 결과는 새로 돌릴때 마다 바뀜...
score: 0.87138504 source: {'goods_name': '맥북', 'price': 100000}
score: 0.87138504 source: {'goods_name': '맥북', 'price': 100000}
score: 0.5754429 source: {'goods_name': '애플 맥북 프로', 'price': 3000000}
score: 0.2876821 source: {'goods_name': '맥북', 'price': 100000}
4. 참고문헌
- 코드 참고