Elasticsearch 认证模拟题 - 3

1、题目

有一索引有 3 个字段,请写一个查询去匹配这三个字段,并且将三个字段的评分相加作为最后的总评分

# 创建索引
PUT task
{
  "mappings": {
    "properties": {
      "fielda":{
        "type": "text"
      },
      "fieldb":{
        "type": "text"
      },
      "fieldc":{
        "type": "text"
      }
    }
  }
}

# 写入数据
POST task/_doc
{
  "fielda":"elasticsearch in action",
  "fieldb":"kibana in action",
  "fieldc":"logstash in action"
}
1.1 考点

1. 相似性检索才有评分
2. 评分函数

  1. 多字段匹配查询
1.2 答案
# 多字段匹配查询
GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "demo in action",
      "type":       "most_fields",
      "fields":     [ "fielda", "fieldb", "fieldc" ]
    }
  }
}

在这里插入图片描述

2、题目

聚合查询出字段 AvgTicketPrice 有最高平均值 的 DestCountry。(这个题目不确定是不是应该这么理解,找出 具有最大平均值 AvgTicketPriceDestCountry

# 使用的是 kibana 的示例数据
GET kibana_sample_data_flights/_search
{
  "size": 1
}

# Reponse
{
  "took" : 30,
  ...,
  "hits" : {
    ...
    "hits" : [
      {
        "_index" : "kibana_sample_data_flights",
        "_id" : "NWMHa48BBwcSojosOzyU",
        "_score" : 1.0,
        "_source" : {
          "FlightNum" : "XLL6LDF",
          "DestCountry" : "ZA",
          "OriginWeather" : "Thunder & Lightning",
          "OriginCityName" : "Jebel Ali",
          "AvgTicketPrice" : 642.5951482867853,
          "DistanceMiles" : 3942.7713488567097,
          "FlightDelay" : false,
          "DestWeather" : "Damaging Wind",
          "Dest" : "OR Tambo International Airport",
          "FlightDelayType" : "No Delay",
          "OriginCountry" : "AE",
          "dayOfWeek" : 4,
          "DistanceKilometers" : 6345.275413654453,
          "timestamp" : "2024-05-10T06:09:09",
          "DestLocation" : {
            "lat" : "-26.1392",
            "lon" : "28.246"
          },
          "DestAirportID" : "JNB",
          "Carrier" : "Logstash Airways",
          "Cancelled" : false,
          "FlightTimeMin" : 302.15597207878346,
          "Origin" : "Al Maktoum International Airport",
          "OriginLocation" : {
            "lat" : "24.896356",
            "lon" : "55.161389"
          },
          "DestRegion" : "SE-BD",
          "OriginAirportID" : "DWC",
          "OriginRegion" : "SE-BD",
          "DestCityName" : "Johannesburg",
          "FlightTimeHour" : 5.035932867979724,
          "FlightDelayMin" : 0
        }
      }
    ]
  }
}

2.1 考点
  1. 桶聚合
  2. 指标聚合
  3. 管道聚合
  4. 管道聚合 buckets_path 参数的语法
3.2 答案
GET kibana_sample_data_flights/_search
{
  "size": 0,
  "aggs": {
    "DestCountry-aggs": {
      "terms": {
        "field": "DestCountry"
      },
      "aggs": {
        "AvgTicketPrice-avg": {
          "avg": {
            "field": "AvgTicketPrice"
          }
        }
      }
    },
    "max_AvgTicketPrice": {
      "max_bucket": {
        "buckets_path": "DestCountry-aggs>AvgTicketPrice-avg"
      }
    }
  }
}

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值