Elasticsearch 认证模拟题 -2

一、题目

有一个索引 task3,其中有 fielda,fieldb,fieldc,fielde 现要求对 task3 重建索引,重建后的索引新增一个字段 fieldg 其值是fielda,fieldb,fieldc,fielde 的值拼接而成。

rust 复制代码
# 创建符合条件的 task3 索引,设置 field字段,并写入数据
PUT task3
{
  "mappings": {
    "properties": {
      "fielda":{
        "type": "keyword"
      },
      "fieldb":{
        "type": "keyword"
      },
      "fieldc":{
        "type": "keyword"
      },
      "fielde":{
        "type": "keyword"
      }
    }
  }
}

POST task3/_bulk
{"index":{}}
{"fielda":"aa","fieldb":"bb","fieldc":"cc","fielde":"dd"}
{"index":{}}
{"fielda":"中华","fieldb":"人民","fieldc":"共和国","fielde":"万岁"}
1.1 考点
  1. 重建索引
  2. 脚本
  3. 或可以通过 管道 来解
1.2 答案
rust 复制代码
# 定义新的索引
PUT task4
{
  "mappings": {
    "properties": {
      "fielda":{
        "type": "keyword"
      },
      "fieldb":{
        "type": "keyword"
      },
      "fieldc":{
        "type": "keyword"
      },
      "fielde":{
        "type": "keyword"
      },
      "fieldg":{
        "type": "keyword"
      }
    }
  }
}

#  重建索引
POST _reindex
{
  "source": {
    "index": "task3"
  },
  "dest": {
    "index": "task4"
  },
  "script": {
    "source": """
    ctx._source.fieldg = "";
    ctx._source.fieldg = ctx._source.fieldg + ctx._source.fielda + "-";
    ctx._source.fieldg = ctx._source.fieldg + ctx._source.fieldb + "-";
    ctx._source.fieldg = ctx._source.fieldg + ctx._source.fieldc + "-";
    ctx._source.fieldg = ctx._source.fieldg + ctx._source.fielde;
    """
  }
}

二、题目

在集群上有一个索引 task1,编写一个查询并满足以下要求:

  1. 定义一个名为 a 的运行时字段,通过 a 字段实现以下聚合(a 字段的值等于 b 字段减去 c 字段)

  2. 聚合a值小于-2的文档

  3. 聚合-5到5之间的文档

  4. 聚合大于5的文档

  5. 建立测试索引

rust 复制代码
# 创建索引
PUT task1
{
  "mappings": {
    "properties": {
      "b":{
        "type": "double"
      },
      "c":{
        "type": "double"
      }
    }
  }
}

# 写入数据
POST task1/_bulk
{"index":{}}
{"b":2,"c":3}
{"index":{}}
{"b":5,"c":1}
{"index":{}}
{"b":6,"c":1}
{"index":{}}
{"b":1,"c":7}
2.1 考点
  1. 聚合:这里用到的范围聚合
  2. 运行时字段
2.2 答案
rust 复制代码
# 使用 runtime_mappings 进行聚类
GET task1/_search
{
  "runtime_mappings": {
    "a": {
      "type": "double",
      "script": {
        "source": """
          emit(doc['b'].value - doc['c'].value)
        """
      }
    }
  },
  "aggs": {
    "bucket_ranges": {
      "range": {
        "field": "a",
        "ranges": [
          { "to": -2 },
          { "from": -5, "to": 5 },
          { "from": 5 }
        ]
      }
    }
  }
}
相关推荐
和裕1 小时前
全纸结构重型纸箱能否满足 1 吨以上设备出口熏蒸豁免要求?通关合规性全解析
大数据·运维·网络·人工智能·算法
代码方舟1 小时前
零信任架构实战:基于天远运营商三要素简版V即时版查询构建自动化电子投保实名核验网关
大数据·人工智能·架构·自动化
xiaohaiAIgeo2 小时前
【2026年】实验室应急预案中通风系统的关键作用
大数据·人工智能·科普知识
Sayai3 小时前
Elasticsearch 日志检索 DSL 实战:时间范围查询、字段去重、分钟级统计与最新日志获取
大数据·运维·elasticsearch·搜索引擎·日志分析
W***25923 小时前
2026 企业 AI 办公工具选型指南:可完成端到端任务的平台怎么评估
大数据·人工智能
龙亘川5 小时前
数字化赋能基层协同治理:亘川智城一网统管平台落地实践思考
大数据·人工智能·智慧城市·开源软件·数据可视化
可乐ea6 小时前
AI Agent 工具调用准确性评测:选择错误与参数错误分开测
大数据·人工智能·算法·大模型·工具调用·ai智能体·agent评测
数智顾问6 小时前
(90页PPT)IBM集团管理驾驶舱项目蓝图规划(附下载方式)
大数据·人工智能·物联网
西木莉6 小时前
大数据安全挑战升级:从隐私保护到信任构建
大数据
wtblszn7 小时前
空压机在线监测物联网方案
大数据·运维·物联网·自动化·能源