Ejemplo n.º 1
0
		public /*internal*/ bool debugCheckedForDeletions = false; // for test purposes.
		
		internal Hits(Searcher s, Query q, Filter f)
		{
			weight = q.Weight(s);
			searcher = s;
			filter = f;
			nDeletions = CountDeletions(s);
			GetMoreDocs(50); // retrieve 100 initially
			lengthAtStart = length;
		}
Ejemplo n.º 2
0
		public override TopFieldDocs Search(Weight weight, Filter filter, int nDocs, Sort sort)
		{
			return Search(weight, filter, nDocs, sort, true);
		}
Ejemplo n.º 3
0
		/// <summary> A search implementation which spans a new thread for each
		/// Searchable, waits for each search to complete and merge
		/// the results back together.
		/// </summary>
		public override TopDocs Search(Weight weight, Filter filter, int nDocs)
		{
			HitQueue hq = new HitQueue(nDocs, false);
			int totalHits = 0;
			MultiSearcherThread[] msta = new MultiSearcherThread[searchables.Length];
			for (int i = 0; i < searchables.Length; i++)
			{
				// search each searchable
				// Assume not too many searchables and cost of creating a thread is by far inferior to a search
				msta[i] = new MultiSearcherThread(searchables[i], weight, filter, nDocs, hq, i, starts, "MultiSearcher thread #" + (i + 1));
				msta[i].Start();
			}
			
			for (int i = 0; i < searchables.Length; i++)
			{
				try
				{
					msta[i].Join();
				}
				catch (System.Threading.ThreadInterruptedException ie)
				{
					// In 3.0 we will change this to throw
					// InterruptedException instead
					SupportClass.ThreadClass.Current().Interrupt();
					throw new System.SystemException(ie.Message, ie);
				}
				System.IO.IOException ioe = msta[i].GetIOException();
				if (ioe == null)
				{
					totalHits += msta[i].Hits();
				}
				else
				{
					// if one search produced an IOException, rethrow it
					throw ioe;
				}
			}
			
			ScoreDoc[] scoreDocs = new ScoreDoc[hq.Size()];
			for (int i = hq.Size() - 1; i >= 0; i--)
			// put docs in array
				scoreDocs[i] = (ScoreDoc) hq.Pop();
			
			float maxScore = (totalHits == 0)?System.Single.NegativeInfinity:scoreDocs[0].score;
			
			return new TopDocs(totalHits, scoreDocs, maxScore);
		}
Ejemplo n.º 4
0
		public override TopFieldDocs Search(Weight weight, Filter filter, int n, Sort sort)
		{
			FieldDocSortedHitQueue hq = null;
			int totalHits = 0;
			
			float maxScore = System.Single.NegativeInfinity;
			
			for (int i = 0; i < searchables.Length; i++)
			{
				// search each searcher
				TopFieldDocs docs = searchables[i].Search(weight, filter, n, sort);
				// If one of the Sort fields is FIELD_DOC, need to fix its values, so that
				// it will break ties by doc Id properly. Otherwise, it will compare to
				// 'relative' doc Ids, that belong to two different searchers.
				for (int j = 0; j < docs.fields.Length; j++)
				{
					if (docs.fields[j].GetType() == SortField.DOC)
					{
						// iterate over the score docs and change their fields value
						for (int j2 = 0; j2 < docs.ScoreDocs.Length; j2++)
						{
							FieldDoc fd = (FieldDoc) docs.ScoreDocs[j2];
							fd.fields[j] = (System.Int32) (((System.Int32) fd.fields[j]) + starts[i]);
						}
						break;
					}
				}
				if (hq == null)
					hq = new FieldDocSortedHitQueue(docs.fields, n);
				totalHits += docs.TotalHits; // update totalHits
				maxScore = System.Math.Max(maxScore, docs.GetMaxScore());
				ScoreDoc[] scoreDocs = docs.ScoreDocs;
				for (int j = 0; j < scoreDocs.Length; j++)
				{
					// merge scoreDocs into hq
					ScoreDoc scoreDoc = scoreDocs[j];
					scoreDoc.doc += starts[i]; // convert doc
					if (!hq.Insert(scoreDoc))
						break; // no more scores > minScore
				}
			}
			
			ScoreDoc[] scoreDocs2 = new ScoreDoc[hq.Size()];
			for (int i = hq.Size() - 1; i >= 0; i--)
			// put docs in array
				scoreDocs2[i] = (ScoreDoc) hq.Pop();
			
			return new TopFieldDocs(totalHits, scoreDocs2, hq.GetFields(), maxScore);
		}
Ejemplo n.º 5
0
			public override TopFieldDocs Search(Weight weight, Filter filter, int n, Sort sort)
			{
				throw new System.NotSupportedException();
			}
