RavenDB:仅在一个查询中给出“搜索”

时间:2013-12-05 21:06:10

标签: ravendb

我的情况是我的用户会看到一个网格,默认情况下,它只会获得前15个结果。但是,他们可以键入名称并在所有页面中搜索项目。

单独,这些都可以正常工作,但我想弄清楚如何使它们作为单个查询工作。这基本上就是它的样子......

// find a filter if the user is searching
var filters = request.Filters.ToFilters();

// determine the name to search by
var name = filters.Count > 0 ? filters[0] : null;

// we need to be able to catch some query statistics to make sure that the
// grid view is complete and accurate
RavenQueryStatistics statistics;

// try to query the items listing as quickly as we can, getting only the
// page we want out of it
var items = RavenSession
    .Query<Models.Items.Item>()
    .Statistics(out statistics) // output our query statistics
    .Search(n => n.Name, name)
    .Skip((request.Page - 1) * request.PageSize)
    .Take(request.PageSize)
    .ToArray();

// we need to store the total results so that we can keep the grid up to date
var totalResults = statistics.TotalResults;

return Json(new { data = items, total = totalResults }, JsonRequestBehavior.AllowGet);

问题是如果没有给出名字,它就不会返回任何内容;这不是理想的结果。 (显然,'null'搜索不起作用。)

1 个答案:

答案 0 :(得分:2)

做这样的事情:

var q= RavenSession
    .Query<Models.Items.Item>()
    .Statistics(out statistics); // output our query statistics

if(string.IsNullOrEmpty(name) == false)
    q = q.Search(n => n.Name, name);


var items = q.Skip((request.Page - 1) * request.PageSize)
    .Take(request.PageSize)
    .ToArray();