Search in sources :

Example 16 with Similarity

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

the class TestMemoryIndex method testSimilarities.

@Test
public void testSimilarities() throws IOException {
    MemoryIndex mi = new MemoryIndex();
    mi.addField("f1", "a long text field that contains many many terms", analyzer);
    IndexSearcher searcher = mi.createSearcher();
    LeafReader reader = (LeafReader) searcher.getIndexReader();
    NumericDocValues norms = reader.getNormValues("f1");
    assertEquals(0, norms.nextDoc());
    float n1 = norms.longValue();
    // Norms are re-computed when we change the Similarity
    mi.setSimilarity(new Similarity() {

        @Override
        public long computeNorm(FieldInvertState state) {
            return 74;
        }

        @Override
        public SimWeight computeWeight(float boost, CollectionStatistics collectionStats, TermStatistics... termStats) {
            throw new UnsupportedOperationException();
        }

        @Override
        public SimScorer simScorer(SimWeight weight, LeafReaderContext context) throws IOException {
            throw new UnsupportedOperationException();
        }
    });
    norms = reader.getNormValues("f1");
    assertEquals(0, norms.nextDoc());
    float n2 = norms.longValue();
    assertTrue(n1 != n2);
    TestUtil.checkReader(reader);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) NumericDocValues(org.apache.lucene.index.NumericDocValues) LeafReader(org.apache.lucene.index.LeafReader) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) Similarity(org.apache.lucene.search.similarities.Similarity) IOException(java.io.IOException) TermStatistics(org.apache.lucene.search.TermStatistics) CollectionStatistics(org.apache.lucene.search.CollectionStatistics) FieldInvertState(org.apache.lucene.index.FieldInvertState) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Test(org.junit.Test)

Example 17 with Similarity

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

the class IndexSchema method readSimilarity.

static SimilarityFactory readSimilarity(SolrResourceLoader loader, Node node) {
    if (node == null) {
        return null;
    } else {
        SimilarityFactory similarityFactory;
        final String classArg = ((Element) node).getAttribute(SimilarityFactory.CLASS_NAME);
        final Object obj = loader.newInstance(classArg, Object.class, "search.similarities.");
        if (obj instanceof SimilarityFactory) {
            // configure a factory, get a similarity back
            final NamedList<Object> namedList = DOMUtil.childNodesToNamedList(node);
            namedList.add(SimilarityFactory.CLASS_NAME, classArg);
            SolrParams params = SolrParams.toSolrParams(namedList);
            similarityFactory = (SimilarityFactory) obj;
            similarityFactory.init(params);
        } else {
            // just like always, assume it's a Similarity and get a ClassCastException - reasonable error handling
            similarityFactory = new SimilarityFactory() {

                @Override
                public Similarity getSimilarity() {
                    return (Similarity) obj;
                }
            };
        }
        return similarityFactory;
    }
}
Also used : Similarity(org.apache.lucene.search.similarities.Similarity) Element(org.w3c.dom.Element) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SchemaSimilarityFactory(org.apache.solr.search.similarities.SchemaSimilarityFactory) ClassicSimilarityFactory(org.apache.solr.search.similarities.ClassicSimilarityFactory)

Example 18 with Similarity

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

the class TestSpans method testSpanScorerZeroSloppyFreq.

public void testSpanScorerZeroSloppyFreq() throws Exception {
    IndexReaderContext topReaderContext = searcher.getTopReaderContext();
    List<LeafReaderContext> leaves = topReaderContext.leaves();
    int subIndex = ReaderUtil.subIndex(11, leaves);
    for (int i = 0, c = leaves.size(); i < c; i++) {
        final LeafReaderContext ctx = leaves.get(i);
        final Similarity sim = new ClassicSimilarity() {

            @Override
            public float sloppyFreq(int distance) {
                return 0.0f;
            }
        };
        final Similarity oldSim = searcher.getSimilarity(true);
        Scorer spanScorer;
        try {
            searcher.setSimilarity(sim);
            SpanQuery snq = spanNearOrderedQuery(field, 1, "t1", "t2");
            spanScorer = searcher.createNormalizedWeight(snq, true).scorer(ctx);
        } finally {
            searcher.setSimilarity(oldSim);
        }
        if (i == subIndex) {
            assertTrue("first doc", spanScorer.iterator().nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
            assertEquals("first doc number", spanScorer.docID() + ctx.docBase, 11);
            float score = spanScorer.score();
            assertTrue("first doc score should be zero, " + score, score == 0.0f);
        } else {
            assertTrue("no second doc", spanScorer == null || spanScorer.iterator().nextDoc() == DocIdSetIterator.NO_MORE_DOCS);
        }
    }
}
Also used : ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) Similarity(org.apache.lucene.search.similarities.Similarity) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Scorer(org.apache.lucene.search.Scorer) IndexReaderContext(org.apache.lucene.index.IndexReaderContext)

