当前位置: 首页 > news >正文

ElasticSearch备考 -- Multi field

一、题目

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的同学,可以私信或留言,我帮您内推,流程快!!!

福利二

福利三

http://www.lryc.cn/news/455413.html

相关文章:

  • 刷题 图论
  • 基于JAVA的鲜花商城管理系统(源码+定制+讲解)鲜花商城管理系统、鲜花商城管理平台、鲜花商城信息管理、鲜花商城系统开发与应用、鲜花在线商城管理系统
  • 深圳大学-Java程序设计-选实验1 基础知识练习
  • 第 33 章 Ajax
  • LeetCode 209 Minimum Size Subarray Sum 题目解析和python代码
  • C# 入坑JAVA 潜规则 注解 列表 listMch,该列表存储了一个映射(Map)的集合 等 入门系列3
  • 2024年9月个人工作生活总结
  • JVM有哪些参数以及如何使用
  • STM32编码器接口解析及抗噪声措施探讨
  • 微软发布Windows 11 2024更新,新型Copilot+ AI PC功能亮相
  • 鹏哥C语言68-70---位操作符+单目操作符+关系操作符
  • showdoc二次开发
  • 力扣16~20题
  • Pikachu-Sql-Inject -基于boolian的盲注
  • 最后30天,你的系统集成项目管理工程师备考进度到哪儿了?
  • 网络安全事件的发生,主要原因是什么
  • 【leetcode】274.H指数
  • 1.Python 引入(字面量、注释、变量、数据类型、数据类型转换、标识符、运算符、字符串扩展)
  • 【AI知识点】梯度消失(Vanishing Gradient)和梯度爆炸(Exploding Gradient)
  • 在 ArkTS 网络请求中,重新封装一下 http 模块
  • Microsoft 更新 Copilot AI,未來將能使用語音並看到你瀏覽的網頁
  • 系统架构设计师-论文题(2021年下半年)
  • selenium的webdriver常用方法和属性介绍(2)
  • 73.【C语言】C/C++的内存区域划分
  • k8s 中存储之 hostPath 卷
  • Cherno游戏引擎笔记(73~90)
  • helm 测试卸载或删除(redis)
  • 关于Qt音乐播放器进度条拖拽无用的问题解决方案
  • Redis:初识Redis
  • 【React】增量传输与渲染