模糊搜索的实现

Posted 雪山上的蒲公英

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模糊搜索的实现相关的知识,希望对你有一定的参考价值。

要实现模糊搜索的功能,及在模糊搜索框内输入要查询的信息,会在用户基础信息栏显示模糊搜索的结果

以用户名模糊搜索为例

页面上实现点击回车键即可进行搜索的功能

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

        <script type="text/javascript">
        <!--
        //按回车键提交表单信息
        document.onkeydown = function(e){
        if(!e) e = window.event;//火狐中是 window.event
        if((e.keyCode || e.which) == 13){
            var obtnSearch=document.getElementById("xqSelect")
            obtnSearch.focus();//让另一个控件获得焦点就等于让文本输入框失去焦点
          //  obtnSearch.click();
                }
            }
            
            var actionPath = $(\'#actionPath\').val();
            var path = $(\'#path\').val();
            var businessType = $(\'#businessType\').val();
            if(!businessType)
            {
                businessType="userType";
            }
            var jspPath = path+"webPage/sf_tree/infodata.jsp?businessType="+businessType+"&type=";
           // alert(actionPath);
            //alert(path);
            //alert(jspPath);
            userTree(jspPath,actionPath,path);
            
           
            //-->
        </script>


················································
<tr>
  <td>    <input type="text" id="txtNebulosity" name="txtNebulosity" size="25" style="height:60px;" /><br/>  <td> </tr>
<tr> <td>   <input type="button" id="nebulosity" name="nebulosity" value="模糊搜索" onclick="nebulosityQuery();" /> <td> </tr> ·····

在sf_leftQuery_js里编写模糊搜索的方法

/*
    模糊搜索
    @parm:queryId 点击查询DOM id
*/
function nebulosityQuery(queryId) {
    var cdbh = window.parent.$(\'#cdbh\').val();
    var nebulositySearch = function () {
        
        var cardNo = $("#cardNo").val();
        var codeNebulosity = $("#codeNebulosity").val();
        var nameNebulosity = $("#nameNebulosity").val();
        var addrNebulosity = $("#addrNebulosity").val();
        
        var cardNoNote = $("#cardNo").attr("noteval");
        var codeNote = $("#codeNebulosity").attr("noteval");
        var nameNote = $("#nameNebulosity").attr("noteval");
        var addrNote = $("#addrNebulosity").attr("noteval");
        $("#" + queryId).datagrid({
            queryParams : {
                "queryVo.firstRequest" : false,
                //"queryVo.nebulosity":$("#txtNebulosity").val(),
                "queryVo.codeNebulosity" : codeNebulosity == codeNote ? "" : codeNebulosity,
                "queryVo.nameNebulosity" : nameNebulosity == nameNote ? "" : nameNebulosity,
                "queryVo.addrNebulosity" : addrNebulosity == addrNote ? "" : addrNebulosity,
                "queryVo.yhkh" : cardNo == cardNoNote ? "" : cardNo,
                "treeUtil.fgs" : "",
                "queryVo.cdbh":cdbh,
            }
            
        });
    }
    $("#nebulosity").click(nebulositySearch);
    $(\'#txtNebulosity\').keypress(function (event) {
        onPressEnter(event, nebulositySearch);
    });
}

搜索的实现是依靠准确的sql查询语句来实现,以下为dao层创建查询sql语句的方法

private Object[] getSql(BDXCPageContext context, QueryVo queryVo) {
        String condition = "yh.xq||yh.yyhbm||yh.yhbm||yh.yhmc||yh.yhbh||yh.fgs||yh.qy||yh.fwz||yh.dz||yh.rlz||yh.yhkh||yh.gnzt||yh.lxdh||yh.jtdh||yh.qtdh||yh.sfzh||yh.kzfs||yh.cnfs||yh.yhlb";
        
        List list = new ArrayList();

        // 基础hql
        StringBuilder baseHql = new StringBuilder();
        // 基础sql
        StringBuilder baseSql = new StringBuilder();
        baseHql.append("from SfJmyhT yh where zf=0 ");
        baseSql.append(" select distinct yh.rowid rd,yh.yhbm,yh.dz from sf_jmyh_t yh left join sf_jmmj_t mj on yh.yhbh = mj.yhbh inner join (")
                .append("select xqqx.mc xq, rlzqx.mc rlz,fwzqx.mc fwz, qyqx.mc qy,fgsqx.mc  fgs, xqqx.yhlx, xqqx.sjqx from sf_sjqx_t xqqx ")
                .append("inner join sf_sjqx_t rlzqx on xqqx.sjbh = rlzqx.bh and xqqx.sjlx = 5 ")
                .append("inner join sf_sjqx_t fwzqx on rlzqx.sjbh = fwzqx.bh and rlzqx.sjlx = 4 ")
                .append("inner join sf_sjqx_t qyqx on fwzqx.sjbh = qyqx.bh and fwzqx.sjlx = 3 ")
                .append("inner join sf_sjqx_t fgsqx on qyqx.sjbh = fgsqx.bh and qyqx.sjlx = 2) qx ")
                .append("on yh.fgs = qx.fgs and yh.qy = qx.qy and yh.fwz = qx.fwz and yh.rlz = qx.rlz and yh.xq = qx.xq where yh.zf = 0");
        // 条件
        StringBuilder whereSql = new StringBuilder();
        //jsfs 为0代表全部查询
        if(!BDXCUtil.objectIsEmpety(queryVo.getJsfs())&&queryVo.getJsfs().equals("0")){
            
        //只查询二部制
        }else if (!BDXCUtil.objectIsEmpety(queryVo.getJsfs())&&queryVo.getJsfs().equals("二部制结算")) {
            whereSql.append(" and mj.jsfs = ?");
            list.add(queryVo.getJsfs());
        //查询计量结算之外的两种情况
        }else{
            whereSql.append(" and mj.jsfs != ?");
            list.add("计量结算");
        }
        // 分公司
        if (!BDXCUtil.objectIsEmpety(queryVo.getFgs())) {
            whereSql.append(" and yh.fgs= ?");
            list.add(queryVo.getFgs());
        }
        // 区域
        if (!BDXCUtil.objectIsEmpety(queryVo.getQy())) {
            whereSql.append(" and yh.qy= ?");
            list.add(queryVo.getQy());
        }
        // 服务站
        if (!BDXCUtil.objectIsEmpety(queryVo.getFwz())) {
            whereSql.append(" and yh.fwz= ?");
            list.add(queryVo.getFwz());
        }
        // 热源
        if (!BDXCUtil.objectIsEmpety(queryVo.getRy())) {
            whereSql.append(" and yh.ry= ?\'");
            list.add(queryVo.getRy());
        }
        // 换热站
        if (!BDXCUtil.objectIsEmpety(queryVo.getRlz())) {
            whereSql.append(" and yh.rlz= ?");
            list.add(queryVo.getRlz());
        }
        // 小区
        if (!BDXCUtil.objectIsEmpety(queryVo.getXq())) {
            whereSql.append(" and yh.xq= ?");
            list.add(queryVo.getXq());
        }
        // 用户卡号
        if (!BDXCUtil.objectIsEmpety(queryVo.getYhkh())) {
            whereSql.append(" and yh.yhkh= ?");
            list.add(queryVo.getYhkh());
        }
        // 用户编码
        if (!BDXCUtil.objectIsEmpety(queryVo.getYhbm())) {
            whereSql.append(" and yh.yhbm= ?");
            list.add(queryVo.getYhbm());
        }
        // 用户编号
        if (!BDXCUtil.objectIsEmpety(queryVo.getYhbh())) {
            whereSql.append(" and yh.yhbh= ?");
            list.add(queryVo.getYhbh());
        }
//        if (!BDXCUtil.objectIsEmpety(queryVo.getYhmc())) {
//            whereSql.append(" and yh.yhmc like ?");
//            list.add("%" + queryVo.getYhmc() + "%");
//        }
        // 用户名称 调整为精确查询 
        if (!BDXCUtil.objectIsEmpety(queryVo.getYhmc())) {
            whereSql.append(" and (yh.yhmc=? or yh.cap_yhmc= ?)");
            list.add(queryVo.getYhmc().toUpperCase());
            list.add(queryVo.getYhmc().toUpperCase());
        }
        // 合同编号
        if (!BDXCUtil.objectIsEmpety(queryVo.getHtbh())) {
            whereSql.append(" and yh.htbh= ?");
            list.add(queryVo.getHtbh());
        }
        // 楼号
        if (!BDXCUtil.objectIsEmpety(queryVo.getLh())) {
            whereSql.append(" and yh.lh= ?");
            list.add(queryVo.getLh());
        }
        // 单元号
        if (!BDXCUtil.objectIsEmpety(queryVo.getDyh())) {
            whereSql.append(" and yh.dyh= ?");
            list.add(queryVo.getDyh());
        }
        
        //楼层
        if (!BDXCUtil.objectIsEmpety(queryVo.getCs())) {
            whereSql.append(" and yh.cs= ?");
            list.add(queryVo.getCs());
        }
        //室号
        if (!BDXCUtil.objectIsEmpety(queryVo.getSh())) {
            whereSql.append(" and yh.sh= ?");
            list.add(queryVo.getSh());
        }
        
        
        // 层数
//        if (!BDXCUtil.objectIsEmpety(queryVo.getCs())) {
//            whereSql.append(" and yh.cs= ?");
//            list.add(new Integer(queryVo.getCs()));
//        }
        
        // 地址
        if (!BDXCUtil.objectIsEmpety(queryVo.getDz())) {
            whereSql.append(" and yh.dz like ?");
            list.add("%" + queryVo.getDz() + "%");
        }
        //单位名称
        if (!BDXCUtil.objectIsEmpety(queryVo.getDwmc())) {
            whereSql.append(" and yh.dwmc like ?");
            list.add("%" + queryVo.getDwmc() + "%");
        }
        // 供暖状态
        if (!BDXCUtil.objectIsEmpety(queryVo.getGnzt())) {
            //散户入网模块 只显示未装用户  由于链接中传参不可用中文,所以用字符代替
            if(!queryVo.getGnzt().equalsIgnoreCase("0")){
                whereSql.append(" and yh.gnzt= ?");
                list.add(queryVo.getGnzt());
            }else{
                whereSql.append(" and (yh.gnzt= ? or yh.gnzt = ?)");
                list.add("未装");
                list.add("未供");
            }
        }
        // 收费员
        if (!BDXCUtil.objectIsEmpety(queryVo.getSfy())) {
            whereSql.append(" and yh.sfy= ?");
            list.add(queryVo.getSfy());
        }
        // 用户类别
        if (!BDXCUtil.objectIsEmpety(queryVo.getYhlb())) {
            whereSql.append(" and yh.yhlb= ?");
            list.add(queryVo.getYhlb());
        }
        // 收费类别
        if (!BDXCUtil.objectIsEmpety(queryVo.getSflb())) {
            whereSql.append(" and yh.sflb= ?");
            list.add(queryVo.getSflb());
        }
        // 最小收费面积
        if (queryVo.getMinNum() != null) {
            whereSql.append(" and yh.sfmj >= ? ");
            list.add(queryVo.getMinNum());
        }
        // 最大收费面积
        if (queryVo.getMaxNum() != null && queryVo.getMaxNum() != 0) {
            whereSql.append(" and yh.sfmj <= ?");
            list.add(queryVo.getMaxNum());
        }
        // 拆网用户
        if (!BDXCUtil.objectIsEmpety(queryVo.getCwyh())) {
            whereSql.append(" and yh.gnzt not in (?)");
            list.add(queryVo.getCwyh());
        }
        // 锁闭阀
        if ("lockValveType".equals(queryVo.getBusinessType())) {
            whereSql.append(" and yh.sfazsbf = ?");
            list.add("是");
        }
        //根据系统菜单判断是单位还是居民用户
        if(!BDXCUtil.objectIsEmpety(queryVo.getCdbh())){
            if("137".equals(queryVo.getCdbh())){
                whereSql.append(" and yh.yhlb<>\'单位\' ");
            }
            if("503".equals(queryVo.getCdbh())){
                whereSql.append(" and yh.yhlb=\'单位\' ");
            }
        }
        // 角色编号
        whereSql.append(" and qx.sjqx like ? "); 
        list.add("%\'" + queryVo.getJsbh() + "\'%");

        String nebulosity = queryVo.getNebulosity();
        if (!BDXCUtil.objectIsEmpety(queryVo.getNebulosity())) {
            String[] nebs = nebulosity.replaceAll("  ", " ").split(" ");
            for (int i = 0; i < nebs.length; i++) {
                whereSql.append(" and instr(").append(condition).append(",?,1,1) > 0");
                list.add(nebs[i].trim());
            }
        }
        String codeNebulosity = queryVo.getCodeNebulosity();
        if (!BDXCUtil.objectIsEmpety(codeNebulosity)) {
            String[] nebs = codeNebulosity.replaceAll("  ", " ").split(" ");
            whereSql.append(" and ( ");
            for (int i = 0; i < nebs.length; i++) {
                if(i!=0)  whereSql.append(" and ");
                whereSql.append("  instr(").append(condition).append(",?,1,1) > 0");
                list.add(nebs[i].trim());
            }
            whereSql.append(" ) ");
        }
        String nameNebulosity = queryVo.getNameNebulosity();
        if (!BDXCUtil.objectIsEmpety(nameNebulosity)) {
            whereSql.append(" and instr(").append("YHMC").append(",?,1,1) > 0");
            list.add(nameNebulosity.trim());
        }
        String addrNebulosity = queryVo.getAddrNebulosity();
        if (!BDXCUtil.objectIsEmpety(addrNebulosity)) {
            whereSql.append(" and instr(").append("DZ").append(",?,1,1) > 0");
            list.add(addrNebulosity.trim());
        }
        String cardNo = queryVo.getYhkh();
        if (!BDXCUtil.objectIsEmpety(cardNo)) {
            whereSql.append(" and instr(yhkh,?,1,1) > 0");
            list.add(cardNo.trim());
        }
        
        // 排序sql
        StringBuilder orderSql = new StringBuilder();
        orderSql.append(" order by yh.yhbm, yh.yhbh");
        StringBuilder order = new StringBuilder();
        order.append(" order by yh.yhbm,yh.dz");
        // hql( =baseSql +whereSql +orderSql)
        StringBuilder hql = new StringBuilder();
        hql.append(baseHql.toString()).append(whereSql.toString()).append(orderSql.toString());
    
        
        // sql
        StringBuilder sql = new StringBuilder();
        sql.append("select yhbh,yhbm,yhmc,dz,jzmj,symj,cgmj,sfmj,gnzt,yhlb,yhzt,fgs,qy,fwz,rlz,xq, ry,lh,dyh,cs,sh,mp,yhkh,")
            .append("sfzh,jtdh,lxdh,qtdh,sfazsbf,rwbm,to_char(rwrq,\'yyyy-MM-dd\') rwrq,to_char(ksgnrq,\'yyyy-MM-dd\') ksgnrq,")
            .append("czy,kzfs,sfy,sfyxyhjf,sfyxyhcx,sfdbh,sfyxsf,byxsfyy,sfyxtg,byxtgyy,sfebz,fwwz,fwcx,hth,zdyx1,bz,grxz,ryxz,yyhbm,sfswhf,gnfs,sflntg ")
            .append("from sf_jmyh_t yh where rowid in(select distinct rd from (select distinct rd,rownum rn from (" + baseSql.toString() +  whereSql.toString() + order.toString() + " ) where rownum <= ?) where rn > ? ) ").append(orderSql.toString());
        // 查询记录数sql
        StringBuilder countSql = new StringBuilder();
        countSql.append("select count(distinct rd) num from (").append(baseSql.toString()).append(whereSql.toString()).append(")");
        // 查询合计sql
        StringBuilder sumSql = new StringBuilder();
        sumSql.append("select sum(jzmj) JZMJ,sum(symj) SYMJ,sum(cgmj) CGMJ,sum(sfmj) SFMJ,count(1) NUM from sf_jmyh_t where rowid in (").append(baseSql.toString()).append(whereSql.toString()).append(")");
        
        Object[] sqls = new Object[6];
        sqls[0] = list;
        sqls[1] = hql.toString();
        sqls[2] = sql.toString();
        sqls[3] = countSql.toString();
        sqls[4] = sumSql.toString();
        sqls[5] = baseSql.toString();
        return sqls;
    }

当执行模糊搜索控制台输出sql语句(这里为方便阅读做了一下分割)

select yhbh,yhbm,yhmc,dz,jzmj,symj,cgmj,sfmj,gnzt,yhlb,yhzt,fgs,qy,fwz,rlz,xq, ry,lh,dyh,cs,sh,mp,yhkh,sfzh,jtdh,lxdh,qtdh,sfazsbf,rwbm,to_char(rwrq,\'yyyy-MM-dd\') rwrq,to_char(ksgnrq,\'yyyy-MM-dd\') ksgnrq,czy,kzfs,sfy,sfyxyhjf,sfyxyhcx,sfdbh,sfyxsf,byxsfyy,sfyxtg,byxtgyy,sfebz,fwwz,fwcx,hth,zdyx1,bz,grxz,ryxz,yyhbm,sfswhf,gnfs,sflntg 

from sf_jmyh_t yh 
where 
rowid in(select distinct rd from (
select distinct rd,rownum rn from ( select distinct yh.rowid rd,yh.yhbm,yh.dz from sf_jmyh_t yh left join sf_jmmj_t mj on yh.yhbh = mj.yhbh inner join (select xqqx.mc xq, rlzqx.mc rlz,fwzqx.mc fwz, qyqx.mc qy,fgsqx.mc  fgs, xqqx.yhlx, xqqx.sjqx 

from sf_sjqx_t xqqx 
inner join 
sf_sjqx_t rlzqx 
on xqqx.sjbh = rlzqx.bh and xqqx.sjlx = 5 inner join sf_sjqx_t fwzqx on rlzqx.sjbh = fwzqx.bh and rlzqx.sjlx = 4 inner join sf_sjqx_t qyqx on fwzqx.sjbh = qyqx.bh and fwzqx.sjlx = 3 inner join sf_sjqx_t fgsqx on qyqx.sjbh = fgsqx.bh and qyqx.sjlx = 2) qx on yh.fgs = qx.fgs and yh.qy = qx.qy and yh.fwz = qx.fwz and yh.rlz = qx.rlz and yh.xq = qx.xq where 
yh.zf = 0 and yh.yhlb<>\'单位\'  and qx.sjqx like ?  order by yh.yhbm,yh.dz ) where rownum <= ?) where rn > ? )  

order by yh.yhbm, yh.yhbh

 

以上是关于模糊搜索的实现的主要内容,如果未能解决你的问题,请参考以下文章

js如何实现模糊查询

java如何实现异步模糊查询

mysql怎么实现全局的模糊搜索?

Ext JS 实现建议词模糊动态搜索功能

搜索框实现模糊查询方法 - js

MySQL简单实现多字段模糊查询