diff --git a/lucene/src/java/org/apache/lucene/search/similarities/BM25Similarity.java b/lucene/src/java/org/apache/lucene/search/similarities/BM25Similarity.java index c9c542af10c..62306fa05ef 100644 --- a/lucene/src/java/org/apache/lucene/search/similarities/BM25Similarity.java +++ b/lucene/src/java/org/apache/lucene/search/similarities/BM25Similarity.java @@ -336,4 +336,12 @@ public class BM25Similarity extends Similarity { public String toString() { return "BM25(k1=" + k1 + ",b=" + b + ")"; } + + public float getK1() { + return k1; + } + + public float getB() { + return b; + } } diff --git a/lucene/src/java/org/apache/lucene/search/similarities/DFRSimilarity.java b/lucene/src/java/org/apache/lucene/search/similarities/DFRSimilarity.java index 6e3039687cc..bd2010e33c5 100644 --- a/lucene/src/java/org/apache/lucene/search/similarities/DFRSimilarity.java +++ b/lucene/src/java/org/apache/lucene/search/similarities/DFRSimilarity.java @@ -83,4 +83,16 @@ public class DFRSimilarity extends SimilarityBase { return "DFR " + basicModel.toString() + afterEffect.toString() + normalization.toString(); } + + public BasicModel getBasicModel() { + return basicModel; + } + + public AfterEffect getAfterEffect() { + return afterEffect; + } + + public Normalization getNormalization() { + return normalization; + } } diff --git a/lucene/src/java/org/apache/lucene/search/similarities/IBSimilarity.java b/lucene/src/java/org/apache/lucene/search/similarities/IBSimilarity.java index eb15e3d6e77..aec89752703 100644 --- a/lucene/src/java/org/apache/lucene/search/similarities/IBSimilarity.java +++ b/lucene/src/java/org/apache/lucene/search/similarities/IBSimilarity.java @@ -91,4 +91,16 @@ public class IBSimilarity extends SimilarityBase { return "IB " + distribution.toString() + "-" + lambda.toString() + normalization.toString(); } + + public Distribution getDistribution() { + return distribution; + } + + public Lambda getLambda() { + return lambda; + } + + public Normalization getNormalization() { + return normalization; + } } diff --git a/lucene/src/java/org/apache/lucene/search/similarities/NormalizationH3.java b/lucene/src/java/org/apache/lucene/search/similarities/NormalizationH3.java index b00fc17b3d6..dc5cd35d8c1 100644 --- a/lucene/src/java/org/apache/lucene/search/similarities/NormalizationH3.java +++ b/lucene/src/java/org/apache/lucene/search/similarities/NormalizationH3.java @@ -41,4 +41,8 @@ public class NormalizationH3 extends Normalization { public String toString() { return "3(" + mu + ")"; } + + public float getMu() { + return mu; + } } diff --git a/lucene/src/java/org/apache/lucene/search/similarities/NormalizationZ.java b/lucene/src/java/org/apache/lucene/search/similarities/NormalizationZ.java index 9f15288f222..224dea8081b 100644 --- a/lucene/src/java/org/apache/lucene/search/similarities/NormalizationZ.java +++ b/lucene/src/java/org/apache/lucene/search/similarities/NormalizationZ.java @@ -41,4 +41,8 @@ public class NormalizationZ extends Normalization { public String toString() { return "Z(" + z + ")"; } + + public float getZ() { + return z; + } } diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 6277d826e70..33490d62ab7 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -171,6 +171,13 @@ New Features * SOLR-2703: Added support for Lucene's "surround" query parser. (Simon Rosenthal, ehatcher) +* SOLR-2754: Added factories for several ranking algorithms: + BM25SimilarityFactory: Okapi BM25 + DFRSimilarityFactory: Divergence from Randomness models + IBSimilarityFactory: Information-based models + LMDirichletSimilarity: LM with Dirichlet smoothing + LMJelinekMercerSimilarity: LM with Jelinek-Mercer smoothing + (David Mark Nemeskey, Robert Muir) Optimizations ---------------------- diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java index 4901c637da8..3e1ac3bce43 100644 --- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java +++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java @@ -470,7 +470,7 @@ public final class IndexSchema { }; log.debug("using default similarityProvider"); } else { - final Object obj = loader.newInstance(((Element) node).getAttribute("class")); + final Object obj = loader.newInstance(((Element) node).getAttribute("class"), "search.similarities."); // just like always, assume it's a SimilarityProviderFactory and get a ClassCastException - reasonable error handling // configure a factory, get a similarity back NamedList args = DOMUtil.childNodesToNamedList(node); @@ -714,7 +714,7 @@ public final class IndexSchema { return null; } else { SimilarityFactory similarityFactory; - final Object obj = loader.newInstance(((Element) node).getAttribute("class")); + final Object obj = loader.newInstance(((Element) node).getAttribute("class"), "search.similarities."); if (obj instanceof SimilarityFactory) { // configure a factory, get a similarity back SolrParams params = SolrParams.toSolrParams(DOMUtil.childNodesToNamedList(node)); diff --git a/solr/core/src/java/org/apache/solr/search/similarities/BM25SimilarityFactory.java b/solr/core/src/java/org/apache/solr/search/similarities/BM25SimilarityFactory.java new file mode 100644 index 00000000000..3b18ef0af94 --- /dev/null +++ b/solr/core/src/java/org/apache/solr/search/similarities/BM25SimilarityFactory.java @@ -0,0 +1,62 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.BM25Similarity; +import org.apache.lucene.search.similarities.Similarity; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.schema.SimilarityFactory; + +/** + * Factory for {@link BM25Similarity} + *

