The Lucene support provides Spring 2 namespaces in order to make easier the configuration of differents entities.
In order to the namespace, you need to configure as a standard XML namespace
on the beans tag in the Spring configuration. This way to do is
the common one used by all the standard Spring 2 namespaces.
The identifier of the Lucene namespace is http://www.springframework.org/schema/lucene
and the related xsd file http://www.springframework.org/schema/lucene/lucene-index.xsd.
The following code describes the configuration of this namespace:
<?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>
Now, you can use the namespace in order to configure easily the Lucene resources of the support.
The central of this namespace is the index tag which allows
to configure the type of directory and the resources to interact with it to both index and
search documents. The following tags can be used inside it:
ramDirectory or fsDirectory in order to
specify the type of directory used;
indexFactory in order to configure the index factory
resource used to index documents
searcherFactory in order to configure the searcher factory
resource used to index documents
Note that it's not mandatory to use both indexFactory and
searcherFactory tags at the same time. If you only need to make searchs,
you can only specify a searcherFactory tag.
The following describes a simple use of the index tag. You can note
that a global Lucene analyzer is specified to the tag. This analyzer is used by both the index factory
and searcher factory.
<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 concurrent="lock" /> <lucene:searcherFactory/> </lucene:index>
Every tags used within the index tag can specify their own
id attribute if necessary. In this case, they are used for the corresponding
created beans. However, and you can see upper, you can specify an id on
the index tag. This attribute is used to determine a default value for the
identifiers of the inner tags if an id attribute is not specified. Here are the
strategies of creating:
For the *Directory tags, the value of the attribute
is directly used;
For the indexFactory tag, the value of the attribute
is suffixed by -indexFactory;
For the searcherFactory tag, the value of the attribute
is suffixed by -searcherFactory;
When using the fsDirectory tag in order to configure a filesystem
directory, you need to use the location attribute to specify its location. The Spring
IO abstraction can be used in the attribute to locate the index in the classpath or in the filesystem
for instance. If an in-memory index is configured with the ramDirectory tag,
there is no need to use any attribute.
For both the indexFactory and searcherFactory tags,
the type attribut can be used in order to specify the type of the
corresponding implementation to used. By default, the value is simple.
The following table describes all the possible values of the type
attribute for the both the later tags:
Table 7.1.
| Tag | Value | Description |
|---|---|---|
indexFactory | simple | Configure the SimpleIndexFactory implementation for
the index factory. In this case, an index reader or writer is created for each
method of the template unless you use the transactional support where the scope
is tied to the transaction. |
searcherFactory | simple | Configure the SimpleSearcherFactory implementation
for the searcher factory. In this case, a index searcher is created for each
method of the template. |
searcherFactory | single | Configure the SingleSearcherFactory implementation
for the searcher factory. In this case, a single index searcher is created and
used for each method of the template. This searcher is destroyed when the
application shutdown. |
For the index factory resource, a concurrent attribut can be used
in order to enable concurrent access to Lucene reader and writer. The only possible value is
lock in order to configure a lock based approach.
Both the index factory and searcher factory need a Lucene analyzer for
their configuration. Like the id attribute, an analyzer can be configured
in the global way according to the analyzer-ref attribute of the
index tag. In this case, the configured analyzer is used by both
index and searcher factories, as shown in the following code:
<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:searcherFactory/> </lucene:index>
If you want to configure the analyzer bean as an inner bean, the analyzer
inner tag can use as described in the following code:
<lucene:index id="myDirectory"> <lucene:analyzer> <bean class="org.apache.lucene.analysis.SimpleAnalyzer"/> </lucene:analyzer> <lucene:fsDirectory location="/org/springframework/lucene/config"/> <lucene:indexFactory/> <lucene:searcherFactory/> </lucene:index>
The namespace provides too the possibility to configure different analyzers for
the index and search factories by using the analyzer-ref attribute
of the indexFactory and searcherFactory tags. The
following code describes the aspect:
<bean id="analyzer1" class="org.apache.lucene.analysis.SimpleAnalyzer"/> <bean id="analyzer2" class="org.apache.lucene.analysis.SimpleAnalyzer"/> <lucene:index id=""> <lucene:fsDirectory location="/org/springframework/lucene/config"/> <lucene:indexFactory id="" concurrent="lock" analyzer-ref="analyzer1"/> <lucene:searcherFactory id="" analyzer-ref="analyzer2"/> </lucene:index>
Like the global approach, both indexFactory and
searcherFactory tags can configure an analyzer as an inner
bean using the analyzer inner tag, as shown below:
<bean id="analyzer" class="org.apache.lucene.analysis.SimpleAnalyzer"/> <lucene:index id=""> <lucene:fsDirectory location="/org/springframework/lucene/config"/> <lucene:indexFactory id="" concurrent="lock" analyzer-ref="analyzer"/> <lucene:searcherFactory id=""> <lucene:analyzer> <bean class="org.apache.lucene.analysis.SimpleAnalyzer"/> </lucene:analyzer> </lucene:searcherFactory> </lucene:index>