Elasticsearch上手——几个基本概念,Elasticsearch的说明文档中,基本概念(Basic Concepts)一节中提到了一些术语,结合实践经验,尝试重新理解一下。
Document(文档)
文档是Elasticsearch存储和建立索引的基本单位,比如一篇文章、一条数据库记录等,通过JSON格式被添加到平台。下面就是一个文档:
1
2
3
4
5
|
{ "user" : "kimchy" , "post_date" : "2009-11-15T14:12:12" , "message" : "trying out Elasticsearch" } |
上面的文档包含了三个字段:user、post_date和message
Type(类型)
我将其理解为文档的类型,如果非要对应到数据库的概念上,那就是相当于MySQL的表结构或者MongoDB的Collection。至于如何定义,完全取决于要实现的业务逻辑。下面一个type是关于上面文档的定义:
1
2
3
4
5
6
7
|
"message" : { "properties" : { "user" : { "type" : "text" }, "post_date" : { "type" : "date" }, "message" : { "type" : "text" } } } |
定义了一个叫做message的type,包含了三个字段,user、post_date和message,字段类型分别为text、date和text。字段类型对创建索引的方式以及可支持的搜索有重要的意义,这里不做进一步解释。
Index(索引)
Index是一系列文档的集合,在开始使用之前必须创建一个确定名称的index,一个index下面可以包含多个type。在索引文档、搜索、更新、删除时都需要指定索引的名称,并且index的名称必须为小写字母。
可以将其简单理解一个数据库,比如一个复杂的电商系统,可以包含多个index,product_index负责产品库,order_index负责订单库等。创建一个包含了type的index例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
PUT blog_index { "mappings" : { "user" : { "properties" : { "title" : { "type" : "text" }, "name" : { "type" : "text" }, "age" : { "type" : "integer" } } }, "blogpost" : { "properties" : { "title" : { "type" : "text" }, "body" : { "type" : "text" }, "user_id" : { "type" : "keyword" }, "created" : { "type" : "date" , "format" : "strict_date_optional_time||epoch_millis" } } } } } |
PUT blog_index 是创建index的命令,blog_index就是index的名字。
上面的例子创建了一个名为blog_index的index,包含了两个type,分别为user和blogpost。