linq to lucene magic unicorn edition

This week I started to look at using Lucene.Net again in a project. Previously I have used it directly however whilst on the new look Lucene.Net website I noticed the linq to lucene project. Which at first look seemed pretty good. However upon looking at the source I found it only works with XML and LinqToSql, this prototype project however was in .Net 4 and used Sql CE4 with Entity Framework 4.1 Code First.




Rather than abandon linq to lucene I decided it should be easy enough to get it working with EF4.1. DatabaseIndexSet contains the LinqToSql specific code so for the purpose of producing a proof of concept this is where all the changes go.



The first item to change is to rescope the generic where clause to be DbContext rather than the linq context.


 public class DatabaseIndexSet : IndexSet
        where TDataContext : DbContext

Next the Write Method needs altering, the LinqToSQL implementation uses the GetTable method, however E.F doesn't really have the same methods. Instead with E.F we can use the Set method with our entity as the generic, this will allow us to obtain all of the records




Finally the GetTableTypes method needs "tweaking", I say tweak as so far its a bit of bodge. The GetTableTypes method is used to find all of the "tables" within our database context which are then later used to create lucene index files. The original LinqToSql method ensured that each property was a generic type and that it is assignable from the base type. For our initial run as our entity classes don't inherit anything I have removed that check, just ensuring the properties are generic. This will be added back in for the final release.



With those changes made we can then use LinqToLucene as follows:


 var index = new EnquiriesIndexContext();
            index.Write();
            var query = from p in index.Parts
                        where p.PartNumber == "ghjtyugfhj-61"
                        select p;
           Console.WriteLine("Simple Query:");
           ObjectDumper.Write(query);
           Console.WriteLine("Query Output: {0}", query.ToString());
           
            var results = query.ToList();
            Console.WriteLine("SimpleDemo returned {0} results", results.Count);



Have a go of the prototype code that is attached, I'll be aiming to tidy it up and get it pushed back to the main project on codeplex over the next week. Enjoy

Update 29/03/2012
I have finally gotten around to updating this and submitting it to CodePlex, further details at http://blog.mjjames.co.uk/2012/03/revisting-lucenetolinq.html

Comments

  1. Hello, Michael!
    Sounds promising. Did you continue working on this project?
    Thanks!
    RP

    ReplyDelete
  2. Sadly it ended up on my TODO list but as my main project that used it went on hold so did this.

    It is something however I'd like to pick up again

    ReplyDelete

Post a Comment

Popular posts from this blog

WebUSB - An unexpected update...

Can you use BuildRoot with Windows Subsystem for Linux......

DotNet CLI , private NuGet feeds and Linux...