在写WEB项目的时候,搜索是必须提供的功能
有时候,客户会输入用空格或其他分隔符分隔的一组关键词,我们需要根据输入找出以下结果:
绝对包含整组关键词连续出现的结果
包含整组关键词里每个词条,但是不一定连续出现的结果
包含整组关键词里任一条或多条关键词的结果

就像你在GOOGLE里搜索SQL SERVER 2000,
它首先严格匹配整个字符串
在找不到的情况下,看有没有包含这三个单词的结果
如果连三个单词都包含的结果都没有,就试着找出包含任一个词的结果
下面的代码是用C#在代码中动态构建这种搜索模式的条件语句的函数
其中KEYWORD就是关键词了
SEARCHMODE就是搜索模式了,1代表精确,2,代表全包含 ,3代表任意包含

string strCondition="Id in (";
            string strSubQuery="select distinct productid from productcateview where id<>0 ";
            if(Request.QueryString["Country"]!=null)
                strSubQuery+=" and industryRegionId="+Request.QueryString["Country"];
            if(Request.QueryString["Industry"]!=null)
                strSubQuery+=" and IndustryId="+Request.QueryString["Industry"];
            if(Request.QueryString["Keyword"]!=null)
            {                
                //SearchMode:搜索模式,1,代表精确搜索,2代表同时包含搜索3,代表的是包含任一单词的搜索
                string strKeyword=Server.UrlDecode(Request.QueryString["Keyword"].Replace("'","''"));
                string strTemp="";
                strSubQuery+=" and (";
                string [] KeywordArray;
                if(strKeyword.IndexOf(',')!=-1)
                    KeywordArray=strKeyword.Split(',');
                else
                    if(strKeyword.IndexOf('|')!=-1)
                    KeywordArray=strKeyword.Split('|');
                else
                    KeywordArray=strKeyword.Split(null);
                //分主关键词
                if(KeywordArray.Length<2||strSearchMode=="1")
                {
                    strSubQuery+="( productfullname+' '+cast(shortdesc as varchar)+' '+keyword+' '+subcategoryname like '%"+strKeyword+"%')";
                }
                else
                {    
                    if(strSearchMode=="2")
                    {
                        strTemp="productfullname+' '+cast(shortdesc as varchar)+' '+keyword+' '+subcategoryname like '";
                        string strSqlKeyword="";
                        for (int i=0;i<KeywordArray.Length;i++)
                        {
                            strSqlKeyword+="%"+KeywordArray[i];
                        }
                        strTemp+=strSqlKeyword+"%'";
                    }
                    if(strSearchMode=="3")
                    {
                        strTemp="";
                        for (int i=0;i<KeywordArray.Length;i++)
                        {
                            strTemp+=" productfullname+' '+cast(shortdesc as varchar)+' '+keyword+' '+subcategoryname like '%"+KeywordArray[i]+"%' or";
                        }
                        strTemp=strTemp.TrimEnd(new char[] {'o','r'});
                    }
                    strSubQuery+=strTemp;
                }
                strSubQuery+=")";
            }
            strCondition+=strSubQuery;
            strCondition+=")";
            return(strCondition);    

三种模式中,第一种会构造出类似下面的SQL条件
Field1+field2+field3 like %keyword%
第二种构造出
field1+field2+field3 like %keyword1%keyword2%keyword3%
第三种构造出
field1+field2+field3 like %keyword1% or field1+field2+field3 like %keyword2% or field1+field2+field3 like%keyword3%
这三种模式来