Lucene2Objects goes public!

I’ve been asked a couple times to release the Lucene2Objects code out in the wild and it was the intention all the way, but I didn’t had the proper time to do it, not the proper bandwidth.
The new Lucene2Objects has a couple new changes and it’s more lightweight than the first versions since it doesn’t depends on Ninject anymore. Feel free to browse the code, leave issues or just email me with any problem you have.
If you are using Lucene2Objects, let me know as I want to know if it’s been useful out there.

All the best,
David

Introducing Lucene2Objects

I’ve been playing with Lucene .NET for over 2 years now. It all started as part of my incorporation to a NLP investigation group and my first task was to look into Lucene since nobody was using it. I was baffled with the strength that Lucene had, besides, the biggest players were using it! Now that I’ve get to know it a bit better I see why so many people use it, put simple: It’s awesome! However, Lucene does have a problem which is the learning curve. Wrapping your head around the concept of documents, queries, analyzers and how to get a pseudo efficient search working are a few of the issues with using Lucene on a project.

Enter Lucene2Objects, my basic idea is to make a simple interface into Lucene for those developers wanting to incorporate search annotations into the domain model. Now, let’s take an example of a system handling messages (of the “Hi! How do you do?” kind, not the WM_PAINT kind), is most probably that users would like to search for something inside their messages. A (very) basic approach gives us a simple class:

public class Message
{
 public int Id { get; set; }

 public string Text { get; set; }

 public string Title { get; set; }

 public DateTime Sent { get; set; }
}

This is neat, but if I want to implement search I can either use the services provided by my DB backend as Full Text Indexing from SQL Server (which is awesome by the way, but lacks some other cool stuff) but the biggest problem is that we would then be fixing (or tightly coupling, for the fan boys of OOP/IoC/SOLID) the data store to the solution of finding a text, which is almost definitely a bad thing.

Now, if we want to use Lucene, we need to make a few configuration stuff, learn some stuff about indexing, tokenizers, analyzers and a huge list of stuff that some folks (me included) find amusing, but others find really boring (not to mention those who find it daunting). But imagine a world where you could do something like this:

var iWriter = new IndexWriter(Environment.CurrentDirectory + @"index");
var message = new Message { Id = 12, Sent = DateTime.Now, 
                            Text = "Some text on the message!", 
                            Title = "This is the title" 
              };
iWriter.AddEntity(message);
iWriter.Close();

Cool uh? Just point a folder and save. Nice! Well, and how would I search for stuff on that folder? Easy piece

var iReader = new IndexReader(Environment.CurrentDirectory + @"index");
var messages = iReader.Search<Message>("text");

foreach (var message in messages) {
 Console.WriteLine("Message: {0}", message.Title);
}

Fine! And how does my model knows where to search? What to index? What not to index? Well, validations were a similar issue, so, why not give it a similar solution? Just annotate away!

[SearchableEntity(DefaultSearchProperty = "Text")]
public class Message
{
 public int Id { get; set; }

 [Indexed]
 public string Text { get; set; }

 [Indexed]
 public string Title { get; set; }

 public DateTime Sent { get; set; }

 public DateTime? Read { get; set; }
}

If you liked that way of handling things with Lucene, you’ll love Lucene2Objects. Keep in mind however, that I’m the only person working with this idea, so if you like it and want to put something into it, let me know! For now, I’ll leave the Lucene2Objects as a package in Nuget, so you can play with it. I’ll put it into my BitBucket repo this week along with my Scaffolders for SharpLite.