Elasticsearch 术语介绍和CRUD实际操作入门

一、Elastic Stack 核心Elasticsearch

Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎。Elasticsearch 是面向文档的,这就意味着它可以像MongoDB一样存储整个对象或者文档。然而它不仅仅是存储,还会索引每个文档的内容,使值可以被索引。我们也可以对文档进行索引,搜索,排序,过滤。

在Elasticsearch中存储数据的行为就叫做索引(indexing)。文档属于一种类型(type),而这些类型存储在索引(index)中。

Elasticsearch常用术语

  • Document 文档数据
  • Index 索引
  • Type 索引中的数据类型
  • Field 字段,文档属性
  • Query DSL 查询语法

对比MySQL、MongoDB、Elasticsearch中的相关概念。

二、本地启动Elasticsearch集群

bin\elasticsearch

启动信息如下所示。

bin\elasticsearch -Ehttp.port=8200 -Epath.data=node2
bin\elasticsearch -Ehttp.port=7200 -Epath.data=node3

 启动完成之后,分别访问:

http://localhost:9200/
http://localhost:8200/
http://localhost:7200/

查看集群

http://localhost:9200/_cat/nodes

显示集群中有3个节点。

http://localhost:9200/_cat/nodes?v

显示标题信息,其中 * 表示为主节点。

http://localhost:9200/_cluster/stats

显示集群详细信息。

三、Elasticsearch中CRUD实际操作

启动Kibana,访问http://localhost:5601,点击Dev Tools 菜单项。

CRUD 基本操作:

  • Create 创建 PUT /index/type/id(data)
  • Read 读取 GET /index/type/id
  • Update 更新 POST /index/type/id/_update(doc)
  • Delete 删除 DELETE /index/type/id

(1)输入POST 脚本,点击执行按钮,查看输出结果。

(2)查询操作 GET

GET /accounts/person/1 获取查询结果

 (3)更新操作 POST

POST /accounts/person/1/_update

{

"doc":{

"lastname":"li"

}

}

再次执行查询操作GET,发现结果已经更新成功。

(4)删除操作DELETE

DELETE /accounts/person/1

四、Elasticsearch 7.*的更新

在前面的操作过程中,会看看到 Deprecation 提示信息,意思是type已经移除了。

从Elasticsearch 6.0开始,官方便不建议一个索引中创建多个类型;在Elasticsearch 7.0中,更是移除了类型(Type)这个概念。

在 Elasticsearch 7.* 中,由于类型(Type)的移除,我们可以理解为,一个索引(index)就是一张 table。

创建一条新的文档,注意:_doc【Type名称,约定都用_doc】,此时就没有Deprecation 提示信息了。

POST /accounts/_doc/3

{

"name": "Tom",

"lastname": "Ma",

"job_desc": "码农"

}

 其他操作类似,将之前的type:person 更新为 _doc即可。

五、Elasticsearch 查询

简单介绍2中查询方式,分别为 query string 和query DSL。

(1)Query String

GET /accounts/_search?q=Senior

(2)Query DSL

GET /accounts/_search

{

"query" :{

"match": {

"name": "jacky"

}

}

}

猜你喜欢

转载自www.cnblogs.com/ilovepython/p/11650227.html
今日推荐