如何在特定领域使用排序

How to do sort in using on particular field

我有一个文件

如何在 elasticsearch 的特定字段上使用排序

我的查询如下

{
   "sort":{
      "name":"desc"
   },
   "from":10,
   "size":149,
   "query":{
      "match_all":{
         
      }
   }
}

我收到错误

Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [name] in order to load field data by uninverting the inverted index. Note that this can use significant memory.')\"}" }

我的索引名称是data_new

下面是插入索引的代码

test = [   {'id':1,'name': 'Cost Accounting 400', 'professor': ['Bill Cage', 'accounting']},
    { 'id':2,  'name': 'Computer Internals 250', 'professor': ['Gregg Payne', 'engineering']},
    {'id':3,   'name': 'Accounting Info Systems 350',   'professor': ['Bill Cage', 'accounting']},
    {'id':4,'name': 'Tax Accounting 200', 'professor': ['Thomas Baszo', 'finance']},
    {'id':5,'name': 'Capital Markets 350', 'professor': ['Thomas Baszo', 'finance']},
    {'id':6,'name': 'Theatre 410', 'professor': ['Sebastian Hern', 'art']},
    {'id':7,'name': 'Accounting 101', 'professor': ['Thomas Baszo', 'finance']},
    {'id':8,'name': 'Marketing 101', 'professor': ['William Smith', 'finance']},
    {'id':8,'name': 'Anthropology 230', 'professor': ['Devin Cranford', 'history']},
    {'id':10,   'name': 'Computer Science 101',
        'professor': ['Gregg Payne', 'engineering']}]
from elasticsearch import Elasticsearch
import json
es = Elasticsearch()
es.indices.create(index='data_new', ignore=400)
for e in test:
        es.index(index="data_new", body=e, id=e['id'])
search = es.search(index="data_new", body={"from" : 0, "size" : 2,"query": {"match_all": {}}})
search['hits']['hits']

预计结束

如何修改search = es.search(index="data_new", body={"from" : 0, "size" : 2,"query": {"match_all": {}}})

我经历了 url https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html 这没有帮助

您可以对索引发出 PUT 请求以将字段数据设置为 true:

curl --location --request PUT 'http://localhost:9200/index_name/info/_mapping' \
--header 'Content-Type: application/json' \
--data-raw '{
  "properties": {
     "desc": { 
       "type":     "text",
       "fielddata": true
     }
  }
}'

其中 desc 是列名

在启用 fielddata 之前,请考虑为什么要使用文本字段进行聚合、排序或在脚本中使用。这样做通常没有意义。

在建立索引之前分析文本字段,以便可以通过搜索 newyork 找到类似 New York 的值。此字段上的术语聚合将 return 一个 new 桶和一个 york 桶,当您可能想要一个名为 New York

的桶时

相反,您应该有一个用于全文搜索的文本字段,以及一个为聚合启用 doc_values 的未分析关键字字段,如下所示:

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

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

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

:

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