亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

FFIVE 2023-10-18 20:47:40
我有一個文件如何在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 Elasticsearchimport jsones = 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']預計出我的第一個預期輸出>我需要對輸出進行name排序第二個預期輸出 > 相對于namethen排序id如何進行修改search = es.search(index="data_new", body={"from" : 0, "size" : 2,"query": {"match_all": {}}})https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html我瀏覽了沒有幫助的網址
查看完整描述

2 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

在啟用之前fielddata,請考慮為什么要使用文本字段進行聚合、排序或在腳本中使用。這樣做通常沒有意義。


在建立索引之前會分析文本字段,以便可以通過搜索或 forNew York找到類似的值。當您可能需要一個名為newyorknewyorkNew 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":{

? ? ?

? ? ?}

? ?}

}

查看完整回答
反對 回復 2023-10-18
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

您可以對索引發出 PUT 請求以將 fielddata 設置為 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列名在哪里


查看完整回答
反對 回復 2023-10-18
  • 2 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號