ELK-ElasticSearch索引详解
1、使用_cat API检测集群是否健康,确保9200端口号可用:
curl 'localhost:9200/_cat/health?v' 注意:绿色表示一切正常,黄色表示所有的数据可用但是部分副本还没有分配,红色表示部分数据因为某些原因不可用。 2、获取集群的节点列表
curl 'localhost:9200/_cat/nodes?v' 3、查看所有索引
curl http://localhost:9200/_cat/indices?v 4、curl用法
-X 指定http的请求方法,有HEAD GET POST PUT DELETE
-d 指定要传输的数据
-H 指定http请求头信息 5、新增索引
现在我们创建一个名为"customer"的索引,然后再查看所有的索引:
curl -XPUT 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v' 6、插入和获取
6.1、插入数据 必须给ES指定索引的类型,如索引为customer,类型为external,ID为1,主体为JSON格式的语句:{ "name": "John Doe" } curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe",
"age": 10
}' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/3?pretty' -d '
{
"name": "AAA",
"age": 20
}' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/4?pretty' -d '
{
"name": "BBB",
"age": 30
}' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/5?pretty' -d '
{
"name": "EEE",
"age": 40
}' 返回结果为:create:true 表示插入成功。 6.2、获取数据
curl -XGET 'localhost:9200/customer/external/1?pretty' 其中含义为:获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观。 7、删除索引
curl -XDELETE 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v' 8、ES基本用法
通过以上命令语句的学习,我们发现索引的增删改查有一个类似的格式,总结如下:
curl -X<REST Verb> <ip>:<Port>/<Index>/<Type>/<ID>
<REST Verb>:REST风格的语法谓词
<ip>:节点ip
<port>:节点端口号,默认9200
<Index>:索引名
<Type>:索引类型
<ID>:操作对象的ID号 ES的动作是以http方法来决定的,常用的http方法: GET/PUT/POST/DELETE 9、修改数据
curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/2?pretty' -d '
{
"name": "aaa"
}' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/2?pretty' -d '
{
"name": "Java"
}' 上述命令语句是:先新增id为1,name为aaa的数据,然后将id为2的name修改为Java。 10.更新数据 10.1 这个例子展示如何将id为1文档的name字段更新为Jane Doe: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane Doe AAA" }
}' 10.2 这个例子展示如何将id为1数据的name字段更新为Jane Doe同时增加字段age为20:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane Doe", "age": 20 }
}' 10.3 也可以通过一些简单的scripts来执行更新。一下语句通过使用script将年龄增加5:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"script" : "ctx._source.age += 5"
}' 11、删除数据
删除数据那是相当的直接. 下面的语句将执行删除Customer中ID为2的数据:
curl -XDELETE 'localhost:9200/customer/external/2?pretty' 12、批处理
下面语句将在一个批量操作中执行创建索引:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
' 查看数据
curl -XGET 'localhost:9200/customer/external/2?pretty'
curl -XGET 'localhost:9200/customer/external/_search?pretty' 下面语句批处理执行更新id为1的数据然后执行删除id为2的数据
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
curl -XGET 'localhost:9200/customer/external/_search?pretty' 13、查询
curl 'localhost:9200/customer/external/_search?q=*&pretty' 上面示例返回所有customer中的索引数据。其中 q=* 表示匹配索引中所有的数据。 等价于:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": { "match_all": {} }
}' 14、查询语言
匹配所有数据,但只返回1个:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": { "match_all": {} },
"size": 1
}' 注意:如果siez不指定,则默认返回10条数据。 curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": { "match_all": {} },
"from": 0,
"size": 10
}' 返回从0到10的数据。(索引下标从0开始) curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "age": { "order": "desc" } }
}' curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "age": { "order": "asc" } }
}'
上述示例匹配所有的索引中的数据,按照age字段降序排序,并且返回前10条(如果不指定size,默认最多返回10条)。 15、执行搜索
下面例子展示如何返回两个字段(name age)
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": { "match_all": {} },
"_source": ["name", "age"]
}' 返回age为20 的数据:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": { "match": { "age": 20 } }
}' 返回name中包含Doe的所有数据::
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": { "match": { "name": "Doe" } }
}' 返回name中包含Doe或者AAA的所有数据:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": { "match": { "name": "Doe AAA" } }
}' 和上面匹配单个词语不同,下面这个例子是多匹配(match_phrase短语匹配),返回name字段中包含短语 “mill lane”的所有数据:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": { "match_phrase": { "name": "mill lane" } }
}' 以下是布尔查询,布尔查询允许我们将多个简单的查询组合成一个更复杂的布尔逻辑查询。 这个例子将两个查询组合,返回name中含有AAA和Doe的所有记录数据,属于and操作:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "name": "Doe" } },
{ "match": { "name": "AAA" } } ]
}
}
}'
上述例子中,must表示所有查询必须都为真才被认为匹配。 相反, 这个例子组合两个查询,返回name中含有Doe或者Lynch的所有记录数据:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": {
"bool": {
"should": [
{ "match": { "name": "Doe" } },
{ "match": { "name": "Lynch" } } ]
}
}
}'
上述例子中,bool表示查询列表中只要有任何一个为真则认为匹配。 下面例子组合两个查询,返回地址中既没有mill也没有lane的所有数据:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": {
"bool": {
"must_not": [
{ "match": { "name": "Doe" } },
{ "match": { "name": "AAA" } } ]
}
}
}'
上述例子中,must_not表示查询列表中没有为真的(也就是全为假)时则认为匹配。 我们可以组合must、should、must_not来实现更加复杂的多级逻辑查询。
下面这个例子返回年龄等于10岁、name不为"Jane Doe AAA"的所有数据:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "age": "10" } }
],
"must_not": [
{ "match": { "name": "Jane Doe AAA" } }
]
}
}
}' 16、过滤filter(查询条件设置)
下面这个例子使用了布尔查询返回age在10到30之间的所有数据。
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match_all": {} }
}
}
}' curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
}
}' 17、聚合 Aggregations
下面这个例子: 将所有的数据按照age分组(group),然后按照分组记录数从大到小排序,返回前十条(默认):
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_age": {
"terms": {
"field": "age"
}
}
}
}'
注意:我们设置size=0,不显示查询hits,因为我们只想看返回的聚合结果。 上述语句类似于以下SQL语句:
select age, count(*) from customer group by age order by count(*) desc 下面这个实例按照age分组,降序排序,返回age的平均值:
curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_age": {
"terms": {
"field": "age"
},
"aggs": {
"average_age": {
"avg": {
"field": "age"
}
}
}
}
}
}' curl -XGET 'localhost:9200/mytest?pretty' 删除索引
curl -XDELETE 'localhost:9200/mytest?pretty' 新增一条记录,并指定为article类型,ID为1
curl -XPUT -H "Content-Type: application/json" 'localhost:9200/mytest/article/1?pretty' -d '
{
"title": "小D课堂啦啦啦",
"content":"xdclass.net 小D课堂成立于2016年的,专注互联网在线教育,课程范围包括前端,后端,大数据,人工智能,微信开发等"
}' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/mytest/article/2?pretty' -d '
{
"title": "test",
"content":"testsfsdfdsfdsf",
"PV":10
}' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/mytest/article/3?pretty' -d '
{
"title": "test",
"content":"testsfsdfdsfdsf",
"PV":23
}' 搜索
curl -XGET 'http://localhost:9200/mytest/article/_search?q=title:D&pretty'
curl -XGET 'http://localhost:9200/mytest/article/_search?q=title:test&pretty' 查看数据
curl http://localhost:9200/es-message-2019.04.23/_search?pretty
ELK-ElasticSearch索引详解的更多相关文章
- ELK查询命令详解
目录 ELK查询命令详解 倒排索引 使用ElasticSearch API 实现CRUD 批量获取文档 使用Bulk API 实现批量操作 版本控制 什么是Mapping? 基本查询(Query查询) ...
- 搜索引擎框架之ElasticSearch基础详解(非原创)
文章大纲 一.搜索引擎框架基础介绍二.ElasticSearch的简介三.ElasticSearch安装(Windows版本)四.ElasticSearch操作客户端工具--Kibana五.ES的常用 ...
- ELK查询命令详解总结
目录 ELK查询命令详解 倒排索引 倒排索引原理 分词器介绍及内置分词器 使用ElasticSearch API 实现CRUD 批量获取文档 使用Bulk API 实现批量操作 版本控制 什么是Map ...
- Elasticsearch配置详解、文档元数据
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 1.Elasticsearch配置文件详解 a. 在上面博客中,我们已经安装并且成功 ...
- MySQL 联合索引详解
MySQL 联合索引详解 联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分.例如索引是key index (a,b,c ...
- Oracle索引详解
Oracle索引详解(二) --索引分类 Oracle 提供了大量索引选项.知道在给定条件下使用哪个选项对于一个程序的性能来说非常重要.一个错误的选择可能会引发死锁,并导致数据库性能急剧下降或进程 ...
- 【详细解析】MySQL索引详解( 索引概念、6大索引类型、key 和 index 的区别、其他索引方式)
[详细解析]MySQL索引详解( 索引概念.6大索引类型.key 和 index 的区别.其他索引方式) MySQL索引的概念: 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分 ...
- elasticsearch系列三:索引详解(分词器、文档管理、路由详解(集群))
一.分词器 1. 认识分词器 1.1 Analyzer 分析器 在ES中一个Analyzer 由下面三种组件组合而成: character filter :字符过滤器,对文本进行字符过滤处理,如 ...
- elasticsearch系列二:索引详解(快速入门、索引管理、映射详解、索引别名)
一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...
- elasticsearch配置详解
一.说明 使用的是新版本5.1,直接从官网下载rpm包进行安装,https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5 ...
随机推荐
- 把网站打包快速在线生成ios app的正确方法
下方的内容不用看了,请点这里 !点这里!IOS APP自助生成系统已上线,请马上去了解下: http://www.tao-jiujiu.com/post/188.html ============= ...
- 我的 FPGA 学习历程(11)—— 实验:按键消抖
按键是一个输入设备,在理论上可以归为开关一类,理想的按键波形如下: 然而由于按键的机械特性,断开和闭合动作是不可能在一瞬间完成的,实际的波形如下: 抖动期间电平处于临界值,由于晶振的频率相当的高,数字 ...
- 对象关系映射 ORM
1.1 作用 MTV框架中包括一个重要的部分,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的工作量,不需要面对因 ...
- git 提交代码到库
今天用git commit -m “注释”提交的时候,注释写错了,于是各种查资料开始了和git bash vim的纠缠...(网上的资料我真是没操作成功,不过最后还是摸索出来了) 首先 使用 git ...
- js的算法题
1.统计一个字符串中出现最多的字母 给出一个字符串,统计出现次数最多的字母.如:“wqeqwhixswiqhdxsq”,其中出现最多的是q. js算法的实现 function findMax(str) ...
- java常用框架
一.SpringMVC http://blog.csdn.net/evankaka/article/details/45501811 Spring Web MVC是一种基于Java的实现了Web MV ...
- IOS开发中关于runtime的认识
首先要知道我们写的代码在程序运行过程中都会被转化成runtime的C代码执行. runtime突出的一点就是OC中消息传递机制的应用.objc_msgsend(target,SEL); 首先我们先看一 ...
- [LeetCode] Fibonacci Number 斐波那契数字
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- tomcat的下载与安装
(1)下载Tomcat 官网地址:http://tomcat.apache.org/whichversion.html (2)安装Tomcat Tomcat有安装版和解压版(绿色版) 安装版以.exe ...
- js生成自定义随机数方法
function getRandom() { var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', ...