2
votes

Say I had a User class like this:

public class User
{
  public bool IsActive {get;set;}
  public string[] Tags{get;set;}
  public string Description {get;set;}
}

I would like to use RavenDB to search for the set of users that match following criteria:

  • IsActive = true
  • Tags contains both 'hello' and 'world'
  • Description has the following phrase 'abject failure'

I have researched the Lucene Query syntax, I have even got some stuff working, but it all feels dreadfully clunky with lots of combinatorial string-building to create a text-based lucene query string. I hesitate to put my code up here because it is quite smelly.

I think what I want to do it submit a Lucene Search for the Description and Tags and then filter it with a Where clause for the IsActive field, perhaps like this Filter RavenDB Search Results. But I got lost.

I am using the latest official release (960) so all the groovy stuff that comes after this is not available to me yet. For example, this solution is verboten as 960 does not appear to support the .As<T>() extension.

Question

How do I construct the required Index and Query to perform a search that combines:

  • a single constraint, eg IsActive
  • a collection constraint, eg Tags
  • a free-text constraint eg Description

to return a strongly typed list of User objects?

Thank you for any code examples or pointers.

1

1 Answers

1
votes

You query it like this:

var results = (from u in Session.Query<User>("YourUserIndex")
              where u.IsActive && u.Tags.Any(x=>x == "hello") && x.Tags.Any(x=>x=="world")
              select u)
              .Search(x=>x.Description, "abject failure")
              .ToList();

Where YourUserIndex looks like this:

from u in docs.Users
select new { u.IsActive, u.Tags, u.Description };

And you need to mark the Description field as analyzed.