使用LINQ搜索关键字 [英] Search Keywords using LINQ
问题描述
如果我有一个包含标题的文本字段,并且有一个关键字列表,那么如何在标题中搜索n个关键字?
If I have a text field that contains say a title and i have a list of keywords, how can i search the title checking for (n) numbers of keywords in the title?
因此,如果我的标题是烤鸡肉,培根和韭菜派",而用户搜索鸡肉培根萝卜",我想返回上述食谱.
So if my title is "Baking a chicken, bacon and leek pie" and the user searches for "chicken bacon turnip" i'd like to return the above recipe.
基本上,我想说的是,如果标题中包含2个或更多的搜索字词,则该标题被视为有效,应将其返回.但如果它仅包含1,则忽略它.
essentially i'd like to say that if the title contains say 2 or more of the search terms then it's deemed as valid and should be returned. but if it only contains 1 then disregard it.
理想情况下,我希望对它们进行加权,以使列表中出现的术语越多,但它可能是版本2.:)
ideally i'd like them weighted so that the more terms that are present the higher in the list it is but that may be version 2. :)
修改
在这一点上,我应该提到我希望它是本机.net和c#.
I should mention at this point that i'd like this to be native .net and c#.
推荐答案
好的,我知道您说过在Linq中做".假设您正在谈论采用.Net本机字符串,并使用Linq to Objects进行处理,那么我猜最明显的解决方案是通过在词边界上使用正则表达式来分解文本.然后迭代与输入词组匹配的每个结果.
Okay, I know you said 'do it in Linq'. ASsuming you're talking about taking .Net native string and doing it using Linq to Objects, then I guess the most obvious solution is going to be to break up the text by a regex working on word boundaries; and then to iterate through each result matching against the input phrases.
但是...
从您对"v2"的想法来看,我认为您可能应该研究更强大且针对文本搜索的东西-那么如何使用
Judging by your idea for the 'v2' I think you should probably be looking at something more powerful and geared around text searching - so how about using a Lucene.Net text index?
它提供非常功能强大且 非常 的快速全文搜索-并具有处理布尔规则的能力;别名,词干,诸如此类的东西.
It offers very powerful and very fast full-text search - and has the ability to process boolean rules; aliases, stemming, all that kind of stuff.
它确实很摇滚.
更新-由于您在注释中提到了SQL的Linq
您还可以在表上使用SQL全文索引,但是有一个陷阱:没有对CONTAINSTABLE等子句的本地Linq To Sql转换.
You can also use SQL Full-Text indexes on your table however, there is one catch: there is no native Linq To Sql translation to the CONTAINSTABLE et al clauses.
因此,您可以通过字符串使用动态查询生成,然后将其提供给DataContext.ExecuteQuery<TResult>
成员.如果select返回构造所需的实体类型所需的列,则它将像超级按钮一样工作.
So instead you can use dynamic query generation via a string, and then feed that into the DataContext.ExecuteQuery<TResult>
member. If the select returns columns required to construct the entity type you want, it'll work like a charm.
或者,当然,您可以只包装一个存储过程来代替它;)
Or, of course, you can just wrap a stored procedure that does it instead ;)
这篇关于使用LINQ搜索关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!