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

一、elasticsearch的使用

Elasticsearch的操作语法为:

[plain] view plain copy
  1. 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的查询方法有哪些

[plain] view plain copy
  1. # curl -XGET 'http://10.0.0.11:9200/_cat'  
  2. =^.^=  
  3. /_cat/allocation  
  4. /_cat/shards  
  5. /_cat/shards/{index}  
  6. /_cat/master  
  7. /_cat/nodes  
  8. /_cat/tasks  
  9. /_cat/indices  
  10. /_cat/indices/{index}  
  11. /_cat/segments  
  12. /_cat/segments/{index}  
  13. /_cat/count  
  14. /_cat/count/{index}  
  15. /_cat/recovery  
  16. /_cat/recovery/{index}  
  17. /_cat/health  
  18. /_cat/pending_tasks  
  19. /_cat/aliases  
  20. /_cat/aliases/{alias}  
  21. /_cat/thread_pool  
  22. /_cat/thread_pool/{thread_pools}  
  23. /_cat/plugins  
  24. /_cat/fielddata  
  25. /_cat/fielddata/{fields}  
  26. /_cat/nodeattrs  
  27. /_cat/repositories  
  28. /_cat/snapshots/{repository}  
  29. /_cat/templates  

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

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

[plain] view plain copy
  1. ]# curl -XGET 'http://10.0.0.11:9200/_cat/health'    
  2. 1506306092 22:21:32 myelk green 2 2 42 21 0 0 0 0 - 100.0%  
  3. ~]# curl -XGET 'http://10.0.0.11:9200/_cat/health?pretty'   
  4. ~]# curl -XGET 'http://10.0.0.11:9200/_cat/health?v'         
  5. epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent  
  6. 1506306255 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)集群及节点相关信息的查看:

查看节点:

[plain] view plain copy
  1. ]# curl -XGET 'http://10.0.0.11:9200/_cat/nodes?v'   

查看集群的健康状况:

[plain] view plain copy
  1. ~]# curl -XGET 'http://10.0.0.11:9200/_cluster/health?pretty'   

查看集群的状态:

[plain] view plain copy
  1. ~]# curl -XGET 'http://10.0.0.11:9200/_cluster/stats?pretty'  

查看节点的状态信息:

[plain] view plain copy
  1. ]# curl -XGET 'http://10.0.0.11:9200/_nodes/stats?pretty'  

2elasticsearch对文档操作

(1)使用elasticsearch创建文档:

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

如下,创建索引及文档:

[plain] view plain copy
  1. ]#  curl -XPUT 'elk1:9200/student/class1/1?pretty' -d '  
  2.  { "name": "dayi123","age": 17,"courses": "jisuanji wangluo jishu"}'   
  3. {  
  4.   "_index" : "student",  
  5.   "_type" : "class1",  
  6.   "_id" : "1",  
  7.   "_version" : 1,  
  8.   "result" : "created",  
  9.   "_shards" : {  
  10.     "total" : 2,  
  11.     "successful" : 2,  
  12.     "failed" : 0  
  13.   },  
  14.   "created" : true  
  15. }  
  16. ]#  curl -XPUT 'elk1:9200/student/class1/1?pretty' -d '  
  17.  { "name": "wo haha","age": 22,"courses": "shu xue"}'                           
  18. {  
  19.   "_index" : "student",  
  20.   "_type" : "class1",  
  21.   "_id" : "1",  
  22.   "_version" : 2,  
  23.   "result" : "updated",  
  24.   "_shards" : {  
  25.     "total" : 2,  
  26.     "successful" : 2,  
  27.     "failed" : 0  
  28.   },  
  29.   "created" : false  
  30. }  

(2)查询数据

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

1)elasticsearch的查询语法:

[plain] view plain copy
  1. 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)简单的查询

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

[plain] view plain copy
  1. ]# curl -XGET 'elk1:9200/student/class1/2?pretty'   
  2. {  
  3.   "_index" : "student",  
  4.   "_type" : "class1",  
  5.   "_id" : "2",  
  6.   "_version" : 4,  
  7.   "found" : true,  
  8.   "_source" : {  
  9.     "courses" : "gao deng shu xue"  
  10.   }  
  11. }  

3)带条件的符合查询

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

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

[plain] view plain copy
  1. ]# curl -XGET 'elk1:9200/student/class1/_search/?q="dayi123"&pretty'  
  2. {  
  3.   "took" : 19,  
  4.   "timed_out" : false,  
  5.   "_shards" : {  
  6.     "total" : 5,  
  7.     "successful" : 5,  
  8.     "failed" : 0  
  9.   },  
  10.   "hits" : {  
  11.     "total" : 1,  
  12.     "max_score" : 0.16203022,  
  13.     "hits" : [  
  14.       {  
  15.         "_index" : "student",  
  16.         "_type" : "class1",  
  17.         "_id" : "1",  
  18.         "_score" : 0.16203022,  
  19.         "_source" : {  
  20.           "name" : "dayi123",  
  21.           "age" : 17,  
  22.           "courses" : "jisuanji wangluo jishu"  
  23.         }  
  24.       }  
  25.     ]  
  26.   }  
  27. }  

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

