Ejemplo n.º 1
0
        /// <summary> Just like {@link #Search(Weight, Filter, int, Sort)}, but you choose
        /// whether or not the fields in the returned {@link FieldDoc} instances
        /// should be set by specifying fillFields.<br/>
        ///
        /// <p/>
        /// NOTE: this does not compute scores by default. If you need scores, create
        /// a {@link TopFieldCollector} instance by calling
        /// {@link TopFieldCollector#create} and then pass that to
        /// {@link #Search(Weight, Filter, Collector)}.
        /// <p/>
        /// </summary>
        public virtual TopFieldDocs Search(Weight weight, Filter filter, int nDocs, Sort sort, bool fillFields)
        {
            nDocs = Math.Min(nDocs, reader.MaxDoc());

            SortField[] fields = sort.fields;
            bool        legacy = false;

            for (int i = 0; i < fields.Length; i++)
            {
                SortField     field     = fields[i];
                System.String fieldname = field.GetField();
                int           type      = field.GetType();
                // Resolve AUTO into its true type
                if (type == SortField.AUTO)
                {
                    int autotype = SortField.DetectFieldType(reader, fieldname);
                    if (autotype == SortField.STRING)
                    {
                        fields[i] = new SortField(fieldname, field.GetLocale(), field.GetReverse());
                    }
                    else
                    {
                        fields[i] = new SortField(fieldname, autotype, field.GetReverse());
                    }
                }

                if (field.GetUseLegacySearch())
                {
                    legacy = true;
                }
            }

            if (legacy)
            {
                // Search the single top-level reader
                TopDocCollector     collector = new TopFieldDocCollector(reader, sort, nDocs);
                HitCollectorWrapper hcw       = new HitCollectorWrapper(collector);
                hcw.SetNextReader(reader, 0);
                if (filter == null)
                {
                    Scorer scorer = weight.Scorer(reader, true, true);
                    if (scorer != null)
                    {
                        scorer.Score(hcw);
                    }
                }
                else
                {
                    SearchWithFilter(reader, weight, filter, hcw);
                }
                return((TopFieldDocs)collector.TopDocs());
            }

            TopFieldCollector collector2 = TopFieldCollector.create(sort, nDocs, fillFields, fieldSortDoTrackScores, fieldSortDoMaxScore, !weight.ScoresDocsOutOfOrder());

            Search(weight, filter, collector2);
            return((TopFieldDocs)collector2.TopDocs());
        }
        private void  RunTest(IndexSearcher searcher, bool reversed)
        {
            BooleanQuery newq  = new BooleanQuery(false);
            TermQuery    query = new TermQuery(new Term("title", "ipod"));

            newq.Add(query, BooleanClause.Occur.SHOULD);
            newq.Add(GetElevatedQuery(new System.String[] { "id", "a", "id", "x" }), BooleanClause.Occur.SHOULD);

            Sort sort = new Sort(new SortField[] { new SortField("id", new ElevationComparatorSource(priority), false), new SortField(null, SortField.SCORE, reversed) });

            TopDocsCollector topCollector = TopFieldCollector.create(sort, 50, false, true, true, true);

            searcher.Search(newq, null, topCollector);

            TopDocs topDocs       = topCollector.TopDocs(0, 10);
            int     nDocsReturned = topDocs.ScoreDocs.Length;

            Assert.AreEqual(4, nDocsReturned);

            // 0 & 3 were elevated
            Assert.AreEqual(0, topDocs.ScoreDocs[0].doc);
            Assert.AreEqual(3, topDocs.ScoreDocs[1].doc);

            if (reversed)
            {
                Assert.AreEqual(2, topDocs.ScoreDocs[2].doc);
                Assert.AreEqual(1, topDocs.ScoreDocs[3].doc);
            }
            else
            {
                Assert.AreEqual(1, topDocs.ScoreDocs[2].doc);
                Assert.AreEqual(2, topDocs.ScoreDocs[3].doc);
            }

            /*
             * for (int i = 0; i < nDocsReturned; i++) {
             * ScoreDoc scoreDoc = topDocs.scoreDocs[i];
             * ids[i] = scoreDoc.doc;
             * scores[i] = scoreDoc.score;
             * documents[i] = searcher.doc(ids[i]);
             * System.out.println("ids[i] = " + ids[i]);
             * System.out.println("documents[i] = " + documents[i]);
             * System.out.println("scores[i] = " + scores[i]);
             * }
             */
        }