Example #1
0
        public virtual void  TestSetNorm_Renamed()
        {
            RAMDirectory store  = new RAMDirectory();
            IndexWriter  writer = new IndexWriter(store, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);

            // add the same document four times
            IFieldable f1 = new Field("field", "word", Field.Store.YES, Field.Index.ANALYZED);
            Document   d1 = new Document();

            d1.Add(f1);
            writer.AddDocument(d1);
            writer.AddDocument(d1);
            writer.AddDocument(d1);
            writer.AddDocument(d1);
            writer.Close();

            // reset the boost of each instance of this document
            IndexReader reader = IndexReader.Open(store, false);

            reader.SetNorm(0, "field", 1.0f);
            reader.SetNorm(1, "field", 2.0f);
            reader.SetNorm(2, "field", 4.0f);
            reader.SetNorm(3, "field", 16.0f);
            reader.Close();

            // check that searches are ordered by this boost
            float[] scores = new float[4];

            new IndexSearcher(store, true).Search(new TermQuery(new Term("field", "word")), new AnonymousClassCollector(scores, this));

            float lastScore = 0.0f;

            for (int i = 0; i < 4; i++)
            {
                Assert.IsTrue(scores[i] > lastScore);
                lastScore = scores[i];
            }
        }
Example #2
0
        public virtual void  TestQuery()
        {
            RAMDirectory dir = new RAMDirectory();
            IndexWriter  iw  = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);

            iw.SetMaxBufferedDocs(2);             // force multi-segment
            AddDoc("one", iw, 1f);
            AddDoc("two", iw, 20f);
            AddDoc("three four", iw, 300f);
            iw.Close();

            IndexReader   ir         = IndexReader.Open(dir);
            IndexSearcher is_Renamed = new IndexSearcher(ir);

            ScoreDoc[] hits;

            // assert with norms scoring turned off

            hits = is_Renamed.Search(new MatchAllDocsQuery(), null, 1000).scoreDocs;
            Assert.AreEqual(3, hits.Length);
            Assert.AreEqual(ir.Document(hits[0].doc).Get("key"), "one");
            Assert.AreEqual(ir.Document(hits[1].doc).Get("key"), "two");
            Assert.AreEqual(ir.Document(hits[2].doc).Get("key"), "three four");

            // assert with norms scoring turned on

            MatchAllDocsQuery normsQuery = new MatchAllDocsQuery("key");

            hits = is_Renamed.Search(normsQuery, null, 1000).scoreDocs;
            Assert.AreEqual(3, hits.Length);

            Assert.AreEqual(ir.Document(hits[0].doc).Get("key"), "three four");
            Assert.AreEqual(ir.Document(hits[1].doc).Get("key"), "two");
            Assert.AreEqual(ir.Document(hits[2].doc).Get("key"), "one");

            // change norm & retest
            ir.SetNorm(0, "key", 400f);
            normsQuery = new MatchAllDocsQuery("key");
            hits       = is_Renamed.Search(normsQuery, null, 1000).scoreDocs;
            Assert.AreEqual(3, hits.Length);

            Assert.AreEqual(ir.Document(hits[0].doc).Get("key"), "one");
            Assert.AreEqual(ir.Document(hits[1].doc).Get("key"), "three four");
            Assert.AreEqual(ir.Document(hits[2].doc).Get("key"), "two");

            // some artificial queries to trigger the use of skipTo():

            BooleanQuery bq = new BooleanQuery();

            bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
            bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
            hits = is_Renamed.Search(bq, null, 1000).scoreDocs;
            Assert.AreEqual(3, hits.Length);

            bq = new BooleanQuery();
            bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
            bq.Add(new TermQuery(new Term("key", "three")), BooleanClause.Occur.MUST);
            hits = is_Renamed.Search(bq, null, 1000).scoreDocs;
            Assert.AreEqual(1, hits.Length);

            // delete a document:
            is_Renamed.GetIndexReader().DeleteDocument(0);
            hits = is_Renamed.Search(new MatchAllDocsQuery(), null, 1000).scoreDocs;
            Assert.AreEqual(2, hits.Length);

            // test parsable toString()
            QueryParser qp = new QueryParser("key", analyzer);

            hits = is_Renamed.Search(qp.Parse(new MatchAllDocsQuery().ToString()), null, 1000).scoreDocs;
            Assert.AreEqual(2, hits.Length);

            // test parsable toString() with non default boost
            Query maq = new MatchAllDocsQuery();

            maq.SetBoost(2.3f);
            Query pq = qp.Parse(maq.ToString());

            hits = is_Renamed.Search(pq, null, 1000).scoreDocs;
            Assert.AreEqual(2, hits.Length);

            is_Renamed.Close();
            ir.Close();
            dir.Close();
        }