• Elasticsearch 认证模拟题 - 5


    一、题目

    .在集群上有一个索引 food_ingredient,搜索需要满足以下要求:

    1. 三个字段 manufacturernamebrand 都能匹配到文本 cake mix
    2. 高亮 字段 name,并加标签
    3. 排序,对字段 brand 正序,_score 降序,返回 20 个文档
    # 创建符合条件的 task 索引,设置 field 字段,并写入数据
    PUT food_ingredient
    {
      "mappings": {
        "properties": {
          "manufacturer":{
            "type": "text"
          },
          "name":{
            "type": "text"
          },
          "brand":{
            "type": "text"
          }
        }
      }
    }
    
    # 写入数据
    POST food_ingredient/_bulk
    {"index":{}}
    {"manufacturer": "cake mix", "name": "cake mix", "brand": "cake mix"}
    
    
    1.1 考点
    1. must 查询
    2. 高亮
    3. 排序
    1.2 答案
    GET food_ingredient/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "manufacturer": "cake mix"
              }
            },
            {
              "match": {
                "name": "cake mix"
              }
            },
            {
              "match": {
                "brand": "cake mix"
              }
            }
          ]
        }
      },
      "highlight": {
        "fields" : {
          "name" : { "pre_tags" : [""], "post_tags" : [""] }
        }
      }, 
      "sort": [
        {
          "brand.keyword": {
            "order": "asc"
          }
        },
        {
          "_score": {
            "order": "desc"
          }
        }
      ]
    }
    

    在这里插入图片描述

    24-06-30
    注: text类型不能排序,这里的 brand 字段应该为 keyword 类型

    PUT food_ingredient
    {
      "mappings": {
        "properties": {
          "manufacturer":{
            "type": "text"
          },
          "name":{
            "type": "text"
          },
          "brand":{
            "type": "keyword"
          }
        }
      }
    }
    

    二、题目

    集群中有 earthquakes 索引,timestamp 字段的格式为 yyyy-MM-dd HH:mm:ss。对 earthquakes 索引按月分桶,并且对 magnitudedepth 进行最大值聚合。

    # 创建索引
    PUT earthquakes
    {
      "settings": {
        "number_of_replicas": 0
      },
      "mappings": {
        "properties": {
          "timestamp":{
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "magnitude":{
            "type": "float"
          },
    	  "type":{
    	    "type":"integer"
    	  },
    	  "depth":{
    	    "type":"float"
    	  }
        }
      }
    }
    
    # 导入数据
    POST earthquakes/_bulk
    {"index":{}}
    {"timestamp":"2012-01-01 12:12:12", "magnitude":4.56, "type":1, "depth":10}
    {"index":{}}
    {"timestamp":"2012-01-01 15:12:12", "magnitude":6.46, "type":2, "depth":11}
    {"index":{}}
    {"timestamp":"2012-02-02 13:12:12", "magnitude":4, "type":2, "depth":5}
    {"index":{}}
    {"timestamp":"2012-03-02 13:12:12", "magnitude":6, "type":3, "depth":8}
    {"index":{}}
    {"timestamp":"1967-03-02 13:12:12", "magnitude":6, "type":2, "depth":6}
    
    2.1 考点
    1. 分桶聚合
    2. 指标聚合
    2.2 答案
    GET earthquakes/_search
    {
      "size": 0,
      "aggs": {
        "sales_over_time": {
          "date_histogram": {
            "field": "timestamp",
            "calendar_interval": "month"
          },
        "aggs": {
          "max_magnitude": {
            "max": {
              "field": "magnitude"
            }
          },
          "max_depth": {
            "max": {
              "field": "depth"
            }
          }
        }
        }
      }
    }
    

    24-06-30
    这里给一个更优的解,避免有太多的无用的 bucket

    GET earthquakes/_search
    {
      "size": 0, 
      "aggs": {
        "range_timestamp": {
          "date_histogram": {
            "field": "timestamp",
            "calendar_interval": "month",
            "min_doc_count": 1
          },
          "aggs": {
            "max_magnitude": {
              "max": { "field": "magnitude" }
            },
            "max_depth": {
              "max": { "field": "depth" }
            }
            
          }
        }
      }
    }
    
  • 相关阅读:
    VBA-自定义面板,使用SQL查询Excel数据
    Web 端项目系统访问页面很慢,后台数据返回很快,网络也没问题,是什么导致的呢?
    C++医学临床影像信息管理系统源码
    云环境下集合隐私计算-解读
    MarkDown+Hbuilder学习
    合肥工业大学计算机网络实验二
    oracle 数据库建集群式数据库的DBLINK的语法
    html列表
    房产中介租房小程序系统开发搭建
    人间清醒,内容为王 - 技术er究竟该如何写博客?1024上海嘉年华之敖丙演讲观后感。
  • 原文地址:https://blog.csdn.net/Wolf_xujie/article/details/139355002