1、query string search

搜索全部商品:

GET /ecommerce/product/_search

took:耗费了几毫秒
timed_out:是否超时,这里是没有
_shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
hits.total:查询结果的数量,3个document
hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据

{"took": 2,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "ecommerce","_type": "product","_id": "2","_score": 1,"_source": {"name": "jiajieshi yagao","desc": "youxiao fangzhu","price": 25,"producer": "jiajieshi producer","tags": ["fangzhu"]}},{"_index": "ecommerce","_type": "product","_id": "1","_score": 1,"_source": {"name": "gaolujie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]}},{"_index": "ecommerce","_type": "product","_id": "3","_score": 1,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]}}]}
}

query string search的由来,因为search参数都是以http请求的query string来附带的

搜索商品名称中包含yagao的商品,而且按照售价降序排序:

GET /ecommerce/product/_search?q=name:yagao&sort=price:desc

结果是:

{"took": 5,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 3,"max_score": null,"hits": [{"_index": "ecommerce","_type": "product","_id": "3","_score": null,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]},"sort": [40]},{"_index": "ecommerce","_type": "product","_id": "1","_score": null,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]},"sort": [30]},{"_index": "ecommerce","_type": "product","_id": "2","_score": null,"_source": {"name": "jiajieshi yagao","desc": "youxiao fangzhu","price": 25,"producer": "jiajieshi producer","tags": ["fangzhu"]},"sort": [25]}]}
}

适用于临时的在命令行使用一些工具,比如curl,快速的发出请求,来检索想要的信息;但是如果查询请求很复杂,是很难去构建的
在生产环境中,几乎很少使用query string search

2、query DSL

DSL:Domain Specified Language,特定领域的语言
http request body:请求体,可以用json的格式来构建查询语法,比较方便,可以构建各种复杂的语法,比query string search肯定强大多了

查询所有的商品

GET /ecommerce/product/_search
{"query": { "match_all": {} }
}

结果是:

{"took": 2,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "ecommerce","_type": "product","_id": "2","_score": 1,"_source": {"name": "jiajieshi yagao","desc": "youxiao fangzhu","price": 25,"producer": "jiajieshi producer","tags": ["fangzhu"]}},{"_index": "ecommerce","_type": "product","_id": "1","_score": 1,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]}},{"_index": "ecommerce","_type": "product","_id": "3","_score": 1,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]}}]}
}

查询名称包含yagao的商品,同时按照价格降序排序

GET /ecommerce/product/_search
{"query" : {"match" : {"name" : "yagao"}},"sort": [{ "price": "desc" }]
}

结果是:

{"took": 3,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 1,"max_score": null,"hits": [{"_index": "ecommerce","_type": "product","_id": "3","_score": null,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]},"sort": [40]}]}
}

如果在查询的时候报如下错误:

{"error": {"root_cause": [{"type": "illegal_argument_exception","reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [price] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}],"type": "search_phase_execution_exception","reason": "all shards failed","phase": "query","grouped": true,"failed_shards": [{"shard": 0,"index": "ecommerce","node": "DEtgiaiTSaWWvmOfvlvc6Q","reason": {"type": "illegal_argument_exception","reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [price] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}}]},"status": 400
}

解决办法是(其中语法是:/type/_mapping/index),然后添加fielddata属性:

PUT /ecommerce/_mapping/product
{"properties": {"price":{"type": "text","fielddata": true}}
}

执行完成之后:

{"acknowledged": true
}

分页查询商品,总共3条商品,假设每页就显示1条商品,现在显示第2页,所以就查出来第2个商品

GET /ecommerce/product/_search
{"query": { "match_all": {} },"from": 1,"size": 1
}

结果是:

{"took": 4,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "ecommerce","_type": "product","_id": "1","_score": 1,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]}}]}
}

指定要查询出来商品的名称和价格就可以

GET /ecommerce/product/_search
{"query": { "match_all": {} },"_source": ["name", "price"]
}

结果是:

{"took": 3,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "ecommerce","_type": "product","_id": "2","_score": 1,"_source": {"price": 25,"name": "jiajieshi yagao"}},{"_index": "ecommerce","_type": "product","_id": "1","_score": 1,"_source": {"price": 30,"name": "jiaqiangban gaoluejie yagao"}},{"_index": "ecommerce","_type": "product","_id": "3","_score": 1,"_source": {"price": 40,"name": "zhonghua yagao"}}]}
}

