文章40 | 阅读 24029 | 点赞0
#首先我们还是先增加几个文档
PUT /myindex/article/1
{
"post_date": "2020-03-14",
"title": "Java",
"content": "java is the best language",
"author_id": 119
}
PUT /myindex/article/2
{"post_date": "2020-03-14" ,
"title": "html",
"content": "I like html",
"author_id": 120
}
PUT /myindex/article/3
{"post_date": "2020-03-14" ,
"title": "es",
"content": "Es is distributed document store" ,
"author_id": 110
}
#查看es的文档映射mapping的数据结构
GET /myindex/article/_mapping
mapping数据结构如下:
{
"myindex" : {
"mappings" : {
"article" : {
"properties" : {
"author_ id" : {
"type" : "long"
},
"content" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"post_ _date" : {
"type" : "date"
},
"post_ date" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
这里我们并没有为索引文档的数据结构做定义,但是我们发现es自动创建了 index type 以及type对应的mapping(dynamic mapping) 说明ElasticSearch能根据我们给的数据自动检测并且给定字段的数据类型
如果给定的 true 、false------>boolean
如果给定的字符串 "字符串"------>string(注意我这里用的是6.8.6版本 string已经被text或者keyword代替)
如果给定的数字 1、2------>long
如果给定的小数 12.34------>double
如果给定的时间 2020-03-14------->date
什么是映射mapping?
mapping定义了type中的每个字段的数据类型以及这些字段的如何分词的等相关属性
我们现在带查询一下添加的文档内容 GET /myindex/article/_search
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "myindex",
"_type" : "article",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"post_ date" : "2020-03-14",
"title" : "html",
"content" : "I like html",
"author_ id" : 120
}
},
{
"_index" : "myindex",
"_type" : "article",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"post_ date" : "2020-03-14",
"title" : "Java",
"content" : "java is the best language",
"author_ id" : 119
}
},
{
"_index" : "myindex",
"_type" : "article",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"post_ _date" : "2020-03-14",
"title" : "es",
"content" : "Es is distributed document store",
"author_ id" : 110
}
}
]
}
}
#查不出来
GET /myindex/article/_search?q=post_date:2020
#可以查出来
GET /myindex/article/_search?q=post_date:2020-03-14
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "myindex",
"_type" : "article",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"post_date" : "2020-03-14",
"title" : "html",
"content" : "I like html",
"author_id" : 120
}
},
{
"_index" : "myindex",
"_type" : "article",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"post_date" : "2020-03-14",
"title" : "Java",
"content" : "java is the best language",
"author_id" : 119
}
},
{
"_index" : "myindex",
"_type" : "article",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"post_date" : "2020-03-14",
"title" : "es",
"content" : "Es is distributed document store",
"author_id" : 110
}
}
]
}
}
#可以查出来
GET /myindex/article/_search?q=content:html
**这是因为ElasticSearch映射mapping进行指定的,post_date是日期类型,而content是字符串类型所以字符串就查询出来了。**像日期date类型和数字long类型要进行精确查询才可以查询,而说明日期和数字类型没有进行分词,而字符串进行了分词。字符串text类型默认进行了分词的
到这里我们知道了mapping有两个作用,一是规定自定的类型,二是规定相关字段的属性(比如是不是进行分词)
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/ywl470812087/article/details/104857287
内容来源于网络,如有侵权,请联系作者删除!