简体   繁体   中英

Spring Beans configuration for Lucene

So I've been testing out a little experiment of mine (or I think this is more like I'm lazy to actually put all of these into java code) by mixing Spring Beans xml configuration and Lucene.

Basically rather than having to code in all the pre-requisites to make an IndexWriter, I'm merely injecting an instance of it to a class. (yes I'm that lazy)

However I've run into a bit of a difficulty with trying to instantiate a SimpleFSDirectory via the beans, notably, I want my Lucene indexes be in a path relative to the web-app. (maybe /WEB-INF/lucene-indexes or something)

My current configuration is like this:

<bean id="standardAnalyzer" class="org.apache.lucene.analysis.standard.StandardAnalyzer">
    <!-- org.apache.lucene.util.Version -->
    <constructor-arg>
        <value>LUCENE_44</value>
    </constructor-arg>
</bean>

<bean id="fileDir" class="java.io.File">
<constructor-arg index="0">
        <value>classpath:/test-lucene-store</value>
    </constructor-arg>
</bean>

<bean id="directory" class="org.apache.lucene.store.SimpleFSDirectory">
    <constructor-arg index="0">
        <ref bean="fileDir"/>
    </constructor-arg>
</bean>

<bean id="indexWriterConfig" class="org.apache.lucene.index.IndexWriterConfig">
    <constructor-arg index="0">
        <value>LUCENE_44</value>
    </constructor-arg>
    <constructor-arg index="1">
        <ref bean="standardAnalyzer" />
    </constructor-arg>
</bean>

<bean id="indexWriter" class="org.apache.lucene.index.IndexWriter">
    <constructor-arg index="0">
        <ref bean="directory" />
    </constructor-arg>
    <constructor-arg index="1">
        <ref bean="indexWriterConfig" />
    </constructor-arg>
</bean>

My test log is saying that classpath:** is not a valid path, however if I simply use /lucene-store , the indexes are created at D://lucene-store (far from the intended target)

Does any of you know a way to do achieve the goal?

Using java.io.File imples a file system location, not a classpath entry. Of course, you can always refer to an exploded .war and do something like this (on Tomcat):

<value>#{ systemProperties['catalina.base'] }/webapps/myapp.war/WEB-INF/lucene-store</value>

However, a much more flexible approach would be to keep the value in an external location (a property file for example):

<value>#{ myConfig['lucene-store-root'] }</value>

... or even use java.io.tmpdir system property for testing purposes:

<value>#{ systemProperties['java.io.tmpdir'] }/lucene-store</value>

You can also switch to a different Directory implementation ( example ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM