如何将此弹性搜索功能得分查询转换为java API [英] How to covert this elastic search functional score query to java API

查看:248
本文介绍了如何将此弹性搜索功能得分查询转换为java API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将以下ES查询转换为Java API?我正在使用弹性搜索2.3.3

How to convert the below ES query to Java API? I am using elastic search 2.3.3

 GET /schema_name/_search
 {
"from": 0,
"size": 200,
"query": {
    "function_score": {
        "query": {
            "match_all": {}
        },
        "boost": "5",
        "functions": [{
                "filter": {
                    "term": {
                        "alert_code": "event_rule_1"
                    }
                },
                "weight": 50
            },
            {
                "filter": {
                    "term": {
                        "alert_code": "event_rule_2"
                    }
                },
                "weight": 30
            },
            {
                "filter": {
                    "term": {
                        "alert_code": "event_rule_3"
                    }
                },
                "weight": 10
            },
            {
                "filter": {
                    "term": {
                        "alert_code": "event_rule_4"
                    }
                },
                "weight": 10
            },
            {
                "filter": {
                    "term": {
                        "alert_code": "event_rule_5"
                    }
                },
                "weight": 50
            },
            {
                "filter": {
                    "term": {
                        "alert_code": "event_rule_6"
                    }
                },
                "weight": 50
            }
        ],
        "max_boost": 50,
        "score_mode": "max",
        "boost_mode": "replace",
        "min_score": 0
      }
    }
 }

我有已经尝试使用Java API使用以下链接编写此ES查询
Elasticsearch FunctionScore使用Java API进行查询

I have already tried to write this ES query using Java API using the link below Elasticsearch FunctionScore query using Java API

但下面的链接似乎适用于较旧的ES版本,我无法在弹性搜索2.3.3中找到这些静态函数。

But the link below seems to be for a older ES version and i am unable to find those static functions in elastic search 2.3.3.

推荐答案

使用java API实现如下

Achieved it as below using the java API

FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders
                    .functionScoreQuery(queryBuilder)
                    .setMinScore(0f)
                    .maxBoost(50f)
                    .scoreMode("max")
                    .boostMode(CombineFunction.REPLACE);

            for (String alertCode : ruleCodesLowerCase) {
                if(alertPriorityMap.get(alertCode.toUpperCase()) != null){
                    functionScoreQueryBuilder.add(QueryBuilders.termQuery(AlertESEnum.ALERT_CODE_FIELD.value(), 
                            alertCode), ScoreFunctionBuilders.weightFactorFunction((AlertPriority.intValue(alertPriorityMap.get(alertCode.toUpperCase()).getPriority()))));
                }
            }

这篇关于如何将此弹性搜索功能得分查询转换为java API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