1.需求简介
以一个博客文章需求案例来演示elasticsearch数据建模的过程,博客索引包括如下字段。
- 标题:title
- 发布日期:publish_date
- 作者:author
- 摘要:abstract
- 网络地址:url
2.blog_index的Mapping
PUT /blog_index
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
}
}
PUT /blog_index/_mapping
{
"properties": {
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"publish_date": {
"type": "date"
},
"author": {
"type": "keyword"
},
"abstract": {
"type": "text"
},
"url": {
"enabled": false
}
}
}
对text类型的文档进行查询或者聚合时,该字段会被elasticsearch分词,如果需要实现完全匹配的功能,这时就需要对该字段设置一个keyword属性,然后将keyword属性的type设置为keyword。
3.需求变更
如果需要新增一个内容content字段,并且这个字段会很大,占据非常多的磁盘空间,这时可对Mapping做如下修改。
PUT /blog_index
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
}
}
PUT /blog_index/_mapping
{
"_source": {
"enabled": false
},
"properties": {
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
},
"store": true
},
"publish_date": {
"type": "date",
"store": true
},
"author": {
"type": "keyword",
"store": true
},
"abstract": {
"type": "text",
"store": true
},
"content": {
"type": "text",
"store": true
},
"url": {
"type": "keyword",
"ignore_above": 100,
"store": true
}
}
}
4.查询
(1).插入文档
POST /blog_index/_doc
{
"title": "hello world",
"author": "steven",
"publish_date": "2021-01-01",
"abstract": "hello my world",
"content": "hello my world",
"url": "https://www.world.com/"
}
POST /blog_index/_search
{
"query": {
"match": {
"title": "hello"
}
}
}
{
"took" : 464,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "blog_index",
"_type" : "_doc",
"_id" : "yoldm3sBEsHOdz1YP8pk",
"_score" : 0.2876821
}
]
}
}
POST /blog_index/_search
{
"stored_fields": ["title", "content"],
"query": {
"match": {
"title": "hello"
}
},
"highlight": {
"fields": {
"title": {}
}
}
}
{
"took" : 106,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "blog_index",
"_type" : "_doc",
"_id" : "yoldm3sBEsHOdz1YP8pk",
"_score" : 0.2876821,
"fields" : {
"title" : [
"hello world"
],
"content" : [
"hello my world"
]
},
"highlight" : {
"title" : [
"<em>hello</em> world"
]
}
}
]
}
}