发表日期:2019年9月18日


什么是ElasticSearch

ElasticSearch是一个集数据存储数据搜索数据分析为一体的系统。它是分布式的,所以能利用分布式来提高其处理能力,具有高可用性和高伸缩性。如果你需要一个能够提供高性能的搜索服务的系统,那么它或许是一个好的选择。

  • 数据存储:是指它能够以JSON格式来存储数据。如果你不在意数据的搜索,你甚至可以像类似使用mongodb那样来单纯把它作为一个数据存储系统使用。
  • 数据搜索:是指它能够对JSON格式的数据进行全文检索等搜索。
  • 数据分析:是指它能够利用一些算法来对JSON文档数据进行分析,比如得出某个月的商品销售增长量。

核心能力

ES的搜索核心

介绍两个搜索方法:顺序扫描查找、全文搜索

搜索引擎选择

搜索的处理

上面提了词的拆分,这里提一些关于底层的搜索处理的内容。介绍一下ElasticSearch另一个协助搜索的关键组件--分词器。ElasticSearch的全文搜索离不开分词器的帮助。

补充:

  • 数据库不是被替代,而是被补充。有时候会将数据同时存储到数据库和ElasticSearch中,在单个查看的时候可以从数据库中查询,在搜索的时候从ElasticSearch中查询;也有的项目由于数据比较简单完全使用elasticsearch来存储数据。
  • ElasticSearch是分布式的,但我们是不需要对其集群进行部署的,它自动进行了集群部署和节点发现等功能,我们只需进行很少的配置就能管理集群。在比较靠后的内容才会讲到如何深入管理集群。

小节总结:

  • 1.本小节简单讲述了ElasticSearch是什么
  • 2.传统搜索的不足
  • 3.使用ElasticSearch搜索的好处
  • 4.全文搜索和倒排索引
  • 5.与其他搜索引擎的比较
  • 6.ElasticSearch对于索引词的处理(这个内容是提前讲的内容,是为了帮助了解倒排索引如何建立索引)

基本学习环境搭建

如何操作ElasticSearch

首先要说的是,ElasticSearch是一款软件,有点类似MySQL,我们要操作它的时候,也要给它发送它能识别的命令,而ElasticSearch是面向restful(不知道restful的自查吧)的,所以我们发送的命令是有点类似发送http请求的。

mysql是3306端口,而elasticsearch支持9200和9300端口操作,其中9200面向http,9300面向tcp。9200能够使用普通的http请求来操作elasticsearch,9300需要连接elasticsearch之后再执行命令。

  • 对于9200,因为面向http,所以我们可以使用postman发http请求来操作elasticsearch。
  • 对于9300,因为面向tcp,我们通常使用一些elasticsearch管理工具(如kibana)进行连接elasticsearch之后再执行命令。(类似navicat之于mysql)

下载、安装和运行(Based Windows)

我们首先来搭建好学习环境,主要是ElasticSearch和Kibana。【请注意,Elasticsearch依赖Java环境】
【Kibana是可选的,下面会介绍一下基于postman的对ElasticSearch操作。】,Kibana是一款对ElasticSearch进行管理的软件,我们可以在Kibana上执行ElasticSearch的命令。

下载:

  • ElasticSearch下载:ElasticSearch下载【这里以6.2的为例】
  • Kibana下载:Kibana下载 【两个下载都是同一个位置,这里下载以6.2的为例】【Kibana的版本尽量与ElasticSearch的一致,有些版本会连接错误】

安装:

  • 两个软件都是不需要安装的,下载后,直接解压即可。

运行:

  • 对于ElasticSearch,可以直接在elasticsearch-6.2.0\bin中运行elasticsearch.bat,当提示“started”时,表示运行成功。
  • 对于Kibana,可以直接在kibana-6.2.0-windows-x86_64\bin中运行kibana.bat,当提示“Server running at http://localhost:5601”时,表示运行成功。【要先运行ElasticSearch再运行Kibana,因为它要与ElasticSearch进行连接】

如何操作ES

ElasticSearch默认的TCP服务端口是9300,Kibana的服务端口是5601,当我们启动了Kibana之后,它会默认帮我们连接上9300,所以我们可以从http://localhost:5601中进入Kibana的管理界面来管理ElasticSearch。

如果你是第一次使用,那么ElasticSearch会自动创建一个名为elasticsearch的集群,Kibana会在这个节点中初始化一个index



我们在Kibana的DevTools中执行一些命令来看一下ElasticSearch。【从http://localhost:5601中进入】

〈一〉ElasticSearch的介绍-LMLPHP



在输入命令GET /_cat/health?v后,点击该行右侧的执行按钮。【GET /_cat/health?v是查看集群的健康状态的命令】

〈一〉ElasticSearch的介绍-LMLPHP

