Exemple #1
0
        /// <summary> Tries to add new documents to hitDocs.
        /// Ensures that the hit numbered <code>min</code> has been retrieved.
        /// </summary>
        private void  GetMoreDocs(int min)
        {
            if (hitDocs.Count > min)
            {
                min = hitDocs.Count;
            }

            int     n       = min * 2;   // double # retrieved
            TopDocs topDocs = (sort == null) ? searcher.Search(weight, filter, n) : searcher.Search(weight, filter, n, sort);

            length = topDocs.totalHits;
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;

            float scoreNorm = 1.0f;

            if (length > 0 && topDocs.GetMaxScore() > 1.0f)
            {
                scoreNorm = 1.0f / topDocs.GetMaxScore();
            }

            int end = scoreDocs.Length < length?scoreDocs.Length:length;

            for (int i = hitDocs.Count; i < end; i++)
            {
                hitDocs.Add(new HitDoc(scoreDocs[i].score * scoreNorm, scoreDocs[i].doc));
            }
        }
        public virtual void  TestCustomSimilarity()
        {
            RAMDirectory dir = new RAMDirectory();

            InitIndex(dir, 10, true, "x");             // documents with two tokens "doc0" and "x", "doc1" and x, etc...
            IndexSearcher srchr  = new IndexSearcher(dir);
            MultiSearcher msrchr = GetMultiSearcherInstance(new Searcher[] { srchr });

            Similarity customSimilarity = new AnonymousClassDefaultSimilarity(this);

            srchr.SetSimilarity(customSimilarity);
            msrchr.SetSimilarity(customSimilarity);

            Query query = new TermQuery(new Term("contents", "doc0"));

            // Get a score from IndexSearcher
            TopDocs topDocs = srchr.Search(query, null, 1);
            float   score1  = topDocs.GetMaxScore();

            // Get the score from MultiSearcher
            topDocs = msrchr.Search(query, null, 1);
            float scoreN = topDocs.GetMaxScore();

            // The scores from the IndexSearcher and Multisearcher should be the same
            // if the same similarity is used.
            Assert.AreEqual(score1, scoreN, 1e-6, "MultiSearcher score must be equal to single esrcher score!");
        }
Exemple #3
0
        /// <summary> Tries to add new documents to hitDocs.
        /// Ensures that the hit numbered <code>min</code> has been retrieved.
        /// </summary>
        private void  GetMoreDocs(int min)
        {
            if (hitDocs.Count > min)
            {
                min = hitDocs.Count;
            }

            int     n       = min * 2;   // double # retrieved
            TopDocs topDocs = (sort == null)?searcher.Search(weight, filter, n):searcher.Search(weight, filter, n, sort);

            length = topDocs.totalHits;
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;

            float scoreNorm = 1.0f;

            if (length > 0 && topDocs.GetMaxScore() > 1.0f)
            {
                scoreNorm = 1.0f / topDocs.GetMaxScore();
            }

            int start = hitDocs.Count - nDeletedHits;

            // any new deletions?
            int nDels2 = CountDeletions(searcher);

            debugCheckedForDeletions = false;
            if (nDeletions < 0 || nDels2 > nDeletions)
            {
                // either we cannot count deletions, or some "previously valid hits" might have been deleted, so find exact start point
                nDeletedHits             = 0;
                debugCheckedForDeletions = true;
                int i2 = 0;
                for (int i1 = 0; i1 < hitDocs.Count && i2 < scoreDocs.Length; i1++)
                {
                    int id1 = ((HitDoc)hitDocs[i1]).id;
                    int id2 = scoreDocs[i2].doc;
                    if (id1 == id2)
                    {
                        i2++;
                    }
                    else
                    {
                        nDeletedHits++;
                    }
                }
                start = i2;
            }

            int end = scoreDocs.Length < length?scoreDocs.Length:length;

            length += nDeletedHits;
            for (int i = start; i < end; i++)
            {
                hitDocs.Add(new HitDoc(scoreDocs[i].score * scoreNorm, scoreDocs[i].doc));
            }

            nDeletions = nDels2;
        }
        public virtual void  TestMaxScore()
        {
            // ask for all results
            TopDocsCollector tdc = doSearch(15);
            TopDocs          td  = tdc.TopDocs();

            Assert.AreEqual(MAX_SCORE, td.GetMaxScore(), 0f);

            // ask for 5 last results
            tdc = doSearch(15);
            td  = tdc.TopDocs(10);
            Assert.AreEqual(MAX_SCORE, td.GetMaxScore(), 0f);
        }
Exemple #5
0
        private float CheckPhraseQuery(Document doc, PhraseQuery query, int slop, int expectedNumResults)
        {
            query.SetSlop(slop);

            RAMDirectory       ramDir   = new RAMDirectory();
            WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer();
            IndexWriter        writer   = new IndexWriter(ramDir, analyzer, MaxFieldLength.UNLIMITED);

            writer.AddDocument(doc);
            writer.Close();

            IndexSearcher searcher = new IndexSearcher(ramDir);
            TopDocs       td       = searcher.Search(query, null, 10);

            //System.out.println("slop: "+slop+"  query: "+query+"  doc: "+doc+"  Expecting number of hits: "+expectedNumResults+" maxScore="+td.getMaxScore());
            Assert.AreEqual(expectedNumResults, td.TotalHits, "slop: " + slop + "  query: " + query + "  doc: " + doc + "  Wrong number of hits");

            //QueryUtils.check(query,searcher);

            searcher.Close();
            ramDir.Close();

            return(td.GetMaxScore());
        }
 public virtual float GetMaxScore()
 {
     return(docs.GetMaxScore());
 }