• ElasticSearch(八)【过滤查询】


    八、过滤查询


    上一篇文章ElasticSearch - 扩展词、停用词配置

    过滤查询,其实准确来说,ES中的查询操作分为2种:查询(query) 过滤(filter)。查询即是之前提到的query查询,它 (查询)默认会计算每个返回文档的得分,然后根据得分排序。而过滤(filter)只会筛选出符合的文档,并不计算得分,而且它可以缓存文档。所以,单从性能考虑,过滤比查询更快。 换句话说,过滤适合在大范围筛选数据,而查询则适合精确匹配数据。一般应用时, 应先使用过滤操作过滤数据,然后使用查询匹配数据

    在这里插入图片描述

    注意

    • 在执行 filter 和 query 时,先执行 filter 在执行 query
    • Elasticsearch 会自动缓存经常使用的过滤器,以加快性能

    常见过滤类型

    • term:单个关键词
    • terms:多个关键词
    • range:范围
    • exists:存在
    • ids:多个id

    term类型

    GET /product/_search
    {
      "query": {
        "bool": {
          "must": [
            {"term": {
              "description": {
                "value": "blog"
              }
            }}
          ],
          "filter": [
            {"term": 
              {"description": "blog"}
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    terms类型

    GET /product/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match_all": {}
            }
          ],
          "filter": [
            {"terms": {
              "description": [
                "blog",
                "vinjcent"
              ]
            }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    range类型

    GET /product/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match_all": {}
            }
          ],
          "filter": [
            {"range": {
              "price": {
                "gte": 0,
                "lte": 20
              }
            }}
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    exists类型,过滤存在指定字段,获取字段不为空的索引记录使用

    GET /product/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match_all": {}
            }
          ],
          "filter": [
            {
              "exists": {
                # 存在某个字段
                "field": "aaa"
              }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    ids类型,过滤含有指定字段的索引记录

    GET /product/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "description": {
                  "value": "blog"
                }
              }
            }
          ],
          "filter": [
            {
              "ids": {
                "values": [
                  "1","2"
                ]
              }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    下一篇文章ElasticSearch - SpringBoot整合

  • 相关阅读:
    支持全光网的波长交换开关发展趋势
    Unity 轮播图
    Amazon云计算AWS(一)
    Knockoutjs属性绑定(Bindings)之流程控制(Control flow)
    leetcode_1792 最大平均通过率
    爬虫都可以干什么?
    Linux配置密钥登录、更换SSH端口Windows更改远程桌面端口
    Imagination CPU系列研讨会|RISC-V平台的性能分析和调试
    并查集&LRUCache
    element-ui动态设置tabel时,表格错乱,表头闪烁的坑
  • 原文地址:https://blog.csdn.net/Wei_Naijia/article/details/126923796