Elasticsearch Search APIs
Elasticsearch Search APIs
By:授客 QQ:1033553122
测试环境:
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的更多相关文章
- org.elasticsearch.search.sort.SortBuilder使用
org.elasticsearch.search.sort.SortBuilder是一个抽象类,有4个子类 org.elasticsearch.search.sort.FieldSortBuilder ...
- ElasticSearch Search API 简介
REST request URI curl 'localhost:9200/bank/_search?q=*&pretty' 1. localhost:9200/bank/_search,以 ...
- Elasticsearch Search API
当执行一个搜索时,它将这个搜索请求广播给所有的索引分片.可以通过提供路由参数来控制要搜索哪些分片.例如,当检索tweets这个索引时,路由参数可以设置为用户名: curl -X POST " ...
- Elasticsearch cat Apis
1._cat列入所有有效命令 GET /_cat 返回:有个猫...所以不难想象为啥是cat api =^.^= /_cat/allocation /_cat/shards /_cat/shards/ ...
- Elasticsearch——Search的基本介绍
Elasticsearch最常用的方法莫过于查询了.Es支持以URI请求参数或者请求体的方式进行查询. 查询范例 Elasticsearch支持对多索引以及多类型进行查询. 比如,下面对某个特定索引的 ...
- es第三篇:Search APIs
大多数search API都是可以操作多个索引的,除了explain API. 当执行一个search API时,可以指定routing参数,去搜索特定的主分片及其副本分片.routing参数值可以是 ...
- elasticsearch中常用的API
elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...
- Apache Solr vs Elasticsearch
http://solr-vs-elasticsearch.com/ Apache Solr vs Elasticsearch The Feature Smackdown API Feature Sol ...
- Elasticsearch 6.4基本操作 - Java版
1. Elasticsearch Java API有四类client连接方式 TransportClient RestClient Jest Spring Data Elasticsearch 其中T ...
随机推荐
- Spring Cloud Alibaba Sentinel 整合 Feign 的设计实现
作者 | Spring Cloud Alibaba 高级开发工程师洛夜 来自公众号阿里巴巴中间件投稿 前段时间 Hystrix 宣布不再维护之后(Hystrix 停止开发...Spring Cloud ...
- python高级-深浅拷贝(16)
一.浅拷贝 浅拷贝是对一个对象的顶层拷贝,通俗地讲就是:拷贝了引用,并没有拷贝内容. a = [1,2,3] print(id(a)) b=a print(b) print(id(b)) a.appe ...
- odoo开发笔记--form视图自定义
form视图自定义的两种场景: 1. 自己重写form视图样式文件 2. form视图,嵌入第三方的系统.
- 【翻译】浏览器渲染Rendering那些事:repaint、reflow/relayout、restyle
原文链接:http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/ 有没有被标题中的5个“R”吓到?今天,我们来讨论一下浏览器的渲 ...
- vue 国际化i18n 多语言切换
安装 npm install vue-i18n 新建一个文件夹 i18n ,内新建 en.js zh.js index.js 三个文件 准备翻译信息 en.js export default { ho ...
- MQ(2)---JMS
JMS 一.理解JMS 1.什么是JMS? JMS即Java消息服务(Java Message Service)应用程序接口,API是一个消息服务的标准或者说是规范,允许应用程序组 ...
- Activity的生命周期函数
前言: 上一篇文章写了关于Activity生命周期和生命周期状态的介绍,这一篇文章给大家聊聊Activity生命周期函数. 主Activity: 应用程序的入口一般都是桌面应用程序图标,用户点击应用图 ...
- jvm详情——2、Java对象在jvm中的大小
Java对象的大小 基本数据的类型的大小是固定的,这里就不多说了.对于非基本类型的Java对象,其大小就值得商榷.在Java中,一个空Object对象的大小是8byte,这个大小只是保存堆中一个没有任 ...
- Java 并发编程-再谈 AbstractQueuedSynchronizer 3 :基于 AbstractQueuedSynchronizer 的并发类实现
公平模式ReentrantLock实现原理 前面的文章研究了AbstractQueuedSynchronizer的独占锁和共享锁,有了前两篇文章的基础,就可以乘胜追击,看一下基于AbstractQue ...
- Webpack中publicPath设置
webpack中的path是当我们build的时候,输出项目打包文件的位置. webpack中的publicPath是我们打算放到web服务器下的目录,如果我们要放到网站的根目录下,那么就无需设置.如 ...