美文网首页
ES-ElasticSearch操作命令大全

ES-ElasticSearch操作命令大全

作者: 勇敢的心888 | 来源:发表于2021-08-09 20:25 被阅读0次

==========

1.检查ES节点是否正常启动

curl http://192.168.6.16:9200

正常状态:

img

非正常状态:

1>确保服务是不是正常启动了,端口用的是哪个

2>防火墙是否关闭或者端口是否开放

3>你的curl命令是否有问题,curl命令可能导致服务无法访问,可以尝试重启服务后,在外部浏览器访问URL地址即可。不一定非得用curl

2.cat检测集群健康状况

curl http://192.168.6.16:9200/_cat/health?v
img

绿色表示一切正常, 黄色表示所有的数据可用但是部分副本还没有分配,红色表示不可用

3.查询es中所有索引,所有已存在的索引

curl http://192.168.6.16:9200/_cat/indices?v
img

4.创建新的索引【索引要求是全小写字符,可以有下划线隔开】

curl -XPUT http://192.168.6.16:9200/my_new_index?pretty

再查看:

curl http://192.168.6.16:9200/_cat/indices?v
img

5.对新增的索引,插入一条数据

type是user, id指定为1

curl -XPUT http://192.168.6.16:9200/my_new_index/user/1?pretty -d  '{"name":"张三","age":"23"}'
img

6.根据ID,获取刚刚索引中新增的数据

curl -XGET http://192.168.6.16:9200/my_new_index/user/1?pretty
img

7.修改数据

7.1先新增一条数据

curl -XPUT http://192.168.6.16:9200/my_new_index/user/2?pretty -d '{"name":"李四","age":"25"}'
img

7.2 根据ID查询这条数据

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
img

7.3修改id为2的数据

curl -XPUT http://192.168.6.16:9200/my_new_index/user/2?pretty -d '{"name":"李四修改","age":"28"}'

即使用相同的新增命令操作 相同的ID,数据不同

img

7.4查询修改结果

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
img

8.更新数据,使用POST请求,注意请求体,格式

curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"doc":{"name":"李四更新","age":"230"}}'
img

查看更新后的数据:

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
img

9.更新数据的同时,新增列

就是将doc中的json数据列增加即可

curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"doc":{"name":"李四更新","age":"230","address":"北京东直门"}}'
img

查看:

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
img

10.将age字段字符串类型,修改为数字类型,并使用简单脚本对其操作

10.1 查看数据

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
img

10.2 将age类型由字符串更改为数值

就是将json中的age的值的引号去掉

curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"doc":{"name":"李四更新","age":230,"address":"北京东直门"}}'
img

10.3 查看修改后数据

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
img

10.4使用简单脚本,对年龄增加5

如果报错。解决方法:https://www.cnblogs.com/sxdcgaq8080/p/11119420.html

curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"script" : "ctx._source.age += 5"}'

查看:

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
img

11.删除数据,根据ID删除

curl -XDELETE http://192.168.6.16:9200/my_new_index/user/2?pretty
img

13.批量插入 bulk

【注意JSON字符串格式】

curl -XPOST http://192.168.6.16:9200/my_new_index/user/_bulk?pretty -d '
{"index":{"_id":"3"}}
{"name":"赵思","age":12}
{"index":{"_id":"4"}}
{"name":"钱三一","age":13}
'
img

想要看插入以后索引下的数据,查询在后面16

14.批处理语句,bulk,更新id为1的数据,删除id为3的数据

curl -XPOST http://192.168.6.16:9200/my_new_index/user/_bulk?pretty -d '
{"update":{"_id":"1"}}
{"doc": {"name":"张三变李四","age":25}}
{"delete":{"_id":"3"}}
'
img

15.导入批量数据集文件json文件【使用bulk批量导入】

测试的json批量数据集文件,java生成代码:

img

View Code

如果报错,解决方案:https://www.cnblogs.com/sxdcgaq8080/p/11119883.html

指定要导入的 索引、type、使用bulk命令 @符号后面跟json文件的绝对路径

curl -XPOST http://192.168.6.16:9200/my_new_index/user/_bulk?pretty --data-binary @/cjf/es/elasticsearch-2.3.3/data/myjson.json
img

查看index详情:

curl http://192.168.6.16:9200/_cat/indices?v

可以看到成功批量插入了200条

img

===================================下来看查询(删除索引在最后)=========================================

16.查询某个索引中的所有数据

curl http://192.168.6.16:9200/my_new_index/_search?q=*&pretty

等价于

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_all":{

        }
    }
}
'
[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '{"query":{ "match_all":{}}}'
img

17.查询指定索引下的数据

【如果不指定size,默认返回10条】

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_all":{

        }
    },
    "size":10
}
'
[ 复制代码

](javascript:void(0);)

img

18.分页查询,从第10条,返回10条

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_all":{

        }
    },
    "from": 10,
    "size": 10
}
'
[ 复制代码

](javascript:void(0);)

img