然后就可以在右侧的结果窗口中查看命令执行结果。

〈一〉ElasticSearch的介绍-LMLPHP

【你现在大概都是看不懂命令的意义和结果的意义的了,不过你应该知道哪里输入命令哪里看执行结果了】

基于postman操作

postman是一个用来发请求的软件,可以使用restful风格的请求来操作elasticsearch。

比如上面的查看集群的命令:GET /_cat/health?v

转成基于restful的是:http://localhost:9200/_cat/health?vIP:9200+命令,其中9200是用于接收restful请求的es监听端口。

〈一〉ElasticSearch的介绍-LMLPHP

补充:

上面的第一次使用并没有涉及到具体的知识,只是让你熟悉一下如何使用Kibana来操作ElasticSearch。下面讲到具体知识点才会具体使用。

小节总结

  • 1.讲了ES如何提供服务:9200,9300
  • 2.讲了如何下载、安装、运行ElasticSearch和Kibana
  • 3.讲述了如何在Kibana中操作ElasticSearch
  • 4.讲述了如何在postman中操作ElasticSearch

需要了解的概念

分布式模型相关

  • 集群Cluster:所谓集群,就是多个服务节点的集合,集群意味着这些节点是能够相互交流的,不然无法进行数据交互。集群的默认名称是"elasticsearch",多个提供服务的节点会根据集群名来自动加入集群
  • 节点Node:节点是集群的一部分,是集群的最小单元,是可以提供服务的节点。
  • 分片shard:分片位于节点上,分片是elasticsearch数据存储的单元,elasticsearch中的数据会存储在分片中。分片可以存储在任意一个节点上。分片分为主分片Primary Shard和副本分片Replica Shard。
  • 主分片Primary Shard:当存储一个文档document的时候,会先存储到主分片中,然后再复制到其他的副本分片Replica Shard中。
  • 副本分片Replica Shard:副本分片是主分片的复制(备份)。默认情况下,主分片有一个副本分片,主分片不能修改,但副本分片可以后续再增加。
    • 为了保证数据的不丢失,通常来说Replica Shard不能与其对应的Primary Shard处于同一个节点中。【因为万一这个节点损坏了,那么存储在这个节点上的原数据(primary shard)和备份数据(replica shard)就全部丢失了】
    • 当主分片挂掉的时候,会选择一个副本分片作为主分片。
    • 查询可以在主分片或副本分片上进行查询,这样可以提供查询效率。【但数据的修改只发生在主分片上。】
    • 一个Primary Shard可以有多个Replica Shard,默认创建是1个。

      〈一〉ElasticSearch的介绍-LMLPHP

数据存储相关

数据存储在shard中,shard中的数据是以文档document为单位的。document存储在index和type划分的逻辑空间中。document以json为格式,每一个key-value中key可以称为域Field。

  • 索引Index:索引是存储具有相同结构的document的集合,意义上有点类似关系型数据库中的数据库,用于存储一系列数据,比如可以说“商品”索引,一般都是个大类,小逻辑划分由Type处理。
  • 类型Type:类型是索引的逻辑分区,意义有点类似关系型数据库中的数据表。用来划分索引下不同子类型的数据,比如商品(索引)可以有电子产品(类型),药品(类型)。在同一个分类下的数据一般都具有同种特征,用来定义数据的字段的数量一般也是相同的。每一个document都有一个type和一个id,在存储文档的时候需要指定索引、类型和ID。
  • 文档Document:类似于关系型数据库中的记录,是ElasticSearch的数据存储的基本单位,格式与JSON相同。例如:
{
"book_id": 1,
"book_name": "Java Core ",
"book_desc": "A good book, you know!",
"category_id": "1",
"category_name": "Computer"
}
  • 域Field:类似于关系型数据库中的字段。
  • elasticsearch是面向restful的,下面是restful请求与elasticsearch操作的对应:
GET读取获取数据
POST新增新增数据
PUT修改修改数据或增加数据
DELETE删除删除数据
  • 索引用来存储数据,分片也是用来存储数据,它们是怎么对应的?一个索引存储在多个分片上,默认情况下,一个索引有五个主分片,五个副本分片。主分片的数量一旦定下来就不能再修改,但副本分片的数量还可以修改。

小节总结

  • 讲了一下ElasticSearch的集群概念,节点是集群的基础服务单位,节点可以提供数据读写服务,数据按分片来存储,主分片是主要数据,可以读取和修改数据,副本分片不支持修改数据。
  • 主分片和副本分片的互斥性(为了保证数据不同时丢失)
  • elasticsearch的数据的逻辑存储结构(索引->类型->文档)。索引是数据的大分类,类型是数据的小分类。
  • 文档的格式
  • 索引与分片的关系。一个索引存储在多个分片上

Hello ElasticSearch

