Elasticsearch Search APIs

By:授客 QQ:1033553122

1. 搜索 1

在单个索引的所有类型中搜索 1

在单个索引的指定类型中搜索 1

在多个指定的索引中搜索 1

在所有索引中搜索 1

2. URI搜索 2

3. 请求体搜索 4

1. query 5

2. from/size 5

3. sort 6

4. source filter 11

5. script field 13

6. doc field 17

7. post filter 18

8. search_after 22

 

测试环境:

Win elasticsearch-5.4.1

1. 搜索

在单个索引的所有类型中搜索

例.在customer索引中查找包含firstname字段,且值字段值包含单词brad的文档

GET /customer/_search?q=firstname:Brad

在单个索引的指定类型中搜索

例.在customer索引的external,sometype类型中查找包含firstname字段,且值字段值包含单词brad的文档

GET /customer/external,sometype/_search?q=firstname:Brad

在多个指定的索引中搜索

例.在customer,account索引中查找包含firstname字段,且值字段值包含单词brad的文档

GET /account,customer/sometype/_search?q=firstname:Brad

GET /account,customer/_search?q=firstname:Brad

注意:索引之间只能以逗号隔开,不能有空格,比如account, customer

在所有索引中搜索

例.在所有索引的sometype类型中查找包含firstname字段,且值字段值包含单词brad的文档

GET /_all/sometype/_search?q=firstname:Brad

例.在所有索引中查找包含firstname字段,且值字段值包含单词brad的文档

GET /_all/_search?q=firstname:Brad

或者

GET /_search?q=firstname:Brad

参考链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html

2. URI搜索

常用参数说明:

q: 要查询的字段值

例. 在customer索引external类型中查找字段值为16623的文档

GET /customer/external/_search?q=16623

_source:指定文档中hits包含的字段值

例. 在customer索引external类型中查找字段值为16623的文档,仅返回firstname,lastname,balance字段

GET /customer/external/_search?q=16623&_source=firstname,lastname,balance

注意:字段值之间只能以逗号分隔,且不能包含空格,比如firstname, lastname,

sort:用于排序文档,格式 fieldName,fieldName:asc 或fieldName:desc

其中,asc表示按fieldName字段值升序排序,同不带fieldName,相反desc表示降序排序,可以按多个字段排序,格式形如 fieldName1:asc,fieldName2:desc,的先按字段fieldName1的值升序排序,fieldName1值相同的话,再按fieldName2的值降序排序

例.查询customer索引external类型中的所有文档,按balance字段值升序排序。

GET /customer/external/_search?sort=balance:asc

例.查询customer索引external类型中的所有文档,按balance字段值升序排序,balance字段值相同则按account_number降序排序。

GET /customer/external/_search?sort=balance:asc,account_number:desc

from:指定需要返回记录的起始索引,默认为0,可以理解为mysql查询 limit子句的 offset

size:需要返回的记录数,默认为10

参考链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html

3. 请求体搜索

例. 查询/customer索引,external类型中包含firstname字段,且值为Braw的记录

POST /customer/external/_search?pretty

{

"query": {

"term": {

"firstname": "braw"

}

}

}

注意:PUT也可以替换为GET

注意:例中,如果把"firstname": "braw" 改成 "firstname": "Braw",查询查不到结果,估计默认设置的情况下,先把文档字段值转小写后进行的比较

返回结果部分截图

说明:默认情况下,查询结果不区分大小,但是字段名是区分大小写的。

常见参数:参考 URI搜索

参考链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html

1. query

参考链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-query.html

2. from/size

例.返回查询结果中,索引大于等于1的记录,总的返回一条记录

POST customer/external/_search?pretty

{

"query": {

"term": {

"firstname": "braw"

}

},

"from": 1,

"size": 1

}

注意:from + size不能大于index.max_result_window设置的值(默认1000)

参考链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html

3. sort

sort 对查询结果排序

例. 对查询结果排序,按account_number降序排序,account_number相同的情况下,按balance升序排序

