一、题目
索引 movie
,保存的电影信息,title
是题目,tags
是电影的标签。写一个查询满足以下要求
- 在
title
中包含 my 或者 me。 - 如果在
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.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,副本为2
- 索引的
pattern
是 mymetrics-*,按要求在 mappings 里增加4个字段:hostname(keyword类型),error_message(text类型,并使用 standard analyzer),timestamp(时间戳),tags(keyword类型、数组),
根据以上信息创建模板,并创建一个数据流,名为 mymetrics-examprod,并向数据流中插入一条数据
2.1 考点
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"
]
}