当前位置: 首页 > news >正文

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"
            ]
          }
        }
      ]
    }
  }

http://www.lryc.cn/news/279278.html

相关文章:

  • java多线程(并发)夯实之路-线程池深入浅出
  • 数据库-列的类型-字符串char类型
  • 大话 JavaScript(Speaking JavaScript):第二十一章到第二十五章
  • ICMP协议
  • 环信服务端下载消息文件---菜鸟教程
  • 创建型模式 | 建造者模式
  • MVC设计模式
  • WSL (2103) ERROR: CreateProcessEntryCommon:493: chdir 错误解决
  • 【二、自动化测试】为什么要做自动化测试?哪种项目适合做自动化?
  • 用ChatGPT来造一个ChatGPT:计算机领域智能问答系统实践(2)
  • Ubuntu开机自动挂载硬盘
  • vue3基础:单文件组件介绍
  • OCR字符识别:开始批量识别身份证信息
  • php多小区智慧物业管理系统源码带文字安装教程
  • 解决虚拟机的网络图标不见之问题
  • 【Spring类路径Bean定义信息扫描】
  • Ubuntu上安装VMware+win11系统手册
  • 2024年1月12日:清爽无糖rio留下唇齿之间的香甜
  • 群晖Synology Drive同步文件时过滤指定文件夹“dist“, “node_modules“
  • 小程序中滚动字幕
  • MySQL中约束是什么?
  • 若依在表格中如何将字典的键值转为中文
  • 用笨办法-刻意练习来提高自己的编程能力
  • FineBI报表页面大屏小屏自适应显示问题
  • NAND Separate Command Address (SCA) 接口命令解读
  • Git的简单使用说明
  • 少儿编程 2023年12月电子学会图形化编程等级考试Scratch二级真题解析(判断题)
  • 前端面试 -- vue系列
  • open3d相关操作总结
  • HTTP数据请求