Search in sources :

Example 6 with ClassicSimilarity

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

the class TestBooleanQueryVisitSubscorers method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    analyzer = new MockAnalyzer(random());
    dir = newDirectory();
    IndexWriterConfig config = newIndexWriterConfig(analyzer);
    // we will use docids to validate
    config.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir, config);
    writer.addDocument(doc("lucene", "lucene is a very popular search engine library"));
    writer.addDocument(doc("solr", "solr is a very popular search server and is using lucene"));
    writer.addDocument(doc("nutch", "nutch is an internet search engine with web crawler and is using lucene and hadoop"));
    reader = writer.getReader();
    writer.close();
    // we do not use newSearcher because the assertingXXX layers break
    // the toString representations we are relying on
    // TODO: clean that up
    searcher = new IndexSearcher(reader);
    searcher.setSimilarity(new ClassicSimilarity());
    scorerSearcher = new ScorerIndexSearcher(reader);
    scorerSearcher.setSimilarity(new ClassicSimilarity());
}
Also used : ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 7 with ClassicSimilarity

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

the class TestBooleanQuery method testNullOrSubScorer.

// LUCENE-1630
public void testNullOrSubScorer() throws Throwable {
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(newTextField("field", "a b c d", Field.Store.NO));
    w.addDocument(doc);
    IndexReader r = w.getReader();
    IndexSearcher s = newSearcher(r);
    // this test relies upon coord being the default implementation,
    // otherwise scores are different!
    s.setSimilarity(new ClassicSimilarity());
    BooleanQuery.Builder q = new BooleanQuery.Builder();
    q.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
    // PhraseQuery w/ no terms added returns a null scorer
    PhraseQuery pq = new PhraseQuery("field", new String[0]);
    q.add(pq, BooleanClause.Occur.SHOULD);
    assertEquals(1, s.search(q.build(), 10).totalHits);
    // A required clause which returns null scorer should return null scorer to
    // IndexSearcher.
    q = new BooleanQuery.Builder();
    pq = new PhraseQuery("field", new String[0]);
    q.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
    q.add(pq, BooleanClause.Occur.MUST);
    assertEquals(0, s.search(q.build(), 10).totalHits);
    DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(Arrays.asList(new TermQuery(new Term("field", "a")), pq), 1.0f);
    assertEquals(1, s.search(dmq, 10).totalHits);
    r.close();
    w.close();
    dir.close();
}
Also used : ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 8 with ClassicSimilarity

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

the class TestBoolean2 method testRandomQueries.

@Test
public void testRandomQueries() throws Exception {
    String[] vals = { "w1", "w2", "w3", "w4", "w5", "xx", "yy", "zzz" };
    int tot = 0;
    BooleanQuery q1 = null;
    try {
        // increase number of iterations for more complete testing
        int num = atLeast(20);
        for (int i = 0; i < num; i++) {
            int level = random().nextInt(3);
            q1 = randBoolQuery(new Random(random().nextLong()), random().nextBoolean(), level, field, vals, null).build();
            // Can't sort by relevance since floating point numbers may not quite
            // match up.
            Sort sort = Sort.INDEXORDER;
            // baseline sim
            QueryUtils.check(random(), q1, searcher);
            try {
                // a little hackish, QueryUtils.check is too costly to do on bigSearcher in this loop.
                // random sim
                searcher.setSimilarity(bigSearcher.getSimilarity(true));
                QueryUtils.check(random(), q1, searcher);
            } finally {
                // restore
                searcher.setSimilarity(new ClassicSimilarity());
            }
            // check diff (randomized) scorers (from AssertingSearcher) produce the same results
            TopFieldCollector collector = TopFieldCollector.create(sort, 1000, false, true, true);
            searcher.search(q1, collector);
            ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
            collector = TopFieldCollector.create(sort, 1000, false, true, true);
            searcher.search(q1, collector);
            ScoreDoc[] hits2 = collector.topDocs().scoreDocs;
            tot += hits2.length;
            CheckHits.checkEqual(q1, hits1, hits2);
            BooleanQuery.Builder q3 = new BooleanQuery.Builder();
            q3.add(q1, BooleanClause.Occur.SHOULD);
            q3.add(new PrefixQuery(new Term("field2", "b")), BooleanClause.Occur.SHOULD);
            TopDocs hits4 = bigSearcher.search(q3.build(), 1);
            assertEquals(mulFactor * collector.totalHits + NUM_EXTRA_DOCS / 2, hits4.totalHits);
            // test diff (randomized) scorers produce the same results on bigSearcher as well
            collector = TopFieldCollector.create(sort, 1000 * mulFactor, false, true, true);
            bigSearcher.search(q1, collector);
            hits1 = collector.topDocs().scoreDocs;
            collector = TopFieldCollector.create(sort, 1000 * mulFactor, false, true, true);
            bigSearcher.search(q1, collector);
            hits2 = collector.topDocs().scoreDocs;
            CheckHits.checkEqual(q1, hits1, hits2);
        }
    } catch (Exception e) {
        // For easier debugging
        System.out.println("failed query: " + q1);
        throw e;
    }
// System.out.println("Total hits:"+tot);
}
Also used : ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) Random(java.util.Random) Test(org.junit.Test)

