Skip to content

Commit bb472e6

Browse files
committed
Add support of LuceneSail#SIMILARITY_CLASS_KEY
- add getter/setter for Similarity algorithm instance Signed-off-by: Jacek Grzebyta <grzebyta.dev@gmail.com>
1 parent 32bea03 commit bb472e6

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

core/sail/fts/lucene-api/src/main/java/org/eclipse/rdf4j/sail/lucene/LuceneSail.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ public class LuceneSail extends NotifyingSailWrapper {
196196
final private Logger logger = LoggerFactory.getLogger(this.getClass());
197197

198198
/**
199-
* Set the parameter "reindexQuery=" to configure the statements to index over. Default value is
200-
* "SELECT ?s ?p ?o ?c WHERE {{?s ?p ?o} UNION {GRAPH ?c {?s ?p ?o.}}} ORDER BY ?s" . NB: the query must
201-
* contain the bindings ?s, ?p, ?o and ?c and must be ordered by ?s.
199+
* Set the parameter "reindexQuery=" to configure the statements to index over. Default value is "SELECT
200+
* ?s ?p ?o ?c WHERE {{?s ?p ?o} UNION {GRAPH ?c {?s ?p ?o.}}} ORDER BY ?s" . NB: the query must contain
201+
* the bindings ?s, ?p, ?o and ?c and must be ordered by ?s.
202202
*/
203203
public static final String REINDEX_QUERY_KEY = "reindexQuery";
204204

@@ -249,6 +249,12 @@ public class LuceneSail extends NotifyingSailWrapper {
249249
*/
250250
public static final String ANALYZER_CLASS_KEY = "analyzer";
251251

252+
/**
253+
* Set this key as sail parameter to configure {@link org.apache.lucene.search.similarities.Similarity}
254+
* class implementation to use for text analysis.
255+
*/
256+
public static final String SIMILARITY_CLASS_KEY = "similarity";
257+
252258
/**
253259
* Set this key as sail parameter to influence whether incomplete queries are treated as failure
254260
* (Malformed queries) or whether they are ignored. Set to either "true" or "false". When ommitted in the
@@ -598,8 +604,7 @@ public Statement mapStatement(Statement statement) {
598604
}
599605

600606
protected Collection<SearchQueryInterpreter> getSearchQueryInterpreters() {
601-
return Arrays.<SearchQueryInterpreter> asList(
602-
new QuerySpecBuilder(incompleteQueryFails),
607+
return Arrays.<SearchQueryInterpreter> asList(new QuerySpecBuilder(incompleteQueryFails),
603608
new DistanceQuerySpecBuilder(luceneIndex), new GeoRelationQuerySpecBuilder(luceneIndex));
604609
}
605610
}

core/sail/fts/lucene/src/main/java/org/eclipse/rdf4j/sail/lucene/LuceneIndex.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
import com.spatial4j.core.context.SpatialContextFactory;
8888
import com.spatial4j.core.shape.Point;
8989
import 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

Comments
 (0)