19.按照age字段倒序排序 sort,取出20条

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_all":{

        }
    },
    "sort":{
        "age":{
            "order":"desc"
        }
    },
    "from": 0,
    "size": 20
}
'
[ 复制代码

](javascript:void(0);)

img

20.只返回想查询的部分字段

只返回name和address列

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_all":{

        }
    },
    "_source":[
        "name",
        "address"
    ]
}
'
[ 复制代码

](javascript:void(0);)

img

21.条件匹配查询

21.1查询age=200的数据

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match":{
            "age":200
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

21.2 查询address中包含 “北京” 的数据

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match":{
            "address":"北京"
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

21.3 查询 address中 包含“北京” 或 “西安”的所有数据 【匹配单个词语 空格分隔】

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match":{
            "address":"北京 西安"
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

21.4 查询address中包含“北京 西安” 完整词语的【短语匹配,“北京 西安”作为一个完整词语查询】、

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_phrase":{
            "address":"北京 西安"
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

22.布尔查询 bool

22.1布尔查询bool and查询,必须同时满足 address中包含“北京”,又要满足address中包含“西安”

must表示所有查询必须都为真才被认为匹配

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "address":"西安"
                    }
                }
            ]
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

22.2 布尔查询bool or查询 address中包含“北京” 或者 address中包含“西安” 都可以

should 表示查询列表中只要有任何一个为真则认为匹配

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "address":"西安"
                    }
                }
            ]
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

22.3 布尔查询bool 都不能满足的 既不能包含这个,也不能包含那个

must_not表示查询列表中没有为真的(也就是全为假)时则认为匹配

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "bool":{
            "must_not":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "address":"西安"
                    }
                }
            ]
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

22.4 这样,就可以布尔查询 多条件组合 查询

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "age":200
                    }
                }
            ],
            "must_not":[
                {
                    "match":{
                        "address":"西安"
                    }
                }
            ]
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

23. 范围查询 range 查询年龄25-30之间的

[ 复制代码

](javascript:void(0);)

curl -XPOST  http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "range":{
            "age":{
                "gte":25,
                "lte":30
            }
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

24.聚合查询 aggs

按照name进行聚合分组,然后按照记录数,从大到小排序,默认返回前10条

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "size":0,
    "aggs":{
        "group_by_name":{
            "terms":{
                "field":"name"
            }
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

25. 聚合查询 aggs ,求age的平均值

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "size":0,
    "aggs":{
        "average_age":{
            "avg":{
                "field":"age"
            }
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

按name分组,求age的平均值

[ 复制代码

](javascript:void(0);)

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "size":0,
    "aggs":{
        "group_by_name":{
            "terms":{
                "field":"name"
            },
            "aggs":{
                "average_age":{
                    "avg":{
                        "field":"age"
                    }
                }
            }
        }
    }
}
'
[ 复制代码

](javascript:void(0);)

img

26 .删除索引

26.1 查看所有索引信息

curl http://192.168.6.16:9200/_cat/indices?v
img

26.2 删除指定索引

curl -XDELETE http://192.168.6.16:9200/my_new_index?pretty
img

26.3 再次查看

curl http://192.168.6.16:9200/_cat/indices?v
img

==========================================

相关文章

  • ES-ElasticSearch操作命令大全

    ========== 1.检查ES节点是否正常启动 正常状态: 非正常状态: 1>确保服务是不是正常启动了,端口用...

  • 电脑cmd命令大全

    cmd命令大全 cmd命令大全 现在大家用的Windows系统,都是离不开DOS命令的操作。学习DOS系统操作,首...

  • kubectl 命令技巧大全

    kubectl 命令技巧大全 Kubctl 命令是操作 kubernetes 集群的最直接和最 skillful ...

  • mac 终端命令

    MacOS 常用终端命令大全: 目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操...

  • 「转载」MATLAB tutorial

    MATLAB命令大全和矩阵操作大全一、矩阵的表示在MATLAB中创建矩阵有以下规则: a、矩阵元素必须在”[ ]”...

  • MacOS 常用终端命令大全

    MacOS 常用终端命令大全: 目录操作 命令——功能描述——示例 mkdir——创建一个目录——mkdir di...

  • Web Jenkins 自动构建打包

    Linux命令参考链接 Linux命令大全Linux命令大全Linux常用命令大全 本文可能到的命令: Jenki...

  • Mac命令行大全及使用技巧

    命令行大全(转载) 1. 目录操作 2. 文件操作 3. 选择操作 4. 安全操作 5.编程操作 6. 进程操作 ...

  • 【Android开发基础系列】ADB操作命令大全

    主要参考: adb操作命令详解及大全 http://blog.csdn.net/janronehoo/articl...

  • vim 操作命令大全

    1. 关于Vim vim是我最喜欢的编辑器,也是Linux下第二强大的编辑器。 虽然emacs是公认的世界第一,我...

网友评论

      本文标题:ES-ElasticSearch操作命令大全

      本文链接:https://www.haomeiwen.com/subject/ujctbltx.html