POST customer/external/_search?pretty

{

"query": {

"match_all": {}

},

"sort": [

{

"account_number":{ "order": "desc"},

"balance":"asc"

}

]

}

说明:

1、desc:降序,asc:升序

如上,"account_number":{ "order": "desc"},也可以简单写成"account_number":"desc",

sort mode选项

mode选项用于字段值为数组列表、多个值组成的字段排序,可选值如下:

min

选择数组中的最小值,用于字段排序

max

选择数组中的最大值,用于字段排序

sum

使用数组中所有值总和,用于字段排序,仅限于字段值由数字组成的数组

avg

使用数组中所有值的均值,用于字段排序,仅限于字段值由数字组成的数组

median

使用数组中所有值的中位数,用于字段排序,仅限于字段值由数字组成的数组

按如下方式创建一些文档记录

PUT /product/fruit/4?pretty

{

"product":"orange",

"price":[12, 17, 22]

}

例子.按price字段的数组均值降序排序查询结果

POST /product/fruit/_search

{

"query": {

"match_all": {}

},

"sort": [

{

"price": {

"order": "desc",

"mode": "avg"

}

}

]

}

嵌套对象里的排序

嵌套对象映射

例.设置offer字段为嵌套对象(同时也会执行类型的创建操作)

PUT /product

{

"mappings": {

"myfruit": {

"properties": {

"offer": {

"type": "nested",

"properties": {

"price": {"type":"short"}

}

}

}

}

}

}

PUT /product/myfruit/1?pretty

{

"product": "orange",

"offer": [{

"price": [

12,

17,

22

]

}]

}

PUT /product/myfruit/2?pretty

{

"product": "apple",

"offer": [{

"price": [

14,

10,

9

]

}]

}

PUT /product/myfruit/3?pretty

{

"product": "apple",

"offer": [

{}

]

}

POST /product/myfruit/_search

{

"query": {

"match_all": {}

},

"sort": [

{

"offer.price": {

"order": "asc",

"mode": "avg",

"nested_path":"offer"

}

}

]

}

说明:

nested_path:指明在哪个嵌套对象上进行排序

missing参数

missing参数用于指定,文档缺乏指定字段时的处理方式,missing参数值可以设置为_last(默认值,即位于最下方)、 _first(位于最上方)、或者其它自定义值,该参数值将用于排序。

修改上述例中,文档3如下

PUT /product/myfruit/3?pretty

{

"product": "apple",

"offer": [

{}

]

}

POST /product/myfruit/_search

{

"query": {

"match_all": {}

},

"sort": [

{

"offer.price": {

"order": "asc",

"mode": "avg",

"missing":"_first",

"nested_path": "offer"

}

}

]

}

返回结果部分截图:

更多参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html

4. source filter

例.不返回查询结果中的字段

POST /customer/external/_search?

{

"query": {

"match_all": {}

},

"_source":false

}

返回结果:

例.仅返回查询结果中指定的字段,firstname,account_number

POST /customer/external/_search?

{

"query": {

"match_all": {}

},

"_source": [

"firstname",

"account_number"

]

}

返回结果部分截图

使用通配符

例.仅返回查询结果中以em,或者字母a开头字段

POST /customer/external/_search?

{

"query": {

"match_all": {}

},

"_source": [

"a*",

"em*"

]

}

返回结果部分截图

includes和excludes

例.仅返回查询结果中字段名以字符a开头,但不以em开头的字段

POST /customer/external/_search?

{

"query": {

"match_all": {}

},

"_source": {

"includes": [

"a*"

],

"excludes": [

"em*"

]

}

}

参考链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html

5. script field

返回脚本计算值(基于不同字段)

例.

POST /customer/external/_search?

{

"query": {

"match_all": {}

},

"script_fields": {

"test1": {

"script": {

"inline": "doc['account_number'].value * 2"

}

},

"test2": {

"script": {

"inline": "doc['account_number'].value * params.factor",

"params": {

"factor": 3

}

}

}

}

}

注意:这里,account_number为文档中已存在的字段名

返回结果

例.

POST /customer/external/_search?

{

"script_fields": {

"test1": {

"script": {

"inline": "params.factor * params.factor",

"params": {

"factor": 3

}

}

}

}

}

返回结果

例.访问 _source,返回firstname的值

POST /customer/external/_search?

{

"query": {

"match_all": {}

},

"script_fields": {

"test1": {

"script": "params['_source']['firstname']"

}

}

}

返回结果部分截图

注意:使用doc['my_field_name'].value比使用arams['_source']['my_field_name']更快更效率,推荐使用

参考链接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html

6. doc field

例.

POST /customer/external/_search?

{

"query": {

"match_all": {}

},

"docvalue_fields" : ["account_number", "test2"]

}

返回结果部分截图

7. post filter

filter在aggregation完成后才被执行。

PUT /shirts

{

"mappings": {

"item": {

"properties": {

"brand": { "type": "keyword"},

"color": { "type": "keyword"},

"model": { "type": "keyword"}

}

}

}

}

PUT /shirts/item/1?refresh

{

"brand": "gucci",

"color": "red",

"model": "slim"

}

例.仅返回搜索结果中包含color为red,brand为gucci的文档记录

POST /shirts/_search

{

"query": {

"bool": {

"filter": [

{

"term": {

"color": "red"

}

},

{

"term": {

"brand": "gucci"

}

}

]

}

}

}

例.仅返回搜索结果中包含color为red,brand为gucci的shirt,按model分组,按分组统计数降序排序

POST /shirts/_search

{

"query": {

"bool": {

"filter": [

{

"term": {

"color": "red"

}

},

{

"term": {

"brand": "gucci"

}

}

]

}

},

"aggs": {

"models": {

"terms": {

"field": "model"

}

}

}

}

返回结果部分截图

例.

例.仅搜索brand值为gucci的shirt,按color分组,降序展示每种color的shirt数量,同时,针对color为red的shirt商品,按model分组统计,降序展示每种model的数量

POST /shirts/_search

{

"query": {

"bool": {

"filter": {

"term": {

"brand": "gucci"

}

}

}

},

"aggs": {

"group_by_colors": {

"terms": {

"field": "color"

}

},

"color_red": {

"filter": {

"term": {

"color": "red"

}

},

"aggs": {

"group_by_models": {

"terms": {

"field": "model"

}

}

}

}

},

   "post_filter": {

"term": {

"color": "red"

}

}

}

说明:   "post_filter",作用于最后,不展示color不为red的shirt记录

返回结果部分截图

8. search_after

例.如下,每页只显示5条记录,按leve_vale降序排序,如果leve_vale相同则按_uid降序排序

POST /fenxi/fenxishuj/_search?

{

"query": {

"match_all": {}

},

"sort": [

{

"leve_vale":"desc",

"_uid": "desc"

}

],

"size":5

}

返回结果部分截图

这时,在不改变页size值的情况下,我们想查看下一页的记录,咋办?

方案:把sort中的参数值,按出现顺序,依次传递给search_after

POST /fenxi/fenxishuj/_search?

{

"query": {

"match_all": {}

},

"search_after":[31,"fenxishuj#9"],

"sort": [

{"leve_vale":"desc",

"_uid": "desc"

}

],

"size":5

}

注意:

1、sort中的参数值要和search_after一一对应(数量&顺序的对应)。

2、使用了search_after的情况下,如果要使用from参数,参数值只能为0 、-1

参考资料:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-after.html

更多资料参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html

