Elastsearch 查询filter过滤
1.filter查询语句
# 建立测试数据_1
POST /store/product/_bulk
{"index": {"_id":1}}
{"price": 10, "productID":"SD1002136"}
{"index": {"_id":2}}
{"price": 20, "productID":"SD2678421"}
{"index": {"_id":3}}
{"price": 30, "productID":"SD8897573"}
{"index": {"_id":4}}
{"price": 30, "productID":"SD4535233"}
#查看测试数据
GET /store/products/_mget
{
"ids":["1","2","3","4"]
}
# 查看library的mapping信息
GET /store/_mapping
#---------------------------------------------------
#简单过滤查询
#最简单的filter查询
# select document from products where price = '20';
# filtered 查询价格是20的商品
GET /store/products/_search
{
"query":{
"filtered":{
"query":{
"match_all":{}
},
"filter":{
"term":{
"price":20
}
}
}
}
}
# 也可以指定多个值
GET /store/products/_search
{
"query":{
"filtered":{
"filter":{
"terms":{
"price":[10, 20]
}
}
}
}
}
# select product from products where productID = 'SD4535233'
#
GET /store/products/_search
{
"query":{
"filtered":{
"filter":{
"term":{
"productID":"SD4535233"
}
}
}
}
}
# 查看分析器解析的结果
GET /_analyze?text=SD4535233
GET /store/_mapping
DELETE /store
# 重新建立一个映射,让productID处于not_analyzed模式
PUT /store
{
"mappings":{
"products":{
"properties":{
"productID":{
"type":"string",
"index":"not_analyzed"
}
}
}
}
}
#-----------------------------------------------------
#bool过滤查询,可以做组合过滤查询
#select product from products where (price = 20 or productID = 'SD1002136') and (price != 30);
# 查询价格等于20的或者productID = 'SD1002136'的商品,排除price = 30的商品。
# 类似的, Elasticsearch也有and,or,not这样的组合条件的查询方式
# 格式如下:
{
"bool":{
"must":[],
"should":[],
"must_not":[],
}
}
# must:条件必须满足,相当于and
# should:条件可以满足也可以不满足,相当于or
# must_not:条件不需要满足,相当于not
GET /store/products/_search
{
"query":{
"filtered":{
"filter":{
"bool":{
"should":[
{"term":{"price":20}},
{"term":{"productID":"SD1002136"}}
],
"must_not":{
"term":{"price":30}
}
}
}
}
}
}
#嵌套查询
# select document from products where productID = 'SD1002136' or (productID = 'SD4535233' and price = 30)
#
GET /store/products/_search
{
"query":{
"filtered":{
"filter":{
"bool":{
"should":[{
{"term":{"productID":"SD1002136"}},
{"bool":{
"must":[
{"term":{"productID":"SD4535233"}},
{"term":{"price":30}}
]
}
}
}]
}
}
}
}
}
# 另外一种 and,or,not查询
# 没有bool, 直接使用and,or,not
#查询价格既是10元,productID又为SD1002136的结果
#and
GET /store/products/_search
{
"query":{
"filter":{
"and":[
{
"term":{
"price":10
}
},
{
"term":{
"productID":"ID1002136"
}
}
]
},
"query":{
"match_all":{}
}
}
}
#or
# 查询价格是10元或者productID是SD4535233的一些商品
GET /store/products/_search
{
"query":{
"filtered":{
"filter":{
"or":[
{
"term":{
"price":10
}
},
{
"term":{
"productID":"SD4535233"
}
}
]
},
"query":{
"match_all":{}
}
}
}
}
# not
# 查询produntID不是SD1002136的商品
GET /store/products/_search
{
"query":{
"filtered":{
"filter":{
"not":{
"term":{
"productID":"SD1002136"
}
}
},
"query":{
"match_all":{}
}
}
}
}
# range 范围过滤
# select document from products where price between 20 and 40;
# gt : >
# lt : <
# gtea : >=
# lte : <=
GET /store/products/_search
{
"query":{
"filtered":{
"filter":{
"range":{
"price":{
"gte":20,
"lt":40
}
}
}
}
}
}
#---------------------------------------
# 过滤空和非空
#建立测试数据_2
POST /test_index/test/_bulk
{"index":{"_id":"1"}}
{"tags":["search"]}
{"index":{"_id":"2"}}
{"tags":["search","open_source"]}
{"index":{"_id":"3"}}
{"other_field": "some data"}
{"index":{"_id":"4"}}
{"tags":null}
{"index":{"_id":"5"}}
{"tags":["search", null]}
# 处理null空值的方法
# select tags from test where tags is not null;
# select tags from test where tags is null;
GET /test_index/test/_search
{
"query":{
"filtered":{
"filter":{
"exists":{"field":"tags"}
}
}
}
}
GET /test_index/test/_search
{
"query":{
"filtered":{
"filter":{
"missing":{"field":"tags"}
}
}
}
}
2.cache缓存
# Elasticsearch在执行带有filter查询时,会打开索引的每个segment文件(Lucene式底层文件),然后去判断里面的文档是否符合filter要求。
# 注意:旧的segment文件不会变,新来的数据会产生新的segment。
# 匹配的结果会用一个大型的BigSet数组来存储,这个数组的值只有0和1
# 匹配:1
# 不匹配:0
# BigSet值是存在内存里面的,而不是硬盘里,所以速度快!
# 开启方式:在filter查询语句后面加上"_cache:"true
# 注意“
# Scriptfilters,Geo-filters,Date ranges这样的过滤方式开启cache无意义
# exists,missing,range,term和terms查询时默认开启cache的
# 如下:
GET /store/products/_search
{
"query":{
"filtered":{
"filter":{
"bool":{
"should":[
{"term":{"price":20}},
{"term":{"productID":"SD1002136"}}
],
"must_not":{
"term":{"price":30}
}
}
}
}
}
}
# 开启缓存
GET /store/products/_search
{
"query":{
"filtered":{
"filter":{
"bool":{
"should":[
{"term":{"price":20}},
{"term":{"productID":"SD1002136"}}
],"_cache":true,
"must_not":{
"term":{"price":30}
}
}
}
}
}
}
# 不开启缓存
GET /store/products/_search
{
"query":{
"filtered":{
"filter":{
"bool":{
"should":[
{"term":{"price":20}},
{"term":{"productID":"SD1002136"}}
],"_cache":false,
"must_not":{
"term":{"price":30}
}
}
}
}
}
}