Elasticsearch 认证模拟题 -2

一、题目

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

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 }
        ]
      }
    }
  }
}
相关推荐
chevysky.cn2 小时前
Elasticsearch部署和集成
大数据·elasticsearch·jenkins
SelectDB技术团队3 小时前
森马服饰从 Elasticsearch 到阿里云 SelectDB 的架构演进之路
elasticsearch·阿里云·doris
青云交3 小时前
Java 大视界 -- Java 大数据在智能医疗远程手术机器人操作数据记录与分析中的应用(342)
java·大数据·数据记录·远程手术机器人·基层医疗·跨院协作·弱网络适配
Elasticsearch3 小时前
Elastic 被评为 2025 年 Gartner® 可观测平台魔力象限™中的领导者
elasticsearch
武子康3 小时前
大数据-38 Redis 分布式缓存 详细介绍 缓存、读写、旁路、穿透模式
大数据·redis·后端
Elasticsearch4 小时前
上下文更长 ≠ 更好:为什么 RAG 仍然重要
elasticsearch
时序数据说4 小时前
时序数据库的存储之道:从数据特性看技术要点
大数据·数据库·物联网·开源·时序数据库·iotdb
bxlj_jcj4 小时前
Flink时间窗口详解
大数据·flink