Chapter 7. Quick start dedicated to the Lucene search support

7.1. Aim of the quickstart

The aim of this section is to provide quickly a short view of the way to implement search on a Lucene index using the Lucene support. It allows to show the usage of the main entities of this support and how to configure them in a simply way.

We assume that you run the previous quick start in order to populate the index and have documents that match the query.

7.2. Configure the search resources

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. An instance of SearcherFactory must be configured too in order to use the search facility of Spring.

The following code shows the configutation of these entities:

<?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:searcherFactory/>
    </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 allow 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.

7.3. Search documents in the index

The next step is to use the configured factory in order to search the index. In that purpose, we create a search service class named SampleSearchService. The later used the abstract class LuceneSearchDaoSupport as parent class to have the setter method for the factory previously configured.

The following code shows the skeleton of the class SampleSearchService:

public class SampleSearchService extends LuceneSearchDaoSupport {

    public void searchDocuments() {
        (...)
    }
}

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="sampleSearchService" class="org.springframework.lucene.sample.SampleSearchService">
        <property name="searcherFactory" ref="myDirectory-searcherFactory"/>
    </bean>
</beans>

You have now a fully configured class for searching. You can now use the central entity of the Lucene search support, the LuceneSearchTemplate, in order to implement your searching features. In the sample, we describe how to search documents basing a string query, as in the following code:

public void searchDocuments() {
    List<String> results = getLuceneSearchTemplate().search("field:lucene", new HitExtractor() {
        public Object mapHit(int id, Document document, float score) {
            return document.get("field");
        }
    });

    for (String field : results) {
        System.out.println("Result: " + field);
    }
}