Using Lucene with Spring – Introduction to Spring Modules

7

While preparing a Lucene presentation and workshop I came
across Spring Modules. It’s a small set of libraries that does things the
Spring-way. It isn’t included in Spring because it doesn’t belong to the
‘core-business’ of Spring. I’m not writing about the possibilities of Spring
Modules,  I’ll only focus on the Lucene
library. Nevertheless it’s worthwhile to take a peek at Spring Modules
yourself.

Lucene is a very fast and easy to use java based search
engine. Spring Modules (SM) hides some implementation details and lets you
configure the search engine in XML (like you’re used to in Spring)....

Before you get
started

SM can be downloaded here
(Project Tools, Documents & Files and pick the version with dependencies).
For this article I used the 0.4 version. Since the Spring community is very active
it’s likely that newer versions will be released soon.

Creating the
directory

In Lucene you can use a file based directory  (FSDirectory) or an in-memory directory,
called a RAMDirectory. The beauty of Spring is that you can change the
directory without changing your code. Let’s take a look at the several bean
definitions for directories.

 

<code><br /><font color="#000000">&lt;bean&nbsp;id=</font><font color="#2a00ff">&quot;ramdDirectory&quot;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>class</strong></font><font color="#000000">=</font><font color="#2a00ff">&quot;org.springmodules.lucene.index.support.RAMDirectoryFactoryBean&quot;&nbsp;</font><font color="#000000">/&gt;</font><br /><br /><font color="#000000">&lt;bean&nbsp;id=</font><font color="#2a00ff">&quot;fsDirectory&quot;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>class</strong></font><font color="#000000">=</font><font color="#2a00ff">&quot;org.springmodules.lucene.index.support.FSDirectoryFactoryBean&quot;</font><font color="#000000">&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;location&quot;&nbsp;</font><font color="#000000">value=</font><font color="#2a00ff">&quot;file:///C:/temp/index</font></code><code><font color="#2a00ff">&quot;&nbsp;</font><font color="#000000">/&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;create&quot;&nbsp;</font><font color="#000000">value=</font><font color="#2a00ff">&quot;true&quot;</font><font color="#000000">/&gt;</font><br /><font color="#000000">&lt;/bean&gt;</font><br /><br /><font color="#000000">&lt;alias&nbsp;name=</font><font color="#2a00ff">&quot;ramDirectory&quot;&nbsp;</font><font color="#000000">alias=</font><font color="#2a00ff">&quot;indexDirectory&quot;&nbsp;</font><font color="#000000">/&gt;</font></code>

 

The first definition is the RAMDirectory, nothing special
here. The second bean has some options. You have to set the location of the
index (this is a file system directory) and whether you want do a full index or
incremental index.

