python下elasticsearch搜索接口介绍
# elasticsearch 默认算法bm25
from elasticsearch import Elasticsearch
import sys
es = Elasticsearch()
# ping 检查是否连接成功
ret = es.ping()
if not ret:
print('您的elasticsearch没有运行或者运行不成功')
sys.exit(-1)
# 搜索接口
# 多个索引
# es.search(index=['index1','index2','index3'])
# 单个索引
# results = es.search(index='test-demo',)
#加参数:_source=False,不返回详细信息
#results = es.search(index='test-demo',_source=False)
#加参数:_source_includes=['title'],只返回title字段
# results = es.search(index='test-demo',_source_includes=['title'])
#加参数:_source_excludes=['title'],返回不包含title的字段
results = es.search(index='test-demo',_source_includes=['title'],q='title:苹果手机',size=5)
# count 显示总数
# total = es.count(index='test-demo')
# 也可加条件统计,q='苹果手机',指定关键词
total = es.count(index='test-demo',q='苹果手机')
print(type(results))