Search in sources :

Example 6 with TFIDFSimilarity

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

the class TestFieldMaskingSpanQuery method testSpans2.

public void testSpans2() throws Exception {
    assumeTrue("Broken scoring: LUCENE-3723", searcher.getSimilarity(true) instanceof TFIDFSimilarity);
    SpanQuery qA1 = new SpanTermQuery(new Term("gender", "female"));
    SpanQuery qA2 = new SpanTermQuery(new Term("first", "james"));
    SpanQuery qA = new SpanOrQuery(qA1, new FieldMaskingSpanQuery(qA2, "gender"));
    SpanQuery qB = new SpanTermQuery(new Term("last", "jones"));
    SpanQuery q = new SpanNearQuery(new SpanQuery[] { new FieldMaskingSpanQuery(qA, "id"), new FieldMaskingSpanQuery(qB, "id") }, -1, false);
    check(q, new int[] { 0, 1, 2, 3 });
    Spans span = q.createWeight(searcher, false, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
    assertNext(span, 0, 0, 1);
    assertNext(span, 1, 1, 2);
    assertNext(span, 2, 0, 1);
    assertNext(span, 2, 2, 3);
    assertNext(span, 3, 0, 1);
    assertFinished(span);
}
Also used : Term(org.apache.lucene.index.Term) TFIDFSimilarity(org.apache.lucene.search.similarities.TFIDFSimilarity)

Example 7 with TFIDFSimilarity

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

the class TestFieldMaskingSpanQuery method testSimple2.

public void testSimple2() throws Exception {
    assumeTrue("Broken scoring: LUCENE-3723", searcher.getSimilarity(true) instanceof TFIDFSimilarity);
    SpanQuery q1 = new SpanTermQuery(new Term("gender", "female"));
    SpanQuery q2 = new SpanTermQuery(new Term("last", "smith"));
    SpanQuery q = new SpanNearQuery(new SpanQuery[] { q1, new FieldMaskingSpanQuery(q2, "gender") }, -1, false);
    check(q, new int[] { 2, 4 });
    q = new SpanNearQuery(new SpanQuery[] { new FieldMaskingSpanQuery(q1, "id"), new FieldMaskingSpanQuery(q2, "id") }, -1, false);
    check(q, new int[] { 2, 4 });
}
Also used : Term(org.apache.lucene.index.Term) TFIDFSimilarity(org.apache.lucene.search.similarities.TFIDFSimilarity)

Example 8 with TFIDFSimilarity

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

the class SweetSpotSimilarityTest method testHyperbolicSweetSpot.

public void testHyperbolicSweetSpot() {
    SweetSpotSimilarity ss = new SweetSpotSimilarity() {

        @Override
        public float tf(float freq) {
            return hyperbolicTf(freq);
        }
    };
    ss.setHyperbolicTfFactors(3.3f, 7.7f, Math.E, 5.0f);
    TFIDFSimilarity s = ss;
    for (int i = 1; i <= 1000; i++) {
        assertTrue("MIN tf: i=" + i + " : s=" + s.tf(i), 3.3f <= s.tf(i));
        assertTrue("MAX tf: i=" + i + " : s=" + s.tf(i), s.tf(i) <= 7.7f);
    }
    assertEquals("MID tf", 3.3f + (7.7f - 3.3f) / 2.0f, s.tf(5), 0.00001f);
    // stupidity
    assertEquals("tf zero", 0.0f, s.tf(0), 0.0f);
}
Also used : TFIDFSimilarity(org.apache.lucene.search.similarities.TFIDFSimilarity)

Example 9 with TFIDFSimilarity

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

the class NormValueSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    IndexSearcher searcher = (IndexSearcher) context.get("searcher");
    final TFIDFSimilarity similarity = IDFValueSource.asTFIDF(searcher.getSimilarity(true), field);
    if (similarity == null) {
        throw new UnsupportedOperationException("requires a TFIDFSimilarity (such as ClassicSimilarity)");
    }
    // Only works if the contribution of the tf is 1 when the freq is 1 and contribution of the idf
    // is 1 when docCount == docFreq == 1
    final SimWeight simWeight = similarity.computeWeight(1f, new CollectionStatistics(field, 1, 1, 1, 1), new TermStatistics(new BytesRef("bogus"), 1, 1));
    final SimScorer simScorer = similarity.simScorer(simWeight, readerContext);
    return new FloatDocValues(this) {

        int lastDocID = -1;

        @Override
        public float floatVal(int docID) throws IOException {
            if (docID < lastDocID) {
                throw new AssertionError("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
            }
            lastDocID = docID;
            return simScorer.score(docID, 1f);
        }
    };
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SimWeight(org.apache.lucene.search.similarities.Similarity.SimWeight) FloatDocValues(org.apache.lucene.queries.function.docvalues.FloatDocValues) SimScorer(org.apache.lucene.search.similarities.Similarity.SimScorer) TFIDFSimilarity(org.apache.lucene.search.similarities.TFIDFSimilarity) TermStatistics(org.apache.lucene.search.TermStatistics) BytesRef(org.apache.lucene.util.BytesRef) CollectionStatistics(org.apache.lucene.search.CollectionStatistics)

Aggregations

TFIDFSimilarity (org.apache.lucene.search.similarities.TFIDFSimilarity)9 Term (org.apache.lucene.index.Term)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 IOException (java.io.IOException)2 TermsEnum (org.apache.lucene.index.TermsEnum)2 FloatDocValues (org.apache.lucene.queries.function.docvalues.FloatDocValues)2 ClassicSimilarity (org.apache.lucene.search.similarities.ClassicSimilarity)2 BytesRef (org.apache.lucene.util.BytesRef)2 HashMap (java.util.HashMap)1 Fields (org.apache.lucene.index.Fields)1 IndexReader (org.apache.lucene.index.IndexReader)1 PostingsEnum (org.apache.lucene.index.PostingsEnum)1 Terms (org.apache.lucene.index.Terms)1 CollectionStatistics (org.apache.lucene.search.CollectionStatistics)1 TermStatistics (org.apache.lucene.search.TermStatistics)1 Similarity (org.apache.lucene.search.similarities.Similarity)1 SimScorer (org.apache.lucene.search.similarities.Similarity.SimScorer)1 SimWeight (org.apache.lucene.search.similarities.Similarity.SimWeight)1