# encoding=utf-8
import json
from elasticsearch import Elasticsearch
from elasticsearch.helpers import scan
es = Elasticsearch(hosts='10.52.66.31')
def select(_index, _type, offset, limit, **kwargs):
return es.search(index=_index, doc_type=_type, body={
'query': kwargs
}, from_=offset, size=limit)['hits']['hits'] if limit else scan(client=es, query={
'query': kwargs
}, index=_index, doc_type=_type, from_=offset)
kwargs = {
'bool': {
'must': {
'match': {
'artist': '刘德华'
}
}
}
}
result = select('xiami_search', 'song', 50, 0, **kwargs)
if not isinstance(result, list):
result = list(result)
with open('/tmp/json.txt', 'w') as json_file:
res = '\n'.join(map(lambda s: json.dumps(s, ensure_ascii=False).encode('utf-8'), result))
print res
json_file.write(res)
elasticsearch搜索例子
最新推荐文章于 2023-12-25 17:20:29 发布