zoukankan      html  css  js  c++  java
  • (15)ElasticSearch Filter查询

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

      1、准备数据

    POST /lib/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、操作演示

      1)查询price是40的;查询price是25或者40的

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

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

    #查询不出
    GET /lib/items/_search
    {
        "query":{
            "bool":{
                "filter":[
                    {"term":{"itemID":"ID100123"}}
                ]
            }
        }
    }
    
    #能查询出
    GET /lib/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
                    }
                }
            }
        }
    }

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

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

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

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

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

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

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

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

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

      4)范围过滤。gt:>;lt:<;gte:>=;lte:<=

      4.1)price价格大于25且小于50的

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

      4.2)查询价格不是null的

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

      

  • 相关阅读:
    java忽略安全警告注解@SuppressWarnings
    spring data jpa 报for input String "id"错误解决
    spring Data Jpa 报错 failed to lazily initialize a collection of role: com.itheim.pojo.Role.users, could not initialize proxy
    记一次Content type 'application/json;charset=UTF-8' not supported解决方案
    包冲突异常 loader constraint violation: when resolving interface method "javax.servlet.jsp.JspApplicationContext.getExpressionFacto
    Java运算符(&)、(|)、(^)、(~)
    Java中的原码、补码、反码
    vim编辑器
    Linux yum、tar、rpm、zip、gzip命令的使用
    error Couldn't find a package.json file in
  • 原文地址:https://www.cnblogs.com/javasl/p/11405405.html
Copyright © 2011-2022 走看看