elk安装及使用二(elasticsearch的简单使用及插件的安装)

来源:互联网 发布:迅雷会员获取软件 编辑:程序博客网 时间:2024/05/20 22:04

elasticsearch的简单使用及插件的安装

一、elasticsearch的使用

Elasticsearch的操作语法为:

curl -X<VERB> 'PROTOCOL://HOST:PORT/<PATH>?QUERY_STRING' -d '<BODY>'

1elasticsearch的基本操作

(1)Elasticsearch状态信息查看

Elasticsearch查看状态信息的查询语法有很多,常用的查询方法有_cluster, _cat, _search,此处主要演示_cluster和_cat方法的使用。

_cat和_cluster查询方法主要查询当前elk信息及elk集群的信息。

查看_cat的查询方法有哪些

# curl -XGET 'http://10.0.0.11:9200/_cat'=^.^=/_cat/allocation/_cat/shards/_cat/shards/{index}/_cat/master/_cat/nodes/_cat/tasks/_cat/indices/_cat/indices/{index}/_cat/segments/_cat/segments/{index}/_cat/count/_cat/count/{index}/_cat/recovery/_cat/recovery/{index}/_cat/health/_cat/pending_tasks/_cat/aliases/_cat/aliases/{alias}/_cat/thread_pool/_cat/thread_pool/{thread_pools}/_cat/plugins/_cat/fielddata/_cat/fielddata/{fields}/_cat/nodeattrs/_cat/repositories/_cat/snapshots/{repository}/_cat/templates

(2)一些常规的查询操作:

查看节点的状态(查看详细信息只需在后面加”?v”即可,将查询结果按格式显示出来,在后面加“?pretty”即可):

]# curl -XGET 'http://10.0.0.11:9200/_cat/health'  1506306092 22:21:32 myelk green 2 2 42 21 0 0 0 0 - 100.0%~]# curl -XGET 'http://10.0.0.11:9200/_cat/health?pretty' ~]# curl -XGET 'http://10.0.0.11:9200/_cat/health?v'       epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent1506306255 22:24:15  myelk   green           2         2     42  21    0    0        0             0                  -                100.006306189 22:23:09 myelk green 2 2 42 21 0 0 0 0 - 100.0%

(3)集群及节点相关信息的查看:

查看节点:

]# curl -XGET 'http://10.0.0.11:9200/_cat/nodes?v' 

查看集群的健康状况:

~]# curl -XGET 'http://10.0.0.11:9200/_cluster/health?pretty' 

查看集群的状态:

~]# curl -XGET 'http://10.0.0.11:9200/_cluster/stats?pretty'

查看节点的状态信息:

]# curl -XGET 'http://10.0.0.11:9200/_nodes/stats?pretty'

2elasticsearch对文档操作

(1)使用elasticsearch创建文档:

在使用elasticsearch创建文档时,如果不存在索引,在创建文档时,同时会创建索引。常见完成后同时会将文档信息返回出来。

如下,创建索引及文档:

]#  curl -XPUT 'elk1:9200/student/class1/1?pretty' -d ' { "name": "dayi123","age": 17,"courses": "jisuanji wangluo jishu"}' {  "_index" : "student",  "_type" : "class1",  "_id" : "1",  "_version" : 1,  "result" : "created",  "_shards" : {    "total" : 2,    "successful" : 2,    "failed" : 0  },  "created" : true}]#  curl -XPUT 'elk1:9200/student/class1/1?pretty' -d ' { "name": "wo haha","age": 22,"courses": "shu xue"}'                         {  "_index" : "student",  "_type" : "class1",  "_id" : "1",  "_version" : 2,  "result" : "updated",  "_shards" : {    "total" : 2,    "successful" : 2,    "failed" : 0  },  "created" : false}

(2)查询数据

前面已经使用了_cat及__cluster查询方法的使用,此处使用_search查询方法来查询数据。

1)elasticsearch的查询语法:

curl -X GET '<SCHEME://<HOST>:<PORT>/[INDEX/TYPE/]_search?q=KEYWORD&sort=DOMAIN:[asc|desc]&from=#&size=#&_source=DOMAIN_LIST'

可跟参数的及说明:

/_search:搜索所有的索引和类型;

/INDEX_NAME/_search:搜索指定的单个索引;

/INDEX1,INDEX2/_search:搜索指定的多个索引;

/s*/_search:搜索所有以s开头的索引;

/INDEX_NAME/TYPE_NAME/_search:搜索指定的单个索引的指定类型;

2)简单的查询

简单的查询只需在后面接上索引即可进行所需要的查询。如:

]# curl -XGET 'elk1:9200/student/class1/2?pretty' {  "_index" : "student",  "_type" : "class1",  "_id" : "2",  "_version" : 4,  "found" : true,  "_source" : {    "courses" : "gao deng shu xue"  }}

3)带条件的符合查询

文本匹配查询条件查询(如q=KEYWORD, 相当于q=_all:KEYWORD),默认操作符:OR/AND,default_operator, 默认值为OR

如:在student/class1中查询有关键字dayi123的文档:

]# curl -XGET 'elk1:9200/student/class1/_search/?q="dayi123"&pretty'{  "took" : 19,  "timed_out" : false,  "_shards" : {    "total" : 5,    "successful" : 5,    "failed" : 0  },  "hits" : {    "total" : 1,    "max_score" : 0.16203022,    "hits" : [      {        "_index" : "student",        "_type" : "class1",        "_id" : "1",        "_score" : 0.16203022,        "_source" : {          "name" : "dayi123",          "age" : 17,          "courses" : "jisuanji wangluo jishu"        }      }    ]  }}

在查询时还可以指定一些其他的选项,常用的其他查询选项有:

自定义分析器:analyzer=

