经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Elasticsearch » 查看文章
Centos7安装ElasticSearch 6.4.1入门教程详解
来源:jb51  时间:2019/5/17 8:29:26  对本文有异议

1.下载ElasticSearch 6.4.1安装包 下载地址:
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.1.tar.gz

2.解压压缩包

  1. [root@localhost ElasticSearch]# tar -zxvf elasticsearch-6.4.1.tar.gz

3.启动ElasticSearch

  1. [root@localhost bin]# ./elasticsearch

以后台方式启动

  1. [root@localhost bin]# ./elasticsearch -d

TIPS:

  1. [root@localhost bin]# ./elasticsearch
  2. [2018-09-19T19:46:09,817][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
  3. org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
  4. at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:140) ~[elasticsearch-6.4.1.jar:6.4.1]
  5. at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:127) ~[elasticsearch-6.4.1.jar:6.4.1]
  6. at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.4.1.jar:6.4.1]
  7. at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.4.1.jar:6.4.1]
  8. at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.4.1.jar:6.4.1]
  9. at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:93) ~[elasticsearch-6.4.1.jar:6.4.1]
  10. at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:86) ~[elasticsearch-6.4.1.jar:6.4.1]
  11. Caused by: java.lang.RuntimeException: can not run elasticsearch as root
  12. at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:104) ~[elasticsearch-6.4.1.jar:6.4.1]
  13. at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:171) ~[elasticsearch-6.4.1.jar:6.4.1]
  14. at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:326) ~[elasticsearch-6.4.1.jar:6.4.1]
  15. at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[elasticsearch-6.4.1.jar:6.4.1]

ElasticSearch 不能以root用户角色启动,因此需要将安装目录授权给其他用户,用其他用户来启动

