Elasticsearch 认证模拟题 - 14

一、题目

在集群中输入以下指令:

PUT phones/_doc/1
{
  "brand":"Samsumg",
  "model":"Galaxy S9+",
  "features":[
    {"type":"os", "value":"Android"},
    {"type":"storage", "value":"64"},
    {"type":"camera_resolution", "value":"12"}
    ]
}
PUT phones/_doc/2
{
  "brand":"Apple",
  "model":"iPhone XR",
  "features":[
    {"type":"os", "value":"Apple 10s"},
    {"type":"storage", "value":"128"},
    {"type":"camera_resolution", "value":"12"}
    ]
}

GET /phones/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "features.type": "storage"
        }},
        {"match": {
          "features.value": "12"
        }}
      ]
    }
  }
}

注意查询语句的查询结果,尽管它们的 type 字段值为 storage 时,value 字段的值都不等于 12,不知道为什么,特征数组的类型和值对象之间的关系丢失了。

现要求新建一个索引 task10,能够保持特征数组中对象和值之间的关系。并将上述两个文档写入到 task10 中,然后编写一个查询 type 字段值为 storage 时,value 字段的值等于 12 的 查询。此时上面两个文档都应该不在你的查询范围内。

1.1 考点
  1. Mapping 中的 Nested
  2. Query DSL 中的 Nested
1.2 答案
# 新建索引结果,设定正确的字段类型
PUT task10
{
  "mappings": {
    "properties": {
      "brand": {
        "type": "keyword"
      },
      "model": {
        "type": "keyword"
      },
      "features": {
        "type": "nested",
        "properties": {
          "type": {
            "type": "keyword"
          },
          "value": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

# 向新索引灌入数据
POST _reindex
{
  "source": {
    "index": "phones"
  },
  "dest": {
    "index": "task10"
  }
}

# 检查验证结果
GET task10/_search
{
  "query": {
    "nested": {
      "path": "features",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "features.type": "storage"
              }
            },
            {
              "match": {
                "features.value": "12"
              }
            }
          ]
        }
      }
    }
  }
}

GET task10/_search
{
  "query": {
    "nested": {
      "path": "features",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "features.type": "storage"
              }
            },
            {
              "match": {
                "features.value": "128"
              }
            }
          ]
        }
      }
    }
  }
}

在这里插入图片描述

二、题目

现有以下文档,请编写一个名为 test_data_stream 数据流满足以下请求:

{
  "@timestamp": "2099-03-08T11:04:05.000Z",
  "message": "test"
}
  1. 数据流索引的主分片数为 3,副本分片数为 1
  2. 将上述文档填充到数据流中去
2.1 考点
  1. 数据流的建立
  2. 数据流数据的写入
2.2 答案
# 建立索引模板
PUT _index_template/my_template
{
  "index_patterns": [
    "test_*"
  ],
  "data_stream": {},
  "template": {
    "settings": {
      "number_of_replicas": 1,
      "number_of_shards": 3
    }
  },
  "priority": 500
}

# 创建数据流
PUT _data_stream/test_data_stream

# 向数据流写入数据
POST test_data_stream/_doc
{
  "@timestamp": "2099-03-08T11:04:05.000Z",
  "message": "test"
}

# 获取数据流中的文档
GET test_data_stream/_search
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值