EFK家族---CentOS7安装Elasticsearch6.4.3和使用

    • *

转载请注明出处:EFK家族—-CentOS7安装Elasticsearch6.4.3和使用

我们在上篇文章中已经了解了Elasticsearch
EFK家族—-Elasticsearch介绍
本章我们来学习安装和使用。

安装

Elastic 需要 Java 8 环境。如果你的机器还没安装 Java,可以参考这篇文章,注意要保证环境变量JAVA_HOME正确设置。
linux软件(一)—-CentOS安装jdk---CentOS安装jdk")

安装完 Java,就可以跟着官方文档安装 Elastic。
直接下载压缩包比较简单。

  1. $ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.3.zip
  2. $ unzip elasticsearch-6.4.3.zip
  3. $ cd elasticsearch-6.4.3/

接着,进入解压后的目录,运行下面的命令,启动 Elastic。

  1. $ ./bin/elasticsearch
  2. $./bin/elasticsearch -d //后端启动

可能遇到的错误-max virtual memory

如果这时报错”max virtual memory areas vm.maxmapcount [65530] is too low”,要运行下面的命令。

  1. $ sudo sysctl -w vm.max_map_count=262144

可能遇到的错误-can not run elasticsearch as root

这是出于系统安全考虑设置的条件。由于ElasticSearch可以接收用户输入的脚本并且执行,为了系统安全考虑,
建议创建一个单独的用户用来运行ElasticSearch
我们需要添加用户。

  1. adduser es //添加用户es
  2. passwd es //给用户es密码赋值

添加完用户之后:
用root用户执行 :

  1. chown -R 用户名 文件夹名
  2. chown -R es /data/elasticsearch-6.4.3

将这几个压缩包所在的文件夹及解压完的文件夹权限给你新建的用户。之后再使用新用户启动就OK了。

  1. su es
  2. ./bin/elasticsearch

可能遇到问题-max file descriptors

max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

切换到root用户

  1. su root
  2. ulimit -Hn //查看硬限制

解决方法

  1. vim /etc/security/limits.conf

添加下面设置 es是启动elasticsearch的用户

  1. es soft nofile 65536
  2. es hard nofile 65536

退出用户重新登录,使配置生效

  1. su es

重新

  1. ulimit -Hn

查看硬限制 会发现数值有4096改成65535

如果启动一切正常,Elastic 就会在默认的9200端口运行。这时,打开另一个命令行窗口,请求该端口,会得到说明信息。

  1. $ curl localhost:9200
  2. {
  3. "name" : "00p5sY4",
  4. "cluster_name" : "elasticsearch",
  5. "cluster_uuid" : "2MjaUbyiQrW1D9PRCCEg-Q",
  6. "version" : {
  7. "number" : "6.4.3",
  8. "build_flavor" : "default",
  9. "build_type" : "zip",
  10. "build_hash" : "fe40335",
  11. "build_date" : "2018-10-30T23:17:19.084789Z",
  12. "build_snapshot" : false,
  13. "lucene_version" : "7.4.0",
  14. "minimum_wire_compatibility_version" : "5.6.0",
  15. "minimum_index_compatibility_version" : "5.0.0"
  16. },
  17. "tagline" : "You Know, for Search"
  18. }

上面代码中,请求9200端口,Elastic 返回一个 JSON 对象,包含当前节点、集群、版本等信息。

按下 Ctrl + C,Elastic 就会停止运行。

默认情况下,Elastic 只允许本机访问,如果需要远程访问,可以修改 Elastic 安装目录的config/elasticsearch.yml文件,去掉network.host的注释,将它的值改成0.0.0.0,然后重新启动 Elastic。

  1. network.host: 0.0.0.0

上面代码中,设成0.0.0.0让任何人都可以访问。线上服务不要这样设置,要设成具体的 IP。

中文分词设置

首先,安装中文分词插件。这里使用的是 ik,也可以考虑其他插件(比如 smartcn)。

  1. $ su root
  2. $ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.3/elasticsearch-analysis-ik-6.4.3.zip

上面代码安装的是6.4.3版的插件,与 Elastic 6.4.3 配合使用。

接着,重新启动 Elastic,就会自动加载这个新安装的插件。

然后,新建一个 Index,指定需要分词的字段。这一步根据数据结构而异,下面的命令只针对本文。基本上,凡是需要搜索的中文字段,都要单独设置一下。

  1. $ curl -X PUT 'localhost:9200/accounts' -d '
  2. {
  3. "mappings": {
  4. "person": {
  5. "properties": {
  6. "user": {
  7. "type": "text",
  8. "analyzer": "ik_max_word",
  9. "search_analyzer": "ik_max_word"
  10. },
  11. "title": {
  12. "type": "text",
  13. "analyzer": "ik_max_word",
  14. "search_analyzer": "ik_max_word"
  15. },
  16. "desc": {
  17. "type": "text",
  18. "analyzer": "ik_max_word",
  19. "search_analyzer": "ik_max_word"
  20. }
  21. }
  22. }
  23. }
  24. }'

