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

  方便理解多种搜索方式。

一、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. SVN提交错误及使用技巧

    错误1: Some of selected resources were not added to version control. Some of selected resources were n ...

  2. Bash基础——工作管理(Job control)

    注:1.这里说的Bash不单纯的指Bash,泛指shell 2.这里的后台指的是Bash下面避免任务(Jobs)被Ctrl+C中断的一种场景,与我们说的deamon那种后台工作的进程不是一个概念,注意 ...

  3. Linux磁盘管理——BIOS和UEFI

    参考:BIOS and UEFI - CompTIA A+ 220-901 - 1.1 BIOS and UEFI As Fast As Possible 严格上来说BIOS和UEFI除了在搜索boo ...

  4. helm笔记

    一.注意事项 1.values.yaml   中可以使用'#'号注释行,而/templates 下的文件不能用#号,如果要注释可以使用 {{/*  context  */}} 2.{{-    #忽略 ...

  5. cal of easter egg

    在Linux中用cal命令查看1752年9月,发现3到13日全都消失了……

  6. Andrew Ng机器学习 五:Regularized Linear Regression and Bias v.s. Variance

    背景:实现一个线性回归模型,根据这个模型去预测一个水库的水位变化而流出的水量. 加载数据集ex5.data1后,数据集分为三部分: 1,训练集(training set)X与y: 2,交叉验证集(cr ...

  7. Luogu P1339 热浪Heat Wave

    Luogu P1339 热浪Heat Wave 裸·单源最短路. 但是! 有以下坑点: 算过复杂度发现Floyd跑不过去就不要用了. 如果建边是双向边,边的数组大小要开两倍! 考场上如果再把初始化的$ ...

  8. Vue移动组件库Mint UI的安装与使用

    一.什么是 Mint UI 1.Mint UI 包含丰富的 CSS 和 JS 组件,可以提升移动端开发效率 2.Mint UI 按需加载组件 3.Mint UI 轻量化 二.Mint UI 的安装 1 ...

  9. 从底层实现剖析Kotlin协变与逆变的原理

    继续还是探究协变与逆变,在正式开始之前,先来对Kotlin和Java的协变与逆变进行一个对比: 1.Kotlin是声明处协变:而在Java中是在使用处协变: 如何理解,我们先来回顾一下在Java使用协 ...

  10. redis cluster集群的原理

    redis集群的概述: 在以前,如果前几年的时候,一般来说,redis如果要搞几个节点,每个节点存储一部分的数据,得借助一些中间件来实现,比如说有codis,或者twemproxy,都有.有一些red ...