Linq query with multiple Contains/Any for RavenDB

CodingInsomnia picture CodingInsomnia · Nov 17, 2010 · Viewed 7.9k times · Source

I have a document class that contains a list of "tags". Something like:

class Item {
  string Name { get; set; }
  List<string> Tags {get; set;}
}

Now I would like to create a query for RavenDB that hands me all items filtered by a list of tags. When using Entity Framework I managed to do this by something like this:

var query = GetQueryable();
foreach (var tag in tags)
{
   query = query.Where(i => i.Tags.Contains(tag));
}

However, this doesn't seem to work with RavenDB, most likely because Contains isn't supported.. I've also tried rewriting it using Any, (Where(i => i.Tags.Any(t=>t == tag))) but that gives me a strange exception:

Unable to cast object of type
'System.Linq.Expressions.PrimitiveParameterExpression`1[System.String]'
to type 'System.Linq.Expressions.MemberExpression

Any great ideas? Am I doing this completely wrong?

Answer

Rob Ashton picture Rob Ashton · Nov 17, 2010

Contains is indeed not yet supported (Perhaps it should be, but that's another matter entirely - we only really add support for various operators when its asked for)

As for multiple queries against Any, I assume you're trying to do dynamic data and you want to achieve something like

"X OR Y OR Z"

That's a tricky one, and the LINQ provider by default will aggregate those multiple WHERE clauses with AND, so your example looks like

"X AND Y AND Z"

Which will obviously never be the case.

Your best option for this one is to drop down to the Lucene query (at least for now) and do something like this:

var results = s.Advanced.LuceneQuery<Item>()
                   .Where(string.Format("Tags,:({0})", string.Join(" OR ", tags))); 

Make sense?

The query above will look something like

"Tags,:(X OR Y OR Z)"

Note: "Tags," informs RavenDB that Tags is an array

Okay, [edit]!

The easiest way to get what you actually want is to do something along these lines

                new IndexDefinition<Item, Item>()
                {
                    Map = docs => from doc in docs
                                  select new
                                  {
                                      Tags = doc.Tags
                                  },
                    Indexes = {{ x => x.Tags, FieldIndexing.Analyzed }}
                }.ToIndexDefinition(store.Conventions));

Then to query for your ands, you can do something like this:

                var results = s.Advanced.LuceneQuery<Item, WhateverYouCalledThatIndex>()
                   .Where(string.Format("Tags:({0})", string.Join(" AND ", tags)));

Now, things to be aware of

       Tags = doc.Tags

Will serialize that entire array into one giant blob, as it's just strings that will work for this example.

I am looking at better ways of expressing this, it is unlikely that we'll come up with a LINQ-ish way of doing this, as it doesn't really map across very well - but it is an answer that will work :)

I think I'd quite like to be able to at least do

  Map = docs => from doc in docs
                                  select new
                                  {
                                      Tags = String.Join(" ", doc.Tags)
                                  },

(This won't work so don't try it), but it is a bit more explicit about what you want to achieve.