2014-02-28 113 views
4

我有一個我在Elasticsearch中排序的控制檯平臺列表。Elasticsearch中的意外(不區分大小寫)字符串排序

這裏的 「姓名」 字段映射:

{ 
    "name": { 
     "type": "multi_field", 
     "fields": { 
      "name": { 
       "type": "string", 
       "index": "analyzed" 
      }, 
      "sort_name": { 
       "type": "string", 
       "index": "not_analyzed" 
      } 
     } 
    } 
} 

當我執行以下查詢

{ 
    "query": { 
    "match_all": {} 
    }, 
    "sort": [ 
     { 
      "name.sort_name": { "order": "asc" } 
     } 
    ], 
    "fields": ["name"] 
} 

我得到這些結果:

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 3, 
     "successful": 3, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 17, 
     "max_score": null, 
     "hits": [ 
      { 
       "_index": "platforms", 
       "_type": "platform", 
       "_id": "1393602489", 
       "_score": null, 
       "fields": { 
        "name": "GameCube" 
       }, 
       "sort": [ 
        "GameCube" 
       ] 
      }, 
      { 
       "_index": "platforms", 
       "_type": "platform", 
       "_id": "1393602490", 
       "_score": null, 
       "fields": { 
        "name": "Gameboy Advance" 
       }, 
       "sort": [ 
        "Gameboy Advance" 
       ] 
      }, 


    { 
      "_index": "platforms", 
      "_type": "platform", 
      "_id": "1393602498", 
      "_score": null, 
      "fields": { 
       "name": "Nintendo 3DS" 
      }, 
      "sort": [ 
       "Nintendo 3DS" 
      ] 
     }, 

     ...remove for brevity ... 

     { 
      "_index": "platforms", 
      "_type": "platform", 
      "_id": "1393602493", 
      "_score": null, 
      "fields": { 
       "name": "Xbox 360" 
      }, 
      "sort": [ 
       "Xbox 360" 
      ] 
     }, 
     { 
      "_index": "platforms", 
      "_type": "platform", 
      "_id": "1393602502", 
      "_score": null, 
      "fields": { 
       "name": "Xbox One" 
      }, 
      "sort": [ 
       "Xbox One" 
      ] 
     }, 
     { 
      "_index": "platforms", 
      "_type": "platform", 
      "_id": "1393602497", 
      "_score": null, 
      "fields": { 
       "name": "iPhone/iPod" 
      }, 
      "sort": [ 
       "iPhone/iPod" 
      ] 
     } 
    ] 
} 

萬事俱備如預期的那樣,除了iPhone/iPod結果在結尾(而不是在GameBoy Advance之後) - 爲什麼名稱中的/對排序有影響?

感謝

回答

15

好了,所以我發現原因是沒有什麼做的/。 ES會按大寫字母和小寫字母排序。在我添加'analyzer': 'sortable'sort_name多域的域映射

{ 
    "analysis": { 
     "analyzer": { 
      "sortable": { 
       "tokenizer": "keyword", 
       "filter": [ 
        "lowercase" 
       ] 
      } 
     } 
    } 
} 

然後:

我添加自定義分析器到索引創建的settings

+0

這真的是可以實現不區分大小寫的排序最簡單的方法? –

+0

這個工作但速度很慢 - 結果需要3秒來確定何時排序升序,15秒排序降序! – danday74

相關問題