+ * Parameters: + *

+ *

+ * Optional settings: + *

+ * @lucene.experimental + */ +public class BM25SimilarityFactory extends SimilarityFactory { + private boolean discountOverlaps; + private float k1; + private float b; + + @Override + public void init(SolrParams params) { + super.init(params); + discountOverlaps = params.getBool("discountOverlaps", true); + k1 = params.getFloat("k1", 1.2f); + b = params.getFloat("b", 0.75f); + } + + @Override + public Similarity getSimilarity() { + BM25Similarity sim = new BM25Similarity(k1, b); + sim.setDiscountOverlaps(discountOverlaps); + return sim; + } +} diff --git a/solr/core/src/java/org/apache/solr/search/similarities/BasicSimilarityProviderFactory.java b/solr/core/src/java/org/apache/solr/search/similarities/BasicSimilarityProviderFactory.java new file mode 100644 index 00000000000..e9dcc11295c --- /dev/null +++ b/solr/core/src/java/org/apache/solr/search/similarities/BasicSimilarityProviderFactory.java @@ -0,0 +1,47 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.SimilarityProvider; // javadoc +import org.apache.solr.schema.IndexSchema; +import org.apache.solr.schema.SimilarityProviderFactory; +import org.apache.solr.search.SolrSimilarityProvider; + +/** + * This class is aimed at non-VSM models, and therefore both the + * {@link SimilarityProvider#coord} and + * {@link SimilarityProvider#queryNorm} methods return {@code 1}. + * @lucene.experimental + */ +public class BasicSimilarityProviderFactory extends SimilarityProviderFactory { + + @Override + public SolrSimilarityProvider getSimilarityProvider(IndexSchema schema) { + return new SolrSimilarityProvider(schema) { + @Override + public float coord(int overlap, int maxOverlap) { + return 1f; + } + + @Override + public float queryNorm(float sumOfSquaredWeights) { + return 1f; + } + }; + } +} diff --git a/solr/core/src/java/org/apache/solr/search/similarities/DFRSimilarityFactory.java b/solr/core/src/java/org/apache/solr/search/similarities/DFRSimilarityFactory.java new file mode 100644 index 00000000000..23ef1a724c1 --- /dev/null +++ b/solr/core/src/java/org/apache/solr/search/similarities/DFRSimilarityFactory.java @@ -0,0 +1,186 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.AfterEffect; +import org.apache.lucene.search.similarities.AfterEffect.NoAfterEffect; // javadoc +import org.apache.lucene.search.similarities.AfterEffectB; +import org.apache.lucene.search.similarities.AfterEffectL; +import org.apache.lucene.search.similarities.BasicModel; +import org.apache.lucene.search.similarities.BasicModelBE; +import org.apache.lucene.search.similarities.BasicModelD; +import org.apache.lucene.search.similarities.BasicModelG; +import org.apache.lucene.search.similarities.BasicModelIF; +import org.apache.lucene.search.similarities.BasicModelIn; +import org.apache.lucene.search.similarities.BasicModelIne; +import org.apache.lucene.search.similarities.BasicModelP; +import org.apache.lucene.search.similarities.DFRSimilarity; +import org.apache.lucene.search.similarities.Normalization; +import org.apache.lucene.search.similarities.Normalization.NoNormalization; // javadoc +import org.apache.lucene.search.similarities.NormalizationH1; +import org.apache.lucene.search.similarities.NormalizationH2; +import org.apache.lucene.search.similarities.NormalizationH3; +import org.apache.lucene.search.similarities.NormalizationZ; +import org.apache.lucene.search.similarities.Similarity; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.schema.SimilarityFactory; + +/** + * Factory for {@link DFRSimilarity} + *

+ * You must specify the implementations for all three components of + * DFR (strings). In general the models are parameter-free, but two of the + * normalizations take floating point parameters (see below): + *

    + *
  1. {@link BasicModel basicModel}: Basic model of information content: + * + *
  2. {@link AfterEffect afterEffect}: First normalization of information + * gain: + * + *
  3. {@link Normalization normalization}: Second (length) normalization: + * + *
+ *

+ *

+ * Optional settings: + *

