简体   繁体   中英

How to covert this elastic search functional score query to java API

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
      }
    }
 }

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

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.

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()))));
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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