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.2 答案
# 多字段匹配查詢
GET /_search
{"query": {"multi_match" : {"query": "demo in action","type": "most_fields","fields": [ "fielda", "fieldb", "fieldc" ]}}
}
2、題目
聚合查詢出字段 AvgTicketPrice
有最高平均值 的 DestCountry
。(這個題目不確定是不是應該這么理解,找出 具有最大平均值 AvgTicketPrice
的 DestCountry
)
# 使用的是 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 考點
- 桶聚合
- 指標聚合
- 管道聚合
- 管道聚合
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"}}}
}