/// <summary> /// Gets the contains search condition. /// </summary> /// <param name="searchText">The search text.</param> /// <param name="fullTextSearchType">Full text search type.</param> /// <param name="implicitOperator">The implicit operator.</param> /// <param name="treatTextualOperatorsAsSearchText">If set to <c>true</c> treat textual operators as search text.</param> /// <returns>A search condition.</returns> public static string GetContainsSearchCondition(string searchText, FullTextSearchType fullTextSearchType, ImplicitOperator implicitOperator, bool treatTextualOperatorsAsSearchText) { //TODO: when a logical operator is not valid at a certain place, handle it as a search word? const string and = "and"; const string andSign = "&"; const string plus = "+"; const string or = "or"; const string orSign = "|"; const string not = "not"; const string notSign = "!"; const string minus = "-"; const string near = "near"; const string nearSign = "~"; const string prefix = "*"; const string parenStart = "("; const string parenEnd = ")"; const string formatNormal = @"""{0}"""; const string formatPrefix = @"""{0}*"""; const string formatInflectional = @"FORMSOF(INFLECTIONAL,""{0}"")"; const string formatThesaurus = @"FORMSOF(THESAURUS,""{0}"")"; string[] logicalSearchTerms = treatTextualOperatorsAsSearchText ? new[] { minus, notSign, andSign, parenStart, parenEnd, prefix, orSign, nearSign, plus } : new[] { minus, notSign, andSign, parenStart, parenEnd, prefix, orSign, nearSign, plus, and, near, not, or }; string firstLogical = null; string secondLogical = null; int normalSearchTerms = 0; int openParens = 0; int openedParens = 0; int closeParens = 0; int closedParens = 0; StringBuilder searchCondition = new StringBuilder(); string[] searchTokens = GetSearchTokens(searchText); for(int i = 0; i < searchTokens.Length; i++) { string searchTerm = searchTokens[i]; if(Array.BinarySearch(logicalSearchTerms, searchTerm, CaseInsensitiveComparer.DefaultInvariant) < 0) { normalSearchTerms++; bool beginningOrEndingParens = openParens > 0 || closeParens > 0; for(; closeParens > 0; closeParens--) { closedParens++; searchCondition.Append(parenEnd); } if(normalSearchTerms > 1) { if(!fullTextSearchType.Equals(FullTextSearchType.Inflectional) && !fullTextSearchType.Equals(FullTextSearchType.Thesaurus) && !beginningOrEndingParens && firstLogical != null && ((!treatTextualOperatorsAsSearchText && IsEqualCaseInsensitive(firstLogical, near)) || firstLogical.Equals(nearSign))) { searchCondition.Append(nearSign); //"near" not supported between parens nor inflectional/thesaurus terms } else if((firstLogical != null && ((!treatTextualOperatorsAsSearchText && IsEqualCaseInsensitive(firstLogical, or)) || firstLogical.Equals(orSign))) || (implicitOperator.Equals(ImplicitOperator.Or) && !(firstLogical != null && ((!treatTextualOperatorsAsSearchText && IsEqualCaseInsensitive(firstLogical, and)) || firstLogical.Equals(andSign) || firstLogical.Equals(plus))))) { searchCondition.Append(orSign); //"or not" not supported } else { searchCondition.Append(andSign); if((firstLogical != null && ((!treatTextualOperatorsAsSearchText && IsEqualCaseInsensitive(firstLogical, not)) || firstLogical.Equals(notSign) || firstLogical.Equals(minus))) || (secondLogical != null && ((!treatTextualOperatorsAsSearchText && IsEqualCaseInsensitive(secondLogical, not)) || secondLogical.Equals(notSign) || secondLogical.Equals(minus)))) { searchCondition.Append(notSign); } } } for(; openParens > 0; openParens--) { openedParens++; searchCondition.Append(parenStart); } string format; switch(fullTextSearchType) { case FullTextSearchType.Inflectional: //prefix terms not supported in inflectional terms searchTerm = searchTerm.TrimEnd(prefix[0]); format = formatInflectional; break; case FullTextSearchType.Thesaurus: //prefix terms not supported in thesaurus terms searchTerm = searchTerm.TrimEnd(prefix[0]); format = formatThesaurus; break; case FullTextSearchType.Prefix: format = formatPrefix; break; default: format = formatNormal; break; } searchCondition.AppendFormat(CultureInfo.InvariantCulture, format, searchTerm); firstLogical = secondLogical = null; } else if(searchTerm.Equals(parenStart)) { openParens++; } else if(searchTerm.Equals(parenEnd)) { if(!openParens.Equals(0)) { openParens--; } else if(openedParens > closedParens + closeParens) { closeParens++; } } else if(firstLogical == null) { firstLogical = searchTerm; } else if(secondLogical == null) { secondLogical = searchTerm; } } for(int i = openedParens - closedParens; i > 0; i--) { searchCondition.Append(parenEnd); } return searchCondition.ToString(); }
/// <summary> /// Gets the contains search condition. /// </summary> /// <param name="searchText">The search text.</param> /// <param name="fullTextSearchType">Full text search type.</param> /// <param name="treatTextualOperatorsAsSearchText">If set to <c>true</c> treat textual operators as search text.</param> /// <returns>A search condition.</returns> public static string GetContainsSearchCondition(string searchText, FullTextSearchType fullTextSearchType, bool treatTextualOperatorsAsSearchText) { return GetContainsSearchCondition(searchText, fullTextSearchType, ImplicitOperator.And, treatTextualOperatorsAsSearchText); }
/// <summary> /// Gets the contains search condition. /// </summary> /// <param name="searchText">The search text.</param> /// <param name="fullTextSearchType">Full text search type.</param> /// <returns>A search condition.</returns> public static string GetContainsSearchCondition(string searchText, FullTextSearchType fullTextSearchType) { return GetContainsSearchCondition(searchText, fullTextSearchType, ImplicitOperator.And, false); }
/// <summary> /// Gets the contains search condition. /// </summary> /// <param name="searchText">The search text.</param> /// <param name="fullTextSearchType">Full text search type.</param> /// <param name="implicitOperator">The implicit operator.</param> /// <param name="treatTextualOperatorsAsSearchText">If set to <c>true</c> treat textual operators as search text.</param> /// <returns>A search condition.</returns> public static string GetContainsSearchCondition(string searchText, FullTextSearchType fullTextSearchType, ImplicitOperator implicitOperator, bool treatTextualOperatorsAsSearchText) { //TODO: when a logical operator is not valid at a certain place, handle it as a search word? const string and = "and"; const string andSign = "&"; const string plus = "+"; const string or = "or"; const string orSign = "|"; const string not = "not"; const string notSign = "!"; const string minus = "-"; const string near = "near"; const string nearSign = "~"; const string prefix = "*"; const string parenStart = "("; const string parenEnd = ")"; const string formatNormal = @"""{0}"""; const string formatPrefix = @"""{0}*"""; const string formatInflectional = @"FORMSOF(INFLECTIONAL,""{0}"")"; const string formatThesaurus = @"FORMSOF(THESAURUS,""{0}"")"; string[] logicalSearchTerms = treatTextualOperatorsAsSearchText ? new[] { minus, notSign, andSign, parenStart, parenEnd, prefix, orSign, nearSign, plus } : new[] { minus, notSign, andSign, parenStart, parenEnd, prefix, orSign, nearSign, plus, and, near, not, or }; string firstLogical = null; string secondLogical = null; int normalSearchTerms = 0; int openParens = 0; int openedParens = 0; int closeParens = 0; int closedParens = 0; StringBuilder searchCondition = new StringBuilder(); string[] searchTokens = GetSearchTokens(searchText); for (int i = 0; i < searchTokens.Length; i++) { string searchTerm = searchTokens[i]; if (Array.BinarySearch(logicalSearchTerms, searchTerm, CaseInsensitiveComparer.DefaultInvariant) < 0) { normalSearchTerms++; bool beginningOrEndingParens = openParens > 0 || closeParens > 0; for (; closeParens > 0; closeParens--) { closedParens++; searchCondition.Append(parenEnd); } if (normalSearchTerms > 1) { if (!fullTextSearchType.Equals(FullTextSearchType.Inflectional) && !fullTextSearchType.Equals(FullTextSearchType.Thesaurus) && !beginningOrEndingParens && firstLogical != null && ((!treatTextualOperatorsAsSearchText && IsEqualCaseInsensitive(firstLogical, near)) || firstLogical.Equals(nearSign))) { searchCondition.Append(nearSign); //"near" not supported between parens nor inflectional/thesaurus terms } else if ((firstLogical != null && ((!treatTextualOperatorsAsSearchText && IsEqualCaseInsensitive(firstLogical, or)) || firstLogical.Equals(orSign))) || (implicitOperator.Equals(ImplicitOperator.Or) && !(firstLogical != null && ((!treatTextualOperatorsAsSearchText && IsEqualCaseInsensitive(firstLogical, and)) || firstLogical.Equals(andSign) || firstLogical.Equals(plus))))) { searchCondition.Append(orSign); //"or not" not supported } else { searchCondition.Append(andSign); if ((firstLogical != null && ((!treatTextualOperatorsAsSearchText && IsEqualCaseInsensitive(firstLogical, not)) || firstLogical.Equals(notSign) || firstLogical.Equals(minus))) || (secondLogical != null && ((!treatTextualOperatorsAsSearchText && IsEqualCaseInsensitive(secondLogical, not)) || secondLogical.Equals(notSign) || secondLogical.Equals(minus)))) { searchCondition.Append(notSign); } } } for (; openParens > 0; openParens--) { openedParens++; searchCondition.Append(parenStart); } string format; switch (fullTextSearchType) { case FullTextSearchType.Inflectional: //prefix terms not supported in inflectional terms searchTerm = searchTerm.TrimEnd(prefix[0]); format = formatInflectional; break; case FullTextSearchType.Thesaurus: //prefix terms not supported in thesaurus terms searchTerm = searchTerm.TrimEnd(prefix[0]); format = formatThesaurus; break; case FullTextSearchType.Prefix: format = formatPrefix; break; default: format = formatNormal; break; } searchCondition.AppendFormat(CultureInfo.InvariantCulture, format, searchTerm); firstLogical = secondLogical = null; } else if (searchTerm.Equals(parenStart)) { openParens++; } else if (searchTerm.Equals(parenEnd)) { if (!openParens.Equals(0)) { openParens--; } else if (openedParens > closedParens + closeParens) { closeParens++; } } else if (firstLogical == null) { firstLogical = searchTerm; } else if (secondLogical == null) { secondLogical = searchTerm; } } for (int i = openedParens - closedParens; i > 0; i--) { searchCondition.Append(parenEnd); } return(searchCondition.ToString()); }
/// <summary> /// Gets the contains search condition. /// </summary> /// <param name="searchText">The search text.</param> /// <param name="fullTextSearchType">Full text search type.</param> /// <param name="treatTextualOperatorsAsSearchText">If set to <c>true</c> treat textual operators as search text.</param> /// <returns>A search condition.</returns> public static string GetContainsSearchCondition(string searchText, FullTextSearchType fullTextSearchType, bool treatTextualOperatorsAsSearchText) { return(GetContainsSearchCondition(searchText, fullTextSearchType, ImplicitOperator.And, treatTextualOperatorsAsSearchText)); }
/// <summary> /// Gets the contains search condition. /// </summary> /// <param name="searchText">The search text.</param> /// <param name="fullTextSearchType">Full text search type.</param> /// <returns>A search condition.</returns> public static string GetContainsSearchCondition(string searchText, FullTextSearchType fullTextSearchType) { return(GetContainsSearchCondition(searchText, fullTextSearchType, ImplicitOperator.And, false)); }