+ * @lucene.experimental + */ +public class DFRSimilarityFactory extends SimilarityFactory { + private boolean discountOverlaps; + private BasicModel basicModel; + private AfterEffect afterEffect; + private Normalization normalization; + + @Override + public void init(SolrParams params) { + super.init(params); + discountOverlaps = params.getBool("discountOverlaps", true); + basicModel = parseBasicModel(params.get("basicModel")); + afterEffect = parseAfterEffect(params.get("afterEffect")); + normalization = parseNormalization( + params.get("normalization"), params.get("mu"), params.get("z")); + } + + private BasicModel parseBasicModel(String expr) { + if ("Be".equals(expr)) { + return new BasicModelBE(); + } else if ("D".equals(expr)) { + return new BasicModelD(); + } else if ("G".equals(expr)) { + return new BasicModelG(); + } else if ("I(F)".equals(expr)) { + return new BasicModelIF(); + } else if ("I(n)".equals(expr)) { + return new BasicModelIn(); + } else if ("I(ne)".equals(expr)) { + return new BasicModelIne(); + } else if ("P".equals(expr)) { + return new BasicModelP(); + } else { + throw new RuntimeException("Invalid basicModel: " + expr); + } + } + + private AfterEffect parseAfterEffect(String expr) { + if ("B".equals(expr)) { + return new AfterEffectB(); + } else if ("L".equals(expr)) { + return new AfterEffectL(); + } else if ("none".equals(expr)) { + return new AfterEffect.NoAfterEffect(); + } else { + throw new RuntimeException("Invalid afterEffect: " + expr); + } + } + + // also used by IBSimilarityFactory + static Normalization parseNormalization(String expr, String mu, String z) { + if (mu != null && z != null) { + throw new RuntimeException( + "specifying mu and z make no sense for: " + expr); + } + if (mu != null && !"H3".equals(expr)) { + throw new RuntimeException( + "parameter mu only makes sense for normalization H3"); + } + if (z != null && !"Z".equals(expr)) { + throw new RuntimeException( + "parameter z only makes sense for normalization Z"); + } + + if ("H1".equals(expr)) { + return new NormalizationH1(); + } else if ("H2".equals(expr)) { + return new NormalizationH2(); + } else if ("H3".equals(expr)) { + return (mu != null) ? new NormalizationH3(Float.parseFloat(mu)) + : new NormalizationH3(); + } else if ("Z".equals(expr)) { + return (z != null) ? new NormalizationZ(Float.parseFloat(z)) + : new NormalizationZ(); + } else if ("none".equals(expr)) { + return new Normalization.NoNormalization(); + } else { + throw new RuntimeException("Invalid normalization: " + expr); + } + } + + @Override + public Similarity getSimilarity() { + DFRSimilarity sim = new DFRSimilarity(basicModel, afterEffect, normalization); + sim.setDiscountOverlaps(discountOverlaps); + return sim; + } +} diff --git a/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java b/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java new file mode 100644 index 00000000000..137a9d1d4e7 --- /dev/null +++ b/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java @@ -0,0 +1,55 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.DefaultSimilarity; +import org.apache.lucene.search.similarities.Similarity; +import org.apache.lucene.search.similarities.TFIDFSimilarity; // javadoc +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.schema.SimilarityFactory; + +/** + * Factory for {@link DefaultSimilarity} + *

+ * DefaultSimilarity is Lucene's default scoring implementation, based + * upon the Vector Space Model. + *

+ * Optional settings: + *

+ * @see TFIDFSimilarity + * @lucene.experimental + */ +public class DefaultSimilarityFactory extends SimilarityFactory { + private boolean discountOverlaps; + + @Override + public void init(SolrParams params) { + super.init(params); + discountOverlaps = params.getBool("discountOverlaps", true); + } + + @Override + public Similarity getSimilarity() { + DefaultSimilarity sim = new DefaultSimilarity(); + sim.setDiscountOverlaps(discountOverlaps); + return sim; + } +} diff --git a/solr/core/src/java/org/apache/solr/search/similarities/IBSimilarityFactory.java b/solr/core/src/java/org/apache/solr/search/similarities/IBSimilarityFactory.java new file mode 100644 index 00000000000..ffb7872d960 --- /dev/null +++ b/solr/core/src/java/org/apache/solr/search/similarities/IBSimilarityFactory.java @@ -0,0 +1,111 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.Distribution; +import org.apache.lucene.search.similarities.DistributionLL; +import org.apache.lucene.search.similarities.DistributionSPL; +import org.apache.lucene.search.similarities.IBSimilarity; +import org.apache.lucene.search.similarities.Lambda; +import org.apache.lucene.search.similarities.LambdaDF; +import org.apache.lucene.search.similarities.LambdaTTF; +import org.apache.lucene.search.similarities.Normalization; +import org.apache.lucene.search.similarities.Similarity; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.schema.SimilarityFactory; +import org.apache.solr.search.similarities.DFRSimilarityFactory; // javadoc + +/** + * Factory for {@link IBSimilarity} + *

+ * You must specify the implementations for all three components of the + * Information-Based model (strings). + *

    + *
  1. {@link Distribution distribution}: Probabilistic distribution used to + * model term occurrence + * + *
  2. + *
  3. {@link Lambda lambda}: λw parameter of the + * probability distribution + * + *
  4. + *
  5. {@link Normalization normalization}: Term frequency normalization + *
    Any supported DFR normalization listed in + {@link DFRSimilarityFactory}
    +
  6. + *
+ *

+ * Optional settings: + *

+ * @lucene.experimental + */ + +public class IBSimilarityFactory extends SimilarityFactory { + private boolean discountOverlaps; + private Distribution distribution; + private Lambda lambda; + private Normalization normalization; + + @Override + public void init(SolrParams params) { + super.init(params); + discountOverlaps = params.getBool("discountOverlaps", true); + distribution = parseDistribution(params.get("distribution")); + lambda = parseLambda(params.get("lambda")); + normalization = DFRSimilarityFactory.parseNormalization( + params.get("normalization"), params.get("mu"), params.get("z")); + } + + private Distribution parseDistribution(String expr) { + if ("LL".equals(expr)) { + return new DistributionLL(); + } else if ("SPL".equals(expr)) { + return new DistributionSPL(); + } else { + throw new RuntimeException("Invalid distribution: " + expr); + } + } + + private Lambda parseLambda(String expr) { + if ("DF".equals(expr)) { + return new LambdaDF(); + } else if ("TTF".equals(expr)) { + return new LambdaTTF(); + } else { + throw new RuntimeException("Invalid lambda: " + expr); + } + } + + @Override + public Similarity getSimilarity() { + IBSimilarity sim = new IBSimilarity(distribution, lambda, normalization); + sim.setDiscountOverlaps(discountOverlaps); + return sim; + } +} diff --git a/solr/core/src/java/org/apache/solr/search/similarities/LMDirichletSimilarityFactory.java b/solr/core/src/java/org/apache/solr/search/similarities/LMDirichletSimilarityFactory.java new file mode 100644 index 00000000000..b118df79000 --- /dev/null +++ b/solr/core/src/java/org/apache/solr/search/similarities/LMDirichletSimilarityFactory.java @@ -0,0 +1,60 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.LMDirichletSimilarity; +import org.apache.lucene.search.similarities.Similarity; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.schema.SimilarityFactory; + +/** + * Factory for {@link LMDirichletSimilarity} + *

+ * Parameters: + *

+ *

+ * Optional settings: + *

+ * @lucene.experimental + */ + +public class LMDirichletSimilarityFactory extends SimilarityFactory { + private boolean discountOverlaps; + private Float mu; + + @Override + public void init(SolrParams params) { + super.init(params); + discountOverlaps = params.getBool("discountOverlaps", true); + mu = params.getFloat("mu"); + } + + @Override + public Similarity getSimilarity() { + LMDirichletSimilarity sim = (mu != null) ? new LMDirichletSimilarity(mu) + : new LMDirichletSimilarity(); + sim.setDiscountOverlaps(discountOverlaps); + return sim; + } +} diff --git a/solr/core/src/java/org/apache/solr/search/similarities/LMJelinekMercerSimilarityFactory.java b/solr/core/src/java/org/apache/solr/search/similarities/LMJelinekMercerSimilarityFactory.java new file mode 100644 index 00000000000..9a8f74376dc --- /dev/null +++ b/solr/core/src/java/org/apache/solr/search/similarities/LMJelinekMercerSimilarityFactory.java @@ -0,0 +1,59 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.LMJelinekMercerSimilarity; +import org.apache.lucene.search.similarities.Similarity; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.schema.SimilarityFactory; + +/** + * Factory for {@link LMJelinekMercerSimilarity} + *

+ * Parameters: + *

+ *

+ * Optional settings: + *

+ * @lucene.experimental + */ + +public class LMJelinekMercerSimilarityFactory extends SimilarityFactory { + private boolean discountOverlaps; + private float lambda; + + @Override + public void init(SolrParams params) { + super.init(params); + discountOverlaps = params.getBool("discountOverlaps", true); + lambda = params.getFloat("lambda", 0.7f); + } + + @Override + public Similarity getSimilarity() { + LMJelinekMercerSimilarity sim = new LMJelinekMercerSimilarity(lambda); + sim.setDiscountOverlaps(discountOverlaps); + return sim; + } +} diff --git a/solr/core/src/test-files/solr/conf/schema-bm25.xml b/solr/core/src/test-files/solr/conf/schema-bm25.xml new file mode 100644 index 00000000000..2dd590c5439 --- /dev/null +++ b/solr/core/src/test-files/solr/conf/schema-bm25.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + 1.2 + 0.76 + + + + + + + + + + + + text + id + diff --git a/solr/core/src/test-files/solr/conf/schema-dfr.xml b/solr/core/src/test-files/solr/conf/schema-dfr.xml new file mode 100644 index 00000000000..c615d633b63 --- /dev/null +++ b/solr/core/src/test-files/solr/conf/schema-dfr.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + I(F) + B + H2 + + + + + + + + I(F) + B + H3 + 900 + + + + + + + + + + + + text + id + diff --git a/solr/core/src/test-files/solr/conf/schema-ib.xml b/solr/core/src/test-files/solr/conf/schema-ib.xml new file mode 100644 index 00000000000..411e481397b --- /dev/null +++ b/solr/core/src/test-files/solr/conf/schema-ib.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + SPL + DF + H2 + + + + + + + + LL + TTF + H3 + 900 + + + + + + + + + + + + text + id + diff --git a/solr/core/src/test-files/solr/conf/schema-lmdirichlet.xml b/solr/core/src/test-files/solr/conf/schema-lmdirichlet.xml new file mode 100644 index 00000000000..8c7c8c990ff --- /dev/null +++ b/solr/core/src/test-files/solr/conf/schema-lmdirichlet.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + 1000 + + + + + + + + + + + + text + id + diff --git a/solr/core/src/test-files/solr/conf/schema-lmjelinekmercer.xml b/solr/core/src/test-files/solr/conf/schema-lmjelinekmercer.xml new file mode 100644 index 00000000000..093be32f71d --- /dev/null +++ b/solr/core/src/test-files/solr/conf/schema-lmjelinekmercer.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + 0.4 + + + + + + + + + + + + text + id + diff --git a/solr/core/src/test-files/solr/conf/schema-tfidf.xml b/solr/core/src/test-files/solr/conf/schema-tfidf.xml new file mode 100644 index 00000000000..8d2ed78c382 --- /dev/null +++ b/solr/core/src/test-files/solr/conf/schema-tfidf.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + text + id + diff --git a/solr/core/src/test-files/solr/conf/schema.xml b/solr/core/src/test-files/solr/conf/schema.xml index c6c256f39f5..a0bda9e0836 100644 --- a/solr/core/src/test-files/solr/conf/schema.xml +++ b/solr/core/src/test-files/solr/conf/schema.xml @@ -406,7 +406,7 @@ - + is there an echo? @@ -672,13 +672,13 @@ A custom similarity provider may be specified here, but the default is fine for most applications. --> - + is there an echo? - + I am your default sim diff --git a/solr/core/src/test-files/solr/conf/solrconfig-collate.xml b/solr/core/src/test-files/solr/conf/solrconfig-basic.xml similarity index 88% rename from solr/core/src/test-files/solr/conf/solrconfig-collate.xml rename to solr/core/src/test-files/solr/conf/solrconfig-basic.xml index 2c9b55c1a6a..c1384a5fee2 100644 --- a/solr/core/src/test-files/solr/conf/solrconfig-collate.xml +++ b/solr/core/src/test-files/solr/conf/solrconfig-basic.xml @@ -17,6 +17,8 @@ limitations under the License. --> + ${tests.luceneMatchVersion:LUCENE_CURRENT} diff --git a/solr/core/src/test/org/apache/solr/schema/IndexSchemaTest.java b/solr/core/src/test/org/apache/solr/schema/IndexSchemaTest.java index 5a0a9f04e4d..4d54a6fd2c5 100644 --- a/solr/core/src/test/org/apache/solr/schema/IndexSchemaTest.java +++ b/solr/core/src/test/org/apache/solr/schema/IndexSchemaTest.java @@ -27,6 +27,7 @@ import org.apache.solr.common.params.MapSolrParams; import org.apache.solr.core.SolrCore; import org.apache.solr.request.LocalSolrQueryRequest; import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.search.similarities.MockConfigurableSimilarityProvider; import org.apache.lucene.search.similarities.SimilarityProvider; import org.junit.BeforeClass; import org.junit.Test; @@ -79,14 +80,6 @@ public class IndexSchemaTest extends SolrTestCaseJ4 { ); clearIndex(); } - - @Test - public void testSimilarityProviderFactory() { - SolrCore core = h.getCore(); - SimilarityProvider similarityProvider = core.getSchema().getSimilarityProvider(); - assertTrue("wrong class", similarityProvider instanceof MockConfigurableSimilarityProvider); - assertEquals("is there an echo?", ((MockConfigurableSimilarityProvider)similarityProvider).getPassthrough()); - } @Test public void testRuntimeFieldCreation() diff --git a/solr/core/src/test/org/apache/solr/schema/TestCollationField.java b/solr/core/src/test/org/apache/solr/schema/TestCollationField.java index 48ad341792c..e92c14ee5ff 100644 --- a/solr/core/src/test/org/apache/solr/schema/TestCollationField.java +++ b/solr/core/src/test/org/apache/solr/schema/TestCollationField.java @@ -73,7 +73,7 @@ public class TestCollationField extends SolrTestCaseJ4 { confDir.mkdir(); // copy over configuration files - FileUtils.copyFile(getFile("solr/conf/solrconfig-collate.xml"), new File(confDir, "solrconfig.xml")); + FileUtils.copyFile(getFile("solr/conf/solrconfig-basic.xml"), new File(confDir, "solrconfig.xml")); FileUtils.copyFile(getFile("solr/conf/schema-collate.xml"), new File(confDir, "schema.xml")); // generate custom collation rules (DIN 5007-2), saving to customrules.dat diff --git a/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java b/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java new file mode 100644 index 00000000000..14ca46cf945 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java @@ -0,0 +1,46 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.Similarity; +import org.apache.lucene.search.similarities.SimilarityProvider; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.core.SolrCore; +import org.apache.solr.search.SolrIndexSearcher; +import org.apache.solr.util.RefCounted; + +public abstract class BaseSimilarityTestCase extends SolrTestCaseJ4 { + + /** returns the similarity in use for the field */ + protected Similarity getSimilarity(String field) { + SolrCore core = h.getCore(); + RefCounted searcher = core.getSearcher(); + Similarity sim = searcher.get().getSimilarityProvider().get(field); + searcher.decref(); + return sim; + } + + /** returns the (Solr)SimilarityProvider */ + protected SimilarityProvider getSimilarityProvider() { + SolrCore core = h.getCore(); + RefCounted searcher = core.getSearcher(); + SimilarityProvider prov = searcher.get().getSimilarityProvider(); + searcher.decref(); + return prov; + } +} diff --git a/solr/core/src/test/org/apache/solr/schema/CustomSimilarityFactory.java b/solr/core/src/test/org/apache/solr/search/similarities/CustomSimilarityFactory.java similarity index 91% rename from solr/core/src/test/org/apache/solr/schema/CustomSimilarityFactory.java rename to solr/core/src/test/org/apache/solr/search/similarities/CustomSimilarityFactory.java index 782bf6b1705..9f036baacbd 100644 --- a/solr/core/src/test/org/apache/solr/schema/CustomSimilarityFactory.java +++ b/solr/core/src/test/org/apache/solr/search/similarities/CustomSimilarityFactory.java @@ -14,9 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.solr.schema; +package org.apache.solr.search.similarities; import org.apache.lucene.search.similarities.Similarity; +import org.apache.solr.schema.SimilarityFactory; public class CustomSimilarityFactory extends SimilarityFactory { @Override diff --git a/solr/core/src/test/org/apache/solr/schema/CustomSimilarityProviderFactory.java b/solr/core/src/test/org/apache/solr/search/similarities/CustomSimilarityProviderFactory.java similarity index 89% rename from solr/core/src/test/org/apache/solr/schema/CustomSimilarityProviderFactory.java rename to solr/core/src/test/org/apache/solr/search/similarities/CustomSimilarityProviderFactory.java index 68de144e6b7..7ac5fd173d2 100644 --- a/solr/core/src/test/org/apache/solr/schema/CustomSimilarityProviderFactory.java +++ b/solr/core/src/test/org/apache/solr/search/similarities/CustomSimilarityProviderFactory.java @@ -14,9 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.solr.schema; +package org.apache.solr.search.similarities; import org.apache.solr.common.util.NamedList; +import org.apache.solr.schema.IndexSchema; +import org.apache.solr.schema.SimilarityProviderFactory; import org.apache.solr.search.SolrSimilarityProvider; public class CustomSimilarityProviderFactory extends SimilarityProviderFactory { diff --git a/solr/core/src/test/org/apache/solr/schema/MockConfigurableSimilarity.java b/solr/core/src/test/org/apache/solr/search/similarities/MockConfigurableSimilarity.java similarity index 96% rename from solr/core/src/test/org/apache/solr/schema/MockConfigurableSimilarity.java rename to solr/core/src/test/org/apache/solr/search/similarities/MockConfigurableSimilarity.java index 74394c4377d..194bad8dd06 100644 --- a/solr/core/src/test/org/apache/solr/schema/MockConfigurableSimilarity.java +++ b/solr/core/src/test/org/apache/solr/search/similarities/MockConfigurableSimilarity.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.solr.schema; +package org.apache.solr.search.similarities; import org.apache.lucene.search.similarities.DefaultSimilarity; diff --git a/solr/core/src/test/org/apache/solr/schema/MockConfigurableSimilarityProvider.java b/solr/core/src/test/org/apache/solr/search/similarities/MockConfigurableSimilarityProvider.java similarity index 93% rename from solr/core/src/test/org/apache/solr/schema/MockConfigurableSimilarityProvider.java rename to solr/core/src/test/org/apache/solr/search/similarities/MockConfigurableSimilarityProvider.java index 3a6e8a40ed0..ce8a02aaee2 100644 --- a/solr/core/src/test/org/apache/solr/schema/MockConfigurableSimilarityProvider.java +++ b/solr/core/src/test/org/apache/solr/search/similarities/MockConfigurableSimilarityProvider.java @@ -14,8 +14,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.solr.schema; +package org.apache.solr.search.similarities; +import org.apache.solr.schema.IndexSchema; import org.apache.solr.search.SolrSimilarityProvider; public class MockConfigurableSimilarityProvider extends SolrSimilarityProvider { diff --git a/solr/core/src/test/org/apache/solr/search/similarities/TestBM25SimilarityFactory.java b/solr/core/src/test/org/apache/solr/search/similarities/TestBM25SimilarityFactory.java new file mode 100644 index 00000000000..e1fbe2a2604 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/search/similarities/TestBM25SimilarityFactory.java @@ -0,0 +1,46 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.BM25Similarity; +import org.apache.lucene.search.similarities.Similarity; +import org.junit.BeforeClass; + +/** + * Tests {@link BM25SimilarityFactory} + */ +public class TestBM25SimilarityFactory extends BaseSimilarityTestCase { + @BeforeClass + public static void beforeClass() throws Exception { + initCore("solrconfig-basic.xml","schema-bm25.xml"); + } + + /** bm25 with default parameters */ + public void test() throws Exception { + assertEquals(BM25Similarity.class, getSimilarity("text").getClass()); + } + + /** bm25 with parameters */ + public void testParameters() throws Exception { + Similarity sim = getSimilarity("text_params"); + assertEquals(BM25Similarity.class, sim.getClass()); + BM25Similarity bm25 = (BM25Similarity) sim; + assertEquals(1.2f, bm25.getK1(), 0.01f); + assertEquals(0.76f, bm25.getB(), 0.01f); + } +} diff --git a/solr/core/src/test/org/apache/solr/search/similarities/TestDFRSimilarityFactory.java b/solr/core/src/test/org/apache/solr/search/similarities/TestDFRSimilarityFactory.java new file mode 100644 index 00000000000..1ba40b67c3f --- /dev/null +++ b/solr/core/src/test/org/apache/solr/search/similarities/TestDFRSimilarityFactory.java @@ -0,0 +1,58 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.AfterEffectB; +import org.apache.lucene.search.similarities.BasicModelIF; +import org.apache.lucene.search.similarities.DFRSimilarity; +import org.apache.lucene.search.similarities.NormalizationH2; +import org.apache.lucene.search.similarities.NormalizationH3; +import org.apache.lucene.search.similarities.Similarity; +import org.junit.BeforeClass; + +/** + * Tests {@link DFRSimilarityFactory} + */ +public class TestDFRSimilarityFactory extends BaseSimilarityTestCase { + @BeforeClass + public static void beforeClass() throws Exception { + initCore("solrconfig-basic.xml","schema-dfr.xml"); + } + + /** dfr with default parameters */ + public void test() throws Exception { + Similarity sim = getSimilarity("text"); + assertEquals(DFRSimilarity.class, sim.getClass()); + DFRSimilarity dfr = (DFRSimilarity) sim; + assertEquals(BasicModelIF.class, dfr.getBasicModel().getClass()); + assertEquals(AfterEffectB.class, dfr.getAfterEffect().getClass()); + assertEquals(NormalizationH2.class, dfr.getNormalization().getClass()); + } + + /** dfr with parameterized normalization */ + public void testParameters() throws Exception { + Similarity sim = getSimilarity("text_params"); + assertEquals(DFRSimilarity.class, sim.getClass()); + DFRSimilarity dfr = (DFRSimilarity) sim; + assertEquals(BasicModelIF.class, dfr.getBasicModel().getClass()); + assertEquals(AfterEffectB.class, dfr.getAfterEffect().getClass()); + assertEquals(NormalizationH3.class, dfr.getNormalization().getClass()); + NormalizationH3 norm = (NormalizationH3) dfr.getNormalization(); + assertEquals(900f, norm.getMu(), 0.01f); + } +} diff --git a/solr/core/src/test/org/apache/solr/search/similarities/TestDefaultSimilarityFactory.java b/solr/core/src/test/org/apache/solr/search/similarities/TestDefaultSimilarityFactory.java new file mode 100644 index 00000000000..f9399952b53 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/search/similarities/TestDefaultSimilarityFactory.java @@ -0,0 +1,36 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.DefaultSimilarity; +import org.junit.BeforeClass; + +/** + * Tests {@link DefaultSimilarityFactory} + */ +public class TestDefaultSimilarityFactory extends BaseSimilarityTestCase { + @BeforeClass + public static void beforeClass() throws Exception { + initCore("solrconfig-basic.xml","schema-tfidf.xml"); + } + + /** default parameters */ + public void test() throws Exception { + assertEquals(DefaultSimilarity.class, getSimilarity("text").getClass()); + } +} diff --git a/solr/core/src/test/org/apache/solr/search/similarities/TestIBSimilarityFactory.java b/solr/core/src/test/org/apache/solr/search/similarities/TestIBSimilarityFactory.java new file mode 100644 index 00000000000..c53f4be6565 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/search/similarities/TestIBSimilarityFactory.java @@ -0,0 +1,60 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.DistributionLL; +import org.apache.lucene.search.similarities.DistributionSPL; +import org.apache.lucene.search.similarities.IBSimilarity; +import org.apache.lucene.search.similarities.LambdaDF; +import org.apache.lucene.search.similarities.LambdaTTF; +import org.apache.lucene.search.similarities.NormalizationH2; +import org.apache.lucene.search.similarities.NormalizationH3; +import org.apache.lucene.search.similarities.Similarity; +import org.junit.BeforeClass; + +/** + * Tests {@link IBSimilarityFactory} + */ +public class TestIBSimilarityFactory extends BaseSimilarityTestCase { + @BeforeClass + public static void beforeClass() throws Exception { + initCore("solrconfig-basic.xml","schema-ib.xml"); + } + + /** spl/df/h2 with default parameters */ + public void test() throws Exception { + Similarity sim = getSimilarity("text"); + assertEquals(IBSimilarity.class, sim.getClass()); + IBSimilarity ib = (IBSimilarity) sim; + assertEquals(DistributionSPL.class, ib.getDistribution().getClass()); + assertEquals(LambdaDF.class, ib.getLambda().getClass()); + assertEquals(NormalizationH2.class, ib.getNormalization().getClass()); + } + + /** ll/ttf/h3 with parameterized normalization */ + public void testParameters() throws Exception { + Similarity sim = getSimilarity("text_params"); + assertEquals(IBSimilarity.class, sim.getClass()); + IBSimilarity ib = (IBSimilarity) sim; + assertEquals(DistributionLL.class, ib.getDistribution().getClass()); + assertEquals(LambdaTTF.class, ib.getLambda().getClass()); + assertEquals(NormalizationH3.class, ib.getNormalization().getClass()); + NormalizationH3 norm = (NormalizationH3) ib.getNormalization(); + assertEquals(900f, norm.getMu(), 0.01f); + } +} diff --git a/solr/core/src/test/org/apache/solr/search/similarities/TestLMDirichletSimilarityFactory.java b/solr/core/src/test/org/apache/solr/search/similarities/TestLMDirichletSimilarityFactory.java new file mode 100644 index 00000000000..8567e9d336a --- /dev/null +++ b/solr/core/src/test/org/apache/solr/search/similarities/TestLMDirichletSimilarityFactory.java @@ -0,0 +1,45 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.LMDirichletSimilarity; +import org.apache.lucene.search.similarities.Similarity; +import org.junit.BeforeClass; + +/** + * Tests {@link LMDirichletSimilarityFactory} + */ +public class TestLMDirichletSimilarityFactory extends BaseSimilarityTestCase { + @BeforeClass + public static void beforeClass() throws Exception { + initCore("solrconfig-basic.xml","schema-lmdirichlet.xml"); + } + + /** dirichlet with default parameters */ + public void test() throws Exception { + assertEquals(LMDirichletSimilarity.class, getSimilarity("text").getClass()); + } + + /** dirichlet with parameters */ + public void testParameters() throws Exception { + Similarity sim = getSimilarity("text_params"); + assertEquals(LMDirichletSimilarity.class, sim.getClass()); + LMDirichletSimilarity lm = (LMDirichletSimilarity) sim; + assertEquals(1000f, lm.getMu(), 0.01f); + } +} diff --git a/solr/core/src/test/org/apache/solr/search/similarities/TestLMJelinekMercerSimilarityFactory.java b/solr/core/src/test/org/apache/solr/search/similarities/TestLMJelinekMercerSimilarityFactory.java new file mode 100644 index 00000000000..6d25bbe04e5 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/search/similarities/TestLMJelinekMercerSimilarityFactory.java @@ -0,0 +1,45 @@ +package org.apache.solr.search.similarities; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.search.similarities.LMJelinekMercerSimilarity; +import org.apache.lucene.search.similarities.Similarity; +import org.junit.BeforeClass; + +/** + * Tests {@link LMJelinekMercerSimilarityFactory} + */ +public class TestLMJelinekMercerSimilarityFactory extends BaseSimilarityTestCase { + @BeforeClass + public static void beforeClass() throws Exception { + initCore("solrconfig-basic.xml","schema-lmjelinekmercer.xml"); + } + + /** jelinek-mercer with default parameters */ + public void test() throws Exception { + assertEquals(LMJelinekMercerSimilarity.class, getSimilarity("text").getClass()); + } + + /** jelinek-mercer with parameters */ + public void testParameters() throws Exception { + Similarity sim = getSimilarity("text_params"); + assertEquals(LMJelinekMercerSimilarity.class, sim.getClass()); + LMJelinekMercerSimilarity lm = (LMJelinekMercerSimilarity) sim; + assertEquals(0.4f, lm.getLambda(), 0.01f); + } +} diff --git a/solr/core/src/test/org/apache/solr/schema/TestPerFieldSimilarity.java b/solr/core/src/test/org/apache/solr/search/similarities/TestPerFieldSimilarity.java similarity index 60% rename from solr/core/src/test/org/apache/solr/schema/TestPerFieldSimilarity.java rename to solr/core/src/test/org/apache/solr/search/similarities/TestPerFieldSimilarity.java index 1ab50255a30..ea46a2d223b 100644 --- a/solr/core/src/test/org/apache/solr/schema/TestPerFieldSimilarity.java +++ b/solr/core/src/test/org/apache/solr/search/similarities/TestPerFieldSimilarity.java @@ -1,4 +1,4 @@ -package org.apache.solr.schema; +package org.apache.solr.search.similarities; /** * Licensed to the Apache Software Foundation (ASF) under one or more @@ -18,18 +18,16 @@ package org.apache.solr.schema; */ import org.apache.lucene.misc.SweetSpotSimilarity; -import org.apache.lucene.search.similarities.DefaultSimilarity; import org.apache.lucene.search.similarities.Similarity; -import org.apache.solr.SolrTestCaseJ4; +import org.apache.lucene.search.similarities.SimilarityProvider; import org.apache.solr.core.SolrCore; -import org.apache.solr.search.SolrIndexSearcher; -import org.apache.solr.util.RefCounted; import org.junit.BeforeClass; +import org.junit.Test; /** * Tests per-field similarity support in the schema */ -public class TestPerFieldSimilarity extends SolrTestCaseJ4 { +public class TestPerFieldSimilarity extends BaseSimilarityTestCase { @BeforeClass public static void beforeClass() throws Exception { @@ -38,69 +36,54 @@ public class TestPerFieldSimilarity extends SolrTestCaseJ4 { /** test a field where the sim is specified directly */ public void testDirect() throws Exception { - SolrCore core = h.getCore(); - RefCounted searcher = core.getSearcher(); - Similarity sim = searcher.get().getSimilarityProvider().get("sim1text"); - assertEquals(SweetSpotSimilarity.class, sim.getClass()); - searcher.decref(); + assertEquals(SweetSpotSimilarity.class, getSimilarity("sim1text").getClass()); } /** ... and for a dynamic field */ public void testDirectDynamic() throws Exception { - SolrCore core = h.getCore(); - RefCounted searcher = core.getSearcher(); - Similarity sim = searcher.get().getSimilarityProvider().get("text_sim1"); - assertEquals(SweetSpotSimilarity.class, sim.getClass()); - searcher.decref(); + assertEquals(SweetSpotSimilarity.class, getSimilarity("text_sim1").getClass()); } /** test a field where a configurable sim factory is defined */ public void testFactory() throws Exception { - SolrCore core = h.getCore(); - RefCounted searcher = core.getSearcher(); - Similarity sim = searcher.get().getSimilarityProvider().get("sim2text"); + Similarity sim = getSimilarity("sim2text"); assertEquals(MockConfigurableSimilarity.class, sim.getClass()); assertEquals("is there an echo?", ((MockConfigurableSimilarity)sim).getPassthrough()); - searcher.decref(); } /** ... and for a dynamic field */ public void testFactoryDynamic() throws Exception { - SolrCore core = h.getCore(); - RefCounted searcher = core.getSearcher(); - Similarity sim = searcher.get().getSimilarityProvider().get("text_sim2"); + Similarity sim = getSimilarity("text_sim2"); assertEquals(MockConfigurableSimilarity.class, sim.getClass()); assertEquals("is there an echo?", ((MockConfigurableSimilarity)sim).getPassthrough()); - searcher.decref(); } /** test a field where no similarity is specified */ public void testDefaults() throws Exception { - SolrCore core = h.getCore(); - RefCounted searcher = core.getSearcher(); - Similarity sim = searcher.get().getSimilarityProvider().get("sim3text"); + Similarity sim = getSimilarity("sim3text"); assertEquals(MockConfigurableSimilarity.class, sim.getClass()); assertEquals("I am your default sim", ((MockConfigurableSimilarity)sim).getPassthrough()); - searcher.decref(); } /** ... and for a dynamic field */ public void testDefaultsDynamic() throws Exception { - SolrCore core = h.getCore(); - RefCounted searcher = core.getSearcher(); - Similarity sim = searcher.get().getSimilarityProvider().get("text_sim3"); + Similarity sim = getSimilarity("text_sim3"); assertEquals(MockConfigurableSimilarity.class, sim.getClass()); assertEquals("I am your default sim", ((MockConfigurableSimilarity)sim).getPassthrough()); - searcher.decref(); } /** test a field that does not exist */ public void testNonexistent() throws Exception { - SolrCore core = h.getCore(); - RefCounted searcher = core.getSearcher(); - Similarity sim = searcher.get().getSimilarityProvider().get("sdfdsfdsfdswr5fsdfdsfdsfs"); + Similarity sim = getSimilarity("sdfdsfdsfdswr5fsdfdsfdsfs"); assertEquals(MockConfigurableSimilarity.class, sim.getClass()); assertEquals("I am your default sim", ((MockConfigurableSimilarity)sim).getPassthrough()); - searcher.decref(); + } + + @Test + public void testSimilarityProviderFactory() { + SolrCore core = h.getCore(); + SimilarityProvider similarityProvider = core.getSchema().getSimilarityProvider(); + assertTrue("wrong class", similarityProvider instanceof MockConfigurableSimilarityProvider); + assertEquals("is there an echo?", ((MockConfigurableSimilarityProvider)similarityProvider).getPassthrough()); } }