Click here to Skip to main content
Click here to Skip to main content
Go to top

Simplifying Lucene using Adapter Pattern, Generics, Reflection, and Custom Attributes

, 14 Dec 2012
Rate this:
Please Sign up or sign in to vote.
using Lucene out of the box using C# .Net and Generics

Introduction 

I am introducing a draft idea of how we can make Lucene.Net more RAD and also simplifying how .NET developers will interact with Lucene.Net.

The idea is very simple. we need to make the developer code normally in C# Generics. That means the developer just needs to build a list of his document objects List<Student>, for example, and passing this to Lucene.Net to store it using default index and store configuration. Also to search Lucene.Net, either send a free string search query, or build a search object that really represents your criteria to search your document.

By building an Adapter component that will handle the communication between Lucene.Net and C# objects using very simple calls that will allow the result of the idea to come to true using custom attributes to allow us to decorate the Document class properties to explicitly say what type of Index or Store you want this property to be configured in Lucene.Net. 

Lucene Cloud Adapter Diagram

I attached a complete working examples that describe the idea, please feel free to try it and add your comment of how we can extend this. 

Using the Code 

This is how the code will look like for pushing data to cloud:

// see now how easy we will store the data in correct lucene format
// 2 lines of code and full OOP
List<Student> Students = GetStudentsFromDB();
LuceneCloudAdapter<Student, SearchStudents> lcA = 
            new LuceneCloudAdapter<Student,SearchStudents>(DocContext);
bool result=lcA.PushToCloud(Students);

This is how the code will look like for searching data using search string: 

LuceneSearchInfo si = new LuceneSearchInfo();
string searchStr = "Name:Haitham";
List<Student> Students = lcA.Search<Release>(searchStr, true, si);

This is how the code will look like for searching data using search object:

//fill the search object with data
SearchStudents se = new SearchStudents();
//.... fill your search object here
// .....

// no we had filled the search object simply you will pass it for search function.
LuceneCloudAdapter<Student, SearchStudents> lcA = 
    new LuceneCloudAdapter<Student, SearchStudents>(DocContext);
LuceneSearchInfo si = new LuceneSearchInfo();
List<Student> Students = lcA.Search<Student>(se,si);

A document class will look like this. You can remove custom attributes if you want to use the default : 

class Student
{
  [LuceneIndex(Value = IndexAttribute.NOT_ANALYZED)]
  [LuceneStore(Value = StoreAttribute.YES)]
  public int id { get; set; }

  [LuceneIndex(Value = IndexAttribute.ANALYZED)]
  [LuceneStore(Value = StoreAttribute.YES)]
  public string name { get; set; }

  [LuceneIndex(Value = IndexAttribute.ANALYZED)]
  [LuceneStore(Value = StoreAttribute.YES)]
  public string department { get; set; }

  [LuceneIndex(Value = IndexAttribute.NOT_ANALYZED)]
  [LuceneStore(Value = StoreAttribute.YES)]
  public DateTime startdate { get; set; }

  [LuceneIndex(Value = IndexAttribute.NOT_ANALYZED)]
  [LuceneStore(Value = StoreAttribute.YES)]
  public float gpa { get; set; }

  [LuceneIndex(Value = IndexAttribute.NOT_ANALYZED)]
  [LuceneStore(Value = StoreAttribute.YES)]
  public double allowance { get; set; }
}

A search class will look like this:

class SearchStudents
{
 // this is the same as the object class 
 // If you will search the field normally add the same field name
 //if you will search the field in range , add from , and to to the field.
 
 public int? id { get; set; }
 public string name { get; set; }
 public string department { get; set; }

 //range search for Start Date and other ranges
 public DateTime? startdatefrom { get; set; }
 public DateTime? startdateto { get; set; }
 public float? gpafrom { get; set; }
 public float? gpato { get; set; }
 public double? allowancefrom { get; set; }
 public double? allowanceto { get; set; }
}

Please see the attached project sample and everything will be very clear. 

Points of Interest 

The idea behind that class that it is used as an Adapter Pattern to simplify the communication between Lucene.Net and C# OOP so developers can send normal objects to search and also receive the result in a collection using Reflection and custom attributes to simplify Lucene and make it more RAD development. The next step really is to componentize this to make it a visual component that can communicate with DB providers like SQL Server, and generate classes and search UI by drag drop, like the DataSet component and other Data Source components. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Haitham Khedre
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 14 Dec 2012
Article Copyright 2012 by Haitham Khedre
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid