Elasticsearch(三)---索引

news2025/7/27 19:25:41

索引文档的语法curl用法

CURL:

简单认为是可以在命令行下访问url的一个工具

curl是利用URL语法在命令行方式下工作的开源文件传输工具,使用curl可以简单实现常见的get/post请求。

curl  

 -X  指定http请求的方法

GET POST  PUT DELETE       restful

-d   指定要传输的数据

新增一条ID是10的用户信息

curl -XPOST http://localhost:8080/user/10   -d 报文体

查询ID是10的用户信息

curl -XGET http://localhost:8080/user/10

修改ID是10的用户信息

curl -XPUT http://localhost:8080/user/10  -d 报文体

删除ID是10的用户信息

curl -XDELETE http://localhost:8080/user/10

索引操作

创建索引blog,默认分片5个,每个分片一个副本分片

# curl -XPOST node3:9200/blog

索引名称不能包含大写字母

# curl -XPUT node3:9200/ABc?pretty
{
  "error" : {
    "root_cause" : [ {
      "type" : "invalid_index_name_exception",
      "reason" : "Invalid index name [ABc], must be lowercase",
      "index" : "ABc"
    } ],
    "type" : "invalid_index_name_exception",
    "reason" : "Invalid index name [ABc], must be lowercase",
    "index" : "ABc"
  },
  "status" : 400
}

不能创建同名索引

# curl -XPUT node3:9200/blog?pretty
{
  "error" : {
    "root_cause" : [ {
      "type" : "index_already_exists_exception",
      "reason" : "already exists",
      "index" : "blog"
    } ],
    "type" : "index_already_exists_exception",
    "reason" : "already exists",
    "index" : "blog"
  },
  "status" : 400
}

在创建索引的时候指定分片的个数以及副本的个数,分片的个数创建索引后不能修改。

# curl -XPOST node3:9200/blog -d '
{
"settings":{
"number_of_replicas":2,
"number_of_shards":3
}
} '

修改blog的副本个数

# curl -XPUT node3:9200/blog/_settings -d '{
"number_of_replicas":1
}'

读写限制:

  • blocks.read_only:true 设置当前索引只允许读不允许写或者更新
  • blocks.read:true 禁止对当前索引进行读操作

blocks.write:true 禁止对当前索引进行写操作

# curl -XPUT node3:9200/blog/_settings -d '{
"blocks.write":true
}'

已经禁止写入了:

# curl -XPOST node3:9200/blog/article/1?pretty -d '{
"title":"java 虚拟机"
}'
{
  "error" : {
    "root_cause" : [ {
      "type" : "cluster_block_exception",
      "reason" : "blocked by: [FORBIDDEN/8/index write (api)];"
    } ],
    "type" : "cluster_block_exception",
    "reason" : "blocked by: [FORBIDDEN/8/index write (api)];"
  },
  "status" : 403
}

恢复写的权限:

# curl -XPUT node3:9200/blog/_settings -d '{
"blocks.write":false
}'

写入数据成功:

# curl -XPUT node3:9200/blog/article/1?pretty -d '{
"title":"java 虚拟机"
}'
{
  "_index" : "blog",
  "_type" : "article",
  "_id" : "1",
  "_version" : 1,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}

查看索引:

# curl node3:9200/blog/_settings?pretty
{
  "blog" : {
    "settings" : {
      "index" : {
        "creation_date" : "1569625739362",
        "number_of_shards" : "3",
        "number_of_replicas" : "1",
        "uuid" : "o4eS-bgoQN2U3u8dohF7zw",
        "version" : {
          "created" : "2020199"
        },
        "blocks" : {
          "write" : "false"
        }
      }
    }
  }
}

创建一个测试索引:

# curl -XPOST node3:9200/myblog?pretty
{
  "acknowledged" : true
}

同时查看多个索引的settings信息:

# curl node3:9200/blog,myblog/_settings?pretty
{
  "blog" : {
    "settings" : {
      "index" : {
        "creation_date" : "1569625739362",
        "number_of_shards" : "3",
        "number_of_replicas" : "1",
        "uuid" : "o4eS-bgoQN2U3u8dohF7zw",
        "version" : {
          "created" : "2020199"
        },
        "blocks" : {
          "write" : "false"
        }
      }
    }
  },
  "myblog" : {
    "settings" : {
      "index" : {
        "creation_date" : "1569626498025",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "EKfVkmbFTEWDR94KaCYb1w",
        "version" : {
          "created" : "2020199"
        }
      }
    }
  }
}

查看集群中所有索引的settings信息:

