ElasticSearch:按特定顺序排序

时间:2015-12-15 13:20:17

标签: elasticsearch

我的文档中有一个索引字符串字段,字母a-z为可能的值 是否可以按特定(给定)顺序对查询结果进行排序?
类似的东西:

{
    "sort" : [
        { "letters" : {"order" : ["k", "g", "a"...]}}
}

2 个答案:

答案 0 :(得分:0)

我认为不是排序,而是可以使用功能评分

"query": {
    "function_score": {
        "query": {},
        "functions": [
           {
                "field_value_factor": 
                {
                    "field": "rank",
                    "factor": 1
                }
            },
            {
                "filter": {"term": {"letters": "k"}},
                "weight": 50
            },
            {
                "filter": {"term": {"letters": "g"}},
                "weight": 40
            },
            {
                "filter": {"term": {"letters": "a"}},
                "weight": 30
            }
        ],
        "score_mode": "sum",
        "boost_mode": "sum"
    }
}

答案 1 :(得分:0)

您可以在ES查询中运行自定义脚本,以按特定顺序对结果进行排序

希望以下链接会有所帮助:)

https://discuss.elastic.co/t/sorting-or-ordering-by-specific-values/67244/2