首页 > 解决方案 > Elasticsearch Painless 获取基于 Doc 值的动态参数

问题描述

在 NodeJS ES 客户端中,我试图传入一个由带有数字值的 id 键入的对象,并将其用于在我的 ES 查询中进行排序。

但是,我不断收到异常。是否可以根据文档中的值获取参数?

  const res = await client.search({
    index: 'some_index',
    size,
    from: 0,
    body: {
      sort: {
        _script: {
          type: 'number',
          script: {
            lang: 'painless',
            source: "params.get(doc['id'].value)",
            params: {
              'some-id': 10,
              'some-other-id': 3,
            },
          },
          order: 'asc',
        },
      },
    },
  })
{
  type: 'script_exception',
  reason: 'runtime error',
  script_stack: [
   "params.get(doc['id'].value)",
   "      ^---- HERE"
  ],
  script: "params.get(doc['id'].value)",
  lang: 'painless'
}

标签: elasticsearchelasticsearch-painless

解决方案


推荐阅读