Example 9 with ClassicSimilarity

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

the class TestSortRescorer method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, newIndexWriterConfig().setSimilarity(new ClassicSimilarity()));
    Document doc = new Document();
    doc.add(newStringField("id", "1", Field.Store.YES));
    doc.add(newTextField("body", "some contents and more contents", Field.Store.NO));
    doc.add(new NumericDocValuesField("popularity", 5));
    iw.addDocument(doc);
    doc = new Document();
    doc.add(newStringField("id", "2", Field.Store.YES));
    doc.add(newTextField("body", "another document with different contents", Field.Store.NO));
    doc.add(new NumericDocValuesField("popularity", 20));
    iw.addDocument(doc);
    doc = new Document();
    doc.add(newStringField("id", "3", Field.Store.YES));
    doc.add(newTextField("body", "crappy contents", Field.Store.NO));
    doc.add(new NumericDocValuesField("popularity", 2));
    iw.addDocument(doc);
    reader = iw.getReader();
    searcher = new IndexSearcher(reader);
    // TODO: fix this test to not be so flaky and use newSearcher
    searcher.setSimilarity(new ClassicSimilarity());
    iw.close();
}
Also used : ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 10 with ClassicSimilarity

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

the class TestNonDefinedSimilarityFactory method testClassic.

public void testClassic() throws Exception {
    // any value below 6.0 should have this behavior
    System.setProperty("tests.luceneMatchVersion", "5.3");
    initCore("solrconfig-basic.xml", "schema-tiny.xml");
    ClassicSimilarity sim = getSimilarity("text", ClassicSimilarity.class);
    assertEquals(true, sim.getDiscountOverlaps());
    System.clearProperty("tests.luceneMatchVersion");
}
Also used : ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity)

Aggregations

ClassicSimilarity (org.apache.lucene.search.similarities.ClassicSimilarity)43 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)14 Document (org.apache.lucene.document.Document)13 Term (org.apache.lucene.index.Term)12 Directory (org.apache.lucene.store.Directory)10 IndexReader (org.apache.lucene.index.IndexReader)9 Similarity (org.apache.lucene.search.similarities.Similarity)9 TermQuery (org.apache.lucene.search.TermQuery)7 BM25Similarity (org.apache.lucene.search.similarities.BM25Similarity)7 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)6 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)5 DocFreqValueSource (org.apache.lucene.queries.function.valuesource.DocFreqValueSource)4 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)4 IDFValueSource (org.apache.lucene.queries.function.valuesource.IDFValueSource)4 JoinDocFreqValueSource (org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource)4 LiteralValueSource (org.apache.lucene.queries.function.valuesource.LiteralValueSource)4 MaxDocValueSource (org.apache.lucene.queries.function.valuesource.MaxDocValueSource)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 Query (org.apache.lucene.search.Query)4 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)3