Example 19 with Similarity

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

the class TestBlockJoin method testScoreMode.

public void testScoreMode() throws IOException {
    Similarity sim = new SimilarityBase() {

        @Override
        public String toString() {
            return "TestSim";
        }

        @Override
        protected float score(BasicStats stats, float freq, float docLen) {
            return freq;
        }
    };
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig().setSimilarity(sim));
    w.addDocuments(Arrays.asList(Collections.singleton(newTextField("foo", "bar bar", Store.NO)), Collections.singleton(newTextField("foo", "bar", Store.NO)), Collections.emptyList(), Collections.singleton(newStringField("type", new BytesRef("parent"), Store.NO))));
    DirectoryReader reader = w.getReader();
    w.close();
    IndexSearcher searcher = newSearcher(reader);
    searcher.setSimilarity(sim);
    BitSetProducer parents = new QueryBitSetProducer(new TermQuery(new Term("type", "parent")));
    for (ScoreMode scoreMode : ScoreMode.values()) {
        Query query = new ToParentBlockJoinQuery(new TermQuery(new Term("foo", "bar")), parents, scoreMode);
        TopDocs topDocs = searcher.search(query, 10);
        assertEquals(1, topDocs.totalHits);
        assertEquals(3, topDocs.scoreDocs[0].doc);
        float expectedScore;
        switch(scoreMode) {
            case Avg:
                expectedScore = 1.5f;
                break;
            case Max:
                expectedScore = 2f;
                break;
            case Min:
                expectedScore = 1f;
                break;
            case None:
                expectedScore = 0f;
                break;
            case Total:
                expectedScore = 3f;
                break;
            default:
                throw new AssertionError();
        }
        assertEquals(expectedScore, topDocs.scoreDocs[0].score, 0f);
    }
    reader.close();
    dir.close();
}
Also used : Similarity(org.apache.lucene.search.similarities.Similarity) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) SimilarityBase(org.apache.lucene.search.similarities.SimilarityBase) BasicStats(org.apache.lucene.search.similarities.BasicStats) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 20 with Similarity

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

the class SchemaSimilarityFactory method getSimilarity.

@Override
public Similarity getSimilarity() {
    if (null == core) {
        throw new IllegalStateException("SchemaSimilarityFactory can not be used until SolrCoreAware.inform has been called");
    }
    if (null == similarity) {
        // Need to instantiate lazily, can't do this in inform(SolrCore) because of chicken/egg
        // circular initialization hell with core.getLatestSchema() to lookup defaultSimFromFieldType
        Similarity defaultSim = null;
        if (null == defaultSimFromFieldType) {
            // nothing configured, choose a sensible implicit default...
            defaultSim = this.core.getSolrConfig().luceneMatchVersion.onOrAfter(Version.LUCENE_6_0_0) ? new BM25Similarity() : new ClassicSimilarity();
        } else {
            FieldType defSimFT = core.getLatestSchema().getFieldTypeByName(defaultSimFromFieldType);
            if (null == defSimFT) {
                throw new SolrException(ErrorCode.SERVER_ERROR, "SchemaSimilarityFactory configured with " + INIT_OPT + "='" + defaultSimFromFieldType + "' but that <fieldType> does not exist");
            }
            defaultSim = defSimFT.getSimilarity();
            if (null == defaultSim) {
                throw new SolrException(ErrorCode.SERVER_ERROR, "SchemaSimilarityFactory configured with " + INIT_OPT + "='" + defaultSimFromFieldType + "' but that <fieldType> does not define a <similarity>");
            }
        }
        similarity = new SchemaSimilarity(defaultSim);
    }
    return similarity;
}
Also used : ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) Similarity(org.apache.lucene.search.similarities.Similarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) SolrException(org.apache.solr.common.SolrException) FieldType(org.apache.solr.schema.FieldType)

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