1

我需要通过用户最喜欢和被阻止的标签来过滤 LicenseeQuery。当用户有一些最喜欢和被阻止的标签时,我已经完成了这项工作,但我正在努力解决用户没有最喜欢或被阻止的标签的情况。我相信我需要使用 MatchAllDocsQuery 的等价物,但我看不到如何通过 RavenDB 客户端 API 实现这一点。

当用户有一个或多个收藏夹和一个或多个被屏蔽的标签时,这可以正常工作:

string favesQuery = String.Join(" OR ", user.FavouriteTags.Select(x => String.Format("Tags:{0}", x)));
string blockedQuery = String.Join(" AND ", user.BlockedTags.Select(x => String.Format("-Tags:{0}", x)));
var articles = RavenSession.Advanced
                           .LuceneQuery<Article>("AllDocs/ByTags")
                           .Include(x => x.Author)
                           .Where(favesQuery)
                           .Boost(0.5m)
                           .Where(blockedQuery)
                           .OrderByDescending(x => x.DatePublished);

我得到的直接问题是Missing where clause,但这是因为String.Join当为空时返回一个空字符串user.FavouriteTags。我可以在 and 前面加上一些东西favesQueryblockedQuery

用 SQL 术语思考,我正在寻找相当于WHERE 1=1 AND ...

相关问题https://groups.google.com/forum/?fromgroups=#!topic/ravendb/JKTpHiFRJLc

4

1 回答 1

1

重新考虑 LINQ 方法的结构。

var articles = RavenSession.Advanced
                           .LuceneQuery<Article>("AllDocs/ByTags")
                           .Include(x => x.Author);
if (user.FavouriteTags.Any()) {
    string favesQuery = String.Join(" OR ", user.FavouriteTags.Select(x => String.Format("Tags:{0}", x)));
    articles = articles.Where(favesQuery).Boost(0.5m);
}
if (user.BlockedTags.Any()) {
    string blockedQuery = String.Join(" AND ", user.BlockedTags.Select(x => String.Format("-Tags:{0}", x)));
    articles = articles.Where(blockedQuery);
}
articles = articles.OrderByDescending(x => x.DatePublished);
于 2012-11-02T23:24:16.213 回答