Finally I included an alias. With this alias you can easily
switch between indexes (you have to use indexDirectory at at least two places
and you don’t want to change it at two places if you want to swap between
indexes.

 

Creating the index

Our first step is to convert our domain objects to Lucene
Documents. We do this by implementing the DocumentCreator interface. This interface
describes the createDocument()
method that returns a Lucene Document. Just convert your domain object in this
method and use the fieldnames you prefer. In my test application I have an
Employee object that implements DocumentCreator, my createDocument Method looks
like this:

 

<code><br /><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#000000">Document&nbsp;createDocument</font><font color="#000000">()&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">Document&nbsp;doc&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">Document</font><font color="#000000">()</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">doc.add</font><font color="#000000">(</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">Field</font><font color="#000000">(</font><font color="#2a00ff">&quot;name&quot;</font><font color="#000000">,&nbsp;getName</font><font color="#000000">()</font><font color="#000000">,&nbsp;Field.Store.YES,&nbsp;Field.Index.TOKENIZED</font><font color="#000000">))</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">doc.add</font><font color="#000000">(</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">Field</font><font color="#000000">(</font><font color="#2a00ff">&quot;department&quot;</font><font color="#000000">,&nbsp;getDepartment</font><font color="#000000">()</font><font color="#000000">,&nbsp;Field.Store.YES,&nbsp;Field.Index.TOKENIZED</font><font color="#000000">))</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">doc.add</font><font color="#000000">(</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">Field</font><font color="#000000">(</font><font color="#2a00ff">&quot;location&quot;</font><font color="#000000">,&nbsp;getLocation</font><font color="#000000">()</font><font color="#000000">,&nbsp;Field.Store.YES,&nbsp;Field.Index.TOKENIZED</font><font color="#000000">))</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>return&nbsp;</strong></font><font color="#000000">doc;</font><br /><font color="#000000">}</font></code>

 

 

Now we have to define an index factory:

 

<code><br /><font color="#000000">&lt;bean&nbsp;id=</font><font color="#2a00ff">&quot;indexFactory&quot;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>class</strong></font><font color="#000000">=</font><font color="#2a00ff">&quot;org.springmodules.lucene.index.support.SimpleIndexFactoryBean&quot;</font><font color="#000000">&gt;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;directory&quot;</font><font color="#000000">&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;ref&nbsp;bean=</font><font color="#2a00ff">&quot;indexDirectory&quot;&nbsp;</font><font color="#000000">/&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;/property&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;analyzer&quot;</font><font color="#000000">&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;bean&nbsp;</font><font color="#7f0055"><strong>class</strong></font><font color="#000000">=</font><font color="#2a00ff">&quot;org.apache.lucene.analysis.SimpleAnalyzer&quot;&nbsp;</font><font color="#000000">/&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;/property&gt;</font><br /><font color="#000000">&lt;/bean&gt;</font></code>

 

We can point our indexing class to the indexFactory:

 

<code><br /><font color="#000000">&lt;bean&nbsp;id=</font><font color="#2a00ff">&quot;indexAccessor&quot;&nbsp;</font><font color="#7f0055"><strong>class</strong></font><font color="#000000">=</font><font color="#2a00ff">&quot;nl.amis.spring.LuceneIndexer&quot;</font><font color="#000000">&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;indexFactory&quot;</font><font color="#000000">&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;ref&nbsp;local=</font><font color="#2a00ff">&quot;indexFactory&quot;&nbsp;</font><font color="#000000">/&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;/property&gt;</font><br /><font color="#000000">&lt;/bean&gt;</font></code>

 

The indexing class extends LuceneIndexSupport. This class
gives us the getTemplate() method. With this method you can do everything with
your index that was possible without using SM.

 

<code><br /><font color="#7f0055"><strong>public&nbsp;class&nbsp;</strong></font><font color="#000000">LuceneIndexer&nbsp;</font><font color="#7f0055"><strong>extends&nbsp;</strong></font><font color="#000000">LuceneIndexSupport&nbsp;</font><font color="#000000">{</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#7f0055"><strong>void&nbsp;</strong></font><font color="#000000">index</font><font color="#000000">()&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">List&lt;Employee&gt;&nbsp;list&nbsp;=&nbsp;</font><font color="#3f7f5f">//populate&nbsp;list&nbsp;here</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>for&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">Employee&nbsp;e&nbsp;:&nbsp;list</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">getTemplate</font><font color="#000000">()</font><font color="#000000">.addDocument</font><font color="#000000">(</font><font color="#000000">e</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br /><font color="#000000">}</font></code>

 

As you can see we can pass the Employee object to the
addDocument method because Employee implemented the DocumentCreator interface.

All you have to do now is run the index() method and your
index is created.

 

Searching your index

The final step is making a searcher for your index. First we
have to write a converter. We have to convert Lucene Documents to domain
objects. Converting is done with a HitExtractor interface. In your class you’ll
have to implement the mapHit(int id, Document document, float score) method. It
returns an Object. When you’re using Java 1.5 
you can return any type (covariance return) and I strongly advise to do
so.

My  class is called
EmployeeHitExtractor:

 

<code><br /><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#000000">Employee&nbsp;mapHit</font><font color="#000000">(</font><font color="#7f0055"><strong>int&nbsp;</strong></font><font color="#000000">id,&nbsp;Document&nbsp;document,&nbsp;</font><font color="#7f0055"><strong>float&nbsp;</strong></font><font color="#000000">score</font><font color="#000000">)&nbsp;{</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Employee&nbsp;e&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">Employee</font><font color="#000000">()</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">e.setDepartment</font><font color="#000000">(</font><font color="#000000">document.get</font><font color="#000000">(</font><font color="#2a00ff">&quot;department&quot;</font><font color="#000000">))</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">e.setLocation</font><font color="#000000">(</font><font color="#000000">document.get</font><font color="#000000">(</font><font color="#2a00ff">&quot;location&quot;</font><font color="#000000">))</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">e.setName</font><font color="#000000">(</font><font color="#000000">document.get</font><font color="#000000">(</font><font color="#2a00ff">&quot;name&quot;</font><font color="#000000">))</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>return&nbsp;</strong></font><font color="#000000">e;</font><br /><font color="#000000">}</font></code>

 

Now we can create the searcher. First we have to add two
beans to our Spring configuration:

 

<code><br /><font color="#000000">&lt;bean&nbsp;id=</font><font color="#2a00ff">&quot;searcherFactory&quot;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>class</strong></font><font color="#000000">=</font><font color="#2a00ff">&quot;org.springmodules.lucene.search.factory.SimpleSearcherFactory&quot;</font><font color="#000000">&gt;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;directory&quot;</font><font color="#000000">&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;ref&nbsp;bean=</font><font color="#2a00ff">&quot;indexDirectory&quot;&nbsp;</font><font color="#000000">/&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;/property&gt;</font><br /><font color="#000000">&lt;/bean&gt;</font></code>
<code><br /><font color="#000000">&lt;bean&nbsp;id=</font><font color="#2a00ff">&quot;indexSearcher&quot;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>class</strong></font><font color="#000000">=</font><font color="#2a00ff">&quot;nl.amis.spring.LuceneIndexSearcher&quot;</font><font color="#000000">&gt;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;searcherFactory&quot;</font><font color="#000000">&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;ref&nbsp;local=</font><font color="#2a00ff">&quot;searcherFactory&quot;&nbsp;</font><font color="#000000">/&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;/property&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;property&nbsp;name=</font><font color="#2a00ff">&quot;analyzer&quot;</font><font color="#000000">&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;bean&nbsp;</font><font color="#7f0055"><strong>class</strong></font><font color="#000000">=</font><font color="#2a00ff">&quot;org.apache.lucene.analysis.SimpleAnalyzer&quot;&nbsp;</font><font color="#000000">/&gt;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">&lt;/property&gt;</font><br /><font color="#000000">&lt;/bean&gt;</font></code>

 

The beans speak for itself. Now we have to create the
LuceneIndexSearcher class. It extends LuceneSearchSupport. That class provides
a getter and setter for the Analyzer and Factory and provides us with a
LuceneIndexTemplate (accessible via getTemplate())

 

Add a getDocs() method to the class:

 

<code><br /><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#7f0055"><strong>int&nbsp;</strong></font><font color="#000000">getDocs</font><font color="#000000">()&nbsp;</font><font color="#7f0055"><strong>throws&nbsp;</strong></font><font color="#000000">ParseException&nbsp;</font><font color="#000000">{</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">LuceneSearchTemplate&nbsp;template&nbsp;=&nbsp;getTemplate</font><font color="#000000">()</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">QueryParser&nbsp;parser&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">QueryParser</font><font color="#000000">(</font><font color="#2a00ff">&quot;location&quot;</font><font color="#000000">,&nbsp;getAnalyzer</font><font color="#000000">())</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Query&nbsp;query&nbsp;=&nbsp;parser.parse</font><font color="#000000">(</font><font color="#2a00ff">&quot;seattle&quot;</font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">HitExtractor&nbsp;hitExtractor&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">EmployeeHitExtractor</font><font color="#000000">()</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">List&nbsp;list&nbsp;=&nbsp;template.search</font><font color="#000000">(</font><font color="#000000">query,&nbsp;hitExtractor</font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>for&nbsp;</strong></font><font color="#000000">(</font><font color="#7f0055"><strong>int&nbsp;</strong></font><font color="#000000">i&nbsp;=&nbsp;</font><font color="#990000">0</font><font color="#000000">;&nbsp;i&nbsp;&lt;&nbsp;list.size</font><font color="#000000">()</font><font color="#000000">;&nbsp;i++</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#000000">list.get</font><font color="#000000">(</font><font color="#000000">i</font><font color="#000000">))</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>return&nbsp;</strong></font><font color="#000000">list.size</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#000000">}</font></code>

 

This class is not quite useful but demonstrates the idea. First
we parse the query with the Analyzer and then get a list with results (which is
immediately converted to list with Employees).

 

Conclusion

The Lucene library for SM is very powerful and forces us to
do the Lucene things more structured. I also think it’s a good thing to have
definitions for the directory, indexer and searcher in a single XML file. Of
course we can do everything manually, but it’s great someone already did this
for us.

I didn’t mention the DatabaseIndexer interface. The
documentation doesn’t seem to be 100% accurate and it assumes you’re using
JDBC. In my opinion JDBC is a bad thing for object mapping (even in Spring),
you should at least use iBatis
and working with a Object mapper also makes it easier to implement the
DocumentCreator interface.

There is a manual in both PDF and HTML. You can find it by
going to the main page of Spring Modules and scroll down to the header “Usage
Instructions”

 

Sources

http://lucene.apache.org

http://springmodules.dev.java.net

 

Download the project source files here

I used Maven2 to build the project. You might have to install Lucene 1.9.1 (or higher), Spring Modules 0.4 (or higher) and the Oracle JDBC driver manually. I used the HR schema, so you have to have that one too. It’s howere easy to change the database or the schema. Feel free to ask for help if it isn’t working.

Share.

About Author

7 Comments

  1. I don’t know whether the API changed between the 0.4 and the 0.8 version (or if this is a typo from the start), but I spent a few hours trying to use Lucene with this configuration with no success.
    For an FS implementation, you need to put the “create true” property node on the indexFactory bean, not on the FSDirectory one.

  2. Sir,
    I need your help to configure Apache Lucene to search Word Documents,PDF and XML.
    Thanking You

  3. Jeroen van Wilgenburg on

    I don’t have much experience with indexing those files and don’t have any examples lying around. You can go to http://www.zilverline.org, download the application and browse the source. This application is a google desktop search made with Lucene and can index the files you mentioned.

  4. Vishakh Cherian on

    Sir,
    I need your hep to configure Apache Lucene to search Word Documents,PDF and XML.
    Thanking You
    Vishakh Cherian

  5. Jeroen van Wilgenburg on

    Thanks for the tip. I heard the name and know it does something with Lucene and a database, but I’ll give it a try.