前面提了一下index,type,document,说了ElasticSearch的逻辑存储空间。

下面以两个实例:“写document->读document”和“写document->搜索document“来初步演示一下如何存储数据和获取数据。

写->读

如果我们要向ElasticSearch中写入一份数据(document),命令的语法应该如下:

PUT /index名称/type名称/document的ID
{
document的数据
}

上面的语法的意思就是向一个index的一个type中插入一个document,document用id作为标识,后面我们取document的数据也将以这个id为依据。其中index,type是可以不需要我们预创建的,在我们还不会如何创建index和type的时候,你可以先随便打个名字(如果ElasticSearch检测到我们输入的index和type是不存在的,那么它就会以默认的规则帮我们创建出来)。

请在kibana的devtool中执行以下命令来存储一份document:

PUT /douban/book/1
{
"book_id":1,
"book_name":"A Clockwork Orange",
"author":"Anthony Burgess",
"summary":"Fully restored edition of Anthony Burgess' original text of A Clockwork Orange, with a glossary of the teen slang 'Nadsat', explanatory notes, pages from the original typescript, interviews, articles and reviews Edited by Andrew Biswell With a Foreword by Martin Amis 'It is a horrorshow story ...' Fifteen-year-old Alex likes lashings of ultraviolence. He and his gang of friends rob, kill and rape their way through a nightmarish future, until the State puts a stop to his riotous excesses. But what will his re-education mean? A dystopian horror, a black comedy, an exploration of choice, A Clockwork Orange is also a work of exuberant invention which created a new language for its characters. This critical edition restores the text of the novel as Anthony Burgess originally wrote it, and includes a glossary of the teen slang 'Nadsat', explanatory notes, pages from the original typescript, interviews, articles and reviews, shedding light on the enduring fascination of the novel's 'sweet and juicy criminality'. Anthony Burgess was born in Manchester in 1917 and educated at Xaverian College and Manchester University. He spent six years in the British Army before becoming a schoolmaster and colonial education officer in Malaya and Brunei. After the success of his Malayan Trilogy, he became a full-time writer in 1959. His books have been published all over the world, and they include The Complete Enderby, Nothing Like the Sun, Napoleon Symphony, Tremor of Intent, Earthly Powers and A Dead Man in Deptford. Anthony Burgess died in London in 1993. Andrew Biswell is the Professor of Modern Literature at Manchester Metropolitan University and the Director of the International Anthony Burgess Foundation. His publications include a biography, The Real Life of Anthony Burgess, which won the Portico Prize in 2006. He is currently editing the letters and short stories of Anthony Burgess.",
"press":"Penguin Classic",
"publication_date":"2000-02-22"
}

如果要获取ES中存储的document,命令的语法应该如下:

GET /index名/type名/id

一样的,请在kibana中执行下述命令来获取我们上面存储的document:

GET /douban/book/1

返回结果:【返回index名称,type名称,id,原始文档等数据】

〈一〉ElasticSearch的介绍-LMLPHP

很明显地,我们获取到了我们刚刚存储进去的数据。

写->搜索

一样的,我们写多一份数据进ES中:

PUT /douban/book/2
{
"book_id":2,
"book_name":"Kubernetes in Action",
"author":"Marko Luksa",
"summary":"Kubernetes in Action teaches you to use Kubernetes to deploy container-based distributed applications. You'll start with an overview of Docker and Kubernetes before building your first Kubernetes cluster. You'll gradually expand your initial application, adding features and deepening your knowledge of Kubernetes architecture and operation. As you navigate this comprehensive guide, you'll explore high-value topics like monitoring, tuning, and scaling.Kubernetes is Greek for \"helmsman,\" your guide through unknown waters. The Kubernetes container orchestration system safely manages the structure and flow of a distributed application, organizing containers and services for maximum efficiency. Kubernetes serves as an operating system for your clusters, eliminating the need to factor the underlying network and server infrastructure into your designs.",
"press":"Manning Publications",
"publish_date":"2017-08-31"
}

下面将演示搜索的功能,搜索的其中一种语法是:

GET /index名/type名/_search
{
"query":{
"match":{
"字段Field名称":"用于搜索的关键字"
}
}
}

那么,根据我们之前存储的数据,如果我们要查询summary字段有Orange的数据的话,命令如下:

GET /douban/book/_search
{
"query": {
"match": {
"summary":"Orange"
}
}
}

返回的结果:

〈一〉ElasticSearch的介绍-LMLPHP

小节总结

  • 这小节涉及到了数据的插入,读取,搜索,但仅仅是一个Hello ElasticSearch的例子,所以没有讲到插入数据应该有的前置知识。理论上,你只需要记得这个流程即可。后面会补充这个流程中没有提到的内容。

--To Be Continue


05-25 16:58