python调用elasticsearch(Python-ElasticSearch搜索查询的讲解)
类别:脚本大全 浏览量:236
时间:2022-01-21 00:20:22 python调用elasticsearch
Python-ElasticSearch搜索查询的讲解Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上。 Lucene 可能是目前存在的,不论开源还是私有的,拥有最先进,高性能和全功能搜索引擎功能的库。但是 Lucene 仅仅只是一个库。为了利用它,你需要编写 Java 程序,并在你的 java 程序里面直接集成 Lucene 包。 更坏的情况是,你需要对信息检索有一定程度的理解才能明白 Lucene 是怎么工作的。Lucene 是 很 复杂的。
在上一篇文章中介绍了ElasticSearch的简单使用,接下来记录一下ElasticSearch的查询:
查询所有数据
|
# 搜索所有数据 es.search(index = "my_index" ,doc_type = "test_type" ) # 或者 body = { "query" :{ "match_all" :{} } } es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
term与terms
|
# term body = { "query" :{ "term" :{ "name" : "python" } } } # 查询name="python"的所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) # terms body = { "query" :{ "terms" :{ "name" :[ "python" , "android" ] } } } # 搜索出name="python"或name="android"的所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
match与multi_match
|
# match:匹配name包含python关键字的数据 body = { "query" :{ "match" :{ "name" : "python" } } } # 查询name包含python关键字的数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) # multi_match:在name和addr里匹配包含深圳关键字的数据 body = { "query" :{ "multi_match" :{ "query" : "深圳" , "fields" :[ "name" , "addr" ] } } } # 查询name和addr包含"深圳"关键字的数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
ids
|
body = { "query" :{ "ids" :{ "type" : "test_type" , "values" :[ "1" , "2" ] } } } # 搜索出id为1或2d的所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
复合查询bool
bool有3类查询关系,must(都满足),should(其中一个满足),must_not(都不满足)
|
body = { "query" :{ "bool" :{ "must" :[ { "term" :{ "name" : "python" } }, { "term" :{ "age" : 18 } } ] } } } # 获取name="python"并且age=18的所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
切片式查询
|
body = { "query" :{ "match_all" :{} } "from" : 2 # 从第二条数据开始 "size" : 4 # 获取4条数据 } # 从第2条数据开始,获取4条数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
范围查询
|
body = { "query" :{ "range" :{ "age" :{ "gte" : 18 , # >=18 "lte" : 30 # <=30 } } } } # 查询18<=age<=30的所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
前缀查询
|
body = { "query" :{ "prefix" :{ "name" : "p" } } } # 查询前缀为"赵"的所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
通配符查询
|
body = { "query" :{ "wildcard" :{ "name" : "*id" } } } # 查询name以id为后缀的所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
排序
|
body = { "query" :{ "match_all" :{} } "sort" :{ "age" :{ # 根据age字段升序排序 "order" : "asc" # asc升序,desc降序 } } } |
filter_path
响应过滤
|
# 只需要获取_id数据,多个条件用逗号隔开 es.search(index = "my_index" ,doc_type = "test_type" ,filter_path = [ "hits.hits._id" ]) # 获取所有数据 es.search(index = "my_index" ,doc_type = "test_type" ,filter_path = [ "hits.hits._*" ]) |
count
执行查询并获取该查询的匹配数
|
# 获取数据量 es.count(index = "my_index" ,doc_type = "test_type" ) |
度量类聚合
- 获取最小值
|
body = { "query" :{ "match_all" :{} }, "aggs" :{ # 聚合查询 "min_age" :{ # 最小值的key "min" :{ # 最小 "field" : "age" # 查询"age"的最小值 } } } } # 搜索所有数据,并获取age最小的值 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
- 获取最大值
|
body = { "query" :{ "match_all" :{} }, "aggs" :{ # 聚合查询 "max_age" :{ # 最大值的key "max" :{ # 最大 "field" : "age" # 查询"age"的最大值 } } } } # 搜索所有数据,并获取age最大的值 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
- 获取和
|
body = { "query" :{ "match_all" :{} }, "aggs" :{ # 聚合查询 "sum_age" :{ # 和的key "sum" :{ # 和 "field" : "age" # 获取所有age的和 } } } } # 搜索所有数据,并获取所有age的和 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
- 获取平均值
|
body = { "query" :{ "match_all" :{} }, "aggs" :{ # 聚合查询 "avg_age" :{ # 平均值的key "sum" :{ # 平均值 "field" : "age" # 获取所有age的平均值 } } } } # 搜索所有数据,获取所有age的平均值 es.search(index = "my_index" ,doc_type = "test_type" ,body = body) |
更多的搜索用法:
https://elasticsearch-py.readthedocs.io/en/master/api.html
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对开心学习网的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/y472360651/article/details/76652021
您可能感兴趣
- 查看mysql索引缓存(MySQL查询缓存的小知识)
- 查看SQL SERVER中某个查询用了多少TempDB空间
- mysql多表连接优化(浅谈Mysql多表连接查询的执行细节)
- mysql如何修改列的默认值(mysql查询的时候给字段赋默认值操作)
- sql数据库查询优化(数据库SQL语句优化总结收藏)
- sqlserver查询自定义时间数据(SQLServer查询某个时间段购买过商品的所有用户)
- sqlserver查询表结构(sql server递归子节点、父节点sql查询表结构的实例)
- mysql 分片键规则(MySql8 WITH RECURSIVE递归查询父子集的方法)
- mysql数据库延时监控(Mysql sql慢查询监控脚本代码实例)
- laravel测试重连数据库(解决在laravel中leftjoin带条件查询没有返回右表为NULL的问题)
- sqlserver分组查询(sql server如何利用开窗函数over进行分组统计)
- mysql简单查询基本语句(详解MySQL 查询语句的执行过程)
- php 数据库查询并写入(PHP实现单条sql执行多个数据的insert语句方法)
- mysql查询语法总结(MySQL全面瓦解之查询的过滤条件详解)
- sql server查询语句语法(详解SQL Server的简单查询语句)
- mysql查询慢有哪些原因(MySQL 查询速度慢的原因)
- 全椒人,38年集体回忆 1980-2018 ,看完不要哭(全椒人38年集体回忆)
- 董元奔吟咏历代文人 1012新旧均可 全椒人张璪 1022 -1093(董元奔吟咏历代文人)
- 泪目 这位 刷屏 的英雄,是全椒人的骄傲(泪目这位刷屏)
- 人从众 火炎焱 全椒再现 正月十六走太平 的魅力(人从众火炎焱全椒再现)
- 官宣 全椒籍明星许海峰 奚秀兰 方芳 王璐瑶携手回家 走太平(全椒籍明星许海峰)
- 以前全椒人是怎么过冬的 满满都是回忆(以前全椒人是怎么过冬的)
热门推荐
- css三种布局模式(CSS实现等分布局的4种方式)
- mysql恢复数据库(MySQL 两种恢复数据的方法)
- windows server开启ftp服务器(全面提高Windows系统FTP服务器的安全性能)
- mysql根据子节点查询父节点(mysql 递归查找菜单节点的所有子节点的方法)
- virtualbox服务器搭建centos(VirtualBox安装CentOS及JDK、Hadoop的安装与配置详细教程)
- python中什么是迭代器(一篇文章彻底搞懂Python中可迭代Iterable、迭代器Iterator与生成器Generator的概)
- python扫描服务器端口(Python实现的对本地host127.0.0.1主机进行扫描端口功能示例)
- css3设置元素的边框图像(详解CSS3 用border写 空心三角箭头 两种写法)
- python菜单栏教程(Python3.5实现的三级菜单功能示例)
- django操作默认数据库(Django数据库类库MySQLdb使用详解)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9