Py学习  »  Elasticsearch

Elasticsearch按自定义项权重排序

Niko Gamulin • 4 年前 • 843 次点击  

我已经存储了包含status属性的文档。我想按状态优先级(而不是按字母顺序)对文档进行排序。我按照前面的答案编写了以下函数,但仍然不能按预期工作;文档按状态名称(按字母顺序)排序:

function getESSortingByStatusQuery(query, order) {
        let statusOrder = ['BLUE', 'RED', 'BLACK', 'YELLOW', 'GREEN'];
        if(order == 'desc'){
            statusOrder.reverse();
        }
        const functions = statusOrder.map((item) => {
            const idx = statusOrder.indexOf(item);
            return {filter: {match: {statusColor: item}},
                weight: (idx + 1) * 50}
        });
        const queryModified = {
            "function_score": {
                "query": {"match_all": {}}, // this is for testing purposes and should be replaced with original query
                "boost": "5",
                "functions": functions,
                "score_mode": "multiply",
                "boost_mode": "replace"
            }
        }
        return queryModified;
    }

如果有人建议根据属性的预定义优先级(在本例中是status)对项目进行排序,我将不胜感激。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51223
 
843 次点击  
文章 [ 2 ]  |  最新文章 4 年前