对多个indices进行操作

es中大多resetapi支持请求多个index, 例如”test1,test2,test3”,index也可以使用通配符, 例如”test*“, 还可以使用+,-来包含或移除某个或某类index, 例如”test*,-test1”
支持设置多个的api的请求字符串可设置以下参数:

  • ignore_unavailable: 是否忽略单个index是否可用(不存在或关闭), true表示忽略, false表示不忽略, 默认为false, 例如查询已经关闭的index:

输入: GET /test1/user,account/_search?ignore_unavailable=false
输出:

1
2
3
4
{
"error": "IndexClosedException[[test1] closed]",
"status": 403
}

输入: GET /test1/user,account/_search?ignore_unavailable=false
输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 0,
"successful": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": 0,
"hits": []
}
}
  • allow_no_indices: 是否忽略通配符匹配不到index(不存在或关闭)的情况, true表示允许, false表示不允许,默认为true, 例如查询已经关闭的index:

输入: GET /test*/_search?allow_no_indices=false
输出:

1
2
3
4
{
"error": "IndexMissingException[[test*] missing]",
"status": 404
}

输入: GET /test*/_search?allow_no_indices=true

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 0,
"successful": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": 0,
"hits": []
}
}
  • expand_wildcards: 设置是否扩展通配符到closed的index中,open表示只在匹配并为open的index中查询,closed表示在匹配的所有的index中查询, 默认为closed, 例如查询已经关闭的index
    输入: GEt /test*/_search?expand_wildcards=closed
    输出:

    1
    2
    3
    4
    {
    "error": "IndexClosedException[[test1] closed]",
    "status": 403
    }

公共参数

  • format: 表示返回数据的格式, 可选值为yaml和json两种, 例如:
    输入: GET /test1/user/_search?format=yaml
    输出:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    ---
    took: 23
    timed_out: false
    _shards:
    total: 5
    successful: 5
    failed: 0
    hits:
    total: 1
    max_score: 1.0
    hits:
    - _index: "test1"
    _type: "user"
    _id: "1"
    _score: 1.0
    _source:
    name: "silence"
  • pretty: 表示在已json格式返回数据时是否以可视化的格式返回, false或未在设置表示不格式化, 否则格式化

  • human: 表示是否对返回结果进行格式化处理,比如3600(s)显示1h

  • 查询结果过滤
    主要使用filter_path参数进行设置

1.在返回结果中我们只关注took, hits.total, hits.hits._id, hits._source, 则我们可以发起如此请求:
输入:GET /test1/user/_search?filter_path=took,hits.total,hits.hits._id,hits.hits._source
输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"took": 1,
"hits": {
"total": 1,
"hits": [
{
"_id": "1",
"_source": {
"name": "silence"
}
}
]
}
}

2.也可以使用统配符进行设置
输入: GET /_nodes/stats?filter_path=nodes.*.*ost*,nodes.*.os.*u
输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"nodes": {
"9jfW4VeWRta-Uq7Cq7bK34": {
"host": "silence",
"os": {
"cpu": {
"sys": 1,
"user": 1,
"idle": 96,
"usage": 2,
"stolen": 0
}
}
}
}
}

3.若层级较多时可使用**进行简化
输入: GET /_nodes/stats?filter_path=nodes.**.*sys*
输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"nodes": {
"9jfW4VeWRta-Uq7Cq7bK34": {
"os": {
"cpu": {
"sys": 2
}
},
"process": {
"cpu": {
"sys_in_millis": 139106
}
}
}
}
}

4.若只需要_source中的某些值,则可以将filter_path和_source参数共同使用
输入: GET /test1/account/_search?filter_path=hits.hits._source&_source=firstname,lastname,gender&size=2
输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"hits": {
"hits": [
{
"_source": {
"firstname": "Rodriquez",
"gender": "F",
"lastname": "Flores"
}
},
{
"_source": {
"firstname": "Opal",
"gender": "M",
"lastname": "Meadows"
}
}
]
}
}

5.flat_settings用于设置在查询setting时,setting中的key格式, 默认为false:
输入: GET /test1/_settings?flat_settings=true
输出:

1
2
3
4
5
6
7
8
9
10
11
{
"test1": {
"settings": {
"index.creation_date": "1442230557598",
"index.uuid": "70bg061IRdKUdDNvgkUBoQ",
"index.version.created": "1060099",
"index.number_of_replicas": "1",
"index.number_of_shards": "5"
}
}
}

输入: GET /test1/_settings?flat_settings=false
输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"test1": {
"settings": {
"index": {
"creation_date": "1442230557598",
"number_of_shards": "5",
"uuid": "70bg061IRdKUdDNvgkUBoQ",
"version": {
"created": "1060099"
},
"number_of_replicas": "1"
}
}
}
}
  • 请求参数格式
    1.boolean: 在es中将”0”, 0, false, “false”, “off”识别为false,其他均按ture处理
    2.number
    3.time: 可以提交一个以毫秒时间的整数或者以日期标识结尾的字符串,例如”2d”表示2天,支持的格式有: y(year),M(month),w(week),d(day),h(hour),m(minute),s(second)
    4.距离: 可以提交一个以米为单位的证书或者以距离表示结尾的字符串,例如”2km”表示2千米,支持的格式有: mi/miles(mile英里), yd/yards(yard码), ft/feet(feet尺), in/inch(inch英寸), km/kilometers(kilometer千米), m/meters(meter米), cm/centimeters(centimeter厘米), mm/millimeters(millimeter毫米), NM/nmi/nauticalmiles(Nautical mile纳米)
    5.模糊类型:
    a.数字,时间, IP:类似于range -fuzzines<=value<=+fuzzines
    b.字符串: 计算编辑距离

  • 返回结果中key的格式为驼峰还是下划线分割, 通过case设置为camelCase则返回驼峰格式,否则为下划线分割形式

  • jsonp: 可以用jsonp回调的方式调用es api, 需要通过callback设置回调函数名称,并且需要在elasticsearch.yml中配置http.jsonp.enable: true来启用jsonp格式

