Elasticsearch uri 按字段名排序

Elasticsearch uri sort by fieldname

如何通过查询在 ES 5.4 版本中对特定字段进行排序 url

http://localhost:9200/companies/emp/_search?q=*&sort=name:desc

我在这里搜索emp,并按降序显示emp名称。 我遇到异常

Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."

有什么帮助吗?

发生这种情况是因为您正尝试使用类型为 "text" 的字段对数据进行排序。如错误所述,您应该将类​​型从 "text" 更改为 "keyword" 或启用字段数据,但我建议您阅读此信息 https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata。 html 在启用它之前。

这里有一个很好的字段数据解释,以及为什么在文本字段上排序不能直接使用:https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

简而言之,文本字段是 运行 通过分析器的,它允许您在接近但不完全匹配的搜索中获得匹配。例如,从上面发布的 link 中,您可以搜索 "new" 或 "york" 并找到 "New York" 的值,因为它已通过分析器 运行。

如果您的字段是关键字,则必须搜索 精确 值才能找到它。

现在,这与排序有何关系:排序和聚合等操作需要知道字段的 确切 值才能对它们执行此类操作。当运行通过分析器时,存储的是分析值,而不是准确值。

我建议稍微更改您的映射以包含两种类型,如下所示:

PUT my_index
{
  "mappings": {
    "emp": {
      "properties": {
        "name": { 
          "type": "text",
          "fields": {
            "keyword": { 
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

http://localhost:9200/companies/emp/_search?q=*&sort=name.keyword:desc

你需要在名字后面加上关键字