执行成功返回

  1. {"acknowledged":true,"shards_acknowledged":true,"index":"accounts"}

上面代码中,首先新建一个名称为accounts的 Index,里面有一个名称为person的 Type。person有三个字段。

  1. user
  2. title
  3. desc

这三个字段都是中文,而且类型都是文本(text),所以需要指定中文分词器,不能使用默认的英文分词器。

Elastic 的分词器称为 analyzer。我们对每个字段指定分词器。

  1. "user": {
  2. "type": "text",
  3. "analyzer": "ik_max_word",
  4. "search_analyzer": "ik_max_word"
  5. }

上面代码中,analyzer是字段文本的分词器,search_analyzer是搜索词的分词器。ik_max_word分词器是插件ik提供的,可以对文本进行最大数量的分词。

可能遇到的问题-application/x-www-form-urlencoded is not supported

{“error”:”Content-Type header [application/x-www-form-urlencoded] is not supported”,”status”:406}

原因
elasticsearch6.x版本需要指定明确的Content-Type,详情参考:
官方解释

解决方法

curl加上-H “Content-Type: application/json”参数
如下:

  1. curl -H "Content-Type: application/json" -X PUT 'localhost:9200/accounts' -d '
  2. {
  3. "mappings": {
  4. "person": {
  5. "properties": {
  6. "user": {
  7. "type": "text",
  8. "analyzer": "ik_max_word",
  9. "search_analyzer": "ik_max_word"
  10. },
  11. "title": {
  12. "type": "text",
  13. "analyzer": "ik_max_word",
  14. "search_analyzer": "ik_max_word"
  15. },
  16. "desc": {
  17. "type": "text",
  18. "analyzer": "ik_max_word",
  19. "search_analyzer": "ik_max_word"
  20. }
  21. }
  22. }
  23. }
  24. }'

使用Elasticsearch数据操作

新增记录

向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向/accounts/person发送请求,就可以新增一条人员记录。

  1. $ curl -H "Content-Type: application/json" -X PUT 'localhost:9200/accounts/person/1' -d '
  2. {
  3. "user": "张三",
  4. "title": "工程师",
  5. "desc": "数据库管理"
  6. }'

服务器返回的 JSON 对象,会给出 Index、Type、Id、Version 等信息。

  1. {
  2. "_index":"accounts",
  3. "_type":"person",
  4. "_id":"1",
  5. "_version":1,
  6. "result":"created",
  7. "_shards":{"total":2,"successful":1,"failed":0},
  8. "_seq_no":0,"_primary_term":1
  9. }

如果你仔细看,会发现请求路径是/accounts/person/1,最后的1是该条记录的 Id。它不一定是数字,任意字符串(比如abc)都可以。

新增记录的时候,也可以不指定 Id,这时要改成 POST 请求。

  1. $ curl -H "Content-Type: application/json" -X POST 'localhost:9200/accounts/person' -d '
  2. {
  3. "user": "李四",
  4. "title": "工程师",
  5. "desc": "系统管理"
  6. }'

上面代码中,向/accounts/person发出一个 POST 请求,添加一个记录。这时,服务器返回的 JSON 对象里面,_id字段就是一个随机字符串。

  1. {
  2. "_index":"accounts",
  3. "_type":"person",
  4. "_id":"AV3qGfrC6jMbsbXb6k1p",
  5. "_version":1,
  6. "result":"created",
  7. "_shards":{"total":2,"successful":1,"failed":0},
  8. "created":true
  9. }

注意,如果没有先创建 Index(这个例子是accounts),直接执行上面的命令,Elastic 也不会报错,而是直接生成指定的 Index。所以,打字的时候要小心,不要写错 Index 的名称。

查看记录

向/Index/Type/Id发出 GET 请求,就可以查看这条记录。

  1. $ curl 'localhost:9200/accounts/person/1?pretty=true'

上面代码请求查看/accounts/person/1这条记录,URL 的参数pretty=true表示以易读的格式返回。

返回的数据中,found字段表示查询成功,_source字段返回原始记录。

  1. {
  2. "_index" : "accounts",
  3. "_type" : "person",
  4. "_id" : "1",
  5. "_version" : 1,
  6. "found" : true,
  7. "_source" : {
  8. "user" : "张三",
  9. "title" : "工程师",
  10. "desc" : "数据库管理"
  11. }
  12. }

如果 Id 不正确,就查不到数据,found字段就是false。

  1. $ curl 'localhost:9200/weather/beijing/abc?pretty=true'
  2. {
  3. "_index" : "accounts",
  4. "_type" : "person",
  5. "_id" : "abc",
  6. "found" : false
  7. }

更新记录

更新记录就是使用 PUT 请求,重新发送一次数据。

  1. $ curl -H "Content-Type: application/json" -X PUT 'localhost:9200/accounts/person/1' -d '
  2. {
  3. "user" : "张三",
  4. "title" : "工程师",
  5. "desc" : "数据库管理,软件开发"
  6. }'