Ejemplo n.º 6
0
       /**
 * Expert: by default, the cached filter will be shared
 * across reopened segments that only had changes to their
 * deletions.  
 *
 * @param filter Filter to cache results of
 * @param deletesMode See {@link DeletesMode}
 */
      public CachingWrapperFilter(Filter filter, DeletesMode deletesMode)
      {
          this.filter = filter;
          cache = new AnonymousFilterCache(deletesMode);
          
          //cache = new FilterCache(deletesMode) 
          // {
          //  public Object mergeDeletes(final IndexReader r, final Object docIdSet) {
          //    return new FilteredDocIdSet((DocIdSet) docIdSet) {
          //      protected boolean match(int docID) {
          //        return !r.isDeleted(docID);
          //      }
          //    };
          //  }
          //};
      }
Ejemplo n.º 7
0
		abstract public void  Search(Weight weight, Filter filter, Collector results);
Ejemplo n.º 8
0
		private void  SearchWithFilter(IndexReader reader, Weight weight, Filter filter, Collector collector)
		{
			
			System.Diagnostics.Debug.Assert(filter != null);
			
			Scorer scorer = weight.Scorer(reader, true, false);
			if (scorer == null)
			{
				return ;
			}
			
			int docID = scorer.DocID();
			System.Diagnostics.Debug.Assert(docID == - 1 || docID == DocIdSetIterator.NO_MORE_DOCS);
			
			// CHECKME: use ConjunctionScorer here?
			DocIdSet filterDocIdSet = filter.GetDocIdSet(reader);
			if (filterDocIdSet == null)
			{
				// this means the filter does not accept any documents.
				return ;
			}
			
			DocIdSetIterator filterIter = filterDocIdSet.Iterator();
			if (filterIter == null)
			{
				// this means the filter does not accept any documents.
				return ;
			}
			int filterDoc = filterIter.NextDoc();
			int scorerDoc = scorer.Advance(filterDoc);
			
			collector.SetScorer(scorer);
			while (true)
			{
				if (scorerDoc == filterDoc)
				{
					// Check if scorer has exhausted, only before collecting.
					if (scorerDoc == DocIdSetIterator.NO_MORE_DOCS)
					{
						break;
					}
					collector.Collect(scorerDoc);
					filterDoc = filterIter.NextDoc();
					scorerDoc = scorer.Advance(filterDoc);
				}
				else if (scorerDoc > filterDoc)
				{
					filterDoc = filterIter.Advance(scorerDoc);
				}
				else
				{
					scorerDoc = scorer.Advance(filterDoc);
				}
			}
		}
Ejemplo n.º 9
0
		/// <summary>Finds the top <code>n</code>
		/// hits for <code>query</code>, applying <code>filter</code> if non-null.
		/// 
		/// </summary>
		/// <throws>  BooleanQuery.TooManyClauses </throws>
		public virtual TopDocs Search(Query query, Filter filter, int n)
		{
			return Search(CreateWeight(query), filter, n);
		}
Ejemplo n.º 10
0
		public virtual void  Search(Weight weight, Filter filter, HitCollector results)
		{
			Search(weight, filter, new HitCollectorWrapper(results));
		}
Ejemplo n.º 11
0
		/// <summary>Lower-level search API.
		/// 
		/// <p/>{@link Collector#Collect(int)} is called for every matching
		/// document.
		/// <br/>Collector-based access to remote indexes is discouraged.
		/// 
		/// <p/>Applications should only use this if they need <i>all</i> of the
		/// matching documents.  The high-level search API ({@link
		/// Searcher#Search(Query, Filter, int)}) is usually more efficient, as it skips
		/// non-high-scoring hits.
		/// 
		/// </summary>
		/// <param name="query">to match documents
		/// </param>
		/// <param name="filter">if non-null, used to permit documents to be collected.
		/// </param>
		/// <param name="results">to receive hits
		/// </param>
		/// <throws>  BooleanQuery.TooManyClauses </throws>
		public virtual void  Search(Query query, Filter filter, Collector results)
		{
			Search(CreateWeight(query), filter, results);
		}
Ejemplo n.º 12
0
		public virtual void  Search(Query query, Filter filter, HitCollector results)
		{
			Search(CreateWeight(query), filter, new HitCollectorWrapper(results));
		}
Ejemplo n.º 13
0
		/// <summary>Search implementation with arbitrary sorting.  Finds
		/// the top <code>n</code> hits for <code>query</code>, applying
		/// <code>filter</code> if non-null, and sorting the hits by the criteria in
		/// <code>sort</code>.
		/// 
		/// <p/>NOTE: this does not compute scores by default; use
		/// {@link IndexSearcher#setDefaultFieldSortScoring} to enable scoring.
		/// 
		/// </summary>
		/// <throws>  BooleanQuery.TooManyClauses </throws>
		public virtual TopFieldDocs Search(Query query, Filter filter, int n, Sort sort)
		{
			return Search(CreateWeight(query), filter, n, sort);
		}
Ejemplo n.º 14
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 = System.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();
		}
Ejemplo n.º 15
0
		abstract public TopDocs Search(Weight weight, Filter filter, int n);
