mirror of https://github.com/apache/lucene.git
SOLR-515: SimilarityFactory capability, allowing parameters from schema.xml to be used in Similarity construction
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@661547 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fb8ea90070
commit
4c5700cdc9
|
@ -281,6 +281,10 @@ New Features
|
|||
ignoreCase="true" tokenizerFactory="solr.CJKTokenizerFactory"/>
|
||||
(koji)
|
||||
|
||||
50. SOLR-515: Added SimilarityFactory capability to schema.xml,
|
||||
making config file parameters usable in the construction of
|
||||
the global Lucene Similarity implementation.
|
||||
(ehatcher)
|
||||
|
||||
Changes in runtime behavior
|
||||
1. SOLR-559: use Lucene updateDocument, deleteDocuments methods. This
|
||||
|
|
|
@ -361,5 +361,15 @@
|
|||
A custom similarity may be specified here, but the default is fine
|
||||
for most applications. -->
|
||||
<!-- <similarity class="org.apache.lucene.search.DefaultSimilarity"/> -->
|
||||
<!-- ... OR ...
|
||||
Specify a SimilarityFactory class name implementation
|
||||
allowing parameters to be used.
|
||||
-->
|
||||
<!--
|
||||
<similarity class="com.example.solr.CustomSimilarityFactory">
|
||||
<str name="paramkey">param value</str>
|
||||
</similarity>
|
||||
-->
|
||||
|
||||
|
||||
</schema>
|
||||
|
|
|
@ -20,11 +20,11 @@ package org.apache.solr.schema;
|
|||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.TokenStream;
|
||||
import org.apache.lucene.document.Fieldable;
|
||||
import org.apache.lucene.search.DefaultSimilarity;
|
||||
import org.apache.lucene.search.Similarity;
|
||||
import org.apache.lucene.queryParser.QueryParser;
|
||||
import org.apache.solr.common.ResourceLoader;
|
||||
import org.apache.solr.common.SolrException;
|
||||
import org.apache.solr.common.params.SolrParams;
|
||||
import org.apache.solr.common.util.DOMUtil;
|
||||
import org.apache.solr.core.SolrConfig;
|
||||
import org.apache.solr.core.Config;
|
||||
|
@ -34,10 +34,7 @@ import org.apache.solr.analysis.TokenizerChain;
|
|||
import org.apache.solr.analysis.TokenizerFactory;
|
||||
import org.apache.solr.search.SolrQueryParser;
|
||||
import org.apache.solr.util.plugin.AbstractPluginLoader;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.w3c.dom.*;
|
||||
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
|
@ -179,12 +176,17 @@ public final class IndexSchema {
|
|||
*/
|
||||
public Collection<SchemaField> getRequiredFields() { return requiredFields; }
|
||||
|
||||
private Similarity similarity;
|
||||
private SimilarityFactory similarityFactory;
|
||||
|
||||
/**
|
||||
* Returns the Similarity used for this index
|
||||
*/
|
||||
public Similarity getSimilarity() { return similarity; }
|
||||
public Similarity getSimilarity() { return similarityFactory.getSimilarity(); }
|
||||
|
||||
/**
|
||||
* Returns the SimilarityFactory used for this index
|
||||
*/
|
||||
public SimilarityFactory getSimilarityFactory() { return similarityFactory; }
|
||||
|
||||
private Analyzer analyzer;
|
||||
|
||||
|
@ -496,13 +498,30 @@ public final class IndexSchema {
|
|||
dynamicFields = (DynamicField[])dFields.toArray(new DynamicField[dFields.size()]);
|
||||
|
||||
|
||||
Node node = (Node) xpath.evaluate("/schema/similarity/@class", document, XPathConstants.NODE);
|
||||
Node node = (Node) xpath.evaluate("/schema/similarity", document, XPathConstants.NODE);
|
||||
if (node==null) {
|
||||
similarity = new DefaultSimilarity();
|
||||
similarityFactory = new SimilarityFactory() {
|
||||
public Similarity getSimilarity() {
|
||||
return Similarity.getDefault();
|
||||
}
|
||||
};
|
||||
log.fine("using default similarity");
|
||||
} else {
|
||||
similarity = (Similarity)solrConfig.getResourceLoader().newInstance(node.getNodeValue().trim());
|
||||
log.fine("using similarity " + similarity.getClass().getName());
|
||||
final Object obj = solrConfig.getResourceLoader().newInstance(((Element) node).getAttribute("class"));
|
||||
if (obj instanceof SimilarityFactory) {
|
||||
// configure a factory, get a similarity back
|
||||
SolrParams params = SolrParams.toSolrParams(DOMUtil.childNodesToNamedList(node));
|
||||
similarityFactory = (SimilarityFactory)obj;
|
||||
similarityFactory.init(params);
|
||||
} else {
|
||||
// just like always, assume it's a Similarlity and get an ClassCastException - reasonable error handling
|
||||
similarityFactory = new SimilarityFactory() {
|
||||
public Similarity getSimilarity() {
|
||||
return (Similarity) obj;
|
||||
}
|
||||
};
|
||||
}
|
||||
log.fine("using similarity factory" + similarityFactory.getClass().getName());
|
||||
}
|
||||
|
||||
node = (Node) xpath.evaluate("/schema/defaultSearchField/text()", document, XPathConstants.NODE);
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package org.apache.solr.schema;
|
||||
|
||||
import org.apache.lucene.search.Similarity;
|
||||
import org.apache.solr.common.params.SolrParams;
|
||||
|
||||
public abstract class SimilarityFactory {
|
||||
protected SolrParams params;
|
||||
|
||||
public void init(SolrParams params) { this.params = params; }
|
||||
public SolrParams getParams() { return params; }
|
||||
|
||||
public abstract Similarity getSimilarity();
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.apache.solr.schema;
|
||||
|
||||
import org.apache.lucene.search.Similarity;
|
||||
|
||||
public class CustomSimilarityFactory extends SimilarityFactory {
|
||||
public Similarity getSimilarity() {
|
||||
return new MockConfigurableSimilarity(params.get("echo"));
|
||||
}
|
||||
}
|
|
@ -26,6 +26,7 @@ import org.apache.solr.core.SolrCore;
|
|||
import org.apache.solr.request.LocalSolrQueryRequest;
|
||||
import org.apache.solr.request.SolrQueryRequest;
|
||||
import org.apache.solr.util.AbstractSolrTestCase;
|
||||
import org.apache.lucene.search.Similarity;
|
||||
|
||||
|
||||
public class IndexSchemaTest extends AbstractSolrTestCase {
|
||||
|
@ -82,4 +83,11 @@ public class IndexSchemaTest extends AbstractSolrTestCase {
|
|||
,"//result/doc[1]/int[@name='id'][.='10']"
|
||||
);
|
||||
}
|
||||
|
||||
public void testSimilarityFactory() {
|
||||
SolrCore core = h.getCore();
|
||||
Similarity similarity = core.getSchema().getSimilarity();
|
||||
assertTrue("wrong class", similarity instanceof MockConfigurableSimilarity);
|
||||
assertEquals("is there an echo?", ((MockConfigurableSimilarity)similarity).getPassthrough());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package org.apache.solr.schema;
|
||||
|
||||
import org.apache.lucene.search.DefaultSimilarity;
|
||||
|
||||
public class MockConfigurableSimilarity extends DefaultSimilarity {
|
||||
private String passthrough;
|
||||
|
||||
public MockConfigurableSimilarity(String passthrough) {
|
||||
this.passthrough = passthrough;
|
||||
}
|
||||
|
||||
public String getPassthrough() {
|
||||
return passthrough;
|
||||
}
|
||||
}
|
|
@ -446,6 +446,8 @@
|
|||
A custom similarity may be specified here, but the default is fine
|
||||
for most applications.
|
||||
-->
|
||||
<!-- <similarity class="org.apache.lucene.search.DefaultSimilarity"/> -->
|
||||
<similarity class="org.apache.solr.schema.CustomSimilarityFactory">
|
||||
<str name="echo">is there an echo?</str>
|
||||
</similarity>
|
||||
|
||||
</schema>
|
||||
|
|
Loading…
Reference in New Issue