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 考点
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"
}
}
}
}