ES之多条件、范围查询
一、多条件查询
1.条件“且”,即查询"title"为"test6",且"num"为5的数据
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下
{
"query":{
"bool":{
"must":[
{
"match":{
"title": "test6"
}
},
{
"match":{
"num": 5
}
}
]
}
}
}
结果如下
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.7917595,
"hits": [
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "s_Hyw30BJJ5e1YHwWWcU",
"_score": 2.7917595,
"_source": {
"title": "test6",
"num": 5,
"date": "20211213"
}
}
]
}
}
2.条件“或”,即查询"title"为"test6",或"title"为"test8"的数据
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下
{
"query":{
"bool":{
"should":[
{
"match":{
"title": "test6"
}
},
{
"match":{
"title": "test8"
}
}
]
}
}
}
结果如下
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.7917595,
"hits": [
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "s_Hyw30BJJ5e1YHwWWcU",
"_score": 1.7917595,
"_source": {
"title": "test6",
"num": 5,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "tfHyw30BJJ5e1YHwbmfT",
"_score": 1.7917595,
"_source": {
"title": "test8",
"num": 5,
"date": "20211213"
}
}
]
}
}
二、范围查询
查询“num”小于4的数据
gt:大于
gte:大于等于
lt:小于
lte:小于等于
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下
{
"query":{
"bool":{
"filter":{
"range":{
"num":{
"lt":4
}
}
}
}
}
}
结果如下文章来源:https://www.toymoban.com/news/detail-504157.html
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "rvHxw30BJJ5e1YHw6meH",
"_score": 0.0,
"_source": {
"title": "test1",
"num": 1,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "r_Hxw30BJJ5e1YHw_2fX",
"_score": 0.0,
"_source": {
"title": "test2",
"num": 2,
"date": "20211213"
}
},
{
"_index": "test-index-1",
"_type": "_doc",
"_id": "sPHyw30BJJ5e1YHwEGfB",
"_score": 0.0,
"_source": {
"title": "test3",
"num": 3,
"date": "20211213"
}
}
]
}
}
注意:should与must或filter在同一层级直接使用时,should会失效,需要加入参数"minimum_should_match":1,或者should当做子层级
共用时参考 https://blog.csdn.net/andy_5826_liu/article/details/103161654文章来源地址https://www.toymoban.com/news/detail-504157.html
到了这里,关于ES之多条件、范围查询的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!