17

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?

CodingInsomnia
  • 1,843
  • 2
  • 15
  • 19
  • What is `GetQueryable()` return type? and what are you doing in `query = query.Where(i => i.Tags.Contains(tag));`? what is the query type? – Saeed Amiri Nov 17 '10 at 20:16
  • GetQueryable() returns an IQueryable. I do have access to the entire DocumentSession though, so that was just an example. – CodingInsomnia Nov 18 '10 at 10:15

1 Answers1

17

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.

Steve
  • 25,806
  • 2
  • 33
  • 43
metdos
  • 13,411
  • 17
  • 77
  • 120
  • Close, but no cigar! ;-) First of all, the version I use doesn't seem to work with the "Tags,:" syntax. However if I create a Tag class with a single property called Name (and use "Tags,Name:"), your solution works. But it doesn't do exactly what I need. I'd like it to return only those Items that have ALL of the tags in the tags list (not any one of the tags). – CodingInsomnia Nov 17 '10 at 21:32
  • Okay, THAT is more difficult, how would you do that as a set based operation in a relational database? I think at this point you might actually have to create an index to merge those tags into a single field and specify the analyzer you you want, and use a Lucene query to say "All of these terms please" – metdos Nov 18 '10 at 12:37
  • Yeah, it's not very pretty in a relational DB either, but the code I wrote in the question actually works with Entity Framework. Not pretty though, and hardly very efficient (which was part of the reason I figured that maybe Raven would be able to do a better job). – CodingInsomnia Nov 19 '10 at 18:21
  • Oh, and just to be clear - your solution works like a charm! Many thanks! – CodingInsomnia Nov 19 '10 at 18:36
  • 12
    Is this still the way to go or is there a better approach? – Giorgi Nov 02 '12 at 14:18
  • 6
    there is a better answer now - to use .In<> operator. see more info here http://stackoverflow.com/questions/7899936/ravendb-how-to-query-with-multiple-search-terms#answer-7902851 – apostolov Dec 04 '14 at 09:13
  • 1
    @apostolov: that question is not the same problem. This is looking for a range of values on a List property, not the simpler string property case. – Luke Baulch Feb 08 '16 at 00:28