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 @ 2020-09-03 23:23  冬马党  阅读(9550)  评论(0编辑  收藏  举报