url访问控制

可以通过代理方式进行es的url访问控制,但是对于multi-search,multi-get和bulk等在请求参数中设置不同的index的情况很难解决.
为防止通过请求体设置index的情况,需要在elasticsearch.yml中设置rest.action.multi.allow_explicit_index:false, 此时es不允许在request body中设置index

如在修改前:
输入:

1
2
3
4
5
POST /test1/user3/_bulk?pretty
{"index" : {"_index" : "test2", "_type" : "user1", "_id" : 1}}
{"name" : "silence1"}
{"index" : {"_index" : "test2", "_type" : "user1", "_id" : 2}}
{"name" : "silence2"}

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"took": 225,
"errors": false,
"items": [
{
"index": {
"_index": "test2",
"_type": "user1",
"_id": "1",
"_version": 1,
"status": 201
}
},
{
"index": {
"_index": "test2",
"_type": "user1",
"_id": "2",
"_version": 1,
"status": 201
}
}
]
}

如在修改后(已重启):
输出:

1
2
3
4
{
"error": "IllegalArgumentException[explicit index in bulk is not allowed]",
"status": 500
}

输入:

1
2
3
4
5
POST /test1/user3/_bulk?pretty
{"index" : {"_id" : 1}}
{"name" : "silence1"}
{"index" : {"_id" : 2}}
{"name" : "silence2"}

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"took": 8,
"errors": false,
"items": [
{
"index": {
"_index": "test1",
"_type": "user3",
"_id": "1",
"_version": 1,
"status": 201
}
},
{
"index": {
"_index": "test1",
"_type": "user3",
"_id": "2",
"_version": 1,
"status": 201
}
}
]
}

elasticsearch 第四篇(API约定)的更多相关文章

  1. Spring Cloud第十四篇 | Api网关Zuul

    ​ 本文是Spring Cloud专栏的第十四篇文章,了解前十三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring C ...

  2. Elasticsearch第四篇:索引别名、添加或修改映射规则

    项目中经常出现的问题,例如添加字段.修改字段,那原先的索引规则就要跟着改变,最好是一开始就给索引一个别名,修改字段时新增映射,然后将笔名指向新的映射,当然需要将之前的索引搬迁到新的映射当中. 1.获取 ...

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

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

  4. ElasticSearch查询 第四篇:匹配查询(Match)

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

  5. ElasticSearch入门 第四篇:使用C#添加和更新文档

    这是ElasticSearch 2.4 版本系列的第四篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  6. ElasticSearch入门 第二篇:集群配置

    这是ElasticSearch 2.4 版本系列的第二篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  7. ElasticSearch入门 第一篇:Windows下安装ElasticSearch

    这是ElasticSearch 2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  8. ElasticSearch查询 第二篇:文档更新

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

  9. 第四篇 SQL Server代理配置数据库邮件

    本篇文章是SQL Server代理系列的第四篇,详细内容请参考原文. 正如这一系列的前几篇所述,SQL Server代理作业是由一系列的作业步骤组成,每个步骤由一个独立的类型去执行.SQL Serve ...

随机推荐

  1. 那些不能遗忘的知识点回顾——C/C++系列(笔试面试高频题)

    有那么一些零碎的小知识点,偶尔很迷惑,偶尔被忽略,偶然却发现它们很重要,这段时间正好在温习这些,就整理在这里,一起学习一起提高!后面还会继续补充. ——前言 1.面向对象的特性 封装.继承.多态. 封 ...

  2. PowerDesigner 15学习笔记:十大模型及五大分类

    个人认为PowerDesigner 最大的特点和优势就是1)提供了一整套的解决方案,面向了不同的人员提供不同的模型工具,比如有针对企业架构师的模型,有针对需求分析师的模型,有针对系统分析师和软件架构师 ...

  3. Mac 必备工具之 brew

    brew 是 Mac 下的一个包管理工具,类似于 centos 下的 yum,可以很方便地进行安装/卸载/更新各种软件包,例如:nodejs, elasticsearch, kibana, mysql ...

  4. 我们一起学习WCF 第四篇单通讯和双向通讯

    前言:由于个人原因很久没有更新这个系列了,我会继续的更新这系列的文章.这一章是单向和双向通讯.所谓的单向就是只有发送却没有回复,双向是既有发送还有回复.就是有来无往代表单向,礼尚往来表示双向.下面我用 ...

  5. HDU-1864:最大报销额(浮点数01背包)

    链接:HDU-4055:最大报销额 题意:现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上,单类物 ...

  6. 虹软2.0版本离线人脸识别C#类库分享

    目前只封装了人脸检测部分的类库,供大家交流学习,肯定有问题,希望大家在阅读使用的时候及时反馈,谢谢!使用虹软技术开发完成 戳这里下载SDKgithub:https://github.com/dayAn ...

  7. mysql把一字段拆分为多行

    sql语句 select a.house_no as '房子',substring_index(substring_index(a.name,',',b.help_topic_id+1),',',-1 ...

  8. 爬虫:Scrapy12 - Stats Collection

    Scrapy 提供了方便的收集数据的机制.数据以 key/value 方式存储,值大多是计数值.该机制叫做数据收集器(Stats Collector),可以通过 Crawler API 的属性 sta ...

  9. javascript event对象操作

    js代码: $(".leads_detail").click(function(e){ e = e || event; var t = e.target || e.srcEleme ...

  10. Full Binary Tree(二叉树找规律)

    Description In computer science, a binary tree is a tree data structure in which each node has at mo ...