一、题目
Create the index `hamlet_2` with one primary shard and no replicas
Copy the mapping of `hamlet_1` into `hamlet_2`, but also define a multi-field for `speaker`. The name of such multi-field is `tokens` and its data type is the (default) analysed string
Reindex `hamlet_1` to `hamlet_2`
`hamlet_1` index mapping
{
"line_number": "text",
"speaker": "text",
"text_entry": "text"
}
Verify that full-text queries on "speaker.tokens" are enabled on `hamlet_2` by running the following command:
GET hamlet_2/_search
{
"query": {
"match": { "speaker.tokens": "hamlet" }
}
}
二、思考
我们来分析一下题目的要求,其中说要求拷贝`hamlet_1`mapping定义,并且要求顶一个的多字段类型speaker,其中一个字段名字叫tokenle,类型为文本类型。
多字段其实主要考察mapping的自定义,我们可以理解为一个字段有多种类型,是通过fields关键字段实现。
三、解题
Step 1、创建 `hamlet_1` mapping
PUT /hamlet_1
{"mappings": {"properties": {"line_number": {"type": "text"},"speaker": {"type": "text"},"text_entry": {"type": "text"}}}
Step 2、拷贝索引hamlet_1结构创建hamlet_2索引
索引中settings 配置
- number_of_shards:索引分片数量
- number_of_replicas:索引中备份数量
多字段要使用fileds 关键字,定义字段和mapping中最外层字段定义一样
PUT /hamlet_2
{"settings": {"number_of_shards": 1,"number_of_replicas": 0},"mappings": {"properties": {"line_number": {"type": "text"},"speaker": {"type": "text","fields": {"token": {"type": "text"}}},"text_entry": {"type": "text"}}}
}
Step 3、reindex 数据
POST _reindex
{"source": {"index": "hamlet_1"},"dest": {"index": "hamlet_2"}
}
Step 4、执行查询
GET hamlet_2/_search
{"query": {"match": {"speaker.tokens": "hamlet"}}
}
四、总结
mapping中多字段定义要是使用关键字 fileds,创建字段和最外层字段格式一样
参考资料
- fields | Elasticsearch Guide [8.15] | Elastic
- Reindex API | Elasticsearch Guide [8.1] | Elastic
送一波福利:
福利一
有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!
有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!
有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!
福利二
福利三