Elasticsearch 认证模拟题 - 20

ops/2024/9/25 6:07:29/

一、题目

定义一个 pipeline,并且将 earthquakes 索引的文档进行更新

  1. pipeline 的 ID 为 earthquakes_pipeline
  2. magnitude_type 的字段值改为大写
  3. 如果文档不包含 batch_number,增加这个字段,将数值设置为 1
  4. 如果已经包含 batch_number,字段值 + 1
# 定义索引结构
PUT earthquakes
{"mappings": {"properties": {"name": {"type": "text"},"level": {"type": "integer"},"magnitude_type": {"type": "keyword"},"batch_number": {"type": "integer"}}}
}# 批量导入数据
POST earthquakes/_bulk
{"index":{}}
{"name":"111","level":1,"magnitude_type":"small","batch_number":22}
{"index":{}}
{"name":"222","level":2,"magnitude_type":"big"}
1.1 考点
  1. Ingest pipelines
  2. Update by query
1.2 答案
# 验证管道是否按照预期执行
POST _ingest/pipeline/_simulate
{"pipeline": {"processors": [{"script": {"lang": "painless","source": """ctx['batch_number'] = ctx['batch_number'] + 1;""","if": "ctx.batch_number != null"}},{"set": {"field": "batch_number","value": 1,"if": "ctx.batch_number == null"}},{"uppercase": {"field": "magnitude_type"}}]},"docs": [{"_source": {"magnitude_type":"small"}},{"_source": {"magnitude_type":"small","batch_number":22}}]
}# 保留一份原始数据,防止更新出错
POST _reindex
{"source": {"index": "earthquakes"},"dest": {"index": "earthquakes_bak"}
}# 定义管道
PUT _ingest/pipeline/earthquakes_pipeline
{"processors": [{"script": {"lang": "painless","source": """ctx['batch_number'] = ctx['batch_number'] + 1;""","if": "ctx.batch_number != null"}},{"set": {"field": "batch_number","value": 1,"if": "ctx.batch_number == null"}},{"uppercase": {"field": "magnitude_type"}}]
}# 批量更新数据
POST earthquakes/_update_by_query?pipeline=earthquakes_pipeline# 检查结果
GET earthquakes/_search

在这里插入图片描述

二、题目

task1 索引中的文档增加一个新的字段 new_field,字段值为 field_a + field_b + field_c

其实这里我有个问题:

  1. field_afield_bfield_c 的值都是数字类型么,要是字符类型咋办
  2. field_afield_bfield_c 的类型不同咋整
2.1 考点
  1. Ingest pipelines
  2. Update by query
2.2 答案
  1. field_afield_bfield_c 都是数字
# 创建索引结构
PUT task1
{"mappings":{"properties":{"field_a":{"type":"integer"},"field_b":{"type":"integer"},"field_c":{"type":"integer"}}}
}# 批量写入数据
POST task1/_bulk
{"index":{}}
{"field_a":1,"field_b":2,"field_c":3}
{"index":{}}
{"field_a":3,"field_b":2,"field_c":3}
{"index":{}}
{"field_a":5,"field_b":2,"field_c":3}# 验证管道的正确性
POST _ingest/pipeline/_simulate
{"pipeline": {"processors": [{"script": {"lang": "painless","source": """ctx['new_field'] = ctx['field_a'] + ctx['field_b'] + ctx['field_c']; """}}]},"docs": [{"_source": {"field_a":5,"field_b":2,"field_c":3}}]
}# 创建管道
PUT _ingest/pipeline/my_pipeline
{"processors": [{"script": {"lang": "painless","source": """ctx['new_field'] = ctx['field_a'] + ctx['field_b'] + ctx['field_c']; """}}]
}# 更新索引数据
POST task1/_update_by_query?pipeline=my_pipeline# 检查结果
GET task1/_search
  1. field_afield_bfield_c 都是字符串,相同的管道就是字符串的拼接
# 创建索引结构
PUT task1
{"mappings":{"properties":{"field_a":{"type":"keyword"},"field_b":{"type":"keyword"},"field_c":{"type":"keyword"}}}
}# 批量写入数据
POST task1/_bulk
{"index":{}}
{"field_a":"a","field_b":"2","field_c":"c"}
{"index":{}}
{"field_a":"1","field_b":"2","field_c":"3"}
{"index":{}}
{"field_a":"d","field_b":"d","field_c":"hello"}# 验证管道的正确性
POST _ingest/pipeline/_simulate
{"pipeline": {"processors": [{"script": {"lang": "painless","source": """ctx['new_field'] = ctx['field_a'] + ctx['field_b'] + ctx['field_c']; """}}]},"docs": [{"_source": {"field_a":"a","field_b":"2","field_c":"c"}}]
}# 创建管道
PUT _ingest/pipeline/my_pipeline
{"processors": [{"script": {"lang": "painless","source": """ctx['new_field'] = ctx['field_a'] + ctx['field_b'] + ctx['field_c']; """}}]
}# 更新索引数据
POST task1/_update_by_query?pipeline=my_pipeline# 检查结果
GET task1/_search
  1. field_afield_bfield_c 是数字和字符串的混合,这里管道会统统当作字符串来处理,还挺有意思,和 java 一样。
