简体   繁体   English

如何根据字段值提高弹性搜索结果

[英]How to boost elastic search results based on a field value

I am using Query String to query elastic search and below is my sample query. 我正在使用查询字符串查询弹性搜索,下面是我的示例查询。

{
    "query_string" : {
        "query" : "Sharmila"
    }
}

I've field called FLAG in the documents. 我在文档中将字段称为FLAG。 I want to boost the results if the FLAG== Y. As custom_score is deprecated, I want to use function_score 如果FLAG == Y,我想提高结果。不建议使用custom_score,我想使用function_score

{
"query": {
    "function_score": {
        "query": {
            "query_string": {
                "query": "Sharmila"
            },
            "functions": [{
                "script_score": {
                    "script": "_score * (doc['FLAG'].value == 'Y' ? 1.2 : 1)"
                }
            }],
        }
    }
}
}

I am getting exception saying No query registered for [script_score]] 我收到异常消息,说没有为[script_score]注册查询

I am using elasticsearch 1.2.2 我正在使用elasticsearch 1.2.2

Any thoughts where I am doing wrong? 有什么想法我做错了吗?

You're almost there, you just need to move the functions property just below the function_score one and not bury it inside your query : 你几乎没有,你只需要在移动functions属性只是下面function_score一个,而不是埋葬你的内部query

{
"query": {
    "function_score": {
        "functions": [{
            "script_score": {
                "script": "_score * (doc['FLAG'].value == 'Y' ? 1.2 : 1)"
            }
        }],
        "query": {
            "query_string": {
                "query": "Sharmila"
            }
        }
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM