2017-09-09 76 views
1

我想按列表長度對搜索進行排序。我有一個字段由Elasticsearch如何按列表長度排序

"authors": { 
    "type": "text", "norms": { "enabled": False }, "analyzer": "autocomplete_with_asciifolding", "search_analyzer": "with_and_without_accent_search", "position_increment_gap": 100, 
    "fields": { 
     "raw" : { # This field is needed for your_paper suggestions search 
      "type" : "string", 
      "analyzer" : 'lower_keyword' 
     } 
    } 
} 

定義現在我想通過這個領域,這可能看起來像排序[「李四」,'邁克史密斯]

繼elasticsearch文檔和一些谷歌搜索我發現

doc['sort'] = { 
      "_script": { 
       "type": "number", 
       "script": "doc['authors'].length", 
       "order": "asc" 
      } 
     } 

我也試過DOC [ '作家'。values.length()和DOC [ '作家'。values.size(),但他們都導致

TransportError: TransportError(500, u'search_phase_execution_exception') 

任何想法如何按列表的長度進行排序?

回答

1

出於性能原因,您無法訪問doc['authors'],因爲field data is disabled in text fields by default。通過authors.raw代替

"authors": { 
    "type": "text", "norms": { "enabled": False }, "analyzer": "autocomplete_with_asciifolding", "search_analyzer": "with_and_without_accent_search", "position_increment_gap": 100, 
    "fields": { 
     "raw" : { # This field is needed for your_paper suggestions search 
      "type" : "string", 
      "analyzer" : 'lower_keyword', 
      "fielddata": True 
     } 
    } 
} 

和排序:

可以,例如,設置fielddata =真在你內部的場raw

doc['sort'] = { 
      "_script": { 
       "type": "number", 
       "script": "doc['authors.raw'].length", 
       "order": "asc" 
      } 
     } 

另一種解決方案是創建一個新的內場的類型keyword並按其分類。但我建議使用raw,因爲它看起來分析儀只是降低了情況。