爆款云主机2核4G限时秒杀,88元/年起!
查看详情

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 618智算钜惠季 爆款云主机2核4G限时秒杀,88元/年起!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 首保服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

      elasticsearch实战三部曲之三:搜索操作

      首页 知识中心 其他 文章详情页

      elasticsearch实战三部曲之三:搜索操作

      2023-05-15 10:03:24 阅读次数:160

      elasticsearch,java

      本文是《elasticsearch实战三部曲》的终篇,作为elasticsearch的核心功能,搜索的重要性不言而喻,今天的实战都会围绕搜索展开;

      环境信息

      • 本次实战用到的elasticsearch版本是6.5.4,安装在Ubuntu 16.04.5 LTS,客户端工具是postman6.6.1;
      • 如果您需要搭建elasticsearch环境,请参考《Linux环境快速搭建elasticsearch6.5.4集群和Head插件》;

      基本情况介绍

      • 本次实战的elasticsearch环境以及搭建完毕,是由两个机器搭建的集群,并且elasticsearch-head也搭建完成:
      • 一号机器,IP地址:192.168.119.152;
      • 二号机器:IP地址:192.168.119.153;
      • elasticsearch-head安装在一号机器,访问地址:http://192.168.119.152:9100
      • 已经建立了索引englishbooks,对应的数据如下所示,请用批量命令导入到elasticsearch:
      {"index":{ "_index": "englishbooks", "_type": "IT", "_id": "1" }}
      {"id":"1","title":"Deep Learning","language":"python","author":"Yoshua Bengio","price":549.00,"publish_time":"2016-11-18","description":"written by three experts in the field, deep learning is the only comprehensive book on the subject."}
      {"index":{ "_index": "englishbooks", "_type": "IT", "_id": "2" }}
      {"id":"2","title":"Compilers","language":"c","author":"Alfred V.Aho","price":62.50,"publish_time":"2011-01-01","description":"In the time since the 1986 edition of this book, the world of compiler designhas changed significantly."}
      {"index":{ "_index": "englishbooks", "_type": "IT", "_id": "3" }}
      {"id":"3","title":"Core Java","language":"java","author":"Horstmann","price":85.90,"publish_time":"2016-06-01","description":"The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "}
      {"index":{ "_index": "englishbooks", "_type": "IT", "_id": "4" }}
      {"id":"4","title":"Thinking in Java","language":"java","author":"Bruce Eckel","price":70.10,"publish_time":"2015-07-06","description":"Thinking in Java should be read cover to cover by every Java programmer, then kept close at hand for frequent reference. The exercises are challenging, and the chapter on Collections is superb!"}
      {"index":{ "_index": "englishbooks", "_type": "IT", "_id": "5" }}
      {"id":"5","title":"The Go Programming Language","language":"go","author":"Alan A.A.Donovan","price":63.90,"publish_time":"2016-01-01","description":"A declaration's lexical block determines its scope, which may be large or small. The declarations of built—in types, functions, and constants like int, len, and true are in the universe block and can be referred to throughout the entire program."}
      
      
      • 相关的文档是批量导入的,关于文档数据和批量操作的细节请参考《elasticsearch实战三部曲之二:文档操作》;

      • books索引的文档内容在head中展示如下图: elasticsearch实战三部曲之三:搜索操作

      数据格式说明

      • 为了便于和读者沟通,我们来约定一下如何在文章中表达请求和响应的信息:

      • 假设通过Postman工具向服务器发送一个PUT类型的请求,地址是:http://192.168.119.152:9200/test001/article/1

      • 请求的内容是JSON格式的,内容如下:

      {
      	“id”:1,
      	"title":"标题a",
      	"posttime":"2019-01-12",
      	"content":"一起来熟悉文档相关的操作"
      }
      
      • 对于上面的请求,我在文章中就以如下格式描述:
      PUT test001/article/1
      
      {
      	“id”:1,
      	"title":"标题a",
      	"posttime":"2019-01-12",
      	"content":"一起来熟悉文档相关的操作"
      }
      
      • 读者您看到上述内容,就可以在postman中发起PUT请求,地址是"test001/article/1"前面加上您的服务器地址,内容是上面的JSON;

      本文中的文档内容暂不涉及中文

      • 文中数据都是英文的,避免在因分词器的分词问题导致搜索不到对应的中文结果,分词器相关的知识会在另一篇文章中详细介绍;

      查看所有数据

      GET englishbooks/_search
      
      {
      	"query":{
      		"match_all":{}
      	}
      }
      
      • 上述查询返回索引books的所有记录,并且文档得分收是1;
      • 您可以将请求的整个JSON删除,只用books/_search这个URL来试试,也能得到所有数据,这是match_all的简写;

      数字字段的精确匹配

      • 查询价格等于549的记录:
      GET englishbooks/_search
      
      {
      	"query":{
      		"constant_score":{
      			"filter":{
      				"term":{"price":549}
      			}
      		}
      	}
      }
      
      • 得到结果:
      {
          "took": 4,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "1",
                      "_score": 1,
                      "_source": {
                          "id": "1",
                          "title": "Deep Learning",
                          "language": "python",
                          "author": "Yoshua Bengio",
                          "price": 549,
                          "publish_time": "2016-11-18",
                          "description": "written by three experts in the field, deep learning is the only comprehensive book on the subject."
                      }
                  }
              ]
          }
      }
      
      • 请求参数中使用了constant_score 后,查询将以非评分模式来执行 term,并以一作为统一评分;

      查看分词效果

      • text类型的字段会被分词后构建倒排索引,来看看title字段的值为"Core Java"时的分词效果:
      GET englishbooks/_analyze
      
      {
      	"field":"title",
      	"text":"Core Java"
      }
      
      • 响应如下所示,"Core Java"被分"core"和"java"两个词,也就是说我们以词项"core"或"java"搜索title字段都能收到对应文档:
      {
          "tokens": [
              {
                  "token": "core",
                  "start_offset": 0,
                  "end_offset": 4,
                  "type": "<ALPHANUM>",
                  "position": 0
              },
              {
                  "token": "java",
                  "start_offset": 5,
                  "end_offset": 9,
                  "type": "<ALPHANUM>",
                  "position": 1
              }
          ]
      }
      
      • 需要注意的是分词后的结果都是小写,这是分词器的处理结果;

      词项查询(term query)

      • 前面我们查看分词效果发现"Core Java"被分"core"和"java"两个词,现在就以"java"为关键词搜索一下试试:
      GET englishbooks/_search
      
      {
      	"query":{
      		"term":{"title":"java"}
      	}
      }
      
      • 结果如下,title中有java关键词的两个文档都被搜到:
      {
          "took": 4,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 2,
              "max_score": 0.5754429,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "4",
                      "_score": 0.5754429,
                      "_source": {
                          "id": "4",
                          "title": "Thinking in Java",
                          "language": "java",
                          "author": "Bruce Eckel",
                          "price": 70.1,
                          "publish_time": "2015-07-06",
                          "description": "Thinking in Java should be read cover to cover by every Java programmer, then kept close at hand for frequent reference. The exercises are challenging, and the chapter on Collections is superb!"
                      }
                  },
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "3",
                      "_score": 0.2876821,
                      "_source": {
                          "id": "3",
                          "title": "Core Java",
                          "language": "java",
                          "author": "Horstmann",
                          "price": 85.9,
                          "publish_time": "2016-06-01",
                          "description": "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "
                      }
                  }
              ]
          }
      }
      

      分词查询(match query)

      • term query的特点是将输入的内容作为一个词项来用,例如以下的查询是没有结果的:
      GET englishbooks/_search
      
      {
      	"query":{
      		"term":{"title":"core java"}
      	}
      }
      
      • 上述查询没有结果的原因,是因为"core java"被当做一个词项去查询了,而title的分词结果中只有"core"、"java"这些分词过的词项,并没有一个叫做"core java"的词项,所以搜不到结果;

      • 如果输入的查询条件"core java"也被做一次分词处理,再把处理结果"core"和"java"用来搜索,应该就能得到结果了,match query就是用来对输入条件做分词处理的,如下:

      GET englishbooks/_search
      
      {
      	"query":{
      		"match":{"title":"Core Java"}
      	}
      }
      
      • 搜索结果如下,包含了java的两条记录都被查出来了:
      {
          "took": 8,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 2,
              "max_score": 0.5754429,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "4",
                      "_score": 0.5754429,
                      "_source": {
                          "id": "4",
                          "title": "Thinking in Java",
                          "language": "java",
                          "author": "Bruce Eckel",
                          "price": 70.1,
                          "publish_time": "2015-07-06",
                          "description": "Thinking in Java should be read cover to cover by every Java programmer, then kept close at hand for frequent reference. The exercises are challenging, and the chapter on Collections is superb!"
                      }
                  },
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "3",
                      "_score": 0.5753642,
                      "_source": {
                          "id": "3",
                          "title": "Core Java",
                          "language": "java",
                          "author": "Horstmann",
                          "price": 85.9,
                          "publish_time": "2016-06-01",
                          "description": "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "
                      }
                  }
              ]
          }
      }
      
      • 如果我们的本意是只要"Core Java"的匹配结果,上面的结果显然是不符合要求的,此时可以给查询条件加个**"operator":"and"**属性,就会查询匹配了所有关键词的文档,注意json的结构略有变化,以前title的属性是搜索条件,现在变成了一个json对象,里面的query属性是原来的搜索条件:
      GET englishbooks/_search
      
      {
      	"query":{
      		"match":{
      			"title":{
      				"query":"Core Java",
      				"operator":"and"
      			}
      		}
      	}
      }
      
      • 这次的搜索结果就是同时匹配了"core"和"java"两个词项的记录了(为什么core和java是小写? 因为"Core Java"被分词后改为了小写,再去搜索的):
      {
          "took": 11,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 1,
              "max_score": 0.5753642,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "3",
                      "_score": 0.5753642,
                      "_source": {
                          "id": "3",
                          "title": "Core Java",
                          "language": "java",
                          "author": "Horstmann",
                          "price": 85.9,
                          "publish_time": "2016-06-01",
                          "description": "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "
                      }
                  }
              ]
          }
      }
      

      match_phrase搜索

      • match_phrase搜索和前面的match搜索相似,并且有以下两个特点:
      • 分词后的所有词项都要匹配上,也就是前面的**"operator":"and"**属性的效果;
      • 分析后的词项顺序要和搜索字段的顺序一致,才能匹配上;
      GET englishbooks/_search
      
      {
      	"query":{
      		"match_phrase":{"title":"Core Java"}
      	}
      }
      
      • 上述查询可以搜索到结果,但如果将"Core Java"改成"Java Core"就搜不到结果了,但是match query用"Java Core"是可以搜到结果的;

      match_phrase_prefix搜索

      • match_phrase_prefix的功能和前面的match_phrase类似,不过match_phrase_prefix支持最后一个词项做前缀匹配,如下所示,"Core J"这个搜索条件用match_phrase是搜不到结果的,但是match_phrase_prefix可以,因为"J"可以作为前缀和"Java"匹配:
      GET englishbooks/_search
      
      {
      	"query":{
      		"match_phrase":{"title":"Core J"}
      	}
      }
      

      multi_match搜素

      • multi_match是在match的基础上支持多字段搜索,以下查询就是用"1986"和"deep"这两个词项,同时搜索title和description两个字段:
      GET englishbooks/_search
      
      {
      	"query":{
      		"multi_match":{
      			"query":"1986 deep",
      			"fields":["title", "description"]
      		}
      	}
      }
      
      • 响应如下,可见title和description中含有词项"1986"或者"deep"的文档都被返回了:
      {
          "took": 4,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 2,
              "max_score": 0.79237825,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "2",
                      "_score": 0.79237825,
                      "_source": {
                          "id": "2",
                          "title": "Compilers",
                          "language": "c",
                          "author": "Alfred V.Aho",
                          "price": 62.5,
                          "publish_time": "2011-01-01",
                          "description": "In the time since the 1986 edition of this book, the world of compiler designhas changed significantly."
                      }
                  },
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "1",
                      "_score": 0.2876821,
                      "_source": {
                          "id": "1",
                          "title": "Deep Learning",
                          "language": "python",
                          "author": "Yoshua Bengio",
                          "price": 549,
                          "publish_time": "2016-11-18",
                          "description": "written by three experts in the field, deep learning is the only comprehensive book on the subject."
                      }
                  }
              ]
          }
      }
      

      terms query

      • terms是term查询的升级,用来查询多个词项:
      GET englishbooks/_search
      
      {
      	"query":{
      		"terms":{
      			"title":["deep", "core"]
      		}
      	}
      }
      
      • 响应如下,title中含有deep和core的文档都被查到:
      {
          "took": 5,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 2,
              "max_score": 1,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "1",
                      "_score": 1,
                      "_source": {
                          "id": "1",
                          "title": "Deep Learning",
                          "language": "python",
                          "author": "Yoshua Bengio",
                          "price": 549,
                          "publish_time": "2016-11-18",
                          "description": "written by three experts in the field, deep learning is the only comprehensive book on the subject."
                      }
                  },
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "3",
                      "_score": 1,
                      "_source": {
                          "id": "3",
                          "title": "Core Java",
                          "language": "java",
                          "author": "Horstmann",
                          "price": 85.9,
                          "publish_time": "2016-06-01",
                          "description": "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "
                      }
                  }
              ]
          }
      }
      

      范围查询

      • range query是范围查询,例如查询publish_time在"2016-01-01"到"2016-12-31"之间的文档:
      GET englishbooks/_search
      
      {
      	"query":{
      		"range":{
      			"publish_time":{
      				"gte":"2016-01-01",
      				"lte":"2016-12-31",
      				"format":"yyyy-MM-dd"
      			}
      		}
      	}
      }
      
      • 篇幅所限,此处略去返回结果;

      exists query

      • exists query返回的是字段中至少有一个非空值的文档:
      GET englishbooks/_search
      
      {
      	"query":{
      		"exists":{
      			"field":"author"
      		}
      	}
      }
      

      前缀查询

      • 用于查询某个字段是否以给定前缀开始:
      GET englishbooks/_search
      
      {
      	"query":{
      		"prefix":{
      			"title":"cor"
      		}
      	}
      }
      
      • 以上请求可以查到title字段为"Core Java"的文档:
      {
          "took": 6,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "3",
                      "_score": 1,
                      "_source": {
                          "id": "3",
                          "title": "Core Java",
                          "language": "java",
                          "author": "Horstmann",
                          "price": 85.9,
                          "publish_time": "2016-06-01",
                          "description": "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "
                      }
                  }
              ]
          }
      }
      

      通配符查询

      • 以下查询,可以搜到title字段中含有"core"的文档,另外需要注意的是,"?"匹配一个字符,"*"匹配零个或者多个字符:
      GET englishbooks/_search
      
      {
      	"query":{
      		"wildcard":{
      			"title":"cor?"
      		}
      	}
      }
      

      正则表达式

      • 使用属性regexp可以进行正则表达式查询,例如查找description字段带有4位数字的分词的文档:
      GET englishbooks/_search
      
      {
      	"query":{
      		"regexp":{
      			"description":"[0-9]{4}"
      		}
      	}
      }
      
      • 查找结果如下,description字段中带有数字1986:
      {
          "took": 4,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "2",
                      "_score": 1,
                      "_source": {
                          "id": "2",
                          "title": "Compilers",
                          "language": "c",
                          "author": "Alfred V.Aho",
                          "price": 62.5,
                          "publish_time": "2011-01-01",
                          "description": "In the time since the 1986 edition of this book, the world of compiler designhas changed significantly."
                      }
                  }
              ]
          }
      }
      

      模糊查询(fuzzy query)

      • fuzzy是通过计算词项与文档的编辑距离来得到结果的,例如查找description字段还有分词"1986"的时候,不小心输入了"1987",通过fuzzy查询也能得到结果,只是得分变低了,请求内容如下所示:
      GET englishbooks/_search
      
      {
      	"query":{
      		"fuzzy":{
      			"description":"1987"
      		}
      	}
      }
      
      • 搜索到的文档如下所示,得分只有0.5942837,低于用"1986"查询的0.79237825:
      {
          "took": 5,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 1,
              "max_score": 0.5942837,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "2",
                      "_score": 0.5942837,
                      "_source": {
                          "id": "2",
                          "title": "Compilers",
                          "language": "c",
                          "author": "Alfred V.Aho",
                          "price": 62.5,
                          "publish_time": "2011-01-01",
                          "description": "In the time since the 1986 edition of this book, the world of compiler designhas changed significantly."
                      }
                  }
              ]
          }
      }
      
      • 需要注意的是,fuzzy查询时消耗资源较大;

      复合查询

      • 常用到的复合查询是bool query,可以用下表中的条件组合查询:
      属性 作用
      must 必须匹配,相当于SQL中的AND
      should 可以匹配,相当于SQL中的OR
      must_not 必须不匹配
      filter 和must一样,但是不评分
      • 以下条件,搜索的是title中带有java,但是不包含core的文档:
      GET englishbooks/_search
      
      {
      	"query":{
      		"bool":{
      			"must":{
      				"term":{"title":"java"}	
      			},
      			"must_not":[
      			{"term":{"title":"core"}}
      			]
      		}
      	}
      }
      
      • 得到的文档中,带有core词项的已经被过滤了:
      {
          "took": 3,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 1,
              "max_score": 0.5754429,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "4",
                      "_score": 0.5754429,
                      "_source": {
                          "id": "4",
                          "title": "Thinking in Java",
                          "language": "java",
                          "author": "Bruce Eckel",
                          "price": 70.1,
                          "publish_time": "2015-07-06",
                          "description": "Thinking in Java should be read cover to cover by every Java programmer, then kept close at hand for frequent reference. The exercises are challenging, and the chapter on Collections is superb!"
                      }
                  }
              ]
          }
      }
      

      脚本查询

      • 可用脚本进行查询,如下是查询价格大于100的所有文档:
      GET englishbooks/_search
      
      {
      	"query":{
      		"bool":{
      			"must":{
      				"script":{
      					"script":{
      						"inline":"doc['price'].value>500",
      						"lang":"painless"
      					}
      				}	
      			}
      		}
      	}
      }
      
      • 得到的结果只有price大于500的文档:
      {
          "took": 8,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "1",
                      "_score": 1,
                      "_source": {
                          "id": "1",
                          "title": "Deep Learning",
                          "language": "python",
                          "author": "Yoshua Bengio",
                          "price": 549,
                          "publish_time": "2016-11-18",
                          "description": "written by three experts in the field, deep learning is the only comprehensive book on the subject."
                      }
                  }
              ]
          }
      }
      

      指定排序字段

      • 默认的排序方式是按照评分来排序的(也就是相关度排序),可以用sort属性来设置排序字段,下面的请求指定了按照price字段降序排序:
      {
      	"query":{
      		"term":{"title":"java"}
      	},
      	"sort":[
      		{"price":{"order":"desc"}}
      	]
      }
      
      • 得到结果:
      {
          "took": 4,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 2,
              "max_score": null,
              "hits": [
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "3",
                      "_score": null,
                      "_source": {
                          "id": "3",
                          "title": "Core Java",
                          "language": "java",
                          "author": "Horstmann",
                          "price": 85.9,
                          "publish_time": "2016-06-01",
                          "description": "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "
                      },
                      "sort": [
                          85.9
                      ]
                  },
                  {
                      "_index": "englishbooks",
                      "_type": "IT",
                      "_id": "4",
                      "_score": null,
                      "_source": {
                          "id": "4",
                          "title": "Thinking in Java",
                          "language": "java",
                          "author": "Bruce Eckel",
                          "price": 70.1,
                          "publish_time": "2015-07-06",
                          "description": "Thinking in Java should be read cover to cover by every Java programmer, then kept close at hand for frequent reference. The exercises are challenging, and the chapter on Collections is superb!"
                      },
                      "sort": [
                          70.1
                      ]
                  }
              ]
          }
      }
      
      • 以上就是常用的搜索操作了,至此,《elasticsearch实战三部曲》系列就全部完成,三篇文章列举的是一些常用的基本操作,希望能帮助读者您快速熟悉elasticsearch,后面咱们再一起深入实战;
      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/zq2599/5530437,作者:程序员欣宸,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:linux-压缩与解压缩

      下一篇:Elasticsearch Search流程

      相关文章

      2025-05-14 10:02:58

      java休眠到指定时间怎么写

      java休眠到指定时间怎么写

      2025-05-14 10:02:58
      java , sleep , Thread , util , 方法
      2025-05-14 10:02:58

      java项目多端数据同步解决方案

      多端数据同步是指在多个设备(例如桌面应用、移动应用、Web应用)之间保持数据的一致性。

      2025-05-14 10:02:58
      java , Spring , WebSocket , 同步 , 数据 , 版本号
      2025-05-13 09:49:12

      Java学习(动态代理的思想详细分析与案例准备)(1)

      Java学习(动态代理的思想详细分析与案例准备)(1)

      2025-05-13 09:49:12
      java , 代理 , 代码 , 对象 , 接口 , 方法 , 需要
      2025-05-09 08:20:32

      基于IDEA的Maven简单工程创建及结构分析

      通过一个 mvn 命令直接让我们创建一个 Maven 的脚手架。

      2025-05-09 08:20:32
      java , Maven , xml , 创建 , 文件 , 文件夹 , 项目
      2025-05-08 09:03:57

      前K个高频元素java

      给定一个非空的整数数组,返回其中出现频率前 前K个高频元素java 高的元素。

      2025-05-08 09:03:57
      java , 元素 , 样例 , 给定
      2025-05-08 09:03:21

      基于java Swing开发的学生成绩管理系统【项目源码+数据库脚本】

      基于java Swing开发的学生成绩管理系统【项目源码+数据库脚本】

      2025-05-08 09:03:21
      java , Swing , 学生 , 源码
      2025-05-08 09:03:21

      java Swing学生成绩管理系统【项目源码+数据库脚本】

      本项目是一套基于java Swing开发的学生成绩管理系统,主要针对计算机相关专业的正在做bishe的学生和需要项目实战练习的Java学习者。

      2025-05-08 09:03:21
      java , 学生 , 成绩 , 数据库 , 源码
      2025-05-07 09:08:08

      java Swing学生选课管理系统【源码+数据库+报告】

      本项目是一套基于java Swing学生选课管理系统,主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者。

      2025-05-07 09:08:08
      java , 学生 , 截图 , 源码
      2025-05-07 09:08:08

      java swing人机对战五子棋(含背景音乐)

      本项目是一套基于java swing的人机对战五子棋系统,主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者。

      2025-05-07 09:08:08
      java , 源码
      2025-04-22 09:40:08

      【ETL工具】kettle 程序报错 Javascript error: TypeError: Cannot call method “trim“ of null

      【ETL工具】kettle 程序报错 Javascript error: TypeError: Cannot call method “trim“ of null

      2025-04-22 09:40:08
      java , javascript , org
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      33561

      阅读量

      5239832

      查看更多

      最新文章

      java休眠到指定时间怎么写

      2025-05-14 10:02:58

      基于IDEA的Maven简单工程创建及结构分析

      2025-05-09 08:20:32

      java中final的用法

      2025-04-11 07:15:54

      使用ZipEntry解压zip文件报错: java.lang.IllegalArgumentException: MALFORMED

      2025-04-09 09:17:07

      总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇

      2025-04-09 09:14:24

      【漏洞复现】CVE-2015-5531 Arbitrary File Reading

      2025-03-24 08:53:15

      查看更多

      热门文章

      在java正则表达式中为什么要对 . 进行两次的转义操作 (\\.)才表示真正的 .

      2023-05-15 10:01:56

      GC是什么? 为什么要有GC?

      2023-05-10 06:02:16

      axios&spring前后端分离传参规范总结

      2023-05-22 08:09:06

      Jsp Ajax之模拟用户注册

      2022-11-17 12:37:24

      定义一个函数,接收三个参数返回一元二次方程

      2023-02-13 07:59:59

      JAVA基础加强笔记

      2022-11-14 02:56:39

      查看更多

      热门标签

      linux java python javascript 数组 前端 docker Linux vue 函数 shell git 节点 容器 示例
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      Base64解码时Illegal base64 character 20问题解决

      【手写Tomcat】1.流程梳理

      用户登陆

      Elasticsearch 写流程

      java通过commons-fileupload实现多张图片的上传(jsp页面)

      Tomcat启动startup.bat一闪而过就消失和乱码的解决方法

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 旗舰店
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 天翼云旗舰店
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号