biofractal
biofractal

Reputation: 19133

How should I filter the results of a RavenDB Lucene search?

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:

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:

to return a strongly typed list of User objects?

Thank you for any code examples or pointers.

Upvotes: 2

Views: 399

Answers (1)

Ayende Rahien
Ayende Rahien

Reputation: 22956

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.

Upvotes: 1

Related Questions