HOWTO:C#字符串到SQL全文目录搜索? [英] Howto: C# string to SQL full-text catalog search?

查看:132
本文介绍了HOWTO:C#字符串到SQL全文目录搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像这样的一些搜索查询:




乔治和NOT华盛顿和亚伯拉罕



狗或猫,不是狼




针对这些搜索我要回去了的乔治或亚伯拉罕结果但华盛顿的等。



基本上我想借此字符串,并可以提交一个上下文搜索到我的全文目录存储过程的搜索。



我假设我应该使用正则表达式,但我很熟悉正则表达式在C#。



我发现这文章: http://support.microsoft.com/kb/246800 我认为这是我。需要做的,但我希望我能有一些帮助,实施



假设你把一个字符串作为参数,并想返回一个字符串:

 字符串输入='乔治·华盛顿AND NOT玛莎或狗'; 

私人字符串interpretSearchQuery(输入)
{
// HALP!

/ *替换和| 而不是与
*和
*而不是
*
*代替'或'| OR NOT与
*或
*OR NOT
*
*添加到字符串的开头和结束串
*的/

返回'乔治·华盛顿,而不是玛莎或狗';
}


解决方案

我会使用解析您的字符串后缀表示法(或波兰式)。

  ** Postfix的算法** 
中的算法评估任何后缀表达式是相当简单:

虽然有输入记号留下

读取输入的下一个标记。

。如果标记是一个价值
其推入堆栈。

否则,该标记是一个运营商(运营商这里既包括运算符和函数)。
已知的先验,操作者需要n个参数。

。如果有对堆栈
(错误)用户大于n的值少具有在表达式未输入足够的值。
,否则,从栈中弹出的前n值。

评估运营商,拥有该值作为参数。
按下返回的结果,如果有的话,背面压入堆栈。

。如果只有一个在堆栈
该值的值是计算的结果。

。如果有在堆栈
(错误)的用户输入有值过多多个值。



所以,把你的输入字符串:




乔治·华盛顿和不玛莎或




和它simplifing为:

  A =乔治
b =华盛顿
C =玛莎
D =狗
&安培; = AND
! = NOT
| = OR

我们将获得的



后缀符号!

AB和C开发|



这意味着:




  1. 推值A(乔治)

  2. 推值b(华盛顿)

  3. 和弹出前两个值
    和推动的结果(George和
    华盛顿)

  4. 推值C(玛莎)

  5. 不要通过弹出前两个值
    和推动的结果(乔治和
    华盛顿)NOT(玛莎)

  6. 推值D(狗)

  7. 或弹出前两个值
    和推动的结果((George和
    华盛顿)NOT(玛莎))OR(狗)


I have some search queries like so:

George AND NOT Washington OR Abraham

Dog OR cat AND NOT Wolf

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

basically I want to take the string and be able to submit a contextual search to my full-text catalog stored procedure search.

I am assuming I should use Regex but I am very unfamiliar with Regex in C#.

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

Assuming you take a string as parameter and would like to return a 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"';
}

解决方案

I would parse your string using Postfix notation (or Polish 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.

So taking your input string:

'George Washington AND NOT Martha OR Dog'

And simplifing it to:

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

We would get the postfix notation of

AB&C!D|

Which means:

  1. Push value A (George)
  2. Push value B (Washington)
  3. AND by popping previous two values and pushing the result (George AND Washington)
  4. Push value C (Martha)
  5. NOT by popping previous two values and pushing the result (George AND Washington) NOT (Martha)
  6. Push value D (Dog)
  7. OR by popping previous two values and pushing the result ((George AND Washington) NOT (Martha)) OR (Dog)

这篇关于HOWTO:C#字符串到SQL全文目录搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