当前位置:首页 > 日记本 > 正文内容

python下elasticsearch搜索接口介绍

zhangchap3年前 (2021-09-11)日记本265
# 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))


标签: elasticsearch
分享给朋友:

相关文章

python下elasticsearch搜索接口封装实现

# -*- coding:utf-8 -*- from elasticsearch import Elasticsearch,Transp...

elasticsearch安装步骤及运行

elasticsearch安装步骤及运行

下载链接:https://www.elastic.co/cn/downloads/elasticsearch虽然服务器是国外的,但是下载速度很快解压到D盘根目录,避免兼容性,不要有中文的路径打开bin...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。