【问题标题】:Elastic search sorts the words within string itself, not the overall results弹性搜索对字符串本身的单词进行排序,而不是对整体结果进行排序
【发布时间】:2015-05-29 22:37:18
【问题描述】:

当我做类似的事情时

    {
  "sort": [
    {
      "name": {"order": "desc"}
    }
  ]}

生成的名称在该名称字符串本身内排序,而不是按整个结果排序:

"name" : "Test asdf"

排序值为

"sort": ["asdf"]

这是预期的吗?我怎样才能让它按名称字段对结果进行整体排序并按指定顺序排列它们(asc vs. desc)

任何帮助表示赞赏,谢谢!

【问题讨论】:

  • 在映射中将名称命名为multi field,其中一个版本设置为未分析,应用于排序。
  • 如果我无法修改映射,有什么办法可以解决吗?

标签: sorting elasticsearch


【解决方案1】:

如果您无法将映射更改为not_analyzed 或使用keyword 分析器,则使用脚本进行排序:

{
  "query": {
    "match_all": {}
  },
  "sort": {
    "_script": {
      "script": "_source.name",
      "lang": "groovy",
      "type": "string",
      "order": "desc"
    }
  }
}

当然,这需要启用动态脚本。如果您无法启用它,或者不想启用它,请将脚本放在文件中并在查询中使用它:

{
  "query": {
    "match_all": {}
  },
  "sort": {
    "_script": {
      "script_file": "source_sorting_script",
      "lang": "groovy",
      "type": "string",
      "order": "asc"
    }
  }
}

其中source_sorting_script.groovy 仅包含_source.name,应放在/config/scripts 目录下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 2011-10-16
    • 2021-11-14
    • 2020-02-10
    相关资源
    最近更新 更多