Elasticsearch 认证模拟题 - 7

一、题目

索引 movie,保存的电影信息,title 是题目,tags 是电影的标签。写一个查询满足以下要求

  1. title 中包含 my 或者 me
  2. 如果在 tags 中包含 romatic movies,该条算分提高,如果不包含则算分不变
PUT movie
{
  "mappings": {
    "properties": {
      "title":{
        "type":"text"
      },
      "tags":{
        "type": "keyword"
      }
    }
  }
}

POST movie/_bulk
{"index": {}}
{"title":"my me", "tags":["romatic movies"]}
{"index": {}}
{"title":"me", "tags":["romatic movies", "action movies"]}
{"index": {}}
{"title":"me", "tags":["action movies"]}
{"index": {}}
{"title":"I", "tags":["action movies"]}
{"index": {}}
{"title":"me", "tags":["horror movie"]}
1.1 考点
  1. bool 查询
  2. boost 参数提高评分
1.2 答案

仔细看了一下题,这个答案现在应该是不对的,错误的答案留着要警醒自己啊

GET movie-1/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "my"
          }
        },
        {
          "match": {
            "title": "me"
          }
        },
        {
          "term": {
            "tags": {
              "value": "romatic movies",
              "boost": 2
            }
          }
        }
      ]
    }
  }
}

正确答案:

POST movie/_search
{
  "query": {
    "bool": {
      "must": {
        "bool": {
          "should": [
            {
              "term": {
                "title": "me"
              }
            },
            {
              "term": {
                "title": "my"
              }
            }
          ]
        }
      },
      "should": [
        {
          "term": {
            "tags": "action movies"
          }
        }
      ],
      "boost": 1
    }
  }
}

在这里插入图片描述

24-06-30:发现这个题其实 用function_score 做更简单一些,但是 bool 组合真的更容易写处理来

GET movie/_search
{
  "query": {
    "function_score": {
      "query": { 
        "bool": {
          "should": [
            {
              "match": {
                "title": "my"
              }
            },
            {
              "match": {
                "title": "me"
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": { "match": { "tags": "action movies" } },
          "weight": 10
        }
      ],
      "score_mode": "sum",
      "boost_mode": "sum"
    }
  }
}

在这里插入图片描述

二、题目

编写一个名为 a_data_stream 的数据流满足以下请求:

  1. 数据流索引主分片数为1,副本为2
  2. 索引的 patternmymetrics-*,按要求在 mappings 里增加4个字段:hostname(keyword类型),error_message(text类型,并使用 standard analyzer),timestamp(时间戳),tags(keyword类型、数组),

根据以上信息创建模板,并创建一个数据流,名为 mymetrics-examprod,并向数据流中插入一条数据

2.1 考点
  1. 创建模板
  2. 创建数据流
  3. 数据流写入数据
2.2 答案
POST /_index_template/data_stream
{
  "index_patterns": [
    "mymetrics-*"
  ],
  "template": {
    "settings": {
      "index.number_of_shards": 1,
      "index.number_of_replicas": 2
    },
    "mappings": {
      "properties": {
        "hostname": {
          "type": "keyword"
        },
        "error_message": {
          "type": "text",
          "analyzer": "standard"
        },
        "timestamp": {
          "type": "date"
        },
        "tags": {
          "type": "keyword"
        }
      }
    }
  },
  "priority": 500,
  "data_stream": {}
}

PUT _data_stream/mymetrics-examprod

POST mymetrics-examprod/_doc
{
  "hostname": "chenqf",
  "errormessage": "I am mistake",
  "@timestamp": "2021-12-27T09:26:22.000Z",
  "tags": [
    "dream",
    "sing"
  ]
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值