I want to be able to bring back all blog posts which have all of the tags I specify.
public class Post
{
public int Name { get; set; }
public List<string> Tags { get; set; }
}
I want to bring back all posts with 'c#' AND 'html' tags.
This question is the same as mine, though I am unable to get my example working. Linq query with multiple Contains/Any for RavenDB
I would like to know why the example below is not bringing back any results when there is one post with 'c#' and 'html' in the tags.
It would be great if anyone could shed light on if there is a new, more elegant way to solve this problem now, ideally with the strongly typed query syntax i.e
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();
}
}
}