elasticsearch 中几种判断条件

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (rule.getOperator().equals(MatchType.equal.name())) {
            if (rule.getType().equals("int")) {
                boolQueryBuilder.must(QueryBuilders.matchPhraseQuery(rule.getAttribute(), rule.getKeyword()));
            } else {
                boolQueryBuilder.must(QueryBuilders.matchPhraseQuery(rule.getAttribute() + ".keyword", rule.getKeyword()));
            }
        } else if (rule.getOperator().equals(MatchType.not_equal.name())) {
            boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery(rule.getAttribute() + ".keyword", rule.getKeyword()));
        } else if (rule.getOperator().equals(MatchType.contains.name())) {
            if (rule.getType().equals("check")) {
                ArrayList arrayList = (ArrayList) rule.getKeyword();
                if (arrayList.size() > 0) {
                    if (arrayList.get(0) instanceof Long || arrayList.get(0) instanceof Integer) {
                        List listID = (List) (rule.getKeyword());
                        boolQueryBuilder.must(QueryBuilders.termsQuery(rule.getAttribute(), listID));
                    } else if (arrayList.get(0) instanceof String) {
                        List listKey = (List) rule.getKeyword();
                        for (String key : listKey
                        ) {
                            boolQueryBuilder.should(QueryBuilders.matchPhraseQuery(rule.getAttribute() + ".keyword", key));
                        }
                        boolQueryBuilder.minimumShouldMatch(1);
                    }
                }

            } else {
                if (StringObjUtil.isAlphaNumeric(rule.getKeyword().toString())) {
                    boolQueryBuilder.must(QueryBuilders.wildcardQuery(rule.getAttribute(), "*" + rule.getKeyword() + "*"));
                } else {
                    boolQueryBuilder.must(QueryBuilders.matchPhraseQuery(rule.getAttribute(), rule.getKeyword()));
                }
            }

        } else if (rule.getOperator().equals(MatchType.not_contains.name())) {
            if (rule.getType().equals("check")) {
                ArrayList arrayList = (ArrayList) rule.getKeyword();
                if (arrayList.size() > 0) {
                    if (arrayList.get(0) instanceof Long || arrayList.get(0) instanceof Integer) {
                        boolQueryBuilder.mustNot(QueryBuilders.termsQuery(rule.getAttribute(), arrayList));
                    } else if (arrayList.get(0) instanceof String) {
                        List listKey = (List) rule.getKeyword();
                        for (String key : listKey
                        ) {
                            boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery(rule.getAttribute() + ".keyword", key));
                        }
                    }
                }
            } else {
                if (StringObjUtil.isAlphaNumeric(rule.getKeyword().toString())) {
                    boolQueryBuilder.mustNot(QueryBuilders.wildcardQuery(rule.getAttribute(), "*" + rule.getKeyword() + "*"));
                } else {
                    boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery(rule.getAttribute(), rule.getKeyword()));
                }
            }
        } else if (rule.getOperator().equals(MatchType.isnull.name())) {
            if (rule.getType().equals("check")){
                boolQueryBuilder.mustNot(QueryBuilders.existsQuery(rule.getAttribute()));
            }else {
                boolQueryBuilder.mustNot(QueryBuilders.wildcardQuery(rule.getAttribute(), "*"));
            }
        } else if (rule.getOperator().equals(MatchType.not_isnull.name())) {
            if (rule.getType().equals("check")){
                boolQueryBuilder.must(QueryBuilders.existsQuery(rule.getAttribute()));
            }else {
                boolQueryBuilder.must(QueryBuilders.wildcardQuery(rule.getAttribute(), "*"));
            }
        } else if (rule.getOperator().equals(MatchType.range.name())) {
            if (rule.getType().equals("date")) {
                if (rule.getKeyword().toString().contains("~")) {
                    String[] dates = rule.getKeyword().toString().split("~");
                    LocalDateTime start = LocalDateTime.parse(dates[0], dateTimeFormatter);
                    LocalDateTime end = LocalDateTime.parse(dates[1], dateTimeFormatter);
                    boolQueryBuilder.must(QueryBuilders.rangeQuery(rule.getAttribute()).from(start).to(end));
                }
            } else {
                boolQueryBuilder.must(QueryBuilders.rangeQuery(rule.getAttribute()));
            }
        } else if (rule.getOperator().equals(MatchType.gt.name())) {
            if (rule.getType().equals("date")) {
                LocalDateTime start = LocalDateTime.parse(rule.getKeyword().toString(), dateTimeFormatter);
                boolQueryBuilder.must(QueryBuilders.rangeQuery(rule.getAttribute()).gt(start));

            } else {
                boolQueryBuilder.must(QueryBuilders.rangeQuery(rule.getAttribute()).gt(rule.getKeyword()));
            }
        } else if (rule.getOperator().equals(MatchType.lt.name())) {
            if (rule.getType().equals("date")) {
                LocalDateTime start = LocalDateTime.parse(rule.getKeyword().toString(), dateTimeFormatter);
                boolQueryBuilder.must(QueryBuilders.rangeQuery(rule.getAttribute()).lt(start));

            } else {
                boolQueryBuilder.must(QueryBuilders.rangeQuery(rule.getAttribute()).lt(rule.getKeyword()));
            }
        }
    

elasticsearch中,对应不同的数据类型,查询不同的条件查询基本写法。

 

contains("包含"),
/**
 * 是
 */
equal("等于"),
/**
 * 包含
 */
not_equal("不等于"),
/**
 * 包含
 */
not_contains("不包含"),

startsWith("开头匹配"),

endsWith("结尾匹配"),

isnull("为空"),

not_isnull("不为空"),

range("范围"),

gt("大于"),

lt("小于");

你可能感兴趣的:(java)