# curl node3:9200/_all/_settings?pretty
{
  "blog" : {
    "settings" : {
      "index" : {
        "creation_date" : "1569625739362",
        "number_of_shards" : "3",
        "number_of_replicas" : "1",
        "uuid" : "o4eS-bgoQN2U3u8dohF7zw",
        "version" : {
          "created" : "2020199"
        },
        "blocks" : {
          "write" : "false"
        }
      }
    }
  },
  "myblog" : {
    "settings" : {
      "index" : {
        "creation_date" : "1569626498025",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "EKfVkmbFTEWDR94KaCYb1w",
        "version" : {
          "created" : "2020199"
        }
      }
    }
  }
}

删除索引,注意备份数据

# curl -XDELETE node3:9200/myblog?pretty
{
  "acknowledged" : true
}

删除不存在的索引

# curl -XDELETE node3:9200/myblog?pretty
{
  "error" : {
    "root_cause" : [ {
      "type" : "index_not_found_exception",
      "reason" : "no such index",
      "resource.type" : "index_or_alias",
      "resource.id" : "myblog",
      "index" : "myblog"
    } ],
    "type" : "index_not_found_exception",
    "reason" : "no such index",
    "resource.type" : "index_or_alias",
    "resource.id" : "myblog",
    "index" : "myblog"
  },
  "status" : 404
}

索引的打开和关闭

ES索引关闭之后几乎不占用系统资源:关闭后索引就不能索引文档和检索文档了

# curl -XPOST node2:9200/blog/_close?pretty
{
  "acknowledged" : true
}

打开索引

# curl -XPOST node2:9200/blog/_open?pretty
{
  "acknowledged" : true
}

创建示例索引

# curl -XPOST node2:9200/myblog1?pretty
{
  "acknowledged" : true
}
# curl -XPOST node2:9200/myblog2?pretty
{
  "acknowledged" : true
}
# curl -XPOST node2:9200/myblog3?pretty
{
  "acknowledged" : true
}

同时关闭多个索引

# curl -XPOST node3:9200/myblog1,myblog2/_close?pretty
{
  "acknowledged" : true
}

同时打开多个索引:

# curl -XPOST node3:9200/myblog1,myblog2/_open?pretty
{
  "acknowledged" : true
}

如果ES集群中不存在开启或关闭请求中的全部索引会报异常:

# curl -XPOST node3:9200/myblog1,myblogx/_open?pretty
{
  "error" : {
    "root_cause" : [ {
      "type" : "index_not_found_exception",
      "reason" : "no such index",
      "resource.type" : "index_or_alias",
      "resource.id" : "myblogx",
      "index" : "myblogx"
    } ],
    "type" : "index_not_found_exception",
    "reason" : "no such index",
    "resource.type" : "index_or_alias",
    "resource.id" : "myblogx",
    "index" : "myblogx"
  },
  "status" : 404
}
# curl -XPOST node3:9200/myblog1,myblog2,myblog3/_close?pretty
{
  "acknowledged" : true
}

可以通过ignore_unavailable参数只操作存在的索引(反斜杠转义&符号)

# curl -XPOST node3:9200/myblog1,myblogx/_open?pretty\&ignore_unavailable=true
{
  "acknowledged" : true
}

关闭所有索引:

# curl -XPOST node3:9200/_all/_close?pretty
{
  "acknowledged" : true
}

打开my开头的索引:

# curl -XPOST node3:9200/my*/_open?pretty
{
  "acknowledged" : true
}

索引别名

就是给一个索引或者多个索引起的一个另一个名字。为名为test1的索引创建别名alias1,命令格式如下:

# curl -XPOST node3:9200/_aliases -d '{
"actions":[
{"add":{"index":"myblog1", "alias":"alias1"}}
]
}'
{"acknowledged":true}

移除别名:

# curl -XPOST node3:9200/_aliases -d '{
"actions":[
{"remove":{"index":"myblog1", "alias":"alias1"}}
]
}'
{"acknowledged":true}

同时给多个index取别名:

# curl -XPOST node3:9200/_aliases -d '{
"actions":[
{"add":{"index":"blog", "alias":"indexok"}},
{"add":{"index":"myblog1", "alias":"indexm"}}
]
}'
{"acknowledged":true}

简写形式:

# curl -XPOST node3:9200/_aliases -d '{
"actions":[
{"add":{"indices":["blog", "myblog1"], "alias":"alias2"}}
]
}'
{"acknowledged":true}

# curl -XPOST node3:9200/_aliases?pretty -d '{
> "actions":[
> {"remove":{"indices":["blog", "myblog1"], "alias":"alias1"}}
> ]
> }'
{
  "acknowledged" : true
}

增加别名和移除别名混合使用:

# curl -XPOST node3:9200/_aliases?pretty -d '{
"actions":[
{"remove":{"index":"blog", "alias":"alias2"}},
{"add":{"index":"myblog1", "alias":"alias3"}}
]
}'
{
  "acknowledged" : true
}