启动成功后,验证,打开新的终端,执行如下命令:

  1. [root@localhost ~]# curl 'http://localhost:9200/?pretty'
  2. {
  3. "name" : "O5BAVYE",
  4. "cluster_name" : "elasticsearch",
  5. "cluster_uuid" : "rw1yjlzkSgODXkUVgIxmxg",
  6. "version" : {
  7. "number" : "6.4.1",
  8. "build_flavor" : "default",
  9. "build_type" : "tar",
  10. "build_hash" : "e36acdb",
  11. "build_date" : "2018-09-13T22:18:07.696808Z",
  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. }
  19. [root@localhost ~]#
  20.  

返回信息则表示安装成功!

4.安装Kibana

Sense 是一个 Kibana 应用 它提供交互式的控制台,通过你的浏览器直接向 Elasticsearch 提交请求。 这本书的在线版本包含有一个 View in Sense 的链接,里面有许多代码示例。当点击的时候,它会打开一个代码示例的Sense控制台。 你不必安装 Sense,但是它允许你在本地的 Elasticsearch 集群上测试示例代码,从而使本书更具有交互性。

下载kibana

Kibana是一个为 ElasticSearch 提供的数据分析的 Web 接口。可使用它对日志进行高效的搜索、可视化、分析等各种操作
https://artifacts.elastic.co/downloads/kibana/kibana-6.4.1-linux-x86_64.tar.gz

下载完成解压Kibana

  1. [root@localhost ElasticSearch]# tar -zxvf kibana-6.4.1-linux-x86_64.tar.gz

修改  配置config目录下的kibana.yml 文件,配置elasticsearch地址和kibana地址信息

  1. server.host: "192.168.92.50" # kibana 服务器地址
  2. elasticsearch.url: "http://192.168.92.50:9200" # ES 地址

启动 Kibana

  1. [root@localhost bin]# ./kibana

安装Kibana本机访问:http://localhost:5601/

选择Dev Tools菜单,即可实现可视化请求

5.安装LogStash

下载logStash
https://artifacts.elastic.co/downloads/logstash/logstash-7.0.1.tar.gz

下载完成解压后,config目录下配置日志收集日志配置文件 logstash.conf

  1. # Sample Logstash configuration for creating a simple
  2. # Beats -> Logstash -> Elasticsearch pipeline.
  3.  
  4. input {
  5. tcp {
  6. mode => "server"
  7. host => "192.168.92.50"
  8. port => 4560
  9. codec => json_lines
  10. }
  11. }
  12. output {
  13. elasticsearch {
  14. hosts => "192.168.92.50:9200"
  15. index => "springboot-logstash-%{+YYYY.MM.dd}"
  16. }
  17. }
  18.  

配置成功后启动logstatsh

  1. [root@localhost bin]# ./logstash -f ../config/logstash.conf

ES  一些基础知识:

索引(名词):

如前所述,一个 索引 类似于传统关系数据库中的一个 数据库 ,是一个存储关系型文档的地方。 索引 (index) 的复数词为 indices 或 indexes 。

索引(动词):

索引一个文档 就是存储一个文档到一个 索引 (名词)中以便它可以被检索和查询到。这非常类似于 SQL 语句中的 INSERT 关键词,除了文档已存在时新文档会替换旧文档情况之外。

倒排索引:

关系型数据库通过增加一个 索引 比如一个 B树(B-tree)索引 到指定的列上,以便提升数据检索速度。Elasticsearch 和 Lucene 使用了一个叫做 倒排索引 的结构来达到相同的目的。

  1. PUT /megacorp/employee/1
  2. {
  3. "first_name" : "John",
  4. "last_name" : "Smith",
  5. "age" : 25,
  6. "about" : "I love to go rock climbing",
  7. "interests": [ "sports", "music" ]
  8. }
  9.  

返回结果:

  1. #! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
  2. {
  3. "_index": "megacorp",
  4. "_type": "employee",
  5. "_id": "1",
  6. "_version": 1,
  7. "result": "created",
  8. "_shards": {
  9. "total": 2,
  10. "successful": 1,
  11. "failed": 0
  12. },
  13. "_seq_no": 0,
  14. "_primary_term": 1
  15. }

路径 /megacorp/employee/1 包含了三部分的信息:

megacorp 索引名称

employee  类型名称

1        特定雇员的ID

放置第二个雇员信息:

  1. {
  2. "_index": "megacorp",
  3. "_type": "employee",
  4. "_id": "2",
  5. "_version": 1,
  6. "result": "created",
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 0,
  13. "_primary_term": 1
  14. }

返回结果:

  1. {
  2. "_index": "megacorp",
  3. "_type": "employee",
  4. "_id": "2",
  5. "_version": 1,
  6. "result": "created",
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 0,
  13. "_primary_term": 1
  14. }

放置第三个雇员信息

  1. {
  2. "_index": "megacorp",
  3. "_type": "employee",
  4. "_id": "3",
  5. "_version": 1,
  6. "result": "created",
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 0,
  13. "_primary_term": 1
  14. }

5.检索文档

检索到单个雇员的数据

GET /megacorp/employee/1

返回结果:

  1. {
  2. "_index": "megacorp",
  3. "_type": "employee",
  4. "_id": "1",
  5. "_version": 1,
  6. "found": true,
  7. "_source": {
  8. "first_name": "John",
  9. "last_name": "Smith",
  10. "age": 25,
  11. "about": "I love to go rock climbing",
  12. "interests": [
  13. "sports",
  14. "music"
  15. ]
  16. }
  17. }

6.轻量搜索

一个 GET 是相当简单的,可以直接得到指定的文档。 现在尝试点儿稍微高级的功能,比如一个简单的搜索!

第一个尝试的几乎是最简单的搜索了。我们使用下列请求来搜索所有雇员:

GET /megacorp/employee/_search

返回结果:

  1. {
  2. "took": 31,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 3,
  12. "max_score": 1,
  13. "hits": [
  14. {
  15. "_index": "megacorp",
  16. "_type": "employee",
  17. "_id": "2",
  18. "_score": 1,
  19. "_source": {
  20. "first_name": "Jane",
  21. "last_name": "Smith",
  22. "age": 32,
  23. "about": "I like to collect rock albums",
  24. "interests": [
  25. "music"
  26. ]
  27. }
  28. },
  29. {
  30. "_index": "megacorp",
  31. "_type": "employee",
  32. "_id": "1",
  33. "_score": 1,
  34. "_source": {
  35. "first_name": "John",
  36. "last_name": "Smith",
  37. "age": 25,
  38. "about": "I love to go rock climbing",
  39. "interests": [
  40. "sports",
  41. "music"
  42. ]
  43. }
  44. },
  45. {
  46. "_index": "megacorp",
  47. "_type": "employee",
  48. "_id": "3",
  49. "_score": 1,
  50. "_source": {
  51. "first_name": "Douglas",
  52. "last_name": "Fir",
  53. "age": 35,
  54. "about": "I like to build cabinets",
  55. "interests": [
  56. "forestry"
  57. ]
  58. }
  59. }
  60. ]
  61. }
  62. }