更加适合生产环境的使用,可以构建复杂的查询

3、query filter

搜索商品名称包含yagao,而且售价大于25元的商品

GET /ecommerce/product/_search
{"query" : {"bool" : {"must" : {"match" : {"name" : "yagao" }},"filter" : {"range" : {"price" : { "gt" : 25 } }}}}
}

结果是:

{"took": 68,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 2,"max_score": 0.25811607,"hits": [{"_index": "ecommerce","_type": "product","_id": "3","_score": 0.25811607,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]}},{"_index": "ecommerce","_type": "product","_id": "1","_score": 0.25316024,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]}}]}
}

4、full-text search(全文检索)

先添加一条记录:

put /ecommerce/product/4
{"name":"special yagao","desc":"special meibai","price":50,"producer":"special yagao producer","tags":["meibai"]
}

进行查询

GET /ecommerce/product/_search
{"query" : {"match" : {"producer" : "yagao producer"}}
}

上面的producer这个字段,会先被拆解,建立倒排索引

special 4
yagao 4
producer 1,2,3,4
gaolujie 1
zhognhua 3
jiajieshi 2

也就是说yagao producer 会被拆解成: yagao和producer,最后查询出来的结果是:

{"took": 4,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"max_score": 0.25811607,"hits": [{"_index": "ecommerce","_type": "product","_id": "1","_score": 0.25811607,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]}},{"_index": "ecommerce","_type": "product","_id": "3","_score": 0.25811607,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]}},{"_index": "ecommerce","_type": "product","_id": "2","_score": 0.1805489,"_source": {"name": "jiajieshi yagao","desc": "youxiao fangzhu","price": 25,"producer": "jiajieshi producer","tags": ["fangzhu"]}},{"_index": "ecommerce","_type": "product","_id": "4","_score": 0.14638957,"_source": {"name": "special yagao","desc": "special meibai","price": 50,"producer": "special yagao producer","tags": ["meibai"]}}]}
}

5、phrase search(短语搜索)

跟全文检索相对应,相反,全文检索会将输入的搜索串拆解开来,去倒排索引里面去一一匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回
phrase search,要求输入的搜索串,必须在指定的字段文本中,完全包含一模一样的,才可以算匹配,才能作为结果返回

GET /ecommerce/product/_search
{"query" : {"match_phrase" : {"producer" : "yagao producer"}}
}

搜索到的结果是:

{"took": 11,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 1,"max_score": 0.70293105,"hits": [{"_index": "ecommerce","_type": "product","_id": "4","_score": 0.70293105,"_source": {"name": "special yagao","desc": "special meibai","price": 50,"producer": "special yagao producer","tags": ["meibai"]}}]}
}

6、highlight search(高亮搜索结果)

GET /ecommerce/product/_search
{"query" : {"match" : {"producer" : "producer"}},"highlight": {"fields" : {"producer" : {}}}
}

查询出来的结果是:

{"took": 50,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"max_score": 0.25811607,"hits": [{"_index": "ecommerce","_type": "product","_id": "1","_score": 0.25811607,"_source": {"name": "jiaqiangban gaoluejie yagao","desc": "gaoxiao meibai","price": 30,"producer": "gaolujie producer","tags": ["meibai","fangzhu"]},"highlight": {"producer": ["gaolujie <em>producer</em>"]}},{"_index": "ecommerce","_type": "product","_id": "3","_score": 0.25811607,"_source": {"name": "zhonghua yagao","desc": "caoben zhiwu","price": 40,"producer": "zhonghua producer","tags": ["qingxin"]},"highlight": {"producer": ["zhonghua <em>producer</em>"]}},{"_index": "ecommerce","_type": "product","_id": "2","_score": 0.1805489,"_source": {"name": "jiajieshi yagao","desc": "youxiao fangzhu","price": 25,"producer": "jiajieshi producer","tags": ["fangzhu"]},"highlight": {"producer": ["jiajieshi <em>producer</em>"]}},{"_index": "ecommerce","_type": "product","_id": "4","_score": 0.14638957,"_source": {"name": "special yagao","desc": "special meibai","price": 50,"producer": "special yagao producer","tags": ["meibai"]},"highlight": {"producer": ["special yagao <em>producer</em>"]}}]}
}

ES的多种搜索机制:query string search,query DSL,query filter,full-text search,phrase search,highlight search相关推荐

  1. Elasticsearch: Query string与Simple query string

    文章目录 1.Query string 1.1 举例 1.2 query_string根一级参数 1.3 查询字符串语法 1.4 proximity query(临近搜索) 1.5 Range(范围) ...

  2. ElasticSearch(四):DSL Query

    ES中提供了一种强大的检索数据方式,这种检索方式称之为Query DSL(Domain Specified Language) Query DSL是利用Rest API传递JSON格式的请求体(Req ...

  3. ES多种搜索方式总结

    本节使用的是ES低版本,包含type. 分类 query string search query DSL query filter full-text search phrase search hig ...

  4. 2021最新版 ElasticSearch 7.6.1 教程详解 爬虫jsoup+es模拟京东搜索(狂神说)

    文章目录 一.ElasticSearch 简介 1.了解创始人 Doug Cutting 2.Lucene 简介 3.ElasticSearch 简介 4.ElasticSearch 和 Solr 的 ...

  5. ES 查询示例 搜索 分组 去重 分页 排序

    es 查询示例 搜索 分组 去重 分页 排序 java 语句 @Autowiredprivate static RestHighLevelClient client;@PostConstructpub ...

  6. es使用pinyin搜索,对应中文没有高亮显示

    刚开始的时候,索引 article 的 mapping {"mapping":{"article":{"mappings":{"p ...

  7. 使用ES的数据搜索功能

    DSL查询文档 elasticsearch的查询是基于JSON风格的DSL来实现的 使用下面的代码首先看es的,把数据批量导入之后在运行在Kibana中 查询所有,查询类型时match_all ,没有 ...

  8. 07实战之电商网站商品管理:多种搜索方式

    作为准备工作,又重新恢复了之前的3个商品文档 本节主要演示多种搜索方式,关于各种搜索语法后续会详细讲解. 1.query string search 之前也用过这个命令 , 搜索全部商品: GET / ...

  9. elasticsearch 基础 —— Query String

    使用查询解析器来解析其内容的查询.下面是一个例子: GET /_search {"query": {"query_string" : {"defaul ...

最新文章

  1. 环境监控告警系统之TIM即时消息推送部署(二)
  2. 元月份退休能享受涨养老金的待遇吗?
  3. 计算机科学与技术毕业生简历,计算机科学与技术专业应届毕业生简历范文
  4. C++基础语法-01-引用
  5. 就算不偷盗,也让你看看计算机里常用的有那些软件--常用软件序列号
  6. CSDN博客下载器的使用教程
  7. 编译原理第三章学习总结
  8. SAP 修改字段长度
  9. WPS2000的目录提取(转)
  10. SpringCloud教程合集
  11. 在破败中崛起,在寂灭中复苏。   沧海成尘,雷电枯竭,那一缕幽雾又一次临近大地,世间的枷锁被打开了,一个全新的世界就此揭开神秘的一角……
  12. linux查找多少天前的文件,linuxfind查找大于多少天的文件,并删除之
  13. 新服务器或者vps如何挂载硬盘(安装硬盘)教程
  14. 小白教程:快速在IDEA上创建包和类(java)
  15. Aligenie语音开发平台(天猫精灵)的对接记录
  16. 索引(从零开始)必须大于或等于零,且小于参数列表的大小。
  17. 保留小数位数spit函数
  18. 前端之refs焦点管理
  19. SIM卡座与SD卡座的生产标准化要求
  20. R语言中rattle安装,GTK+反复不成功的问题

热门文章

  1. python爬虫简单的添加代理进行访问
  2. Django框架深入了解_03(DRF之认证组件、权限组件、频率组件、token)
  3. Uipath 学习栏目基础教学:2Uipath变量介绍
  4. 电气期刊论文实现:基于遗传优化的非侵入式居民负荷分解方法(有代码)
  5. JavaScript实现radianToDegree弧度到度算法(附完整源码)
  6. OpenCASCADE:教程概述
  7. wxWidgets:wxListBox类用法
  8. DCMTK:读取多个图像的示例应用程序
  9. VTK:可视化之AnnotatedCubeActor
  10. VTK:PolyData之AttachAttributes