Elasticsearch Search APIs的更多相关文章

  1. org.elasticsearch.search.sort.SortBuilder使用

    org.elasticsearch.search.sort.SortBuilder是一个抽象类,有4个子类 org.elasticsearch.search.sort.FieldSortBuilder ...

  2. ElasticSearch Search API 简介

    REST request URI curl 'localhost:9200/bank/_search?q=*&pretty' 1. localhost:9200/bank/_search,以 ...

  3. Elasticsearch Search API

    当执行一个搜索时,它将这个搜索请求广播给所有的索引分片.可以通过提供路由参数来控制要搜索哪些分片.例如,当检索tweets这个索引时,路由参数可以设置为用户名: curl -X POST " ...

  4. Elasticsearch cat Apis

    1._cat列入所有有效命令 GET /_cat 返回:有个猫...所以不难想象为啥是cat api =^.^= /_cat/allocation /_cat/shards /_cat/shards/ ...

  5. Elasticsearch——Search的基本介绍

    Elasticsearch最常用的方法莫过于查询了.Es支持以URI请求参数或者请求体的方式进行查询. 查询范例 Elasticsearch支持对多索引以及多类型进行查询. 比如,下面对某个特定索引的 ...

  6. es第三篇:Search APIs

    大多数search API都是可以操作多个索引的,除了explain API. 当执行一个search API时,可以指定routing参数,去搜索特定的主分片及其副本分片.routing参数值可以是 ...

  7. elasticsearch中常用的API

    elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...

  8. Apache Solr vs Elasticsearch

    http://solr-vs-elasticsearch.com/ Apache Solr vs Elasticsearch The Feature Smackdown API Feature Sol ...

  9. Elasticsearch 6.4基本操作 - Java版

    1. Elasticsearch Java API有四类client连接方式 TransportClient RestClient Jest Spring Data Elasticsearch 其中T ...

随机推荐

  1. php 中构造函数和析构函数

    构造函数: 在对象实例化时被调用,一个类中只能有一个构造函数,在类中起初始化的作用. 析构函数: 在对象结束时被自动调用. 话不多说,用一段代码来说明两者的区别: <?php //定义一个类 c ...

  2. 微信小程序onLaunch异步,首页onLoad先执行?

    本来按照事件顺序,小程序初始化时触发App里的onLaunch,后面再执行页面Page里的onLoad,但是在onLaunch里请求获取是否有权限,等待返回值的时候Page里的onLoad事件就已经执 ...

  3. Unity教程之-UGUI美术字体的制作与使用

    文章转载自:http://www.unity.5helpyou.com/3211.html 游戏制作中,经常需要使用各种花哨的文字或者数字,而字体库往往不能达到我们需要的效果,因此需要一种用图片替代文 ...

  4. SaltStack安装配置详解

    一.简介 Saltstack 比 Puppet 出来晚几年,是基于Python 开发的,也是基于 C/S 架构,服务端 master 和客户端 minions :Saltstack 和 Puppet ...

  5. Http协议中get和post的区别

    get(默认值)是通过URL传递表单值,数据追加在action属性后面. post传递的表单值是隐藏到http报文体中,url中看不到. get是通过url传递表单值,post通过url看不到表单域的 ...

  6. maven创建一个简单的web项目

    1.确认maven插件和配置在eclipse中已经完成 如果没完成,可参考这篇博客:http://www.cnblogs.com/mmzs/p/8191979.html 2.在eclipse中用mav ...

  7. NLP入门(四)命名实体识别(NER)

      本文将会简单介绍自然语言处理(NLP)中的命名实体识别(NER).   命名实体识别(Named Entity Recognition,简称NER)是信息提取.问答系统.句法分析.机器翻译等应用领 ...

  8. 第一册:lesson forty five。

    原文: The boss's letter. A:Can you come here a minute please,Bob? B:Yes,sir. A:Where is C? B:She is ne ...

  9. 第一册:lesson nineteen。

    原文:tired and thirsty. A:What's the matter,children? B:We are tired and thirsty. A:Sit down,here. Are ...

  10. 用Vue.js搭建一个小说阅读网站

    目录 1.简介 2.如何使用vue.js 3.部署api服务器 4.vue.js路由配置 5.实现页面加载数据 6.测试vue项目 7.在正式环境部署 8.Vue前端代码下载 1.简介 这是一个使用v ...