实体框架、全文搜索和临时表

发布于 2024-08-05 01:57:47 字数 371 浏览 6 评论 0原文

我有一个 LINQ-2-Entity 查询构建器,根据相当复杂的搜索表单嵌套不同类型的Where 子句。到目前为止效果很好。

现在我需要在一些查询中使用 SQL Server 全文搜索索引。是否有机会将搜索项直接添加到 LINQ 查询中,并将分数作为可选属性提供?

如果没有,我可以编写一个存储过程来加载与全文搜索条件匹配的所有行 ID 的列表,然后使用 LINQ-2-Entity 查询加载详细数据并评估其他可选过滤条件每行一个循环。从性能角度来看,这当然是一个非常糟糕的主意。

另一种选择是使用存储过程将与全文搜索匹配的所有行 ID 插入到临时表中,然后让 LINQ 查询连接临时表。问题是:如何在 LINQ 查询中连接临时表,因为它不能是实体模型的一部分?

I have a LINQ-2-Entity query builder, nesting different kinds of Where clauses depending on a fairly complex search form. Works great so far.

Now I need to use a SQL Server fulltext search index in some of my queries. Is there any chance to add the search term directly to the LINQ query, and have the score available as a selectable property?

If not, I could write a stored procedure to load a list of all row IDs matching the full-text search criteria, and then use a LINQ-2-Entity query to load the detail data and evaluate other optional filter criteria in a loop per row. That would be of course a very bad idea performance-wise.

Another option would be to use a stored procedure to insert all row IDs matching the full-text search into a temporary table, and then let the LINQ query join the temporary table. Question is: how to join a temporary table in a LINQ query, as it cannot be part of the entity model?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2024-08-12 01:57:47

我想我可能会建议采用混合方法。

  1. 编写一个存储过程来返回您需要的所有信息。
  2. 将实体映射到结果。该实体可以为此唯一目的而创建。或者,使用实体框架的版本 4,它允许映射复杂类型来启动过程结果。要点是,我们不会尝试将过程结果强制为现有实体类型,而是将它们作为自己的类型进行处理。
  3. 现在您可以构建 LINQ to Entities 查询。

示例查询:

var q = from r in Context.SearchFor("searchText")
        let fooInstance = (r.ResultType == "Foo")
            ? Context.Foos.Where(f => f.Id == r.Id)
            : null
        where ((fooInstance == null) || (fooInstance.SpecialCriterion == r.SpecialCriterion))    
        select {
            // ...

这超出了我的想象,因此语法可能不正确。重要的一点是将搜索结果视为一个实体。

或者:使用更灵活的 FTS 系统,该系统可以在构建索引时执行“特殊”的按类型过滤。

I think I would probably suggest a hybrid approach.

  1. Write a stored procedure which returns all the information you need.
  2. Map an entity to the results. The entity can be created for this sole purpose. Alternately, use version 4 of the Entity Framework, which allows mapping complex types to start procedure results. The point is that instead of trying to coerce the procedure results in to existing entity types, were going to handle them as their own type.
  3. Now you can build a LINQ to Entities query.

Sample query:

var q = from r in Context.SearchFor("searchText")
        let fooInstance = (r.ResultType == "Foo")
            ? Context.Foos.Where(f => f.Id == r.Id)
            : null
        where ((fooInstance == null) || (fooInstance.SpecialCriterion == r.SpecialCriterion))    
        select {
            // ...

This is off the top of my head, so the syntax might not be right. The important point is treating search results as an entity.

Alternately: Use a more flexible FTS system, which can do the "special", per-type filtering when building the index.

飘逸的'云 2024-08-12 01:57:47

我见过这样的 EF4 代码:

var query = context.ExecuteStoreQuery<Person>(
        "SELECT * FROM People WHERE FREETEXT(*,{0})", 
        searchText
    ).AsQueryable();

在某些情况下,这可能比创建存储过程或 UDP 更简单。

I've seen code like this for EF4:

var query = context.ExecuteStoreQuery<Person>(
        "SELECT * FROM People WHERE FREETEXT(*,{0})", 
        searchText
    ).AsQueryable();

This may be simpler than creating a stored proc or UDP in some cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文