基本调用
安装
conda insatll elasticsearch
基本调用
from elasticsearch import Elasticsearch
HOSTS = 'http://abc.com'
INDEX = 'abc'
es = Elasticsearch(HOSTS)
js = es.search(INDEX, {
'query': {
'match_all': {
}}})
print(js)
>>> from elasticsearch import Elasticsearch
>>> es = Elasticsearch()
>>> es.index(index="my-index", doc_type="test-type", id=42, body={
"any": "data"})
{
'_id': '42', '_index': 'my-index', '_type': 'test-type', '_version': 1, 'ok': True}
>>> es.get(index="my-index", doc_type="test-type", id=42)['_source']
{
'any': 'data'}
封装自用
from elasticsearch import Elasticsearch
HOSTS = 'http://abc.com'
INDEX = 'abc'
SIZE = 100
SCROLL = '5m'
DOC_TYPE = '_doc'
class ES:
def __init__(self, hosts=HOSTS):
self.es = Elasticsearch(hosts)
@staticmethod
def yellow(x):
print('\033[93m{}\033[0m'.format(x))
def search(self, body=None, index=INDEX):
return self.es.search(index, body or {
'query': {
'match_all': {
}}})
def scroll(self, body, index=INDEX, size=SIZE, return_ls=False):
"""分批取数"""
js = self.es.search(index, body, scroll=SCROLL, size=size)
scroll_id = js['_scroll_id']
if return_ls:
yield js['hits']['hits']
total