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索引详解的更多相关文章

  1. ELK查询命令详解

    目录 ELK查询命令详解 倒排索引 使用ElasticSearch API 实现CRUD 批量获取文档 使用Bulk API 实现批量操作 版本控制 什么是Mapping? 基本查询(Query查询) ...

  2. 搜索引擎框架之ElasticSearch基础详解(非原创)

    文章大纲 一.搜索引擎框架基础介绍二.ElasticSearch的简介三.ElasticSearch安装(Windows版本)四.ElasticSearch操作客户端工具--Kibana五.ES的常用 ...

  3. ELK查询命令详解总结

    目录 ELK查询命令详解 倒排索引 倒排索引原理 分词器介绍及内置分词器 使用ElasticSearch API 实现CRUD 批量获取文档 使用Bulk API 实现批量操作 版本控制 什么是Map ...

  4. Elasticsearch配置详解、文档元数据

    目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 1.Elasticsearch配置文件详解 a. 在上面博客中,我们已经安装并且成功 ...

  5. MySQL 联合索引详解

    MySQL 联合索引详解   联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分.例如索引是key index (a,b,c ...

  6. Oracle索引详解

    Oracle索引详解(二) --索引分类   Oracle 提供了大量索引选项.知道在给定条件下使用哪个选项对于一个程序的性能来说非常重要.一个错误的选择可能会引发死锁,并导致数据库性能急剧下降或进程 ...

  7. 【详细解析】MySQL索引详解( 索引概念、6大索引类型、key 和 index 的区别、其他索引方式)

    [详细解析]MySQL索引详解( 索引概念.6大索引类型.key 和 index 的区别.其他索引方式) MySQL索引的概念: 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分 ...

  8. elasticsearch系列三:索引详解(分词器、文档管理、路由详解(集群))

    一.分词器 1. 认识分词器  1.1 Analyzer   分析器 在ES中一个Analyzer 由下面三种组件组合而成: character filter :字符过滤器,对文本进行字符过滤处理,如 ...

  9. elasticsearch系列二:索引详解(快速入门、索引管理、映射详解、索引别名)

    一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...

  10. elasticsearch配置详解

    一.说明 使用的是新版本5.1,直接从官网下载rpm包进行安装,https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5 ...

随机推荐

  1. 英语知识积累-D01-body+animal

    My body What's your name? Here are lots of children playing. Are they happy or sad? Who's waving at ...

  2. PPT vba从Execl 拷贝图表

    在PPT 需要引用Execl的COM组件 Dim wkb As Workbook Sub Change() Set wkb = Workbooks.Open("D:\D2_月报基础数据.xl ...

  3. H5上传功能

    近期开发一个关于微信公总号二次开发中,上传图片的需求,测试几个开源插件,更新一些心得,有需要可留言!!! plupload plupload多张上传图片的一个参考demo ajaxFileUpload ...

  4. 实现lodash.get功能

    function deepGet(object, path, defaultValue) { return (!Array.isArray(path) ? path.replace(/\[/g, '. ...

  5. OpenCV常用头文件介绍

    转载:https://www.cnblogs.com/wangguchao/p/7244483.html 1.OpenCV包含的模块 cv – 核心函数库 cvaux – 辅助函数库 cxcore – ...

  6. 连接Redis_五种数据格式

    前面我们已经准备成功开启Redis服务,其端口号为6379,接下来我们就看看如何使用C#语言来操作Redis.就如MongoDB一样,要操作Redis服务,自然就需要下载C#的客户端,这里通过Nuge ...

  7. 你不知道的JS之作用域和闭包(五)作用域闭包

    原文:你不知道的js系列 一个简单粗暴的定义 闭包就是即使一个函数在它所在的词法作用域外部被执行,这个函数依然可以访问这个作用域. 比如: function foo() { var a = 2; fu ...

  8. pytorch可视化工具visdom

    visdom的github repo: https://github.com/facebookresearch/visdom 知乎一个教程:https://zhuanlan.zhihu.com/p/3 ...

  9. js-day06-jQuery事件和DOM操作-练习题

    jQuery事件绑定 js中绑定事件,三种方式: 方式1: 直接在元素上,增加onXxx事件属性. <button onclick="alert(1);">点我< ...

  10. SQL优化方法:

    1.查看连接对象 1 USE master 2 GO 3 --如果要指定数据库就把注释去掉 4 SELECT * FROM sys.[sysprocesses] WHERE [spid]>50 ...