Elasticsearch如何按列表长度排序

时间:2017-09-09 08:36:01

标签: 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'
        }
    }
}

现在我想根据这个字段进行排序,这可能看起来像[' John Doe'迈克史密斯']

根据elasticsearch文档和一些谷歌搜索,我找到了

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

我还尝试了doc [' authors']。values.length()和doc [' authors']。values.size(),但所有这些都导致

TransportError: TransportError(500, u'search_phase_execution_exception')

任何想法如何按列表的长度排序?

1 个答案:

答案 0 :(得分:1)

由于性能原因,您无法doc['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 = true:

authors.raw

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

keyword

另一种解决方案是创建一个raw类型的新内部字段并按其排序。但我建议使用MS Access (2013),因为它看起来只是降低了分析结果。