Howto: C # string for full-text search in SQL query?

I have searches like:

George AND NOT Washington OR Abraham

Dog OR cat AND NOT A WOLF

for these searches, I would like to get results for George or Abraham, but not in Washington, etc.

Basically I want to take a string and provide a contextual search in a full-text search for directory stored procedures.

I assume I should use Regex, but I am very new to Regex in C #.

I found this article: http://support.microsoft.com/kb/246800 , which I think I need to do, but I was hoping that I could have some help with the implementation.

Assuming you take the string as a parameter and want to return the string:

string input = 'George Washington AND NOT Martha OR Dog';

private string interpretSearchQuery(input)
{
     // HALP!

        /* replace ' AND ' | ' AND NOT ' with
         * " AND "
         * " AND NOT "
         * 
         * replace ' OR ' | ' OR NOT ' with
         * " OR "
         * " OR NOT "
         * 
         * add " to beginning of string and " to end of string
         */

     return '"George Washington" AND NOT "Martha" OR "Dog"';
}
+1
3

, Postfix notation ( ).

**Postfix algorithm**
The algorithm for evaluating any postfix expression is fairly straightforward:

While there are input tokens left    

  Read the next token from input.

  If the token is a value
    Push it onto the stack.

  Otherwise, the token is an operator (operator here includes both operators, and functions). 
   It is known a priori that the operator takes n arguments. 

   If there are fewer than n values on the stack 
     (Error) The user has not input sufficient values in the expression. 
   Else, Pop the top n values from the stack. 

   Evaluate the operator, with the values as arguments. 
   Push the returned results, if any, back onto the stack. 

If there is only one value in the stack 
  That value is the result of the calculation. 

If there are more values in the stack 
  (Error) The user input has too many values.

, :

' '

:

A = George 
B = Washington
C = Martha
D = Dog
& = AND
! = NOT
| = OR

AB &! C D |

:

  • Push value A (George)
  • B ()
  • , (George AND ).
  • C (Martha)
  • , (George AND ) ()
  • D ()
  • (( ) ()) ()
+4

... , .

string input = "George Washington AND NOT Martha OR Dog";

private string interpretSearchQuery(string input)
{
    StringBuilder builder = new StringBuilder();
    var tokens = input.Split( ' ' );

    bool quoteOpen = false;
    foreach( string token in tokens )
    {
        if( !quoteOpen && !IsSpecial( token ) )
        {
            builder.AppendFormat( " \"{0}", token );
            quoteOpen = true;
        }
        else if( quoteOpen && IsSpecial( token ))
        {
            builder.AppendFormat( "\" {0}", token );
            quoteOpen = false;
        }
        else
        {
            builder.AppendFormat( " {0}", token );
        }
    }

    if( quoteOpen )
    {
        builder.Append( "\"" );
    }

    return "'" + builder.ToString().Trim() + "'";
}

public static bool IsSpecial( string token )
{
    return string.Compare( token, "AND", true ) == 0 ||
        string.Compare( token, "OR", true ) == 0 ||
        string.Compare( token, "NOT", true ) == 0;
}
+3

Here is the solution I came across. The only problem is that the distorted search queries will not be processed and executed correctly:

private string interpretSearchTerm(string searchTerm)
        {
            string term = "";
            /* replace ' AND ' | ' AND NOT ' with
             * " AND "
             * " AND NOT "
             * 
             * replace ' OR ' | ' OR NOT ' with
             * " OR "
             * " OR NOT "
             * 
             * add " to beginning of string and " to end of string
             */
            if (searchTerm.IndexOf("AND") > -1
                || searchTerm.IndexOf("OR") > -1
                || searchTerm.IndexOf("AND NOT") > -1
                || searchTerm.IndexOf("OR NOT") > -1)
            {
                term = searchTerm.Replace(" AND NOT ", "\"AND NOT\"")
                        .Replace(" AND ", "\"AND\"")
                        .Replace(" OR NOT", "\"OR NOT\"")
                        .Replace(" OR ", "\"OR\"");
                term = "\"" + term + "\"";
                return term;
            }
            else if (searchTerm.IndexOf("\"") > -1) return searchTerm;
            else return "\"" + searchTerm + "\"";
        }

Now I will talk about the implementation of the postfix algorithm proposed by GalacticJello. I will send it when I earn it.

0
source

Source: https://habr.com/ru/post/1772051/


All Articles