> 文章列表 > ElasticSearch - 文档 | 索引文档 | 检索文档 | 创建索引并指明映射

ElasticSearch - 文档 | 索引文档 | 检索文档 | 创建索引并指明映射

ElasticSearch - 文档 | 索引文档 | 检索文档 | 创建索引并指明映射

文章目录

      • 1. ElasticSearch是面向文档
      • 2. 索引员工文档
      • 3. 检索员工文档
      • 4. 映射

1. ElasticSearch是面向文档的

在应用程序中对象很少只是一个简单的键和值的列表。通常,它们拥有更复杂的数据结构,可能包括日期、地理信息、其他对象或者数组等。

Elasticsearch 是面向文档的,意味着它存储整个对象或文档。Elasticsearch 不仅存储文档,而且索引每个文档的内容,使之可以被检索。在 Elasticsearch 中,我们对文档进行索引、检索、排序和过滤—而不是对行列数据。这是一种完全不同的思考数据的方式,也是 Elasticsearch 能支持复杂全文检索的原因。

ES使用 json 文档代表了一个对象,如 user 对象:

{"email":      "john@smith.com","first_name": "John","last_name":  "Smith","info": {"bio":         "Eco-warrior and defender of the weak","age":         25,"interests": [ "dolphins", "whales" ]},"join_date": "2014/05/01"
}

虽然原始的 user 对象很复杂,但这个对象的结构和含义在 JSON 版本中都得到了体现和保留。

2. 索引员工文档

第一个业务需求是存储员工数据。 这将会以员工文档的形式存储:一个文档代表一个员工。存储数据到 Elasticsearch 的行为叫做索引 ,但在索引一个文档之前,需要确定将文档存储在哪里。

索引(名词):一个索引类似于传统关系数据库中的一个数据库,是一个存储关系型文档的地方。
索引(动词):索引一个文档就是存储一个文档到一个索引中以便被检索和查询。类似于 SQL 中的 INSERT 关键词。

对于员工目录,我们将做如下操作:

  • 每个员工索引一个文档,文档包含该员工的所有信息。
  • 该文档位于索引 user 内。
  • 该索引保存在我们的 Elasticsearch 集群中。
PUT /user/_doc/1
{"first_name" : "John","last_name" :  "Smith","age" :        25,"about" :      "I love to go rock climbing","interests": [ "sports", "music" ]
}

json 文档 包含了这位员工的所有详细信息,他的名字叫 John Smith ,今年 25 岁,喜欢攀岩。无需进行执行管理任务,如创建一个索引或指定每个属性的数据类型之类的(可以不事先创建索引,也可以不事先指定映射),可以直接只索引一个文档。Elasticsearch 默认地完成其他一切,因此所有必需的管理任务都在后台使用默认设置完成。

索引更多的文档:

PUT /user/_doc/2
{"first_name" : "zhangsan","last_name" :  "Smith","age" :        25,"about" :      "I love to go rock climbing","interests": [ "sports", "music" ]
}
PUT /user/_doc/3
{"first_name" : "lisi","last_name" :  "Smith","age" :        25,"about" :      "I love to go rock climbing","interests": [ "sports", "music" ]
}

3. 检索员工文档

目前我们已经在Elasticsearch中存储了一些数据,接下来就能专注于实际应用的业务需求了。

① 第一个需求可以查询到单个雇员的数据:

GET /user/_doc/1

执行 一个 HTTP GET 请求并指定文档的地址就可以返回原始的 json 文档,返回结果包含了文档的一些元数据,以及 _source 属性,内容是 John Smith 雇员的原始 json 文档:

{"_index" : "user","_type" : "_doc","_id" : "1","_version" : 1,"_seq_no" : 0,"_primary_term" : 1,"found" : true,"_source" : {"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests" : ["sports","music"]}
}

将 HTTP 命令由 PUT 改为 GET 可以用来检索文档,同样的,可以使用 DELETE 命令来删除文档,以及使用 HEAD 指令来检查文档是否存在。如果想更新已存在的文档,只需再次 PUT

② 请求来搜索所有雇员:

GET /user/_search
{"took" : 3,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "user","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests" : ["sports","music"]}},{"_index" : "user","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"first_name" : "zhangsan","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests" : ["sports","music"]}},{"_index" : "user","_type" : "_doc","_id" : "3","_score" : 1.0,"_source" : {"first_name" : "lisi","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests" : ["sports","music"]}}]}
}

注意:返回结果不仅告知匹配了哪些文档,还包含了整个文档本身:显示搜索结果给最终用户所需的全部信息。

③ 尝试下搜索名为 John 的雇员,我们通过一个URL参数来传递查询信息给搜索接口:

GET /user/_search?q=first_name:John
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 0.9808292,"hits" : [{"_index" : "user","_type" : "_doc","_id" : "1","_score" : 0.9808292,"_source" : {"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests" : ["sports","music"]}}]}
}

4. 映射

映射是定义文档及其包含的字段如何存储和索引的过程。每个文档都是字段的集合,每个字段都有自己的数据类型。在映射数据时,创建一个映射定义,其中包含与文档相关的字段类型。

1、字符串类型:

(1) text类型:当一个字段需要用于全文搜索(会被分词),比如产品名称、产品描述信息, 就应该使用text类型。该类型字段会通过分析器转成terms list,然后存入索引。该类型字段不用于排序、聚合操作。

(2)keyword类型:当一个字段需要按照精确值进行过滤、排序、聚合等操作时, 就应该使用keyword类型。该类型的字段值不会被分析器处理(分词)

2、数值类型:

(1) byte:有符号的8位整数, 范围: [-128 ~ 127]
(2) short:有符号的16位整数, 范围: [-32768 ~ 32767]
(3) integer:有符号的32位整数, 范围: [−231−231 ~ 231231-1]
(4) long:有符号的64位整数, 范围: [−263−263 ~ 263263-1]
(5) float:32位单精度浮点数
(6) double:64位双精度浮点数
(7) half_float:16位半精度IEEE 754浮点类型
(8) scaled_float:缩放类型的的浮点数, 比如price字段只需精确到分, 57.34缩放因子为100, 存储结果为5734

3、boolean类型:

可以使用boolean类型的(true、false)也可以使用string类型的(“true”、“false”)。

4、日期类型:

JSON没有日期数据类型, 所以在ES中, 日期可以是:

  • 包含格式化日期的字符串, “2018-10-01”, 或"2018/10/01 12:10:30".
  • 代表时间毫秒数的长整型数字.
  • 代表时间秒数的整数.

5、复杂数据类型:

es支持复杂的数据类型,包括:object、array、nested。

① 查看索引员工文档时默认创建的映射:

GET /user/_mapping
{"user" : {"mappings" : {"properties" : {"about" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"age" : {"type" : "long"},"first_name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"interests" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"last_name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}}}}
}

② 索引文档之前,先创建索引并指定映射(文档理解为对象,映射理解为对象中属性的类型):

PUT /user
{"mappings": {"properties": {"age": {"type": "long"},"about": {"analyzer": "ik_max_word","search_analyzer": "ik_smart","type": "text"},"first_name": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"last_name": {"analyzer": "ik_max_word","search_analyzer": "ik_smart","type": "text"},"interests": {"analyzer": "ik_max_word","search_analyzer": "ik_smart","type": "text"}}}
}

哈萨克斯坦旅游网