通过姓名模糊匹配来获得结果

GET /megacorp/employee/_search?q=last_name:Smith

返回结果:

  1. {
  2. "took": 414,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 2,
  12. "max_score": 0.2876821,
  13. "hits": [
  14. {
  15. "_index": "megacorp",
  16. "_type": "employee",
  17. "_id": "2",
  18. "_score": 0.2876821,
  19. "_source": {
  20. "first_name": "Jane",
  21. "last_name": "Smith",
  22. "age": 32,
  23. "about": "I like to collect rock albums",
  24. "interests": [
  25. "music"
  26. ]
  27. }
  28. },
  29. {
  30. "_index": "megacorp",
  31. "_type": "employee",
  32. "_id": "1",
  33. "_score": 0.2876821,
  34. "_source": {
  35. "first_name": "John",
  36. "last_name": "Smith",
  37. "age": 25,
  38. "about": "I love to go rock climbing",
  39. "interests": [
  40. "sports",
  41. "music"
  42. ]
  43. }
  44. }
  45. ]
  46. }
  47. }

7.使用查询表达式搜索

领域特定语言 (DSL), 指定了使用一个 JSON 请求

  1. GET /megacorp/employee/_search
  2. {
  3. "query" : {
  4. "match" : {
  5. "last_name" : "Smith"
  6. }
  7. }
  8. }
  9.  

返回结果:

  1. {
  2. "took": 7,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 2,
  12. "max_score": 0.2876821,
  13. "hits": [
  14. {
  15. "_index": "megacorp",
  16. "_type": "employee",
  17. "_id": "2",
  18. "_score": 0.2876821,
  19. "_source": {
  20. "first_name": "Jane",
  21. "last_name": "Smith",
  22. "age": 32,
  23. "about": "I like to collect rock albums",
  24. "interests": [
  25. "music"
  26. ]
  27. }
  28. },
  29. {
  30. "_index": "megacorp",
  31. "_type": "employee",
  32. "_id": "1",
  33. "_score": 0.2876821,
  34. "_source": {
  35. "first_name": "John",
  36. "last_name": "Smith",
  37. "age": 25,
  38. "about": "I love to go rock climbing",
  39. "interests": [
  40. "sports",
  41. "music"
  42. ]
  43. }
  44. }
  45. ]
  46. }
  47. }

8.更复杂的搜索

搜索姓氏为 Smith 的雇员,但这次我们只需要年龄大于 30 的,使用过滤器 filter ,它支持高效地执行一个结构化查询

  1. GET /megacorp/employee/_search
  2. {
  3. "query" : {
  4. "bool": {
  5. "must": {
  6. "match" : {
  7. "last_name" : "smith"
  8. }
  9. },
  10. "filter": {
  11. "range" : {
  12. "age" : { "gt" : 30 }
  13. }
  14. }
  15. }
  16. }
  17. }

其中:range 过滤器 , 它能找到年龄大于 30 的文档,其中 gt 表示_大于(_great than)

返回结果:

  1. {
  2. "took": 44,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 0.2876821,
  13. "hits": [
  14. {
  15. "_index": "megacorp",
  16. "_type": "employee",
  17. "_id": "2",
  18. "_score": 0.2876821,
  19. "_source": {
  20. "first_name": "Jane",
  21. "last_name": "Smith",
  22. "age": 32,
  23. "about": "I like to collect rock albums",
  24. "interests": [
  25. "music"
  26. ]
  27. }
  28. }
  29. ]
  30. }
  31. }

9.全文搜索

搜索下所有喜欢攀岩(rock climbing)的雇员

  1. GET /megacorp/employee/_search
  2. {
  3. "query" : {
  4. "match" : {
  5. "about" : "rock climbing"
  6. }
  7. }
  8. }

