Filter查询

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

  • 数据准备

    POST /lib3/user/_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"}
    # 查看mapping
    GET /lib3/_mapping
    {
     "lib3": {
       "mappings": {
         "user": {
           "properties": {
             "itemID": {
               "type": "text",
               "fields": {
                 "keyword": {
                   "type": "keyword",
                   "ignore_above": 256
                }
              }
            },
             "price": {
               "type": "long"
            }
          }
        }
      }
    }
    }
  • 查询

    GET /lib3/user/_search
    {
     "query": {
       "bool": {
         "filter": {
           "term": {
             "price": 40
          }
        }
      }
    }
    }
    # 查询多个值
    GET /lib3/user/_search
    {
     "query": {
       "bool": {
         "filter": {
           "terms": {
             "price": [25,40]
          }
        }
      }
    }
    }
    # 查询不出来,因为itemID text类型并且进行了倒排索引,分词后转为小写存储
    GET /lib3/user/_search
    {
     "query": {
       "bool": {
         "filter": {
           "term": {
             "itemID": "ID100124"
          }
        }
      }
    }
    }
    # 改为小写
    GET /lib3/user/_search
    {
     "query": {
       "bool": {
         "filter": {
           "term": {
             "itemID": "id100124"
          }
        }
      }
    }
    }
    # 查询结果
    {
     "took": 3,
     "timed_out": false,
     "_shards": {
       "total": 5,
       "successful": 5,
       "failed": 0
    },
     "hits": {
       "total": 1,
       "max_score": 0,
       "hits": [
        {
           "_index": "lib3",
           "_type": "user",
           "_id": "2",
           "_score": 0,
           "_source": {
             "price": 50,
             "itemID": "ID100124"
          }
        }
      ]
    }
    }

bool过滤查询

  • 可以实现组合过滤查询

  • 格式

    {
       "bool":{"must":[],"should":[],"must_not":[]}
    }
    • must:必须满足的条件 --and

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

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

    GET /lib3/user/_search
    {
     "query": {
       "bool": {
         "should": [
          {"term": {"price": 25}},
          {"term": {"itemID": "id100123"}}
        ]
        , "must_not": [
          {"term": {
             "price": 40
          }}
        ]
      }
    }
    }
    # 还可以嵌套
    GET /lib3/user/_search
    {
     "query": {
       "bool": {
         "should": [
          { "term": {"price": 25}},
          {
             "bool": {
               "must": [
                {"term":{"itemID":"id100123"}},
                {"term":{"price":40}}
              ]
            }
          }
        ]
      }
    }
    }

范围过滤

  • gt: >

  • lt: <

  • gte: >=

  • lte: <=

    # 范围过滤
    GET /lib3/user/_search
    {
     "query": {
       "bool": {
         "filter": {
           "range": {
             "price": {
               "gt": 25,
               "lt": 50
            }
          }
        }
      }
    }
    }
    # 非空过滤
    GET /lib3/user/_search
    {
     "query": {
       "bool": {
         "filter": {
           "exists": {
             "field": "price"
          }
        }
      }
    }
    }

Filter查询的更多相关文章

  1. Lucene6去掉了Filter但是可以用BooleanQuery实现Filter查询

    Lucene在6.0版本之后彻底废除了Filter的使用,采用BooleanQuery来实现Filter的功能,核心代码如下: TermQuery termQuery = new TermQuery( ...

  2. Django的filter查询

    Django的filter查询 name__contains表示精确大小写的模糊查询 使用name__icontains表示忽略大小写 year_count = DownloadFile.object ...

  3. Elasticsearch(5) --- Query查询和Filter查询

    Elasticsearch(5) --- Query查询和Filter查询 这篇博客主要分为 :Query查询和Filter查询.有关复合查询.聚合查询也会单独写篇博客. 一.概念 1.概念 一个查询 ...

  4. HBase中多Filter查询示例

    在Hbase查询中有时需要用到多个Filter关联的查询. 代码如下: ArrayList<Filter> listForFilters = new ArrayList<Filter ...

  5. EXTJS4扩展实例:如何使用filter查询treepanel

    我们在使用普通的store时,extjs提供了filterBy,filter等多种方法来过滤数据达到查询效果,但在treepanel中的streeStore却没有实现这个查询,于是,就有了这篇文章. ...

  6. Django object filter查询[转]

    用PYTHON ,DJANGO 做站,在通常的情况下,需要用到 orM 的查询方法,比如object.filter(tag__contains='keywords').... 在这种情况下,如果你跟踪 ...

  7. Dynamics 365 Web Api之基于single-valued navigation property的filter查询

    本篇要讲的是dynamics 新版本中web api的一个改进功能,虽然改进的很有限,但至少是改进了. 举个例子,我们现在知道联系人的名字vic,我们想找出客户记录中主要联系人名字为vic的所有客户, ...

  8. LDAP的filter查询详解

    转: 等于(EQUAL TO):  =大于等于(Greater than):  >=小于等于(Less than):  <=通配符(wildcard):  *  逻辑运算符:逻辑与(log ...

  9. 【转】elasticsearch的查询器query与过滤器filter的区别

    很多刚学elasticsearch的人对于查询方面很是苦恼,说实话es的查询语法真心不简单-  当然你如果入门之后,会发现elasticsearch的rest api设计是多么有意思. 说正题,ela ...

随机推荐

  1. (O)JS高阶函数应用——函数节流

    在一些函数需被频繁调用的场景,如:window.onresize.mousemove.scroll滚动事件.上传进度等等,操作频繁导致性能消耗过高,而造成浏览器卡顿现象,我们可以通过函数节流的方式解决 ...

  2. (O)js核心:this

    什么是this this是js中的一个关键词,它总是指向一个对象,而具体指向哪个对象是在运行时基于函数的执行环境动态绑定的,而非函数被声明时的环境. 当函数被调用时,this被添加到作用域中,例如: ...

  3. css 需要阴影的效果

    box-shadow: 0 0 10px 10px #b9bcbf; CSS3 box-shadow 属性 CSS 参考手册 实例 向 div 元素添加 box-shadow: div { box-s ...

  4. c#while循环注意continue的地方

    在使用while 时发现一个很大的问题,continue最好慎用! private void do() { int i = 0; while (true) { //continue;//绝对的死循环 ...

  5. Netty 源码 Channel(二)核心类

    Netty 源码 Channel(二)核心类 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) 一.Channel 类图 二. ...

  6. match

    //清空数据match (n) detach delete n (一)查询节点1.查询所有节点 //查询数据库中的所有节点 match(n)return n 2.查询带有某个标签的所有节点 //查询数 ...

  7. 第一个spring boot 程序

    安装.运行.预览省略 错误1:8080端口被IIS占用,关闭它 Description: The Tomcat connector configured to listen on port 8080 ...

  8. ajax post 请求 ,java端使用 request.getParameter 获取不到数据问题

    js端 $.ajax({ type:'POST', data:{a:1}, url:_this.apiUrl+url, dataType:'json',//使用jsonp方式请求 contentTyp ...

  9. window下文件在Linux下文件乱码解决

    在使用iconv转换文件的字符编码时,如果遇到类似“iconv: illegal input sequence at position”的错误,原因是需要转换的字符编码没有涵盖文件中的字符,比如,将一 ...

  10. ceres入门学习

    转载自https://www.jianshu.com/p/e5b03cf22c80 Ceres solver 是谷歌开发的一款用于非线性优化的库,在谷歌的开源激光雷达slam项目cartographe ...