当前位置:网站首页>Mongo fuzzy query, with special characters that need to be escaped, and then query

Mongo fuzzy query, with special characters that need to be escaped, and then query

2022-06-22 04:35:00 12 procedural ape

mongdb Fuzzy query , There are also special symbols in the content to be retrieved , cause Unable to retrieve data , Want to achieve the detection effect , Need to be right Detect special characters in the content to escape , To achieve the retrieval effect

One 、 Escape regular special characters Method


    /** *  Escape regular special characters  ($()*+.[]?\^{},|) * * @param keyword * @return */
    public static String escapeExprSpecialWord(String keyword) {
    
        String[] fbsArr = {
     "\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|" };
        for (String key : fbsArr) {
    
            if (keyword.contains(key)) {
    
                keyword = keyword.replace(key, "\\" + key);
            }
        }
        return keyword;
    }

Two 、 application

        Criteria criteria = new Criteria();

        // Project name 
        if (!ObjectUtils.isEmpty(mixingStationProductionRequesJson.getFgcmc())) {
    
// criteria.and("fgcmc").regex(".*?" + mixingStationProductionRequesJson.getFgcmc() + ".*?");
            // Project name with (), It can not be detected   Use backslashes in parentheses "\" Transference  \(
            String name=escapeExprSpecialWord(mixingStationProductionRequesJson.getFgcmc());
            criteria.and("fgcmc").regex("^.*"+name+".*$");
        }
         Query query = Query.query(criteria);
         List<MixingStationRealDataVo> list=mongoTemplate.find(query,MixingStationRealDataVo.class);
原网站

版权声明
本文为[12 procedural ape]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220431191424.html