我希望能够恢复所有包含我指定的所有标签的博客文章。
public class Post
{
public int Name { get; set; }
public List<string> Tags { get; set; }
}
我想带回所有带有“c#”和“html”标签的帖子。
这个问题和我的一样,虽然我无法让我的例子工作。 具有多个包含/任何用于 RavenDB 的 Linq 查询
我想知道为什么当标签中有一篇带有“c#”和“html”的帖子时,下面的示例没有返回任何结果。
如果有人能阐明现在是否有一种新的、更优雅的方法来解决这个问题,那就太好了,最好是使用强类型查询语法,即
var query = s.Query<Entity, IndexClass>()
-
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Raven.Abstractions.Indexing;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
namespace RavenDB
{
public class Blog
{
public string Name { get; set; }
public List<string> Tags { get; set; }
}
public class BlogsByTags : AbstractIndexCreationTask<Blog>
{
public BlogsByTags()
{
Map = docs => from doc in docs
select new
{
Tags = doc.Tags
};
Index(x => x.Tags, FieldIndexing.Analyzed);
}
}
[TestFixture]
public class Runner : UsingEmbeddedRavenStore
{
[Test]
public void Run()
{
Open();
IndexCreation.CreateIndexes(typeof(BlogsByTags).Assembly, Store);
var blogs = new List<Blog>
{
new Blog{Name = "MVC", Tags = new List<string>{"html","c#"}},
new Blog{Name = "HTML5", Tags = new List<string>{"html"}},
new Blog{Name = "Version Control", Tags = new List<string>{"git"}},
};
using (var session = Store.OpenSession())
{
foreach (var blog in blogs)
{
session.Store(blog);
}
session.SaveChanges();
}
var tags = new List<string> { "c#", "html" };
List<Blog> blogQueryResults;
using (var s = Store.OpenSession())
{
blogQueryResults = s.Advanced.LuceneQuery<Blog, BlogsByTags>()
.Where(string.Format("Tags:({0})", string.Join(" AND ", tags))).ToList();
}
Assert.AreEqual(1, blogQueryResults.Count());
}
}
public abstract class UsingEmbeddedRavenStore
{
protected EmbeddableDocumentStore Store { get; set; }
protected void Open()
{
Store = new EmbeddableDocumentStore
{
RunInMemory =
true
};
Store.Initialize();
}
protected void Dispose()
{
Store.Dispose();
}
}
}