# 创建索引结构
PUT task1
{"mappings":{"properties":{"field_a":{"type":"keyword"},"field_b":{"type":"keyword"},"field_c":{"type":"keyword"}}}
}# 批量写入数据
POST task1/_bulk
{"index":{}}
{"field_a":"a","field_b":2,"field_c":"c"}
{"index":{}}
{"field_a":"1","field_b":2,"field_c":"3"}
{"index":{}}
{"field_a":"d","field_b":2,"field_c":"hello"}# 验证管道的正确性
POST _ingest/pipeline/_simulate
{"pipeline": {"processors": [{"script": {"lang": "painless","source": """ctx['new_field'] = ctx['field_a'] + ctx['field_b'] + ctx['field_c']; """}}]},"docs": [{"_source": {"field_a":"1","field_b":2,"field_c":"3"}}]
}# 创建管道
PUT _ingest/pipeline/my_pipeline
{"processors": [{"script": {"lang": "painless","source": """ctx['new_field'] = ctx['field_a'] + ctx['field_b'] + ctx['field_c']; """}}]
}# 更新索引数据
POST task1/_update_by_query?pipeline=my_pipeline# 检查结果
GET task1/_search

在这里插入图片描述


http://www.ppmy.cn/ops/48850.html

相关文章

【深度学习】基于EANet模型的图像识别和分类技术

1.引言 1.1.EANet模型简介 EANet(External Attention Transformer)是一种深度学习模型,它结合了Transformer架构和外部注意力机制,特别适用于图像分类等计算机视觉任务。以下是关于EANet的详细解释: 1.1.1 定义与背…

重新安装 Windows 10 后如何恢复丢失的数据?

“嗨,我的 Windows 10 崩溃了,所以我不得不重新安装它。我使用 USB 可启动驱动器重新安装了操作系统。但是,重新安装后,C 盘上的所有先前文件都丢失了。有什么方法可以恢复丢失的文件吗?” - Jacky 在大多数情况下&am…

什么是props?

props(properties的缩写)在React中是用来传递数据的一种机制。它们是组件之间沟通的一种方式,允许父组件向子组件传递数据或配置。在React中,组件可以接受任意数量的props,这些props可以是任何数据类型,如字…

Three.js动效(第15辑):让前端手撕UI,拳打后端的效果。

three.js的设计效果非常复杂,后端提供的数据接口问题百出,这很容易让前端手撕UI、拳打后端,这种请详细该如何办呢? 前端 VS UI: 1. 沟通协调:UI和前端应该加强沟通,理解对方的工作难点和需求&…

Android APP memory统计方法

目录 进程的内存信息概述 关键的术语 测试步骤 测试步骤 数据处理 数据分析: 进程内存信息 Dumpsys meminfo -a PID Procrank Procmem PID 特殊内存信息 Mali ION(multi-media,gralloc) 进程地址空间信息 /proc/pid/smaps Showmap PID …

大数据开发语言Scala(一) - Scala入门

引言 在当今的大数据时代,数据量和数据处理的复杂性不断增加,传统的编程语言已经难以满足需求。Scala作为一门新兴的编程语言,以其简洁、强大和高效的特性,迅速成为大数据开发的热门选择。本文将详细介绍Scala语言的基础知识&…

C语言小例程20/100

题目&#xff1a;一个数如果恰好等于它的因子之和&#xff0c;这个数就称为"完数"。例如61&#xff0b;2&#xff0b;3.编程找出1000以内的所有完数。 #include<stdio.h> #define N 1000 int main() {int i,j,k,n,sum;int a[256];for(i2;i<N;i){suma[0]1;k…

Elixir学习笔记——别名、需要、导入和使用

为了便于软件重用&#xff0c;Elixir 提供了三个指令&#xff08;alias、require 和 import&#xff09;以及一个名为 use 的宏&#xff0c;总结如下&#xff1a; # 为模块添加别名&#xff0c;以便可以将其称为 Bar 而不是 Foo.Bar alias Foo.Bar, as: Bar # 需要模块才能使…