当前位置:   article > 正文

Elasticsearch 认证模拟题 - 16

Elasticsearch 认证模拟题 - 16

一、题目

创建一个搜索模版,要求 match_prase 查询,并且用指定的格式高亮,并排序

# 创建索引
PUT my_index
{
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "a":{
        "type": "text"
      },
      "b":{
        "type": "integer"
      }
    }
  }
}

# 灌入数据
POST my_index/_bulk
{"index":{}}
{"a":"elasticsearch in action", "b":1}
{"index":{}}
{"a":"kibana in action", "b":2}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
1.1 考点
  1. 搜索模板
  2. 高亮
  3. 排序
1.2 答案
# 创建索引模板
PUT _scripts/my_search_template
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match_phrase": {
          "a": "{{query_string}}"
        }
      },
      "highlight": {
        "fields": {
          "a": {
            "pre_tags": [
              "<em>"
            ],
            "post_tags": [
              "</em>"
            ]
          }
        }
      },
      "sort": [
        {
          "b": "desc"
        }
      ]
    }
  }
}

# 查询索引模板预览
POST _render/template
{
  "id": "my_search_template",
  "params": {
    "query_string": "hello world"
  }
}

# 查询结果验证
GET my_index/_search/template
{
  "id": "my_search_template",
  "params": {
    "query_string": "kibana is action"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

二、题目

查询索引 task1,获取每个月 price 字段的平均值,并返回 2022 年的数据。

PUT task1
{
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "rq":{
        "type": "date"
      },
      "price":{
        "type": "integer"
      }
    },
    "runtime":{
      "price_flag":{
        "type":"long",
        "script":{
          "source":
          """
          if(doc['price'].value<=100 && doc['price'].value>0) 
          emit(-1);
          if(doc['price'].value>100 && doc['price'].value<=200) 
          emit(1);
          """
        }
      }
    }
  }
}

POST task1/_bulk
{"index":{}}
{"rq":"2021-01-01","price":"50"}
{"index":{}}
{"rq":"2021-02-01","price":"150"}
{"index":{}}
{"rq":"2021-03-01","price":"250"}
{"index":{}}
{"rq":"2021-04-01","price":"50"}
{"index":{}}
{"rq":"2021-05-01","price":"250"}
{"index":{}}
{"rq":"2021-06-01","price":"150"}
{"index":{}}
{"rq":"2021-07-01","price":"50"}
{"index":{}}
{"rq":"2021-08-01","price":"450"}
{"index":{}}
{"rq":"2021-09-01","price":"80"}
{"index":{}}
{"rq":"2021-10-01","price":"550"}
{"index":{}}
{"rq":"2021-11-01","price":"50"}
{"index":{}}
{"rq":"2021-12-01","price":"500"}
{"index":{}}
{"rq":"2021-12-01","price":"50"}
{"index":{}}
{"rq":"2022-01-01","price":"200"}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
2.1 考点
  1. 聚合
  2. 聚合后通过查询过滤结果(这个知识点真的是一顿好找)
2.2 答案
GET task1/_search
{
  "aggs": {
    "every_month": {
      "date_histogram": {
        "field": "rq",
        "calendar_interval": "month"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  },
  "post_filter": {
    "range": {
      "rq": {
        "gte": "2021-12-31",
        "lte": "2023-01-01"
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号