且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在特定字段上使用进行排序

更新时间:2023-12-04 19:04:16

在启用 fielddata 之前,请考虑为什么将文本字段用于聚合,排序或在脚本中使用。

Before you enable fielddata, consider why you are using a text field for aggregations, sorting, or in a script. It usually doesn’t make sense to do so.

在索引之前分析文本字段,以便可以像 New York 这样的值进行索引。通过搜索 new york 可以找到。当您可能希望将单个存储桶称为时,此字段上的字词汇总将返回 new 存储桶和 york 存储桶纽约

A text field is analyzed before indexing so that a value like New York can be found by searching for new or for york. A terms aggregation on this field will return a new bucket and a york bucket, when you probably want a single bucket called New York

相反,您应该有一个用于全文搜索的文本字段,以及一个具有 doc_values 启用了聚合,如下所示:

Instead, you should have a text field for full text searches, and an unanalyzed keyword field with doc_values enabled for aggregations, as follows:

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

我想您已经可以使用 name.keyword 将名称作为关键字,如下所示,

I guess you can already treat name as keyword using name.keyword like below,

GET /data_new/_search
{
  "sort" : [
     { "name.keyword" : {"order" : "asc"}}
  ],
 "from":10,
 "size":149,
 "query":{
   "match_all":{
     
     }
   }
}

请参见

https://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/fielddata.html
> https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request- sort.html