Elasticsearch系列(3):Elasticsearch操作入门
创建Index
新建Index,可以直接向Elastic服务器发送PUT请求,比如下面的命令创建了一个名为:logdb的Index。
[root@elsearchserver ~]# curl -X PUT 'http://192.168.1.40:9200/logdb'
Elastic服务器返回一个JSON对象,里面的acknowledged字段为true表示操作成功。
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "logdb"
}
注意:索引名必须全部小写,且不能以下划线开头,不能包含逗号。
新增记录
向指定的/Index/Type发送POST请求,就可以在Index里面新增一条记录。比如,向/logdb/debuglog发送请求,就可以新增一条debuglog调试日志记录。如下代码:
curl -X POST 'http://192.168.1.40:9200/logdb/debuglog' -d '
{
"SystemCode": "Ubtrip",
"Source": "ApprovalService",
"Message": "数据库管理"
}'
上面的代码,向/logdb/debuglog发出了一个POST请求,添加一条记录。这时服务器返回的JSON对象里面,_id是一个随机字符串,如下代码:
{
"_index": "logdb",
"_type": "debuglog",
"_id": "qpzDUWIB9teAN1UfUbN_",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
修改记录
修改记录就是使用PUT请求,重新向Elastic服务器发送一次数据,下面的代码中,我们将原记录的Message由“数据库管理”修改为了“数据库管理,即DBA”,请求代码如下:
http://192.168.1.40:9200/logdb/debuglog/qpzDUWIB9teAN1UfUbN_
{
"SystemCode": "Ubtrip",
"Source": "ApprovalService",
"Message": "数据库管理,即DBA"
}
服务器返回结果如下代码:
{
"_index": "logdb",
"_type": "debuglog",
"_id": "qpzDUWIB9teAN1UfUbN_",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
从结果可以看到,以下几个字段发生了变化。
"_version": 2,
"result": "updated",
查询记录
向/Index/Type发送GET请求,就可以查询记录。例如:查询索引logdb下Type为debuglog的所有记录,请求url如下:
http://192.168.1.40:9200/logdb/debuglog/_search?pretty=true
URL参数的pretty=true表过以易读格式返回。
服务器返回结果如下:
{
"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "logdb",
"_type" : "debuglog",
"_id" : "qpzDUWIB9teAN1UfUbN_",
"_score" : 1.0,
"_source" : {
"SystemCode" : "Ubtrip",
"Source" : "ApprovalService",
"Message" : "数据库管理,即DBA"
}
},
{
"_index" : "logdb",
"_type" : "debuglog",
"_id" : "q5zTUWIB9teAN1UfvLNS",
"_score" : 1.0,
"_source" : {
"SystemCode" : "Ubtrip",
"Source" : "ApprovalService",
"Message" : "数据库管理,即DBA"
}
}
]
}
}
删除记录
删除记录就是向服务器发送一个DELETE请求,例如,我要删除/logdb/debuglog下_id为“q5zTUWIB9teAN1UfvLNS”的记录,请求URL如下:
curl -X DELETE 'http://192.168.1.40:9200/logdb/debuglog/q5zTUWIB9teAN1UfvLNS'
服务器返回结果如下:
{
"_index": "logdb",
"_type": "debuglog",
"_id": "q5zTUWIB9teAN1UfvLNS",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
再次查询/logdb/debuglog下所有记录,发现只剩下一条了,已经找不到_id为“q5zTUWIB9teAN1UfvLNS”的记录了。
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "logdb",
"_type" : "debuglog",
"_id" : "qpzDUWIB9teAN1UfUbN_",
"_score" : 1.0,
"_source" : {
"SystemCode" : "Ubtrip",
"Source" : "ApprovalService",
"Message" : "数据库管理,即DBA"
}
}
]
}
}
Elasticsearch系列(3):Elasticsearch操作入门的更多相关文章
- Elasticsearch系列---初识Elasticsearch
Elasticsearch是什么? Elasticsearch简称ES,是一个基于Lucene构建的开源.分布式.Restful接口的全文搜索引擎,还是一个分布式文档数据库.天生就是分布式.高可用.可 ...
- elasticsearch系列一elasticsearch(ES简介、安装&配置、集成Ikanalyzer)
一.ES简介 1. ES是什么? Elasticsearch 是一个开源的搜索引擎,建立在全文搜索引擎库 Apache Lucene 基础之上 用 Java 编写的,它的内部使用 Lucene 做索引 ...
- Elasticsearch 系列3 --- Elasticsearch配置
一. 位置 ES的配置文件位于安装目录\config下面,主要有 (1) elasticsearch.yml ES系统的配置: (2) jvm.options Java虚拟机配置: (3) log4j ...
- ElasticSearch实战系列六: Logstash快速入门和实战
前言 本文主要介绍的是ELK日志系统中的Logstash快速入门和实战 ELK介绍 ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是 ...
- ElasticSearch实战系列八: Filebeat快速入门和使用---图文详解
前言 本文主要介绍的是ELK日志系统中的Filebeat快速入门教程. ELK介绍 ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是 ...
- Elasticsearch系列(五)----JAVA客户端之TransportClient操作详解
Elasticsearch JAVA操作有三种客户端: 1.TransportClient 2.JestClient 3.RestClient 还有种是2.3中有的NodeClient,在5.5.1中 ...
- Elasticsearch 术语介绍和CRUD实际操作入门
一.Elastic Stack 核心Elasticsearch Elasticsearch 是一个分布式.RESTful 风格的搜索和数据分析引擎.Elasticsearch 是面向文档的,这就意味着 ...
- SpringBoot系列之Elasticsearch极速入门与实际教程
@ 目录 一.什么Elasticsearch? 二.Elasticsearch安装部署 2.1 Elasticsearch安装环境准备 2.2 Docker环境安装Elasticsearch 2.3 ...
- Elasticsearch索引和文档操作
列出所有索引 现在来看看我们的索引 GET /_cat/indices?v 响应 health status index uuid pri rep docs.count docs.deleted st ...
- ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解
前言 在上一篇中介绍了ElasticSearch集群和kinaba的安装教程,本篇文章就来讲解下 ElasticSearch的DSL语句使用. ElasticSearch DSL 介绍 Elastic ...
随机推荐
- java points[复习]
1 - & 与 && 的区别: &:不管左边是true还是false,右端都会进行运算: &&:当左端为false时,右端不再进行运算: 即在与运算时, ...
- swust oj 987
输出用先序遍历创建的二叉树是否为完全二叉树的判定结果 1000(ms) 10000(kb) 2553 / 5268 利用先序递归遍历算法创建二叉树并判断该二叉树是否为完全二叉树.完全二叉树只能是同深度 ...
- 快速实现office文档在线预览展示(doc,docx,xls,xlsx,ppt,pptx)
微软:https://view.officeapps.live.com/op/view.aspx?src=(输入你的文档在服务器中的地址):
- ASP.NET Core知多少(6):VS Code联调Angular + .NetCore
ASP.NET Core知多少系列:总体介绍及目录 1. 引言 最近在看<程序员的成长课>,讲到程序员如何构建技能树,印象深刻.作为一名后台开发的程序员,深感技能单一,就别说技能树了.作为 ...
- Lesson 29 Taxi!
Text Captain Ben Fawcett has bought an unusual taxi and has begun a new serivice. The 'taxi' is a sm ...
- [.net 面向对象程序设计深入](26)实战设计模式——策略模式 Strategy (行为型)
[.net 面向对象程序设计深入](26)实战设计模式——策略模式 Strategy (行为型) 1,策略模式定义 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模 ...
- [Swift]LeetCode36. 有效的数独 | Valid Sudoku
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- [Swift]LeetCode207. 课程表 | Course Schedule
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- [Swift]LeetCode564. 寻找最近的回文数 | Find the Closest Palindrome
Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...
- [Swift]LeetCode865. 具有所有最深结点的最小子树 | Smallest Subtree with all the Deepest Nodes
Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A n ...