(15)ElasticSearch Filter查询

filter是不计算相关性的,同时可以cache,因此,filter速度要快于query

1、准备数据

POST /lib7/items/_bulk
{"index":{"_id":1}}
{"price":40,"itemID":"ID100123"}
{"index":{"_id":2}}
{"price":50,"itemID":"ID100124"}
{"index":{"_id":3}}
{"price":25,"itemID":"ID100125"}
{"index":{"_id":4}}
{"price":30,"itemID":"ID100126"}
{"index":{"_id":5}}
{"price":null,"itemID":"ID100127"}

2、查询price是40的;查询price是25或者40的

GET /lib7/items/_search
{
    "query":{
        "bool":{
            "filter":[
                {"term":{"price":40}}
            ]
        }
    }
}

GET /lib7/items/_search
{
    "query":{
        "bool":{
            "filter":[
                {"terms":{"price":[25,40]}}
            ]
        }
    }
}

3、查询itemID是ID100123的,用第一种方式查询不出来,因为创建时itemID的mapping类型默认是text,存储时分词,存储的是id100123。默认小写。

#查询不出
GET /lib7/items/_search
{
    "query":{
        "bool":{
            "filter":[
                {"term":{"itemID":"ID100123"}}
            ]
        }
    }
}

#能查询出
GET /lib7/items/_search
{
    "query":{
        "bool":{
            "filter":[
                {"term":{"itemID":"id100123"}}
            ]
        }
    }
}

  或者自定义mapping,改为不分词的:

#查看mapping
GET /lib7/_mapping

#删除mapping
DELETE  lib7

#重新创建mapping
PUT /lib7
{
    "mappings":{
        "items":{
            "properties":{
                "itemID":{
                    "type":"text",
                    "index":false
                }
            }
        }
    }
}

4、bool过滤查询。可以实现组合过滤查询,格式如下 :

  {"bool":{"must":[],"should":[],"must_not":[]}}

  must:必须满足的条件---and

  should:可以满足也可以不满足的条件---or

  must_not:不需要满足的条件-----not

(4.1)查询price是25或者itemID是id100123的 并且 price不能是30

GET /lib7/items/_search
{
    "post_filter":{
        "bool":{
            "should":[
                {"term":{"price":25}},
                {"term":{"itemID":"id100123"}}
            ],
            "must_not":{
                "term":{"price":30}
            }
        }
    }
}

#或者
GET /lib7/items/_search
{
    "query":{
        "bool":{
            "should":[
                {"term":{"price":25}},
                {"term":{"itemID":"id100123"}}
            ],
            "must_not":{
                "term":{"price":30}
            }
        }
    }
}

(4.2)嵌套使用bool:要么id100123,要么(id是100124价格是40)并且价格 不能是30

GET /lib7/items/_search
{
    "post_filter":{
        "bool":{
            "should":[
                {"term":{"itemID":"id100123"}},
                {"bool":{"must":[
                    {"term":{"itemID":"id100124"}},
                    {"term":{"price":40}}
                ]}}
            ],
            "must_not":{
                "term":{"price":30}
            }
        }
    }
}

5、范围过滤。gt:>;lt:<;gte:>=;lte:<=

(5.1)price价格大于25且小于50的

GET /lib7/items/_search
{
    "post_filter":{
        "range":{
            "price":{
                "gt":25,"lt":50
            }
        }
    }
}

(5.2)查询价格不是null的

GET /lib7/items/_search
{
    "query":{
        "bool":{
            "filter":{
                "exists":{
                    "field":"price"
                }
            }
        }
    }
}

 

  

转载于:https://www.cnblogs.com/javasl/p/11405405.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值