结果排序:sort=DOMAIN:[asc|desc]

搜索超时:timeout=

查询结果窗口:from=,默认为0size=,默认为10

4)根据字符串查询

如:根据简单字符串查询:

]# curl -XGET 'elk1:9200/student/class1/_search?pretty' -d'{    "query": {      "simple_query_string": {           "query": "dayi123 OR gao deng",         "fields": ["course"]       }     }}'{  "took" : 96,  "timed_out" : false,  "_shards" : {    "total" : 5,    "successful" : 5,    "failed" : 0  },  "hits" : {    "total" : 0,    "max_score" : null,    "hits" : [ ]  }}
根据查询字符串查询:

]# curl -XGET 'elk1:9200/student/class1/_search?pretty' -d'{   "query": {      "query_string": {        "fields": ["age"],          "query": "[10 TO 30]"         }     }}'{  "took" : 113,  "timed_out" : false,  "_shards" : {    "total" : 5,    "successful" : 5,    "failed" : 0  },  "hits" : {    "total" : 1,    "max_score" : 1.0,    "hits" : [      {        "_index" : "student",        "_type" : "class1",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "name" : "dayi123",          "age" : 17,          "courses" : "jisuanji wangluo jishu"        }      }    ]  }}

    更多的_search查询操作可以查看官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search.html

(3)更新文档

更新文档时使用POST,使用PUT时会将原有的文档覆盖。

]#  curl -XPOST 'elk1:9200/student/class1/2?pretty' -d ' { "courses": "gao deng shu xue"}'                            {  "_index" : "student",  "_type" : "class1",  "_id" : "2",  "_version" : 4,  "result" : "updated",  "_shards" : {    "total" : 2,    "successful" : 2,    "failed" : 0  },  "created" : false}

(4)删除文档

删除时可以直接使用-XDELETE删除文档以及索引

删除文档:

]#  curl -XDELETE 'elk1:9200/student/class1/2'  

删除索引:

]#  curl -XDELETE 'elk1:9200/student'

二、elasticsearch插件的安装

1、elasticsearch插件安装方法

在安装完elasticsearch后会生成“/usr/share/elasticsearch/bin/elasticsearch-plugin”文件,而elasticsearch-plugin只要用来进行插件的安装,具体的用法可通过-h参数查看,具体用法如下:

[root@elk1 ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin -hA tool for managing installed elasticsearch pluginsCommands--------list - Lists installed elasticsearch pluginsinstall - Install a pluginremove - removes a plugin from ElasticsearchNon-option arguments:command              Option         Description        ------         -----------        -h, --help     show help          -s, --silent   show minimal output-v, --verbose  show verbose output

如果安装本地插件,可通过加入参数"-f file:///插件全路径"来进行插件的安装。

2、elasticsearch-head插件的安装

在elasticsearch5版本中的head插件已经不通过elasticsearch-plugin 来进行安装,已经成为了一个独立的服务,需要单独进行安装,(安装方法可参考GitHub网站:https://github.com/mobz/elasticsearch-head)

(1)安装前准备工作

Elasticsearch-head插件我们通过git下载,通过npm的安装运行,所以在安装前需要安装git及npm软件包。

npm包存在于epel yum仓库中,所以在安装前先要安装epelyum仓库

]# yum install epel-release -y]# yum install git -y]# yum install npm -y

安装elasticsearch-head插件前要安装grunt,否则在运行elasticsearch时会报错。

]# npm install -g grunt-cli]# npm install grunt --save-dev 

(2)安装elasticsearch-head插件

]# cd /usr/local]# git clone git://github.com/mobz/elasticsearch-head.git

  安装elasticsearch插件时,需要在线安装,由于默认使用的是国外站点,由于站点不未定及网速慢,下载耗时,所以,我们此处使用淘宝站点进行安装。

]# cd elasticsearch-head]# npm install -g cnpm --registry=https://registry.npm.taobao.org

  安装完成后需要启动elasticsearch-head服务,默认启动时在前台运行的,此处我们将他放在后台运行。

]# nohup npm run start &

     Elasticsearch 是以服务的方式运行的,启动后会占用9100端口,我们通过查看服务端口查看服务启动是否成功。需要注意的是,该服务的启动程序只能在”/usr/local/elasticsearch-head”目录下运行。

]# ss -tanlState      Recv-Q Send-Q      Local Address:Port        Peer Address:Port              LISTEN     0      128                     *:9100                     *:*                  LISTEN     0      128                     *:22                       *:*                  LISTEN     0      100             127.0.0.1:25                      *:*                  LISTEN     0      128           ::ffff:10.0.0.12:9200             :::*                  LISTEN     0      128           ::ffff:10.0.0.12:9300             :::*     

(3)配置elasticsearch使支持elasticsearch-head服务。

安装完成后需要修改elasticsearch的配置文件,使elasticsearch支持elasticsearch-head插件,这样,才能使elasticsearch-head服务正常使用,需要在elasticsearch的配置文件中增加如下内容:

http.cors.enabled: truehttp.cors.allow-origin: "*"

修改完elasticsearch后需要重启elasticsearch。

]# systemctl restart elasticsearch.service

(4)elasticsearch的使用

    重启完elasticsearch后elasticsearch-head服务就可以正常使用呢,可以通过浏览器来查看管理elasticsearch。登录地址为:http://elsserverIP:9100

   通过网页打开后,在如下图一所示的输入框中,输入elasticsearch的地址(任何一台节点均可),点击连接,即可连接到elasticsearch。连接成功后,会显示出elk的集群名称,集群的健康值会变为gree。


图一 连接elk

连接成功elk后可以以网页的形式做查询操作,如图二,在复合查询中,在搜索栏输入查询条件,在左侧输入查询方式,点击提交请求即可进行查询。



图二  在网页上进行查询操作