我希望能夠帶回所有包含我指定的所有標籤的博客文章。RavenDB - 用多個詞條查詢集合
public class Post
{
public int Name { get; set; }
public List<string> Tags { get; set; }
}
我想帶'c#'和'html'標籤帶回所有帖子。
這個問題和我的一樣,雖然我無法讓我的例子工作。 Linq query with multiple Contains/Any for RavenDB
我想知道爲什麼下面的例子沒有帶回任何結果,當標籤中有'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();
}
}
}
在那裏有問題嗎?請將您的代碼減少到僅支持您的問題的部分,並清楚地詢問問題。謝謝。 – 2013-05-07 14:35:49
感謝您對不清楚的問題表示歉意。重新編寫使用博客文章/標籤,使問題更清晰,代碼已被剝離,只顯示相關方面。 – CountZero 2013-05-07 22:14:29