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. (转)Eclipse导入EPF配置文件

      为了美化Eclipse大家可以从 http://www.eclipsecolorthemes.org/ 下载EPF配置文件,使用方法如下 (1)从File菜单 选择Import (2) 选择Gen ...

  2. Linux下查看系统启动 、运行以及安装时间

    1.uptime命令  例子显示已启动13天 当前用户一个 [root@localhost-live version-build-related]# uptime :: up days, :, use ...

  3. Jenkins2.0中的pipeline

    jenkins的实现是标准的master/slave模式,用户与master交互,master将job分布到slave上运行. jenkins的基本概念: 1. master, 也就是jenkins的 ...

  4. c++课设学生成绩与学籍管理系统

    题目要求(手打,累):设计一个类CStudent,类中包含一个学生的基本数据如下: 编号,姓名,性别,年龄,数学成绩,计算机成绩,外语成绩. 并假设编号为整数,且从1号往后连续编码:姓名为字符串,性别 ...

  5. Codeforces 792B. Counting-out Rhyme

    B. Counting-out Rhyme time limit per test: 1 second memory limit per test: 256 megabytes input: stan ...

  6. mysqli_query数据库有数据,查不出来

    MySQLDB.class.php <?php /** * 数据库操作工具类 */ class MySQLDB { // 定义相关属性 private $host;// 主机地址 private ...

  7. Valid Mountain Array LT941

    Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A ...

  8. 【UI测试】--合理性

  9. canvas和图片互转

    原文:http://www.jb51.net/html5/160920.html 这么神奇么?先记录一下. 使用JavaScript将图片拷贝进画布 要想将图片放入画布里,我们使用canvas元素的d ...

  10. oracle 中删除表 drop delete truncate

    oracle 中删除表 drop delete truncate   相同点,使用drop delete truncate 都会删除表中的内容 drop table 表名 delete from 表名 ...