본문 바로가기

카테고리 없음

Elasticsearch docker 이용해서 해보기

 

docker로 elasricsearch 실행하기 

docker run -it -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.5.1

 

curl날려서 구동잘 되는지확인해보기 

>curl -X GET http://localhost:9200

 

 

 

 ES 버전에 맞는 클라이언트 라이브러리를 호스트에 설치한다.

pip install elasticsearch==7.5.1

이후 로컬호스트에 설치된 ES 에 인덱스를 생성해준다.

from elasticsearch import Elasticsearch
from elasticsearch import helpers

es = Elasticsearch(hosts=['http://localhost'], port='9200')
# print(f'es.info : {es.info()}')

# es.info : {'name': 'cfde261bd240', 'cluster_name': 'docker-cluster', 
#            'cluster_uuid': 'EDk_40cxQCy0ZGIl9zJluw', 
#            'version': {'number': '7.5.1', 
#                        'build_flavor': 'default', 
#                        'build_type': 'docker',
#                         'build_hash': '3ae9ac9a93c95bd0cdc054951cf95d88e1e18d96', 
#                         'build_date': '2019-12-16T22:57:37.835892Z',
#                         'build_snapshot': False, 
#                         'lucene_version': '8.3.0', 
#                         'minimum_wire_compatibility_version': '6.8.0', 
#                         'minimum_index_compatibility_version': '6.0.0-beta1'}, 
#             'tagline': 'You Know, for Search'}

def make_index(es, index_name) :
    '''인덱스를 신규 생성한다 (존재하면 삭제 후 생성)'''
    if es.indices.exists(index=index_name) :
        es.indices.delete(index=index_name)
        print(f'index delete')
    es.indices.create(index=index_name)
    print(f'index create')
    # print(f'es.indices.create(index=index_name) : {es.indices.create(index=index_name)}')
    # {'acknowledged': True, 'shards_acknowledged': True, 'index': 'goods'}
index_name = 'goods'
make_index(es, index_name)

doc1 = {'goods_name' : '삼성 노트북', 'price' : 1000000}
doc2 = {'goods_name' : '엘지 노트북', 'price' : 1000000}
doc3 = {'goods_name' : '삼성 핸드폰', 'price' : 1000000}

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.indices.refresh(index=index_name) #업데이트 시켜주기 

# res = es.search(index=index_name, body={'from' : 0, 'size':10, 'query':{'match':{'goods_name':'노트북'}}}) # 특정값 조회
res = es.search(index=index_name, body={'from' : 0, 'size':10, 'query':{'match_all':{}}}) # 전체 데이터 조회 

print(f'res : {res}')
# res : {'took': 5, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': 
# {'value': 3, 'relation': 'eq'}, 'max_score': 1.0, 'hits': [{'_index': 'goods', '_type': 'string', '_id': 'JlZUl5EB2ETTievbCnU{'value': 3, 'relation': 'eq'}, 'max_score': I', '_score': 1.0, '_source': {'goods_name': '삼성 노트북', 'price': 1000000}}, {'_index': 'goods', '_type': 'string', '_id':'삼성 노트북', 'price': 1000000}}, {'_index': 'J1ZUl5EB2ETTievbCnUh', '_score': 1.0, '_source': {'goods_name': '엘지 노트북', 'price': 1000000}}, {'_index': 'goods', '_ty1000000}}, {'_index': 'goods', '_type': 'stripe': 'string', '_id': 'KFZUl5EB2ETTievbCnUp', '_score': 1.0, '_source': {'goods_name': '삼성 핸드폰', 'price': 1000000}}]}}