Elasticsearch 认证模拟题 - 16

一、题目

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

rust 复制代码
# 创建索引
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.1 考点
  1. 搜索模板
  2. 高亮
  3. 排序
1.2 答案
rust 复制代码
# 创建索引模板
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"
  }
}

二、题目

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

rust 复制代码
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"}
2.1 考点
  1. 聚合
  2. 聚合后通过查询过滤结果(这个知识点真的是一顿好找)
2.2 答案
rust 复制代码
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 小时前
专题:2025即时零售与各类人群消费行为洞察报告|附400+份报告PDF、原数据表汇总下载
大数据·人工智能
武子康1 小时前
大数据-30 ZooKeeper Java-API 监听节点 创建、删除节点
大数据·后端·zookeeper
小手WA凉1 小时前
Hadoop之MapReduce
大数据·mapreduce
二闹2 小时前
SLF4J/Logback 配置与 ELK 集成实战指南
后端·elasticsearch·logstash
AgeClub2 小时前
服务600+养老社区,Rendever如何通过“VR+养老”缓解老年孤独?
大数据·人工智能
SeaTunnel3 小时前
SeaTunnel 社区月报(5-6 月):全新功能上线、Bug 大扫除、Merge 之星是谁?
大数据·开源·bug·数据集成·seatunnel
时序数据说3 小时前
Java类加载机制及关于时序数据库IoTDB排查
java·大数据·数据库·物联网·时序数据库·iotdb
大数据CLUB5 小时前
基于spark的航班价格分析预测及可视化
大数据·hadoop·分布式·数据分析·spark·数据可视化
格调UI成品5 小时前
预警系统安全体系构建:数据加密、权限分级与误报过滤方案
大数据·运维·网络·数据库·安全·预警
reddingtons8 小时前
Adobe Firefly AI驱动设计:实用技巧与创新思维路径
大数据·人工智能·adobe·illustrator·photoshop·premiere·indesign