Search in sources :

Example 31 with Similarity

use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.

the class ChangedSchemaMergeTest method testSanityOfSchemaSimilarityFactoryInform.

public void testSanityOfSchemaSimilarityFactoryInform() {
    // sanity check that SchemaSimilarityFactory will throw an Exception if you
    // try to use it w/o inform(SolrCoreAware) otherwise assertSimilarity is useless
    SchemaSimilarityFactory broken = new SchemaSimilarityFactory();
    broken.init(new ModifiableSolrParams());
    // NO INFORM
    try {
        Similarity bogus = broken.getSimilarity();
        fail("SchemaSimilarityFactory should have thrown IllegalStateException b/c inform not used");
    } catch (IllegalStateException expected) {
        assertTrue("GOT: " + expected.getMessage(), expected.getMessage().contains("SolrCoreAware.inform"));
    }
}
Also used : Similarity(org.apache.lucene.search.similarities.Similarity) SchemaSimilarityFactory(org.apache.solr.search.similarities.SchemaSimilarityFactory) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 32 with Similarity

use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.

the class TestPerFieldSimilarity method testFactory.

/** test a field where a configurable sim factory is defined */
public void testFactory() throws Exception {
    Similarity sim = getSimilarity("sim2text");
    assertEquals(MockConfigurableSimilarity.class, sim.getClass());
    assertEquals("is there an echo?", ((MockConfigurableSimilarity) sim).getPassthrough());
}
Also used : Similarity(org.apache.lucene.search.similarities.Similarity) SweetSpotSimilarity(org.apache.lucene.misc.SweetSpotSimilarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity)

Example 33 with Similarity

use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.

the class TestPerFieldSimilarity method testDefaultsDynamic.

/** ... and for a dynamic field */
public void testDefaultsDynamic() throws Exception {
    Similarity sim = getSimilarity("text_sim3");
    assertEquals(BM25Similarity.class, sim.getClass());
}
Also used : Similarity(org.apache.lucene.search.similarities.Similarity) SweetSpotSimilarity(org.apache.lucene.misc.SweetSpotSimilarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity)

Example 34 with Similarity

use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.

the class TestSweetSpotSimilarityFactory method computeNorm.

private static float computeNorm(Similarity sim, int length) throws IOException {
    String value = IntStream.range(0, length).mapToObj(i -> "a").collect(Collectors.joining(" "));
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim));
    w.addDocument(Collections.singleton(newTextField("foo", value, Store.NO)));
    DirectoryReader reader = DirectoryReader.open(w);
    w.close();
    IndexSearcher searcher = new IndexSearcher(reader);
    searcher.setSimilarity(sim);
    Explanation expl = searcher.explain(new TermQuery(new Term("foo", "a")), 0);
    reader.close();
    dir.close();
    Explanation norm = findExplanation(expl, "fieldNorm");
    assertNotNull(norm);
    return norm.getValue();
}
Also used : IntStream(java.util.stream.IntStream) Explanation(org.apache.lucene.search.Explanation) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) BeforeClass(org.junit.BeforeClass) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) SweetSpotSimilarity(org.apache.lucene.misc.SweetSpotSimilarity) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) IndexWriter(org.apache.lucene.index.IndexWriter) TermQuery(org.apache.lucene.search.TermQuery) Similarity(org.apache.lucene.search.similarities.Similarity) Directory(org.apache.lucene.store.Directory) Store(org.apache.lucene.document.Field.Store) Collections(java.util.Collections) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) Explanation(org.apache.lucene.search.Explanation) Term(org.apache.lucene.index.Term) Directory(org.apache.lucene.store.Directory)

Example 35 with Similarity

use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.

the class TestDFRSimilarityFactory method test.

/** 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());
}
Also used : DFRSimilarity(org.apache.lucene.search.similarities.DFRSimilarity) Similarity(org.apache.lucene.search.similarities.Similarity) DFRSimilarity(org.apache.lucene.search.similarities.DFRSimilarity)

Aggregations

Similarity (org.apache.lucene.search.similarities.Similarity)48 BM25Similarity (org.apache.lucene.search.similarities.BM25Similarity)15 ClassicSimilarity (org.apache.lucene.search.similarities.ClassicSimilarity)15 Directory (org.apache.lucene.store.Directory)9 PerFieldSimilarityWrapper (org.apache.lucene.search.similarities.PerFieldSimilarityWrapper)8 SweetSpotSimilarity (org.apache.lucene.misc.SweetSpotSimilarity)7 IOException (java.io.IOException)6 Document (org.apache.lucene.document.Document)5 Term (org.apache.lucene.index.Term)5 IndexSearcher (org.apache.lucene.search.IndexSearcher)5 Collectors (java.util.stream.Collectors)4 IntStream (java.util.stream.IntStream)4 Field (org.apache.lucene.document.Field)4 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)4 NormValueSource (org.apache.lucene.queries.function.valuesource.NormValueSource)4 BytesRef (org.apache.lucene.util.BytesRef)4 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 Store (org.apache.lucene.document.Field.Store)3 IndexWriter (org.apache.lucene.index.IndexWriter)3 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)3