简体   繁体   English

如何将此弹性搜索功能分数查询隐蔽到Java API

[英]How to covert this elastic search functional score query to java API

How to convert the below ES query to Java API? 如何将下面的ES查询转换为Java API? I am using elastic search 2.3.3 我正在使用弹性搜索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
      }
    }
 }

I have already tried to write this ES query using Java API using the link below Elasticsearch FunctionScore query using Java API 我已经尝试使用下面的链接使用Java API编写此ES查询,即使用Java API的Elasticsearch FunctionScore查询

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. 但是下面的链接似乎是针对旧版ES的,我无法在弹性搜索2.3.3中找到那些静态函数。

Achieved it as below using the java API 使用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()))));
                }
            }

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

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