关于这篇文档,还是学习网上的,纯属复制,感觉总结的不错。

  方便理解多种搜索方式。

一、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的更多相关文章

  1. jsonp跨越请求百度搜索api 实现下拉列表提示

    题目来源: 最近在做百度IFE前端技术学院的题,然后有一题就是模拟百度搜索智能提示.题目是开源的,稍后给出地址. 因为博主没学过后端啊,欲哭无泪,所以不能实现后端模糊搜索,那如果前端ajax纯粹请求一 ...

  2. 百度音乐搜索API介绍

    百度音乐搜索API的请求地址如下: [html] view plaincopy http://box.zhangmen.baidu.com/x?op=12&count=1&title= ...

  3. ElasticSearch查询 第一篇:搜索API

    <ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...

  4. elasticsearch系列四:搜索详解(搜索API、Query DSL)

    一.搜索API 1. 搜索API 端点地址 从索引tweet里面搜索字段user为kimchy的记录 GET /twitter/_search?q=user:kimchy 从索引tweet,user里 ...

  5. Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)

    Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery) 体验 冒号搜索 1. 获取谷歌搜索api 谷歌搜索api教程 2. 后台调用 程序入口 main.go // ...

  6. 使用Javascript从Google Places搜索api获取纬度和经度

    如何使用谷歌地图搜索框api从搜索到的位置获取经度和纬度. 我使用与谷歌演示相同的代码 – https://developers.google.com/maps/documentation/javas ...

  7. 一个扩展搜索API的优化过程

    概述 API 是一个服务的门面,就像衣装是人的形象一样. 优雅的 API 设计,能让业务方使用起来倍儿爽,提升开发效率,降低维护成本:糟糕的 API 设计,则让业务方遭心,陷入混沌. 本文将展示一个扩 ...

  8. Google的搜索API的Delphi封装

    这个东西实现了已经有一段时间了,那个时候谷歌还没有退出中国内地呢!而现在呢,谷歌都退了有一些日子了!紧以此纪念一番! 话说谷歌API,我相信很多人应该都知道!不晓得在实际应用中,用的人多不多(我说的不 ...

  9. Elasticsearch 搜索API

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...

随机推荐

  1. systemctl 常用操作

    以samba为列 systemctl start smb       #启动smb服务 systemctl restart smb   #重启smb服务 systemctl stop smb      ...

  2. Mysql开启GTID后遇到错误跳过方法

    处理方法如下: 一:跳过错误 这个GTID_NEXT的4,是在master 上正常执行的最大id + 1,即Executed_Gtid_Set里面master uuid执行过的最大值 3+ 1 STO ...

  3. 隐藏按钮button

    <td> <input id="del" type="button" value="删除" onclick="u ...

  4. spark HMM

    Scala实现的: https://github.com/skrusche63/spark-intent/tree/master/src/main/scala/de/kp/scala/hmm http ...

  5. Spring-02 -Spring 创建对象的三种方式 :1.通过构造方法创建/2.实例工厂/3.静态工厂

    通过构造方法创建  1.1 无参构造创建:默认情况. 1.2 有参构造创建:需要明确配置 1.2.1 需要在类中提供有参构造方法 1.2.2 在 applicationContext.xml 中设置调 ...

  6. Django之路——1 Django的简介

    今天我们来学习django,在学习Django之前我们先来了解一下django和web开发中的http协议 1.mvc模型和mtv模型 既然学习Django,那么我们一定要知道web开发中的mvc模型 ...

  7. TJOI2016 字符串

    字符串 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为n的字符串s,和m个问题.佳媛姐姐必须正确回答这m个问题,才能打开箱子拿到礼物,升职 ...

  8. ping命令传递信息

    IP: # 适用于 eth0 inet addr: IP的情况 ping `ifconfig eth0|grep 'inet '|awk '{ print $2}'|awk -F: '{print $ ...

  9. 【CSP-S 2019】【洛谷P5666】树的重心【主席树】【树状数组】【dfs】

    题目: 题目链接:https://www.luogu.com.cn/problem/P5666 小简单正在学习离散数学,今天的内容是图论基础,在课上他做了如下两条笔记: 一个大小为 \(n\) 的树由 ...

  10. Acwing P274 移动服务 题解

    每日一题 day21 打卡 Analysis DP的状态为已经完成的请求数量,通过指派一位服务员可以把”完成i - 1个请求的状态”转移到”完成i个请求的状态”那么我们可以知道转移从dp[i - 1] ...