Elasticsearch (python)

Simba_bยท2022๋…„ 10์›” 26์ผ
0

Elatsticsearch

๋ชฉ๋ก ๋ณด๊ธฐ
4/5
post-thumbnail

๐Ÿ’ป python์œผ๋กœ Elasticsearch ์‚ฌ์šฉ


ES์—ฐ๊ฒฐ ๋ฐ ์ธ๋ฑ์Šค ์ƒ์„ฑ

  • ์—ฐ๊ฒฐ
# ES ํŒจํ‚ค์ง€ import
from elasticsearch import Elasticsearch

  • ์—ฐ๊ฒฐ ๋ฐ ํ™•์ธ
# es ์—ฐ๊ฒฐ
# localhost์ž๋ฆฌ์— ip์ฃผ์†Œ ๊ฐ€๋Šฅ
es = Elasticsearch('http://localhost:9200/'])

#์—ฐ๊ฒฐ ์—ฌ๋ถ€ ํ™•์ธ
print('elasticsearch connected info \n',es.info())

  • ์ธ๋ฑ์Šค ํ™•์ธ
# ์ „์ฒด ์ธ๋ฑ์Šค ํ™•์ธ(์ถœ๋ ฅ)
def allIndex():
	print(es.cat.indices())
    
# ์˜ˆ์‹œ) ์ „์ฒด ์ธ๋ฑ์Šค ํ™•์ธ 
allIndex()

  • ์ธ๋ฑ์Šค ์ƒ์„ฑ ์‚ญ์ œ
# ์ธ๋ฑ์Šค ์ƒ์„ฑ ํ•จ์ˆ˜
def make_index(es,index_name):
    es.indices.create(index=index_name)

# ์˜ˆ์‹œ) crawling์ด๋ผ๋Š” ์ธ๋ฑ์Šค ์ƒ์„ฑ
make_index(es, 'crawling') 

# ์ธ๋ฑ์Šค ์‚ญ์ œ ํ•จ์ˆ˜
def del_index(es, index_name):
    es.indices.delete(index=index_name)

# ์˜ˆ์‹œ) crawling์ด๋ผ๋Š” ์ธ๋ฑ์Šค ์‚ญ์ œ
del_index(es, 'crawling') 


ES ๋ฐ์ดํ„ฐ ํ™•์ธ

๐Ÿšจ data return์ด ์•„๋‹Œ ์ฝ˜์†”์ฐฝ ์ถœ๋ ฅ ํ™•์ธ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์œผ๋ ค๊ณ  ํ•  ๊ฒฝ์šฐ ํ•จ์ˆ˜ ์ˆ˜์ • ํ•„์š”

  • data ์ €์žฅ
# data ์ €์žฅ ํ•จ์ˆ˜
# doc์€ jsonํ˜•ํƒœ
def save_data(es,index_name,  doc):
    es.index(index=index_name, body=doc)
    print('save_data success')

  • data ๊ฐฏ์ˆ˜ ํ™•์ธ
# ์ธ๋ฑ์Šค์˜ ๋ฐ์ดํ„ฐ ๊ฐฏ์ˆ˜
def count_data(es, index_name):
    num=es.count(index=index_name)
    print(num['count'])

# ์˜ˆ์‹œ) crawling์ธ๋ฑ์Šค์˜ ๋ฐ์ดํ„ฐ ๊ฐฏ์ˆ˜ ์ถœ๋ ฅ
count_data(es, "crawling")

  • data ์กฐํšŒ
# ์ธ๋ฑ์Šค์˜ data ์กฐํšŒ
from pprint import pprint
def get_data(es,index_name):
    data=es.search(index=index_name)
    pprint(data, indent=4)
    
# ์˜ˆ์‹œ) crawling์ธ๋ฑ์Šค์˜ data์กฐํšŒ
get_data(es,"crawling")

0๊ฐœ์˜ ๋Œ“๊ธ€