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 分页查询的优化技巧)
- python创建列表并查询(python列表使用实现名字管理系统)
- sql查询重复记录
- sqlserver分表后如何查询(SQL Server中row_number分页查询的用法详解)
- laravel关联查询限制数量(laravel-admin 后台表格筛选设置默认的查询日期方法)
- laravel自定义条件查询(解决laravel groupBy 对查询结果进行分组出现的问题)
- laravel查询条件数组写法(解决Laravel 使用insert插入数据,字段created_at为0000的问题)
- sql查询地址中包含某个字段(SQL查询字段被包含语句)
- zabbix 查询linux使用率高的进程(详解Linux使用ss命令结合zabbix对socket做监控)
- mysql关联查询详解(详解mysql 组合查询)
- mysql xml转换json(Mysql将查询结果集转换为JSON数据的实例代码)
- sql怎么写递归(sql server实现递归查询的方法示例)
- sql查询过程解析(SQL 联合查询与XML解析实例详解)
- sqlserver查询信息表(SQL Server中Table字典数据的查询SQL示例代码)
- python抓取天气数据(Python实战之制作天气查询软件)
- laravel语法有哪些(对laravel in 查询的使用方法详解)
- 靳东领衔打造高精职场 新丽出品《精英律师》曝定妆照(靳东领衔打造高精职场)
- 靳东新剧《精英律师》定档,众星云集,这剧可追(靳东新剧精英律师定档)
- 精英律师 廖佳敏封印恋情曝光,顾婕马失前蹄 你个老不死的(廖佳敏封印恋情曝光)
- 以家人之名广受好评,剧情生动引起观众共鸣,演员张新成圈粉无数(以家人之名广受好评)
- 三兄妹感情再遇波折,人设接连崩塌 《以家人之名》剧情猜不透(三兄妹感情再遇波折)
- 《小敏家》金波想要复婚 这只是他圈套的第1步,更可恶的在后面(小敏家金波想要复婚)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9