输出如下:

  1. {
  2. "_index":"accounts",
  3. "_type":"person",
  4. "_id":"1",
  5. "_version":2,
  6. "result":"updated",
  7. "_shards":{"total":2,"successful":1,"failed":0},
  8. "created":false
  9. }

上面代码中,我们将原始数据从”数据库管理”改成”数据库管理,软件开发”。 返回结果里面,有几个字段发生了变化。

  1. "_version" : 2,
  2. "result" : "updated",
  3. "created" : false

可以看到,记录的 Id 没变,但是版本(version)从1变成2,操作类型(result)从created变成updated,created字段变成false,因为这次不是新建记录。

返回所有记录

使用 GET 方法,直接请求/Index/Type/_search,就会返回所有记录。

  1. $ curl 'localhost:9200/accounts/person/_search'
  2. {
  3. "took":2,
  4. "timed_out":false,
  5. "_shards":{"total":5,"successful":5,"failed":0},
  6. "hits":{
  7. "total":2,
  8. "max_score":1.0,
  9. "hits":[
  10. {
  11. "_index":"accounts",
  12. "_type":"person",
  13. "_id":"AV3qGfrC6jMbsbXb6k1p",
  14. "_score":1.0,
  15. "_source": {
  16. "user": "李四",
  17. "title": "工程师",
  18. "desc": "系统管理"
  19. }
  20. },
  21. {
  22. "_index":"accounts",
  23. "_type":"person",
  24. "_id":"1",
  25. "_score":1.0,
  26. "_source": {
  27. "user" : "张三",
  28. "title" : "工程师",
  29. "desc" : "数据库管理,软件开发"
  30. }
  31. }
  32. ]
  33. }
  34. }

上面代码中,返回结果的 took字段表示该操作的耗时(单位为毫秒),timed_out字段表示是否超时,hits字段表示命中的记录,里面子字段的含义如下。

total:返回记录数,本例是2条。
max_score:最高的匹配程度,本例是1.0。
hits:返回的记录组成的数组。
返回的记录中,每条记录都有一个_score字段,表示匹配的程序,默认是按照这个字段降序排列。

全文搜索

Elastic 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。

  1. $ curl -H "Content-Type: application/json" 'localhost:9200/accounts/person/_search' -d '
  2. {
  3. "query" : { "match" : { "desc" : "软件" }}
  4. }'

上面代码使用 Match 查询,指定的匹配条件是desc字段里面包含”软件”这个词。返回结果如下。

  1. {
  2. "took":3,
  3. "timed_out":false,
  4. "_shards":{"total":5,"successful":5,"failed":0},
  5. "hits":{
  6. "total":1,
  7. "max_score":0.28582606,
  8. "hits":[
  9. {
  10. "_index":"accounts",
  11. "_type":"person",
  12. "_id":"1",
  13. "_score":0.28582606,
  14. "_source": {
  15. "user" : "张三",
  16. "title" : "工程师",
  17. "desc" : "数据库管理,软件开发"
  18. }
  19. }
  20. ]
  21. }
  22. }

Elastic 默认一次返回10条结果,可以通过size字段改变这个设置。

  1. $ curl -H "Content-Type: application/json" 'localhost:9200/accounts/person/_search' -d '
  2. {
  3. "query" : { "match" : { "desc" : "管理" }},
  4. "size": 1
  5. }'

上面代码指定,每次只返回一条结果。

还可以通过from字段,指定位移。

  1. $ curl -H "Content-Type: application/json" 'localhost:9200/accounts/person/_search' -d '
  2. {
  3. "query" : { "match" : { "desc" : "管理" }},
  4. "from": 1,
  5. "size": 1
  6. }'

上面代码指定,从位置1开始(默认是从位置0开始),只返回一条结果。

逻辑运算

如果有多个搜索关键字, Elastic 认为它们是or关系。

  1. $ curl -H "Content-Type: application/json" 'localhost:9200/accounts/person/_search' -d '
  2. {
  3. "query" : { "match" : { "desc" : "软件 系统" }}
  4. }'

上面代码搜索的是软件 or 系统。

如果要执行多个关键词的and搜索,必须使用布尔查询。

  1. $ curl -H "Content-Type: application/json" 'localhost:9200/accounts/person/_search' -d '
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. { "match": { "desc": "软件" } },
  7. { "match": { "desc": "管理" } }
  8. ]
  9. }
  10. }
  11. }'

删除记录

删除记录就是发出 DELETE 请求。

  1. $ curl -X DELETE 'localhost:9200/accounts/person/1'

参考链接

ElasticSearch 官方手册
A Practical Introduction to Elasticsearch

转载请注明出处:EFK家族—-CentOS7安装Elasticsearch6.4.3和使用


Original url: Access
Created at: 2018-11-15 17:23:45
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论