8787import com .spatial4j .core .context .SpatialContextFactory ;
8888import com .spatial4j .core .shape .Point ;
8989import com .spatial4j .core .shape .Shape ;
90+ import org .apache .lucene .search .similarities .DefaultSimilarity ;
91+ import org .apache .lucene .search .similarities .Similarity ;
9092
9193/**
9294 * A LuceneIndex is a one-stop-shop abstraction of a Lucene index. It takes care of proper synchronization of
@@ -118,6 +120,8 @@ public class LuceneIndex extends AbstractLuceneIndex {
118120
119121 private volatile Analyzer queryAnalyzer ;
120122
123+ private volatile Similarity similarity ;
124+
121125 /**
122126 * The IndexWriter that can be used to alter the index' contents. Created lazily.
123127 */
@@ -162,6 +166,7 @@ public synchronized void initialize(Properties parameters)
162166 super .initialize (parameters );
163167 this .directory = createDirectory (parameters );
164168 this .analyzer = createAnalyzer (parameters );
169+ this .similarity = createSimilarity (parameters );
165170 // slightly hacky cast to cope with the fact that Properties is
166171 // Map<Object,Object>
167172 // even though it is effectively Map<String,String>
@@ -203,6 +208,21 @@ protected Analyzer createAnalyzer(Properties parameters)
203208 return analyzer ;
204209 }
205210
211+ protected Similarity createSimilarity (Properties parameters )
212+ throws Exception
213+ {
214+ Similarity similarity ;
215+ if (parameters .containsKey (LuceneSail .SIMILARITY_CLASS_KEY )) {
216+ similarity = (Similarity )Class .forName (
217+ parameters .getProperty (LuceneSail .SIMILARITY_CLASS_KEY )).newInstance ();
218+ }
219+ else {
220+ similarity = new DefaultSimilarity ();
221+ }
222+
223+ return similarity ;
224+ }
225+
206226 private void postInit ()
207227 throws IOException
208228 {
@@ -212,6 +232,7 @@ private void postInit()
212232 if (!DirectoryReader .indexExists (directory )) {
213233 logger .debug ("creating new Lucene index in directory {}" , directory );
214234 IndexWriterConfig indexWriterConfig = new IndexWriterConfig (analyzer );
235+ indexWriterConfig .setSimilarity (similarity );
215236 indexWriterConfig .setOpenMode (OpenMode .CREATE );
216237 IndexWriter writer = new IndexWriter (directory , indexWriterConfig );
217238 writer .close ();
@@ -274,7 +295,9 @@ public synchronized IndexSearcher getIndexSearcher()
274295 if (closed .get ()) {
275296 throw new SailException ("Index has been closed" );
276297 }
277- return getCurrentMonitor ().getIndexSearcher ();
298+ IndexSearcher indexSearcher = getCurrentMonitor ().getIndexSearcher ();
299+ indexSearcher .setSimilarity (similarity );
300+ return indexSearcher ;
278301 }
279302
280303 /**
0 commit comments