简体   繁体   English

通过实体框架的SQL Server Freetext

[英]Sql Server Freetext through Entity Framework

I have an existing website developped using ASP.NET MVC 3 and Entity Framework 4 querying an Sql Server 2008 database. 我有一个使用ASP.NET MVC 3和Entity Framework 4查询Sql Server 2008数据库的现有网站。 It contains a search form with around 10 fields, and when the user clicks the submit button I dynamically create an Entity SQL request containing only the specified search fields, omitting the empty ones. 它包含一个带有大约10个字段的搜索表单,当用户单击“提交”按钮时,我动态创建了一个仅包含指定搜索字段的Entity SQL请求,省略了空字段。 It works. 有用。 So far so good. 到目前为止,一切都很好。

Now, the client wants a Full Text Search behavior for one of the fields. 现在,客户希望其中一个字段具有“全文本搜索”行为。 I see this request as being pretty complex because (AFAIK) : 我认为此请求非常复杂,因为(AFAIK):

  • Entity Framework does not natively supports full text search 实体框架本身不支持全文搜索
  • I want to avoid stored procedures to wrap the FTS syntax because so far I've only used "static" SPs, keeping the logic in the .NET code. 我想避免使用存储过程来包装FTS语法,因为到目前为止,我只使用了“静态” SP,并将逻辑保留在.NET代码中。 So I want to try to avoid building the query inside the procedure. 因此,我想尝试避免在过程内部建立查询。 And creating one procedure per possible search field combination is not an option. 而且,不能为每个可能的搜索字段组合创建一个过程。

Solutions I could think of so far : 到目前为止我能想到的解决方案:

  • Putting a stored procedure or user defined function as a seach preadicate in the WHERE clause (i'm not sure that's possible though) 将存储过程或用户定义的函数放在WHERE子句中作为seach前缀(但我不确定这是否可行)
  • Getting the FTS results alone in a temporary table, and execute the other filters on that temporary table. 仅在临时表中获取FTS结果,然后在该临时表上执行其他过滤器。 I'm afraid of poor performances if there are a lot of FTS results with this technique... 如果使用此技术的FTS结果很多,我担心性能会很差...

What's the best way for daeling with this ? 敢于这样做的最好方法是什么?

Can you not just use raw sql? 您不仅可以使用原始sql吗? then you can keep the logic in your .NET code. 那么您可以将逻辑保留在.NET代码中。

So, it would look something like: 因此,它看起来像:

string sql = "DO FULLTEXT STUFF";
MyObjectContext.ExecuteStoreQuery<MyEntity>(sql, .......);

You don't have to think about performance - this will be slow anyway because you will be replacing indexed full text search with standard string comparing on concatenated value. 您不必考虑性能-无论如何这都会很慢,因为您将用标准字符串对串联值进行比较来替换索引的全文本搜索。

There are three ways to go: 共有三种方法:

  • Dynamically create ESQL query as you do now but use LIKE on concatenated value of your columns 像现在一样动态创建ESQL查询,但对列的串联值使用LIKE
  • User defined SQL function or model defined function for evaluating search check imported to your EDMX model and exposed for Linq-to-entities queries. 用户定义的SQL函数或模型定义的函数,用于评估导入到EDMX模型并针对Linq-to-entities查询公开的搜索检查。
  • Instead of searching on table directly use view with computed column (containing all ten fields) and run "full text" on that column. 与其直接在表上搜索,不如使用带有计算列(包含所有十个字段)的视图,然后在该列上运行“全文”。

Any of this methods is not solution for performance. 这些方法都不是性能的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM