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);
}
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 });
}
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);
}
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);
}
};
}
Aggregations