Elasticsearch sorting on string not returning expected results

前端 未结 2 1082
一生所求
一生所求 2021-02-13 14:43

When sorting on a string field with multiple words, Elasticsearch is splitting the string value and using the min or max as the sort value. I.E.: when sorting on a field with th

相关标签:
2条回答
  • 2021-02-13 15:16

    If you want the sorting to be case-insensitive "index": "not_analyzed" doesn't work, so I've created a custom sort analyzer.

    index-settings.yml

    index :   
        analysis :
            analyzer :
                sort :
                    type : custom
                    tokenizer : keyword
                    filter : [lowercase]
    

    Mapping:

    ...
    "articleName": {
        "type": "string",
        "analyzer": "standard",
        "fields": {
            "sort": {
                "type": "string",
                "analyzer": "sort"
            }
        }
    }
    ...
    
    0 讨论(0)
  • 2021-02-13 15:17

    As mconlin mentioned if you want to sort on the unanalyzed doc field you need to specify "index": "not_analyzed" to sort as you described. But if you're looking to be able to keep this field tokenized to search on, this post by sloan shows a great example. Using multi-field to keep two different mappings for a field is very common in Elasticsearch.

    Hope this helps, let me know if I can offer more explanation.

    0 讨论(0)
提交回复
热议问题