FilterQuery过滤查询

ES中的查询操作分为两种:查询和过滤。查询即是之前提到的query查询,它默认会计算每个返回文档的得分,然后根据得分排序。而过滤只会筛选出符合条件的文档,并不计算得分,并且可以缓冲记录。所以我们在大范围筛选数据时,应先使用过滤操作过滤数据,然后使用查询匹配数据。

1.使用

1.1初始化创建商品索引

#创建商品索引
#id,title,price,created_at,description
PUT /products
{
  "settings": {
    "number_of_shards": 1, 
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
        "id":{
          "type":"integer"
        },
        "title":{
          "type":"keyword"
        },
        "price":{
          "type":"double"
        },
        "created_at":{
          "type":"date"
        },
        "description":{
          "type":"text",
          "analyzer": "ik_max_word" #使用ik分词器
        }
    }
  }

1.2插入数据

POST /products/_doc/1
{
  "id":1,
  "title":"库迪咖啡",
  "price":"10.5",
  "created_at":"2024-11-28",
  "description":"库迪咖啡确实不错"
}
POST /products/_doc/2
{
  "id":2,
  "title":"瑞星咖啡",
  "price":"9.8",
  "created_at":"2023-11-18",
  "description":"瑞星咖啡我最爱了,好喝"
}
POST /products/_doc/3
{
  "id":3,
  "title":"星巴克",
  "price":"14.5",
  "created_at":"2024-11-18",
  "description":"太苦了,咖啡不好喝"

1.3过滤类型——term

GET products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "description": {
              "value": "咖啡"
            }
          }
        }
      ],
      "filter": [
        {
          "term": {
            "description": "瑞星"
          }
        }
      ]
    }
  }

 

1.4过滤类型——terms

GET products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "terms": {
            "description": [
              "瑞星",
              "好喝"
            ]
          }
        }
      ]
    }
  }
}

 

1.5过滤类型——range

GET products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "range": {
            "price": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ]
    }
  }

1.6过滤类型——exists

GET products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "exists": {
            "field": "title"  #过滤出带某个字段的数据,比如先拿到有title字段的数据
          }
        }
      ]
    }
  }

1.7过滤类型——ids

GET products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "description": {
              "value": "好喝"
            }
          }
        }
      ],
      "filter": [
        {
          "ids": {          #根据数据id过滤出在ids里面的数据
            "values": [
              "1",
              "2"
            ]
          }
        }
      ]
    }
  }

相关推荐

  1. uniapp实现下拉过滤查询列表

    2024-01-13 01:02:01       9 阅读
  2. Django中drf动态过滤查询

    2024-01-13 01:02:01       10 阅读
  3. Clickhouse查询语句执行过程

    2024-01-13 01:02:01       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-13 01:02:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-13 01:02:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-13 01:02:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-13 01:02:01       18 阅读

热门阅读

  1. 【web安全】弱口令,以及不同领域的弱口令爆破

    2024-01-13 01:02:01       41 阅读
  2. autox.js嘎嘎牛p的悬浮窗模板

    2024-01-13 01:02:01       39 阅读
  3. 4 微信小程序

    2024-01-13 01:02:01       33 阅读
  4. 深入理解区间合并:让数字之间的故事更加有序

    2024-01-13 01:02:01       34 阅读
  5. linux系统nginx工具的一些应用

    2024-01-13 01:02:01       34 阅读
  6. C# 快速模指数运算 快速求余运算

    2024-01-13 01:02:01       34 阅读
  7. Linux中关于文件权限详解

    2024-01-13 01:02:01       37 阅读
  8. checkpoint存的是参数还是模型?

    2024-01-13 01:02:01       37 阅读
  9. 面试 React 框架八股文十问十答第六期

    2024-01-13 01:02:01       31 阅读
  10. e.printStackTrace()不会打印到日志文件中

    2024-01-13 01:02:01       35 阅读