Elasticsearch 学习笔记——2.es 的简单命令操做

1. 导入数据

首先,咱们须要一些数据来支持咱们的操做,这里我采用的是使用 filebeat 来采集数据到 es ,filebeat 也是 elastic 系列的产品,专门用来收集日志文件,使用十分的简单,在官网(下载地址)下载安装包解压,而后修改一下配置文件 filebeat.yml,具体的修改以下:数据库

图片描述

将 enabled 修改成 true,表示启用导入配置,而后在路径 paths 那里配置日志文件的地址。json

图片描述

而后修改输出设置,将数据输出到 es 中,这里须要配置 elasticsearch 的地址,我这里是 192.168.66.135:9201,你配置成本身在 es 的配置文件中设置的 ip 和 port 就好了。app

启动 filebeat 的命令:./filebeat -e -c filebeat.yml -d "publish" ,这里须要确保 es 也是启动的状态,而后 filebeat 会链接到 es,将数据输出。curl

完成后,使用命令 curl -X GET "192.168.66.135:9201/_cat/indices?v" 查看建立的 index 状况:elasticsearch

图片描述

2. 简单的命令操做

在 elasticsearch 中,数据是以文档的形式存放的,这也是它能支持全文本搜索的缘由,有三个比较关键的概念须要理解一下,首先是 index,这个至关于关系型数据库中的一个数据库。还有 Type ,表示数据的分类,相似于数据库中的一个 table,es 的数据是以 json 格式表示的,每一条数据叫作 document。url

只不过根据规划,es 6.x 版只容许每一个 Index 包含一个 Type,7.x 版将会完全移除 Type。spa

有了数据以后,接下来介绍一些查询数据的经常使用命令:rest

1. 数据 日志

1.插入数据:code

curl -X PUT "192.168.66.135:9200/megacorp/employee/1" -H 'Content-Type: application/json' -d'
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
'

上面的命令,在 index 为 megacorp,而且 Type 为 employee 下面建立了一条数据,id 是 1,若是不指定 id 的话,es 会自动生成。

注意:若是 es 中没有命令中的 index ,它会自动建立,因此,插入数据的时候,须要注意 index 名称的正确性。

2.更新数据,仍是使用上面的命令,从新 PUT 便可。

3.删除数据,使用 DELETE 命令,例如删除上面插入的数据:curl -X DELETE "192.168.66.135:9200/megacorp/employee/1"

2. 索引

4.查看 es 中全部的 index 状况:curl -X GET "192.168.66.128:9200/_cat/indices?v"

5.删除 index :curl -X DELETE '192.168.66.135:9200/megacorp' ,删除名为 megacorp 的 index。

3. 搜索

6.查看 es 中的全部数据:http://192.168.66.128:9200/_search ,这条命令还能够加上一些其余的条件,例如:

  • 查看 megacorp 索引下的全部数据:http://192.168.66.128:9200/megacorp/_search
  • 查看 megacorp1, megacorp2 两个 索引下的全部数据:http://192.168.66.128:9200/megacorp1,megacorp2/_search
  • 查看 megacorp 索引下的 employee 类型的数据:http://192.168.66.128:9200/megacorp/employee/_search
  • 在全部的索引中搜索 user1 和 user2 类型的数据:http://192.168.66.128:9200/_all/user1,user2/_search

7.简单搜索:例如要查找 source 下面 message 这一列的数据中,包含 了某个字符串(这里以 connect 为例)的全部记录:http://192.168.66.135:9201/filebeat-6.5.4-2019.04.06/_search?q=message:connect

8.全文搜索,可实现上面这种包含某个字符串的搜索,搜索的文本以下:

curl -X GET "192.168.66.135:9200/megacor/search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "message" : "connect"
        }
    }
}
'

9.搜索分页:

  • 从搜索的结果中选取前 50 条记录:http://192.168.66.128:9200/megacorp/_search?size=50
  • 从第 3 页中选取 10 条记录:http://192.168.66.128:9200/megacorp/_search?size=10&from=3

OK,今天就暂时介绍这么多了,后面再继续写。