elasticsearch uri按字段名排序

m3eecexj  于 2021-05-29  发布在  Hadoop
关注(0)|答案(3)|浏览(610)

如何通过查询url对es5.4版本中的特定字段进行排序

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."

有什么帮助吗?

ercv8c1e

ercv8c1e1#

下面是fielddata的一个很好的解释,以及为什么在文本字段上排序不只是开箱即用:https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html
简言之,文本字段是通过分析器运行的,它允许您在接近的搜索中获得点击率,但不完全匹配。例如,从上面发布的链接中,您可以搜索“new”或“york”,并找到“new york”的值,因为它是通过分析器运行的。
如果您的字段是关键字,则必须搜索确切的值才能找到它。
现在,这与排序有何关系:排序和聚合等操作需要知道字段的确切值,才能对字段执行类似的操作。在运行分析程序时,会存储分析的值,而不是确切的值。
我建议您稍微更改Map以包含这两种类型,如下所示:

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

gg0vcinb2#

http://localhost:9200/companies/emp/\u search?q=*&sort=名称。keyword:desc
你需要在名字后面加关键字

tjrkku2a

tjrkku2a3#

这是因为您试图使用类型为“text”的字段对数据进行排序。正如错误所说,您应该将类型从“text”更改为“keyword”或启用fielddata,但我建议您阅读本文infohttps://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html 在启用之前。

相关问题