以下elasticsearch查询按单term
分组,并根据自定义评分函数对存储桶进行排序:
GET /my-index/_search
{
"size": 0,
"query": {
"function_score": {
"score_mode": "sum",
"functions": [
{
"weight": 5,
"gauss": {
"relevant_at": {
"scale": "31d"
}
}
},
{
"weight": 2,
"gauss": {
"relevant_at": {
"scale": "365d"
}
}
}
],
"query": {
"multi_match": {
"fields": [
"admit_email",
"admit_name",
"scan_code"
],
"query": "my-query",
"type": "phrase_prefix",
"operator": "and"
}
}
}
},
"aggs": {
"by_order": {
"terms": {
"field": "order_id",
"size": 20,
"order": {
"max_score": "desc"
}
},
"aggs": {
"max_score": {
"max": {
"script": "_score"
}
}
}
}
}
}
}
我想添加第二个terms
分组。下一个查询在语法上是合法的,但它只根据_score
对子桶进行排序。返回的桶不是得分最高的桶,也不是正确的顺序:
GET /my-index/_search
{
"size": 0,
"query": {
"function_score": {
"score_mode": "sum",
"functions": [
{
"weight": 5,
"gauss": {
"relevant_at": {
"scale": "31d"
}
}
},
{
"weight": 2,
"gauss": {
"relevant_at": {
"scale": "365d"
}
}
}
],
"query": {
"multi_match": {
"fields": [
"admit_email",
"admit_name",
"scan_code"
],
"query": "my-query",
"type": "phrase_prefix",
"operator": "and"
}
}
}
},
"aggs": {
"by_order": {
"terms": {
"field": "order_id",
"size": 20
},
"aggs": {
"by_session": {
"terms": {
"field": "event_session_id",
"order": {
"max_score": "desc"
}
},
"aggs": {
"max_score": {
"max": {
"script": "_score"
}
}
}
}
}
}
}
}
我尝试了许多返回此错误的类似查询:
{
"error": {
"root_cause": [
{
"type": "aggregation_initialization_exception",
"reason": "Aggregator [max_score] of type [max] cannot accept sub-aggregations"
}
],
"type": "aggregation_initialization_exception",
"reason": "Aggregator [max_score] of type [max] cannot accept sub-aggregations"
},
"status": 500
}
我见过,正在使用@Veneeth Mohan的script approach。它有效,但我觉得应该有一种没有连接的方法。