模糊搜索的实现

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

以用户名模糊搜索为例

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

<%@ 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
时间: 2024-10-18 00:16:29

模糊搜索的实现的相关文章

研究模糊搜索

最近常听见旁边的大牛们说什么模糊搜索,自己想了想  有点尴尬   全忘了 今天看看复习了一下,话不多说  直接上代码. 1.html结构: <label for="searchShop" class="clear pos-a" style="top:17px;">            <input type="text" id="searchShop" placeholder="

用angular做的模糊搜索

今天大家来试一试用angular做一下简单的搜索功能吧: 首先我们需要写html的部分,我们需要设置几个条件,比如按什么来排序,按升序还是降序搜索,和一个文本框来设置模糊搜索: <nav> <select ng-model="a"> <option value="num">按编号排序</option> <option value="name">按姓名排序</option> &

模糊搜索 like 的优化 -- 问题仍在破解

跟随表哥学习了点内容.一点小问题就容易烦我一整天.但是到最后问题解决的时候,总是挺刺激的.于是乎就碰到了这么一个问题. 向数据库中插入一百万条记录,然后通过模糊搜索. 内容大概就是如此. 搜索的语句一开始是:select * from n_news where  content like "%中国%" limit 10 offset 3.一开始搜索,速度很快.一瞬间的事情,但是随着offset和length的值不断增大,的发现查询的结果原来越慢.几秒,十几秒.那么问题来了,如何优化查询

jquery+ajax 实现text框模糊搜索并可利用listbox实时显示模糊搜索列表结果

功能描述: text框中输入,text框下面的listbox中实时显示依据输入的内容进行模糊搜索的结果 js代码 1 $j(function() { 2 $j("input[id='txtCos']").bind('input propertychange', function () { searchCostCenterbyName($j(this).val()); }); 3 }); 4 5 function searchCostCenterbyName(parameters) {

专注UI——实用技术:模糊搜索

在现在的项目中,需要做模糊搜索,在以前技术的基础上很快得完成了第一版,大家先看看第一版的效果,我们一会做评论: 初级: 我们可能部分源码(附件中会有全部源码) <span style="font-size:18px;"> <div style="position:absolute;background-color:white;border-style:solid;border-width:1px;padding-top:2px;"> <

存储过程模糊搜索,按匹配率排序初探

最近的项目中有个搜索的功能,本来说,搜索简单做就可以了,直接like百分号就Ok了.但想了想,咱必须做点高大上的东西出来啊,再加上想练习下我并不熟练的存储过程,所以,决定搞得高大上些.以前做搜索的时候没有考虑那么多,但毕竟项目的使用者是广大用户,为了增加用户体验,所以应该提供给用户最想搜索的结果给用户,这里就涉及到了排序,而且是按匹配率排序,也可以说是精确度吧.网上找了会也没找到合适的.所以就自己写了. 首先说下我的思维原理:第一步,将要搜索的关键词进行分割,单独进行模糊匹配.例如:“我是中国人

专注UI——有用技术:模糊搜索

在如今的项目中.须要做模糊搜索,在曾经技术的基础上非常快得完毕了第一版.大家先看看第一版的效果,我们一会做评论: 0基础: 我们可能部分源代码(附件中会有所有源代码) <span style="font-size:18px;"> <div style="position:absolute;background-color:white;border-style:solid;border-width:1px;padding-top:2px;">

前端js模糊搜索(模糊查询)

1.html结构: <label for="searchShop" class="clear pos-a" style="top:17px;">            <input type="text" id="searchShop" placeholder="场所搜索">            <input type="button"

自制仿360首页支持拼音输入全模糊搜索和自动换肤

360首页搜索效果如下 1.完成编写的schoolnet校园网主要目录结构如下 主要实现支持中文.拼音首字母.拼音全字母的智能搜索和换肤 页面效果如下 主要核心代码如下 1.head.jsp <%@page import="java.io.File"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@