返回结果:

  1. {
  2. "took": 17,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 2,
  12. "max_score": 0.5753642,
  13. "hits": [
  14. {
  15. "_index": "megacorp",
  16. "_type": "employee",
  17. "_id": "1",
  18. "_score": 0.5753642,
  19. "_source": {
  20. "first_name": "John",
  21. "last_name": "Smith",
  22. "age": 25,
  23. "about": "I love to go rock climbing",
  24. "interests": [
  25. "sports",
  26. "music"
  27. ]
  28. }
  29. },
  30. {
  31. "_index": "megacorp",
  32. "_type": "employee",
  33. "_id": "2",
  34. "_score": 0.2876821,
  35. "_source": {
  36. "first_name": "Jane",
  37. "last_name": "Smith",
  38. "age": 32,
  39. "about": "I like to collect rock albums",
  40. "interests": [
  41. "music"
  42. ]
  43. }
  44. }
  45. ]
  46. }
  47. }

10.全文搜索

找出一个属性中的独立单词是没有问题的,但有时候想要精确匹配一系列单词或者短语 。 比如, 我们想执行这样一个查询,仅匹配同时包含 “rock” 和 “climbing” ,并且 二者以短语 “rock climbing” 的形式紧挨着的雇员记录。

  1. GET /megacorp/employee/_search
  2. {
  3. "query" : {
  4. "match_phrase" : {
  5. "about" : "rock climbing"
  6. }
  7. }
  8. }

返回结果:

  1. {
  2. "took": 142,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 0.5753642,
  13. "hits": [
  14. {
  15. "_index": "megacorp",
  16. "_type": "employee",
  17. "_id": "1",
  18. "_score": 0.5753642,
  19. "_source": {
  20. "first_name": "John",
  21. "last_name": "Smith",
  22. "age": 25,
  23. "about": "I love to go rock climbing",
  24. "interests": [
  25. "sports",
  26. "music"
  27. ]
  28. }
  29. }
  30. ]
  31. }
  32. }

11.高亮搜索

许多应用都倾向于在每个搜索结果中 高亮 部分文本片段,以便让用户知道为何该文档符合查询条件。在 Elasticsearch 中检索出高亮片段也很容易。

增加参数: highlight

  1. GET /megacorp/employee/_search
  2. {
  3. "query" : {
  4. "match_phrase" : {
  5. "about" : "rock climbing"
  6. }
  7. },
  8. "highlight": {
  9. "fields" : {
  10. "about" : {}
  11. }
  12. }
  13. }

返回结果:

  1. {
  2. "took": 250,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 0.5753642,
  13. "hits": [
  14. {
  15. "_index": "megacorp",
  16. "_type": "employee",
  17. "_id": "1",
  18. "_score": 0.5753642,
  19. "_source": {
  20. "first_name": "John",
  21. "last_name": "Smith",
  22. "age": 25,
  23. "about": "I love to go rock climbing",
  24. "interests": [
  25. "sports",
  26. "music"
  27. ]
  28. },
  29. "highlight": {
  30. "about": [
  31. "I love to go <em>rock</em> <em>climbing</em>"
  32. ]
  33. }
  34. }
  35. ]
  36. }
  37. }

其中高亮模块为highlight属性

12.分析

Elasticsearch 有一个功能叫聚合(aggregations),允许我们基于数据生成一些精细的分析结果。聚合与 SQL 中的 GROUP BY 类似但更强大。

举个例子,挖掘出雇员中最受欢迎的兴趣爱好:

  1. GET /megacorp/employee/_search
  2. {
  3. "aggs": {
  4. "all_interests": {
  5. "terms": { "field": "interests" }
  6. }
  7. }
  8. }

返回结果:

  1. {
  2. ...
  3. "hits": { ... },
  4. "aggregations": {
  5. "all_interests": {
  6. "buckets": [
  7. {
  8. "key": "music",
  9. "doc_count": 2
  10. },
  11. {
  12. "key": "forestry",
  13. "doc_count": 1
  14. },
  15. {
  16. "key": "sports",
  17. "doc_count": 1
  18. }
  19. ]
  20. }
  21. }
  22. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号