The aim of this section is to provide quickly a short view of the way to implement indexing on a Lucene index using the Lucene support. It allows us to show the usage of the main entities of this support and how to configure them in a simply way.
The Lucene support of Spring provides a dedicated namespace in order to easily configure resources related to the index. It supports both in memory and filesystem indexes.
The first step of the configuration is to create a root structure of the Spring XML file integrating the lucene namespace. This configuration is shown in the following code:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lucene="http://www.springframework.org/schema/lucene" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lucene http://www.springframework.org/schema/lucene/lucene-index.xsd"> (...) </beans>
For the example, we configure a simple filesystem index whose the content is located in the classpath. The tag index of the namespace and its attributes must be used in order to configure this aspect, as shown in the following code:
<?xml version="1.0" encoding="UTF-8"?> <beans (...)> <bean id="analyzer" class="org.apache.lucene.analysis.SimpleAnalyzer"/> <lucene:index id="myDirectory" analyzer-ref="analyzer"> <lucene:fsDirectory location="/org/springframework/lucene/config"/> <lucene:indexFactory/> </lucene:index> </beans>
You can note that the simple analyzer of Lucene is used and configured as a bean in the previous code.
Now that you have configured the index with the index tag, the lucene support allows you to access a factory in order to obtain resources in order to interact with the index. This factory is automatically configured as a Spring bean with the identifier specified on the index tag.
The next step is to use the configured factory in order to interact with the index, indexing documents in our case. In that purpose, we create an indexing service class named SampleIndexingService. The later used the abstract class LuceneIndexDaoSupport as parent class to have the setter method for the factory previously configured.
The following code shows the skeleton of the class SampleIndexingService:
public class SampleIndexingService extends LuceneIndexDaoSupport { public void indexDocuments() { (...) } }
The configuration of this class in Spring is really simple, as described in the following code:
<?xml version="1.0" encoding="UTF-8"?> <beans (...)> (...) <lucene:index id="myDirectory" analyzer-ref="analyzer"> (...) </lucene:index> <bean id="sampleIndexingService" class="org.springframework.lucene.sample.SampleIndexingService"> <property name="indexFactory" ref="myDirectory-indexFactory"/> </bean> </beans>
You have now a fully configured class for indexing. You can now use the central entity of the Lucene indexing support, the LuceneIndex>Template, in order to implement your indexing features. In the sample, we describe how to index two documents, as in the following code:
public void indexDocuments() { Document document1 = new Document(); document1.add(new Field("field", "a sample 1", Field.Store.YES, Field.Index.ANALYZED)); document1.add(new Field("sort", "2", Field.Store.YES, Field.Index.NOT_ANALYZED)); getLuceneIndexTemplate().addDocument(document1); Document document2 = new Document(); document2.add(new Field("field", "a sample 2", Field.Store.YES, Field.Index.ANALYZED)); document2.add(new Field("sort", "1", Field.Store.YES, Field.Index.NOT_ANALYZED)); getLuceneIndexTemplate().addDocument(document2); }
The support provides an implementation in order to use the declarative transaction support of Spring. This implementation, the class LuceneIndexTransactionManager, can be configured basing on the previous factory. The following code describes the configuration of the manager:
<?xml version="1.0" encoding="UTF-8"?> <beans (...)> (...) <bean id="luceneIndexTransactionManager" class="org.springframework.lucene.index.factory.LuceneIndexTransactionManager"> <property name="indexFactory" ref="myDirectory-indexFactory"/> </bean> </beans>
Then, the aop and transactional supports of Spring can be used in order to apply transactions on the indexDocuments method of the SampleIndexingService class, as described below:
<?xml version="1.0" encoding="UTF-8"?> <beans (...)> (...) <aop:config> <aop:advisor pointcut="execution(* *..SampleIndexingService.*(..))" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="indexDocuments"/> </tx:attributes> </tx:advice> </beans>