Filter過濾查詢
filter是不計算相關性的,同時可以緩存。因此filter速度快於query。
我們先在kibana上先添加數據來做准備
POST /lib4/items/_bulk
{ "index": { "_id": 1 }} { "price" : 40, "itemID" : "ID1001" } { "index": { "_id": 2 }} { "price" : 50, "itemID" : "ID1002" } { "index": { "_id": 3 }} { "price" : 25, "itemID" : "ID1004" } { "index": { "_id": 4 }} { "price" : 30, "itemID" : "ID1004" } { "index": { "_id": 5 }} { "price" : null, "itemID" : "ID1005" }
首先,我們過濾查詢價格等於40的文檔,如下寫法
GET /lib4/items/_search
{
"query": {
"bool": {
"filter": [
{ "term":{"price":40}}
]
}
}
}
bool過濾查詢
bool查詢可以實現組合過濾查詢
格式:
{"bool" : {"must":[],"should":[],"must_not":[] } }
must:必須滿足的條件 (相當於and)
should:可以滿足也可以不滿足的條件 (相當於or)
must_not:不需要滿足的條件 (相當於not)
GET /lib4/items/_search #滿足價格是25或者ID是1004,同時價格不為30
{
"query": {
"bool":{
"should": [
{"term":{"price": 25}},
{"term":{"itemID": "id1004"}}
],
"must_not": [
{"term":{"price": 30}}
]
}
}
}
范圍過濾
gt:>
lt:<
gte:>=
lte:<=
GET /lib4/items/_search #range表示取一定范圍的數據
{
"query": {
"bool":{
"filter": {
"range": {
"price": {
"gt": 25,
"lte": 50
}
}
}
}
}
}