ElasticSearch按多个脚本排序

时间:2019-04-03 00:38:54

标签: sorting elasticsearch elastic-stack

我正在尝试使用多个脚本对Elastic 6.7中的结果进行排序

不幸的是,我没有找到尝试多种组合的方法。

我不能仅使用一个脚本来完成所有操作,因为过滤器和排序是动态的。

这是我的查询

{
  "query": {
    "bool": {
      "must": [],
      "filter": []
    }
  },
  "sort": {
    "_script": [
      {
        "type": "number",
        "order": "desc",
        "script": {
          "lang": "painless",
          "source": "doc['status.keyword'].value == 'active' ? 1 : 0;"
        }
      },
      {
        "type": "number",
        "order": "desc",
        "script": {
          "lang": "painless",
          "source": "(doc['s_price'].value/doc['currency.usd_c'].value*params.cvs)",
          "params": {
            "cvs": 42.71
          }
        }
      }
    ]
  },
}

谢谢!

1 个答案:

答案 0 :(得分:1)

POST /_update_by_query
{
  "script": {
    "source": "ctx._source.active += ctx._source['status.keyword'].value == 'active' ? 1 : 0;",
    "lang": "painless"
  },
  "query": {
    "match_all": {}
  }
}

GET /_search
{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                  "must": [],
                  "filter": []
                }
              },
            "script_score" : {
                "script": {
                  "source": "(doc['s_price'].value/doc['currency.usd_c'].value*params.cvs)",
                  "params": {
                    "cvs": 42.71
                  }
                }
            }
        }
    },
    "sort": [{
        "active": "desc"
    },
    {
        "score": "desc"
    }]
}

使用批量更新为其中一个查询添加字段active。并使用function_score查询来计算另一个值。首先按active,然后按score

排序