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 } } } } }}