NoSQL Zone is brought to you in partnership with:

Ayende Rahien is working for Hibernating Rhinos LTD, a Israeli based company producing developer productivity tools for OLTP applications such as NHibernate Profiler (nhprof.com), Linq to SQL Profiler(l2sprof.com), Entity Framework Profiler (efprof.com) and more. Ayende is a DZone MVB and is not an employee of DZone and has posted 479 posts at DZone. You can read more from them at their website. View Full User Profile

The Lazy Man's Comprehensive RavenDB Search

04.27.2012
| 3100 views |
  • submit to reddit

RavenDB supports many types of searches, and in this case, I want to show something that belongs to the cool parts of the pile, but also on the “you probably don’t really want to do this”.

First, let me explain why this is cool, then we will talk about why you probably don’t want to do that (and finally, about scenarios where you actually do want this).

Here is an index that will allow you to search over all of the values of all of the properties in the user entity:

    public class Users_AllProperties : AbstractIndexCreationTask<User, Users_AllProperties.Result>
    {
        public class Result
        {
            public string Query { get; set; }
        }
        public Users_AllProperties()
        {
            Map = users =>
                  from user in users
                  select new
                  {
                      Query = AsDocument(user).Select(x => x.Value)
                  };
            Index(x=>x.Query, FieldIndexing.Analyzed);
        }
    }

This can be easily query for things like:

    s.Query<Users_AllProperties.Result, Users_AllProperties>()
        .Where(x=>x.Query == "Ayende") // search first name
        .As<User>()
        .ToList()


    s.Query<Users_AllProperties.Result, Users_AllProperties>()
        .Where(x=>x.Query == "Rahien") // search last name
        .As<User>()
        .ToList()

The fun part is that because we are actually going to index all the properties values into the Query field, which then allow us to easily query for every one of the values without any trouble.

The problem with that is that this is also quite wasteful and likely to lead to bad results down the road. Why?

For two major reasons. First, because this is going to index everything, and would result in larger index, more IO, etc. The second reason is that it is going to lead to bad results because you are now searching over everything, including the “last login date” and the “password hint”. That means that your search results relevancy is going to be poor.

So why would you ever want to do something like that if it is bad?

Well, there are a few scenarios where this is applicable. You need to do that if you want to be able to search over completely / mostly dynamic entities. And you want to do that if you have entities which are specifically generated for the purpose of being searched.

Both cases are fairly rare (the first case is usually covered by dynamic indexing, anyway), so I wanted to point this out, and also point out that it is usually far better to just specify what are the fields that actually matter for you.

 

Published at DZone with permission of Ayende Rahien, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)