Ejemplo n.º 16
0
		public override void  Search(Weight weight, Filter filter, Collector collector)
		{
			
			if (filter == null)
			{
				for (int i = 0; i < subReaders.Length; i++)
				{
					// search each subreader
					collector.SetNextReader(subReaders[i], docStarts[i]);
					Scorer scorer = weight.Scorer(subReaders[i], !collector.AcceptsDocsOutOfOrder(), true);
					if (scorer != null)
					{
						scorer.Score(collector);
					}
				}
			}
			else
			{
				for (int i = 0; i < subReaders.Length; i++)
				{
					// search each subreader
					collector.SetNextReader(subReaders[i], docStarts[i]);
					SearchWithFilter(subReaders[i], weight, filter, collector);
				}
			}
		}
Ejemplo n.º 17
0
		abstract public TopFieldDocs Search(Weight weight, Filter filter, int n, Sort sort);
Ejemplo n.º 18
0
        /**
          * New deletes are ignored by default, which gives higher
          * cache hit rate on reopened readers.  Most of the time
          * this is safe, because the filter will be AND'd with a
          * Query that fully enforces deletions.  If instead you
          * need this filter to always enforce deletions, pass
          * either {@link DeletesMode#RECACHE} or {@link
          * DeletesMode#DYNAMIC}.
          * @param filter Filter to cache results of
          */
        public CachingWrapperFilter(Filter filter) : this(filter, DeletesMode.IGNORE)
		{
		}
Ejemplo n.º 19
0
		public virtual Hits Search(Query query, Filter filter)
		{
			return new Hits(this, query, filter);
		}
Ejemplo n.º 20
0
		public ConstantScoreQuery(Filter filter)
		{
			this.filter = filter;
		}
Ejemplo n.º 21
0
		public virtual Hits Search(Query query, Filter filter, Sort sort)
		{
			return new Hits(this, query, filter, sort);
		}
Ejemplo n.º 22
0
			public override void  Search(Weight weight, Filter filter, Collector results)
			{
				throw new System.NotSupportedException();
			}
Ejemplo n.º 23
0
		/// <summary> Constructs a new query which applies a filter to the results of the original query.
		/// Filter.getDocIdSet() will be called every time this query is used in a search.
		/// </summary>
		/// <param name="query"> Query to be filtered, cannot be <code>null</code>.
		/// </param>
		/// <param name="filter">Filter to apply to query results, cannot be <code>null</code>.
		/// </param>
		public FilteredQuery(Query query, Filter filter)
		{
			this.query = query;
			this.filter = filter;
		}
Ejemplo n.º 24
0
		public override TopDocs Search(Weight weight, Filter filter, int nDocs)
		{
			
			HitQueue hq = new HitQueue(nDocs, false);
			int totalHits = 0;
			
			for (int i = 0; i < searchables.Length; i++)
			{
				// search each searcher
				TopDocs docs = searchables[i].Search(weight, filter, nDocs);
				totalHits += docs.TotalHits; // update totalHits
				ScoreDoc[] scoreDocs = docs.ScoreDocs;
				for (int j = 0; j < scoreDocs.Length; j++)
				{
					// merge scoreDocs into hq
					ScoreDoc scoreDoc = scoreDocs[j];
					scoreDoc.doc += starts[i]; // convert doc
					if (!hq.Insert(scoreDoc))
						break; // no more scores > minScore
				}
			}
			
			ScoreDoc[] scoreDocs2 = new ScoreDoc[hq.Size()];
			for (int i = hq.Size() - 1; i >= 0; i--)
			// put docs in array
				scoreDocs2[i] = (ScoreDoc) hq.Pop();
			
			float maxScore = (totalHits == 0)?System.Single.NegativeInfinity:scoreDocs2[0].score;
			
			return new TopDocs(totalHits, scoreDocs2, maxScore);
		}
Ejemplo n.º 25
0
		// inherit javadoc
		public override TopDocs Search(Weight weight, Filter filter, int nDocs)
		{
			
			if (nDocs <= 0)
			{
				throw new System.ArgumentException("nDocs must be > 0");
			}
            nDocs = System.Math.Min(nDocs, reader.MaxDoc());

			TopScoreDocCollector collector = TopScoreDocCollector.create(nDocs, !weight.ScoresDocsOutOfOrder());
			Search(weight, filter, collector);
			return collector.TopDocs();
		}
Ejemplo n.º 26
0
		// inherit javadoc
		public override void  Search(Weight weight, Filter filter, Collector collector)
		{
			for (int i = 0; i < searchables.Length; i++)
			{
				
				int start = starts[i];
				
				Collector hc = new AnonymousClassCollector(collector, start, this);
				
				searchables[i].Search(weight, filter, hc);
			}
		}
Ejemplo n.º 27
0
		public MultiSearcherThread(Searchable searchable, Weight weight, Filter filter, int nDocs, FieldDocSortedHitQueue hq, Sort sort, int i, int[] starts, System.String name):base(name)
		{
			this.searchable = searchable;
			this.weight = weight;
			this.filter = filter;
			this.nDocs = nDocs;
			this.hq = hq;
			this.i = i;
			this.starts = starts;
			this.sort = sort;
		}