ES之多條件、范圍查詢
一、多條件查詢
1.條件“且”,即查詢"title"為"test6",且"num"為5的數(shù)據(jù)
【GET】請求:http://127.0.0.1:9200/test-index-1/_search,參數(shù)如下
{
"query":{
"bool":{
"must":[
{
"match":{
"title": "test6"
}
},
{
"match":{
"num": 5
}
}
]
}
}
}
結(jié)果如下
{
"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"的數(shù)據(jù)
【GET】請求:http://127.0.0.1:9200/test-index-1/_search,參數(shù)如下
{
"query":{
"bool":{
"should":[
{
"match":{
"title": "test6"
}
},
{
"match":{
"title": "test8"
}
}
]
}
}
}
結(jié)果如下
{
"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的數(shù)據(jù)
gt:大于
gte:大于等于
lt:小于
lte:小于等于
【GET】請求:http://127.0.0.1:9200/test-index-1/_search,參數(shù)如下
{
"query":{
"bool":{
"filter":{
"range":{
"num":{
"lt":4
}
}
}
}
}
}
結(jié)果如下文章來源:http://www.zghlxwxcb.cn/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會失效,需要加入?yún)?shù)"minimum_should_match":1,或者should當(dāng)做子層級
共用時參考 https://blog.csdn.net/andy_5826_liu/article/details/103161654文章來源地址http://www.zghlxwxcb.cn/news/detail-504157.html
到了這里,關(guān)于ES之多條件、范圍查詢的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!