一尘不染

如何将此Elastic search功能分数查询隐蔽到Java API

elasticsearch

如何将下面的ES查询转换为Java API?我正在使用elasticsearch2.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查询,即使用Java API的 Elasticsearch
FunctionScore查询

但是下面的链接似乎是针对旧版ES的,我无法在elasticsearch2.3.3中找到这些静态函数。


阅读 252

收藏
2020-06-22

共1个答案

一尘不染

使用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()))));
                }
            }
2020-06-22