别名和索引是一对一的,使用别名索引文档或者根据ID查询文档是可以的,但是如果别名和索引是一对多,使用别名会发生错误,以为ES不知道把文档写入哪个索引中或者从哪个索引读取文档。

ES支持通过通配符同时给多个索引设置别名:

# curl -XPOST node3:9200/_aliases -d '{
> "actions":[
> {"add":{"index":"mybl*", "alias":"a1"}}
> ]
> }'
{"acknowledged":true}

查看别名信息:

# curl node3:9200/myblog1/_aliases?pretty
{
  "testblog1" : {
    "aliases" : {
      "a1" : { }
    }
  }
}

查看所有别名信息:

# curl node2:9200/_aliases?pretty
{
  "testblog3" : {
    "aliases" : {
      "a1" : { }
    }
  },
  "testblog2" : {
    "aliases" : {
      "a1" : { }
    }
  },
  "testblog1" : {
    "aliases" : {
      "a1" : { }
    }
  },
  "testok2" : {
    "aliases" : { }
  },
  "testok1" : {
    "aliases" : { }
  }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1156722.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【POI-EXCEL-下拉框】POI导出excel下拉框数据太多导致下拉框不显示BUG修复

RT 最近在线上遇到一个很难受的BUG,我一度以为是我代码逻辑出了问题,用了Arthas定位分析之后,开始坚定了信心:大概率是POI的API有问题,比如写入数据过多。 PS:上图为正常的下拉框。但是,当下拉…

一文看懂图像格式 RAW、RGB、YUV、Packed/Unpacked、Bayer、MIPI、Planar、Semi-Planar、Interleaved

目录 一、通用属性 1. Packed/Unpacked 2. 压缩/非压缩 二、RAW 1. Bayer格式 2. 分类 3. MIPI RAW 三、RGB 分类 四、YUV 1. YUV与RGB转换 2. 分类 3. 内存计算 五、压缩格式 有的人,错过了,一生再也找寻不到。 本文详细分析各种图像格式…

[GDOUCTF 2023]<ez_ze> SSTI 过滤数字 大括号{等

SSTI模板注入-中括号、args、下划线、单双引号、os、request、花括号、数字被过滤绕过(ctfshow web入门370)-CSDN博客 ssti板块注入 正好不会 {%%}的内容 学习一下 经过测试 发现过滤了 {{}} 那么我们就开始吧 我们可以通过这个语句来查询是否存在ss…

鸿蒙切换到主线程

鸿蒙和安卓都是一样的视图操作都需要在主线程或者UI(视图线程)中处理,否则就会报错。 在安卓中可以通过: View.post(new Runnable() {Overridepublic void run() {textView.setText("更新textView");} });runOnUiThread(new Runnable() {public void run…

算法学习打卡day36| 738.单调递增的数字、 968.监控二叉树、贪心算法阶段学习总结

738.单调递增的数字 力扣题目链接 题目描述&#xff1a; 当且仅当每个相邻位数上的数字 x 和 y 满足 x < y 时&#xff0c;我们称这个整数是单调递增的。 给定一个整数 n &#xff0c;返回 小于或等于 n 的最大数字&#xff0c;且数字呈 单调递增 。 示例 1: 输入: n 10 …

JVM虚拟机:通过一个例子解释JVM中栈结构的使用

代码 代码解析 main方法执行&#xff0c;创建栈帧并压栈。 int d8&#xff0c;d为局部变量&#xff0c;是基础类型&#xff0c;它位于虚拟机栈的局部变量表中 然后创建了一个TestDemo的对象&#xff0c;这个对象在堆中&#xff0c;并且这个对象的成员变量&#xff08;day&am…

安防视频监控平台EasyCVR(V.3.4)新功能:告警查询操作步骤

视频集中存储/云存储/视频监控管理平台EasyCVR能在复杂的网络环境中&#xff0c;将分散的各类视频资源进行统一汇聚、整合、集中管理&#xff0c;实现视频资源的鉴权管理、按需调阅、全网分发、智能分析等。AI智能大数据视频分析EasyCVR平台已经广泛应用在工地、工厂、园区、楼…

2024王道考研计算机组成原理——中央处理器

CPU的运算器其实就是进行固定的数据处理&#xff0c;后面讲的CPU主要侧重的是它的控制器功能 运算器的基本结构 左右两边都是16位&#xff0c;因为寄存器可能位于左右两端的一边(源/目的操作数) A、B两端都要接一堆线 通用寄存器 ALU都在运算器当中 从主存来的数据直接放到…

3201. 任务调度

有若干个任务需要在一台机器上运行。 它们之间没有依赖关系&#xff0c;因此可以被按照任意顺序执行。 该机器有两个 CPU 和一个 GPU。 对于每个任务&#xff0c;你可以为它分配不同的硬件资源: 在单个 CPU 上运行。在两个 CPU 上同时运行。在单个 CPU 和 GPU 上同时运行。在两…

RabbitMQ消息队列笔记

目录 docker Java 导包 配置文件 Work Queues 消息堆积 消息生产者发送消息到队列 消息消费者接收消息 Fanout交换机 Direct交换机发送消息 用Java代码创建交换机和队列、绑定 Direct交换机 Direct交换机发送消息 用Java代码创建交换机和队列、绑定 基于注解声明队…

Rust 语言常见的一些概念(上)

目录 1、变量的可变性 常量 隐藏 2、数据类型 2.1 标量类型 整型 浮点型 数值运算 布尔型 字符类型 复合类型 元组类型 数组类型 1、变量的可变性 变量默认是不可改变的&#xff08;immutable&#xff09;。这是 Rust 提供给你的众多优势之一&#xff0c;让你得以…

win32 读写UTF-8格式的文件的方法

1&#xff0c;写入数据 最开始是在写入数据前先写入三个字节 BYTE btHead[] { 0xEF,0xBB,0xBF }; ::WriteFile(hFile, btHead, 3, &dwWrite, 0); ::WriteFile(hFile, str, lstrlen(str)*sizeof(TCHAR), &dwWrite, 0);这样写入后文件样式为&#xff1a; 格式是UTF-8…

基于Python制作一个动物识别小程序

目录 引言研究背景目的与意义 动物识别技术概述基本原理图像处理与特征提取机器学习与深度学习方法 数据集与数据预处理数据收集与构建数据预处理步骤数据增强技术 特征提取与选择基础特征提取方法特征选择与降维 引言 研究背景 动物识别是计算机视觉和模式识别领域的重要研究…

《深入浅出OCR》实战:基于CRNN的文字识别

✨专栏介绍: 经过几个月的精心筹备,本作者推出全新系列《深入浅出OCR》专栏,对标最全OCR教程,具体章节如导图所示,将分别从OCR技术发展、方向、概念、算法、论文、数据集等各种角度展开详细介绍。 💙个人主页: GoAI |💚 公众号: GoAI的学习小屋 | 💛交流群: 7049325…

在python中加载tensorflow-probability模块和numpy模块

目录 操作步骤&#xff1a; 注意&#xff1a; 问题&#xff1a; 解决办法&#xff1a; 操作步骤&#xff1a; 在虚拟环境的文件夹中&#xff0c;找到Scripts文件夹&#xff0c;点击进去&#xff0c;找到地址栏&#xff0c;在地址栏中输入cmd&#xff0c;进入如下界面。 输…

国产数据库兼容过程中涉及的MySQL非严格模式

点击上方蓝字关注我 在国产数据库兼容适配过程中&#xff0c;经常遇到因源数据库是MySQL&#xff0c;迁移至其他国产数据库后&#xff0c;因MySQL端兼容模式有非严格模式&#xff0c;导致适配过程过程中需要做调整。那么&#xff0c;MySQL主要的非严格模式小结如下&#xff1a;…

约会杭州云栖2023:为了无法计算的价值一起努力

&#x1f3c6;作者简介&#xff0c;黑夜开发者&#xff0c;CSDN领军人物&#xff0c;全栈领域优质创作者✌&#xff0c;CSDN博客专家&#xff0c;阿里云社区专家博主&#xff0c;2023年6月CSDN上海赛道top4。 &#x1f3c6;数年电商行业从业经验&#xff0c;历任核心研发工程师…

C++初阶 类和对象(上)

前言&#xff1a;C初阶系列&#xff0c;每一期博主都会使用简单朴素的语言将对应的知识分享给大家&#xff0c;争取让所有人都可以听懂&#xff0c;C初阶系列会持续更新&#xff0c;上学期间将不定时更新&#xff0c;但总会更的 目录 一、什么是面向对象编程 二、什么是类和如…

AST反混淆实战|变种ob混淆还原指南一

关注它&#xff0c;不迷路。 本文章中所有内容仅供学习交流&#xff0c;不可用于任何商业用途和非法用途&#xff0c;否则后果自负&#xff0c;如有侵权&#xff0c;请联系作者立即删除&#xff01; 1.需求 ob混淆是我们最常见的混淆代码&#xff0c;标准的混淆 可以用星…

如何读懂深度学习python项目,以`Multi-label learning from single positive label`为例

Paper : Multi-label learning from single positive label Code 先读一读README.md 可能有意想不到的收获&#xff1b; 实验环境设置要仔细看哦&#xff01; 读论文 如何读论文&#xff0c;Readpaper经典十问 &#xff08;可能在我博客里有写&#xff09; How to read a …