自定义分析器:analyzer=

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

搜索超时:timeout=

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

4)根据字符串查询

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

[plain] view plain copy
  1. ]# curl -XGET 'elk1:9200/student/class1/_search?pretty' -d'  
  2. {  
  3.     "query": {  
  4.       "simple_query_string": {    
  5.          "query": "dayi123 OR gao deng",  
  6.          "fields": ["course"]  
  7.        }  
  8.      }  
  9. }'  
  10. {  
  11.   "took" : 96,  
  12.   "timed_out" : false,  
  13.   "_shards" : {  
  14.     "total" : 5,  
  15.     "successful" : 5,  
  16.     "failed" : 0  
  17.   },  
  18.   "hits" : {  
  19.     "total" : 0,  
  20.     "max_score" : null,  
  21.     "hits" : [ ]  
  22.   }  
  23. }  
根据查询字符串查询:

[plain] view plain copy
  1. ]# curl -XGET 'elk1:9200/student/class1/_search?pretty' -d'  
  2. {  
  3.    "query": {  
  4.       "query_string": {  
  5.         "fields": ["age"],  
  6.           "query": "[10 TO 30]"    
  7.        }  
  8.      }  
  9. }'  
  10. {  
  11.   "took" : 113,  
  12.   "timed_out" : false,  
  13.   "_shards" : {  
  14.     "total" : 5,  
  15.     "successful" : 5,  
  16.     "failed" : 0  
  17.   },  
  18.   "hits" : {  
  19.     "total" : 1,  
  20.     "max_score" : 1.0,  
  21.     "hits" : [  
  22.       {  
  23.         "_index" : "student",  
  24.         "_type" : "class1",  
  25.         "_id" : "1",  
  26.         "_score" : 1.0,  
  27.         "_source" : {  
  28.           "name" : "dayi123",  
  29.           "age" : 17,  
  30.           "courses" : "jisuanji wangluo jishu"  
  31.         }  
  32.       }  
  33.     ]  
  34.   }  
  35. }  

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

(3)更新文档

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

[plain] view plain copy
  1. ]#  curl -XPOST 'elk1:9200/student/class1/2?pretty' -d '  
  2.  { "courses": "gao deng shu xue"}'                              
  3. {  
  4.   "_index" : "student",  
  5.   "_type" : "class1",  
  6.   "_id" : "2",  
  7.   "_version" : 4,  
  8.   "result" : "updated",  
  9.   "_shards" : {  
  10.     "total" : 2,  
  11.     "successful" : 2,  
  12.     "failed" : 0  
  13.   },  
  14.   "created" : false  
  15. }  

(4)删除文档

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

删除文档:

[plain] view plain copy
  1. ]#  curl -XDELETE 'elk1:9200/student/class1/2'    

删除索引:

[plain] view plain copy
  1. ]#  curl -XDELETE 'elk1:9200/student'  

二、elasticsearch插件的安装

1、elasticsearch插件安装方法

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

[plain] view plain copy
  1. [[email protected] ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin -h  
  2. A tool for managing installed elasticsearch plugins  
  3.   
  4. Commands  
  5. --------  
  6. list - Lists installed elasticsearch plugins  
  7. install - Install a plugin  
  8. remove - removes a plugin from Elasticsearch  
  9.   
  10. Non-option arguments:  
  11. command                
  12.   
  13. Option         Description          
  14. ------         -----------          
  15. -h, --help     show help            
  16. -s, --silent   show minimal output  
  17. -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仓库

[plain] view plain copy
  1. ]# yum install epel-release -y  
  2. ]# yum install git -y  
  3. ]# yum install npm -y  

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

[plain] view plain copy
  1. ]# npm install -g grunt-cli  
  2. ]# npm install grunt --save-dev   

(2)安装elasticsearch-head插件

[plain] view plain copy
  1. ]# cd /usr/local  
  2. ]# git clone git://github.com/mobz/elasticsearch-head.git  

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

[plain] view plain copy
  1. ]# cd elasticsearch-head  
  2. ]# npm install -g cnpm --registry=https://registry.npm.taobao.org  

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

[plain] view plain copy
  1. ]# nohup npm run start &  

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

[plain] view plain copy
  1. ]# ss -tanl  
  2. State      Recv-Q Send-Q      Local Address:Port        Peer Address:Port                
  3. LISTEN     0      128                     *:9100                     *:*                    
  4. LISTEN     0      128                     *:22                       *:*                    
  5. LISTEN     0      100             127.0.0.1:25                      *:*                    
  6. LISTEN     0      128           ::ffff:10.0.0.12:9200             :::*                    
  7. LISTEN     0      128           ::ffff:10.0.0.12:9300             :::*       

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

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

[plain] view plain copy
  1. http.cors.enabled: true  
  2. http.cors.allow-origin: "*"  

修改完elasticsearch后需要重启elasticsearch。

[plain] view plain copy
  1. ]# systemctl restart elasticsearch.service  

(4)elasticsearch的使用

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

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

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

图一 连接elk

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

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

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




转载至https://blog.csdn.net/dayi_123/article/details/78161060



相关文章: