首页 > 解决方案 > 在热门点击聚合中访问脚本排序上下文中的字段

问题描述

我有这个文件,我想这样排序:

我想在热门聚合中根据权重进行排序。例子 :

{
    "string_ticket_name": "ticket D",
    "object_tasks": [
        {
            "user_id": 1,
            "id_status_viewed": [
                1
            ],
            "id_status_new": [0]
        }
    ]
}

这是我尝试过的:

{
    "aggs": {
        "object_tasks": {
            "nested": {
                "path": "object_tasks"
            },
            "aggs": {
                "names": {
                    "top_hits": {
                        "sort": [
                            {
                                "_script": {
                                    "type": "number",
                                    "script": {
                                        "lang": "painless",
                                        "source": "for (item in params._source.id_status_viewed) { if (item == 1) {return 2;}} for (item in params._source.id_status_new) { if (item == 1) {return 0;}}"
                                    },
                                    "order": "asc"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}

但由于某种原因,我无法访问 id_status_viewed

"script_stack": [
                    "for (item in params._source.id_status_viewed) { ",
                    "                           ^---- HERE"
                ],
"reason": "Cannot invoke \"Object.getClass()\" because \"receiver\" is null"

下面有更多细节(映射)

{
    "mappings": {
        "properties": {
            "object_views": {
                "type": "nested",
                "properties": {
                    "number_rank": {
                        "type": "double"
                    },
                    "string_status": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword"
                            }
                        }
                    },
                    "user_id": {
                        "type": "long"
                    }
                }
            },
            "string_ticket_name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword"
                    }
                }
            },
            "object_tasks": {
                "type": "nested",
                "properties": {
                    "string_ticket_name": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword"
                            }
                        }
                    },
                    "user_id": {
                        "type": "long"
                    }
                }
            }
        }
    }
}

标签: elasticsearch

解决方案


nested上下文中,您不能迭代嵌套的父级——您只能访问“当前”嵌套的子文档。也就是说,而不是params._source你会使用doc[...]符号:

{
  "aggs": {
    "object_tasks": {
      "nested": {
        "path": "object_tasks"
      },
      "aggs": {
        "names": {
          "top_hits": {
            "sort": [
              {
                "_script": {
                  "type": "number",
                  "script": {
                    "lang": "painless",
                    "source": """
                      def status_viewed = doc['object_tasks.id_status_viewed'].empty ? null : doc['object_tasks.id_status_viewed'].value;
                      def status_new = doc['object_tasks.id_status_new'].empty ? null : doc['object_tasks.id_status_new'].value;
                      
                      if (status_viewed == 1) { return 2; }
                      if (status_new == 1) { return 0; }
                      
                      // fallback
                      return Long.MAX_VALUE;
                    """
                  },
                  "order": "asc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

推荐阅读