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"]
}
}
}
index与match举例讲解# 查询name和addr包含"深圳"关键字的数据
es.search(index="my_index",doc_type="test_type",body=body)
ids
body = {
"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
执⾏查询并获取该查询的匹配数
# 获取数据量
度量类聚合
获取最⼩值
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)
更多的搜索⽤法:
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,谢谢⼤家对的⽀持。如果你想了解更多相关内容请查看下⾯相关链接
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论