es filter 的使用

批量创建数据

复制代码
GET /lib4/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"}
复制代码

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

过滤查询

复制代码

GET /lib4/items/_search

{
"query":{
        "bool":{
            "filter":[
                {"term":
                    {"price":40}
                }
            ]
        }
    }
}
复制代码

text类型进行term查询的问题

text类型数据,es默认会对此字段查询使用分词,

复制代码

GET /lib4/items/_search
查不出数据
{
    "query":{
        "bool":{
            "filter":[
                {"term":
                    {"itemID":"ID100124"}
                }
            ]
        }
    }
}
能查出数据
{
    "query":{
        "bool":{
            "filter":[
                {"term":
                    {"itemID":"id100124"}
                }
            ]
        }
    }
}
复制代码

bool 过滤查询

{
    "bool":{
        "must":[], -- 必须满足的条件--and
        "should":[],-- 可以满足也可以不满足的条件--or
        "must_not":[]-- 不能满足的条件--not
    }
}

bool查询例子

复制代码

GET /lib4/items/_search
{
    "query":{
            "bool":{
                "should":[
                        {"term":{
                            "price":25
                        }},
                        {"term":{
                            "itemID":"id100123"
                        }}
            
            ],
            "must_not":[
                    {"term":{
                            "price":40
                        }}
                ]
        }
    }

}
复制代码

范围查询,gt -- > , lt -- < , gte -- >= , lte -- <=

复制代码
GET /lib4/items/_search
{
    "query":{
            "bool":{
                "filter":{
                    "range":{
                        "price":{
                            "gt":25,
                            "lt":50
                        }
                    }
                }
        }
    }

}
复制代码

字段存在查询

复制代码
GET /lib4/items/_search
{
    "query":{
            "bool":{
                "filter":{
                    "exists":{
                        "field":"price"
                    }
                }
        }
    }

}
复制代码

只有filter的查询,不评分

复制代码
GET /lib4/items/_search
{
    "query":{
            "constant_score":{
                "filter":{
                    "term":{
                        "price":30
                    }
                }
        }
    }

}
复制代码

 

posted @   冬马党  阅读(9656)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示