开发者

NHibernate - Log items that appear in a search result

I am using NHibernate in an MVC 2.0 application. Essentially I want to keep track of the number of times each product shows up in a search result. For example, when somebody searches for a widget the product named WidgetA will show up in the first page of the search results. At t开发者_如何学Chis point i will increment a field in the database to reflect that it appeared as part of a search result.

While this is straightforward I am concerned that the inserts themselves will greatly slow down the search result. I would like to batch my statements together but it seems that coupling my inserts with my select may be counter productive. Has anyone tried to accomplish this in NHibernate and, if so, are there any standard patterns for completing this kind of operation?


Interesting question!

Here's a possible solution:

var searchResults = session.CreateCriteria<Product>()
    //your query parameters here
    .List<Product>();
session.CreateQuery(@"update Product set SearchCount = SearchCount + 1
                      where Id in (:productIds)")
       .SetParameterList("productIds", searchResults.Select(p => p.Id).ToList())
       .ExecuteUpdate();

Of course you can do the search with Criteria, HQL, SQL, Linq, etc.

The update query is a single round trip for all the objects, so the performance impact should be minimal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