007 搜索API
关于这篇文档,还是学习网上的,纯属复制,感觉总结的不错。
方便理解多种搜索方式。
一、Query String Search(‘Query String’方式的搜索)
1.搜索全部商品
GET /shop_index/productInfo/_search
结果:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "2",
"_score": 1,
"_source": {
"test": "test"
}
},
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "zyWpRGkB8mgaHjxk0Hfo",
"_score": 1,
"_source": {
"name": "HuaWei P20",
"desc": "Expen but easy to use",
"price": 5300,
"producer": "HuaWei Producer",
"tags": [
"Expen",
"Fast"
]
}
},
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "1",
"_score": 1,
"_source": {
"name": "HuaWei Mate8",
"desc": "Cheap and easy to use",
"price": 2500,
"producer": "HuaWei Producer",
"tags": [
"Cheap",
"Fast"
]
}
}
]
}
}
说明:
took:耗费了几毫秒
timed_out:是否超时,这里是没有
_shards:数据被拆到了5个分片上,搜索时使用了5个分片,5个分片都成功地返回了数据,失败了0个,跳过了0个
hits.total:查询结果的数量,3个document
max_score:就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也越高
hits.hits:包含了匹配搜索的document的详细数据
2.搜索商品名称中包含HuaWei的商品,而且按照售价降序排序:
下面这种方法也是"Query String Search"的由来,因为search参数都是以http请求的query string来附带的
GET /shop_index/productInfo/_search?q=name:HuaWei&sort=price:desc
结果:
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "zyWpRGkB8mgaHjxk0Hfo",
"_score": null,
"_source": {
"name": "HuaWei P20",
"desc": "Expen but easy to use",
"price": 5300,
"producer": "HuaWei Producer",
"tags": [
"Expen",
"Fast"
]
},
"sort": [
5300
]
},
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "1",
"_score": null,
"_source": {
"name": "HuaWei Mate8",
"desc": "Cheap and easy to use",
"price": 2500,
"producer": "HuaWei Producer",
"tags": [
"Cheap",
"Fast"
]
},
"sort": [
2500
]
}
]
}
}
二、Query DSL(DSL: Domain Specified Language,特定领域的语言)
1.说明
这种方法是通过一个json格式的http request body请求体作为条件,可以完成多种复杂的查询需求,比query string的功能更加强大
2.搜索所有商品
GET /shop_index/productInfo/_search
{
"query": {
"match_all": {}
}
}
3.查询名称中包含HuaWei的商品,并且按照价格降序排列
GET /shop_index/productInfo/_search
{
"query": {
"match": {
"name": "HuaWei"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
4.分页查询第二页,每页1条记录
GET /shop_index/productInfo/_search
{
"query": {
"match_all": {}
},
"from": 1,
"size": 1
}
注意:
(1)在实际项目中,如果有条件查询之后再需要分页,不需要单独查询总条数,ES会返回满足条件的总条数,可以直接使用;
(2)ES的分页默认from是从0开始的;
5.只查询特定字段,比如:name,desc和price字段,其他字段不需要返回
GET /shop_index/productInfo/_search
{
"query": {
"match": {
"name": "HuaWei"
}
},
"_source": ["name","desc","price"]
}
结果:
{
"took": 27,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "zyWpRGkB8mgaHjxk0Hfo",
"_score": 0.2876821,
"_source": {
"price": 5300,
"name": "HuaWei P20",
"desc": "Expen but easy to use"
}
},
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "1",
"_score": 0.2876821,
"_source": {
"price": 2500,
"name": "HuaWei Mate8",
"desc": "Cheap and easy to use"
}
}
]
}
}
三.Query Filter(对查询结果进行过滤)
1.查询名称中包含HuaWei,并且价格大于4000的商品记录
GET /shop_index/productInfo/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "HuaWei"
}
}
],
"filter": {
"range": {
"price": {
"gt": 4000
}
}
}
}
}
}
结果:
{
"took": 195,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "zyWpRGkB8mgaHjxk0Hfo",
"_score": 0.2876821,
"_source": {
"name": "HuaWei P20",
"desc": "Expen but easy to use",
"price": 5300,
"producer": "HuaWei Producer",
"tags": [
"Expen",
"Fast"
]
}
}
]
}
}
四、全文索引(Full-Text Search)
1.搜索生产厂商字段中包含"HuaWei MateProducer"的商品记录
GET /shop_index/productInfo/_search
{
"query": {
"match": {
"producer": "HuaWei MateProducer"
}
}
}
结果:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.5753642,
"hits": [
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "SiUBRWkB8mgaHjxkJHyS",
"_score": 0.5753642,
"_source": {
"name": "HuaWei Mate10",
"desc": "Cheap and Beauti",
"price": 2300,
"producer": "HuaWei MateProducer",
"tags": [
"Cheap",
"Beauti"
]
}
},
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "HuaWei Mate8",
"desc": "Cheap and easy to use",
"price": 2500,
"producer": "HuaWei Producer",
"tags": [
"Cheap",
"Fast"
]
}
},
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "zyWpRGkB8mgaHjxk0Hfo",
"_score": 0.18232156,
"_source": {
"name": "HuaWei P20",
"desc": "Expen but easy to use",
"price": 5300,
"producer": "HuaWei Producer",
"tags": [
"Expen",
"Fast"
]
}
},
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "CSX8RGkB8mgaHjxkV3w1",
"_score": 0.18232156,
"_source": {
"name": "HuaWei nova 4e",
"desc": "cheap and look nice",
"price": 1999,
"producer": "HuaWei Producer",
"tags": [
"Cheap",
"Nice"
]
}
}
]
}
}
从以上结果中可以看到:
id为"SiUBRWkB8mgaHjxkJHyS"的记录score分数最高,表示匹配度最高;
原因:
producer分完词之后包括的词语有:
(1).HuaWei:
匹配到改词的记录ID:'SiUBRWkB8mgaHjxkJHyS','1','CSX8RGkB8mgaHjxkV3w1','zyWpRGkB8mgaHjxk0Hfo'
(2).MateProducer:
匹配到该词的记录ID:'SiUBRWkB8mgaHjxkJHyS'
由于"HuaWei MateProducer"两次匹配到ID为'SiUBRWkB8mgaHjxkJHyS'的记录,所以该记录的score分数最高。
五、Phrase Search(短语搜索)
短语索引和全文索引的区别:
(1)全文匹配:将要搜索的内容分词,然后挨个单词去倒排索引中匹配,只要匹配到任意一个单词,就算是匹配到记录;
(2)短语索引:输入的搜索串,必须在指定的字段内容中,完全包含一模一样的,才可以算匹配,才能作为结果返回;
1.搜索name中包含"HuaWei MateProducer"短语的商品信息
GET /shop_index/productInfo/_search
{
"query": {
"match_phrase": {
"producer": "HuaWei MateProducer"
}
}
}
六、Highlight Search(搜索高亮显示)
高亮搜索指的是搜索的结果中,将某些特别需要强调的词使用特定的样式展示出来。
1.搜索商品名称中包含"Xiao'Mi"的商品,并将搜索的关键词高亮显示
GET /shop_index/productInfo/_search
{
"query": {
"match": {
"name": "Xiao'Mi"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
结果:
{
"took": 348,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "shop_index",
"_type": "productInfo",
"_id": "HiX9RGkB8mgaHjxk4nxC",
"_score": 0.2876821,
"_source": {
"name": "Xiao'Mi 9",
"desc": "Expen but nice and Beauti",
"price": 3500,
"producer": "XiaoMi Producer",
"tags": [
"Expen",
"Beauti"
]
},
"highlight": {
"name": [
"<em>Xiao'Mi</em> 9"
]
}
}
]
}
}
007 搜索API的更多相关文章
- jsonp跨越请求百度搜索api 实现下拉列表提示
题目来源: 最近在做百度IFE前端技术学院的题,然后有一题就是模拟百度搜索智能提示.题目是开源的,稍后给出地址. 因为博主没学过后端啊,欲哭无泪,所以不能实现后端模糊搜索,那如果前端ajax纯粹请求一 ...
- 百度音乐搜索API介绍
百度音乐搜索API的请求地址如下: [html] view plaincopy http://box.zhangmen.baidu.com/x?op=12&count=1&title= ...
- ElasticSearch查询 第一篇:搜索API
<ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...
- elasticsearch系列四:搜索详解(搜索API、Query DSL)
一.搜索API 1. 搜索API 端点地址 从索引tweet里面搜索字段user为kimchy的记录 GET /twitter/_search?q=user:kimchy 从索引tweet,user里 ...
- Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)
Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery) 体验 冒号搜索 1. 获取谷歌搜索api 谷歌搜索api教程 2. 后台调用 程序入口 main.go // ...
- 使用Javascript从Google Places搜索api获取纬度和经度
如何使用谷歌地图搜索框api从搜索到的位置获取经度和纬度. 我使用与谷歌演示相同的代码 – https://developers.google.com/maps/documentation/javas ...
- 一个扩展搜索API的优化过程
概述 API 是一个服务的门面,就像衣装是人的形象一样. 优雅的 API 设计,能让业务方使用起来倍儿爽,提升开发效率,降低维护成本:糟糕的 API 设计,则让业务方遭心,陷入混沌. 本文将展示一个扩 ...
- Google的搜索API的Delphi封装
这个东西实现了已经有一段时间了,那个时候谷歌还没有退出中国内地呢!而现在呢,谷歌都退了有一些日子了!紧以此纪念一番! 话说谷歌API,我相信很多人应该都知道!不晓得在实际应用中,用的人多不多(我说的不 ...
- Elasticsearch 搜索API
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
随机推荐
- django框架介绍安装-自写框架
原文链接:https://www.cnblogs.com/maple-shaw/p/8862330.html Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户 ...
- git---怎样将分支上的一个单文件合并到主分支上(master)
一.首先切换到主分支 注意将分支上的数据全部提交 以免造成数据冲突或丢失 git checkeout master 二.选择要合并的文件 git checkout --patch 分支名称 要合并 ...
- 均分纸牌(Noip2002)
1320:[例6.2]均分纸牌(Noip2002) 时间限制: 1000 ms 内存限制: 65536 KB提交数: 3537 通过数: 1839 [题目描述] 有n堆纸牌,编 ...
- django 时间格式(全局修改,不用过滤器)
百度了一圈,很没创意的用过滤器,前端每次显示时间表格都要用过滤器,这种挺烦的.隐约记得以前见过没有用过滤器的.换google https://stackoverflow.com/questions/5 ...
- LA 3704细胞自动机——循环矩阵&&矩阵快速幂
题目 一个细胞自动机包含 $n$ 个格子,每个格子的取值为 $0 \sim m-1$.给定距离 $d$,则每次操作是将每个格子的值变为到它的距离不超过 $d$ 的所有格子的在操作之前的值的和除以 $m ...
- MySQL常用五大引擎的区别
MyISAM: 如果你有一个 MyISAM 数据表包含着 FULLTEXT 或 SPATIAL 索引,你将不能把它转换为使用 另一种引擎,因为只有 MyISAM 支持这两种索引. BLOB: 如果你有 ...
- YAML_08 handlers触发器
ansible]# vim adhttp.yml --- - hosts: cache remote_user: root tasks: - copy: src: /r ...
- bzoj 1072: [SCOI2007]排列perm 状压dp
code: #include <bits/stdc++.h> #define N 1005 using namespace std; void setIO(string s) { stri ...
- learning AWT Jrame
import java.awt.*; public class FrameTest { public static void main(String[] args) { var f = new Fra ...
- [hdu contest 2019-07-29] Azshara's deep sea 计算几何 动态规划 区间dp 凸包 graham扫描法
今天hdu的比赛的第一题,凸包+区间dp. 给出n个点m个圆,n<400,m<100,要求找出凸包然后给凸包上的点连线,连线的两个点不能(在凸包上)相邻,连线不能与圆相交或相切,连线不能相 ...