Elasticsearch 认证模拟题 - 8

一、题目

在集群中输入以下指令:

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. nested 字段
  2. nested 查询
1.2 答案
# 创建索引
PUT task10
{
  "mappings": {
    "properties": {
      "brand": {
        "type": "keyword"
      },
      "model": {
        "type": "keyword"
      },
      "features": {
        "type": "nested", 
        "properties": {
          "type": {
            "type" : "keyword"
          },
          "value": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

# 写入数据
POST task10/_bulk
{"index":{}}
{"brand":"Samsumg","model":"Galaxy S9+","features":[{"type":"os","value":"Android"},{"type":"storage","value":"64"},{"type":"camera_resolution","value":"12"}]}
{"index":{}}
{"brand":"Apple","model":"iPhone XR","features":[{"type":"os","value":"Apple 10s"},{"type":"storage","value":"128"},{"type":"camera_resolution","value":"12"}]}
{"index":{}}
{"brand":"Apple","model":"iPhone XR","features":[{"type":"os","value":"Apple 10s"},{"type":"storage","value":"12"},{"type":"camera_resolution","value":"12"}]}

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

二、题目

二个索引,主索引是 miantable,用户索引是 usertablemiantable 通过 userid 关联 usertableusertable 总共有4个用户数据。

要求:

  1. 新建表 miantable_v1miantable_v1 包含 miantable 的所有字段
  2. miantable_v1 新增 userName ,通过 useridusertable 索引中 userName 关联到 miantable_v1
# 创建索引结构
PUT usertable
{
  "mappings": {
    "properties": {
      "userid": {
        "type": "integer"
      },
      "username": {
        "type": "keyword"
      }
    }
  }
}

PUT miantable
{
  "mappings": {
    "properties": {
      "f_crm_id": {
        "type": "integer"
      },
      "miantable_name": {
        "type": "keyword"
      },
      "userid": {
        "type": "integer"
      }
    }
  }
}

# 批量写入数据
POST usertable/_bulk
{"index":{"_id":1}}
{"userid":1, "username":"tom"}
{"index":{"_id":2}}
{"userid":2, "username":"white"}
{"index":{"_id":3}}
{"userid":3, "username":"john"}
{"index":{"_id":4}}
{"userid":4, "username":"green"}

POST miantable/_bulk
{"index":{"_id":1}}
{"f_crm_id":1, "miantable_name":"name1", "userid":1}
{"index":{"_id":2}}
{"f_crm_id":2, "miantable_name":"name2", "userid":2}
{"index":{"_id":3}}
{"f_crm_id":3, "miantable_name":"name3", "userid":3}
{"index":{"_id":4}}
{"f_crm_id":4, "miantable_name":"name4", "userid":1}
{"index":{"_id":5}}
{"f_crm_id":5, "miantable_name":"name5", "userid":1}
{"index":{"_id":6}}
{"f_crm_id":6, "miantable_name":"name6", "userid":2}
{"index":{"_id":7}}
{"f_crm_id":7, "miantable_name":"name7", "userid":2}
{"index":{"_id":8}}
{"f_crm_id":8, "miantable_name":"name8", "userid":4}
{"index":{"_id":9}}
{"f_crm_id":9, "miantable_name":"name9", "userid":4}
{"index":{"_id":10}}
{"f_crm_id":10, "miantable_name":"name10", "userid":1}
{"index":{"_id":11}}
{"f_crm_id":11, "miantable_name":"name11", "userid":2}
{"index":{"_id":12}}
{"f_crm_id":12, "miantable_name":"name12", "userid":5}
2.1 考点
  1. Enrich
  2. 重建索引

这里我认为直接看 enrich 的三个例子是最快能理解的

2.2 答案
# 新建enrich 策略
PUT /_enrich/policy/my-policy
{
  "match": {
    "indices": "usertable",
    "match_field": "userid",
    "enrich_fields": ["username"]
  }
}

# 执行策略
POST /_enrich/policy/my-policy/_execute

# 建立管道
PUT _ingest/pipeline/my-pipeline
{
  "processors" : [
    {
      "enrich": {
        "policy_name": "my-policy",
        "field": "userid",
        "target_field": "add_field",
        "max_matches": "1"
      }
    },
    {
      "rename": {
        "field": "add_field.username",
        "target_field": "username",
        "ignore_failure": true
      }
    },
    {
      "remove": {
        "field": "add_field",
        "ignore_failure": true
      }
    }
  ]
}

# 重建索引
POST _reindex
{
  "source": {
    "index": "miantable"
  },
  "dest": {
    "index": "miantable_v1",
    "pipeline": "my-pipeline"
  }
}

# 查询结果
GET miantable_v1/_search
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值