Search is a required function when writing a WEB project.
Sometimes, the customer will enter a set of keywords separated by spaces or other delimiters. We need to find the following results based on the input:
Result of continuous occurrence of the entire set of keywords
Contains each entry in the entire set of keywords, but not necessarily consecutive results
Result of containing one or more keywords in the entire set of keywords
Just as you search for SQL SERVER 2000 in GOOGLE,
First, it strictly matches the entire string
If no result is found, check whether the result contains the three words.
If no result is returned for all three words, try to find the result that contains any word.
The following code uses C # To dynamically construct a function for the conditional statement of this search mode in the code.
Here, KEYWORD is the KEYWORD.
SEARCHMODE is the search mode. 1 indicates accuracy, 2 indicates full inclusion, and 3 indicates arbitrary inclusion.
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: search mode. The value 1 indicates exact search. The value 2 indicates that search is included in the search mode. The value 3 indicates a search that contains any word.
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 );
// Sub-master keyword
If (KeywordArray. Length <2 | strSearchMode = "1 ")
{
StrSubQuery + = "(productfullname +'' + cast (includesc as varchar) + ''+ keyword +'' + subcategoryname like '% "+ strKeyword +" % ')";
}
Else
{
If (strSearchMode = "2 ")
{
StrTemp = "productfullname +'' + cast (includesc 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 (includesc as varchar) + ''+ keyword +'' + subcategoryname like '% "+ KeywordArray [I] +" %' or ";
}
StrTemp = strTemp. TrimEnd (new char [] {'O', 'R '});
}
StrSubQuery + = strTemp;
}
StrSubQuery + = ")";
}
StrCondition + = strSubQuery;
StrCondition + = ")";
Return (strCondition );
In the three modes, the first one constructs SQL conditions similar to the following
Field1 + field2 + field3 like % keyword %
Second, construct
Field1 + field2 + field3 like % keyword1 % keyword2 % keyword3 %
Third, construct
Field1 + field2 + field3 like % keyword1 % or field1 + field2 + field3 like % keyword2 % or field1 + field2 + field3 like % keyword3 %
These three modes
KEYWORD1, KEYWORD2, and KEYWORD are words separated by KEYWORD.
Generally, it can be assumed that the customer is separated by a blank character, OR, | OR, OR ;.
However, in general, customers will always use spaces to separate