Esempio n. 1
0
	public Result Search (string term, int count, int start) {
		try {
			Query q1 = Parse (term, "hottext", true);
			Query q2 = Parse (term, "text", false);
			q2.SetBoost (0.7f);
			Query q3 = Parse (term, "examples", false);
			q3.SetBoost (0.5f);
			BooleanQuery q = new BooleanQuery();
			q.Add (q1, BooleanClause.Occur.SHOULD);
			q.Add (q2, BooleanClause.Occur.SHOULD);
			q.Add (q3, BooleanClause.Occur.SHOULD);
			TopDocs top = SearchInternal (q, count, start);
			Result r = new Result (term, searcher, top.ScoreDocs);
			Results.Add (r);
			return r;
		} catch (IOException) {
			Console.WriteLine ("No index in {0}", dir);
			return null;
		}
	}
Esempio n. 2
0
		public override Query Rewrite(IndexReader reader)
		{
			if (!termLongEnough)
			{
				// can only match if it's exact
				return new TermQuery(term);
			}
			
			FilteredTermEnum enumerator = GetEnum(reader);
			int maxClauseCount = BooleanQuery.GetMaxClauseCount();
			ScoreTermQueue stQueue = new ScoreTermQueue(maxClauseCount);
			ScoreTerm reusableST = null;
			
			try
			{
				do 
				{
					float score = 0.0f;
					Term t = enumerator.Term();
					if (t != null)
					{
						score = enumerator.Difference();
						if (reusableST == null)
						{
							reusableST = new ScoreTerm(t, score);
						}
						else if (score >= reusableST.score)
						{
							// reusableST holds the last "rejected" entry, so, if
							// this new score is not better than that, there's no
							// need to try inserting it
							reusableST.score = score;
							reusableST.term = t;
						}
						else
						{
							continue;
						}
						
						reusableST = (ScoreTerm) stQueue.InsertWithOverflow(reusableST);
					}
				}
				while (enumerator.Next());
			}
			finally
			{
				enumerator.Close();
			}
			
			BooleanQuery query = new BooleanQuery(true);
			int size = stQueue.Size();
			for (int i = 0; i < size; i++)
			{
				ScoreTerm st = (ScoreTerm) stQueue.Pop();
				TermQuery tq = new TermQuery(st.term); // found a match
				tq.SetBoost(GetBoost() * st.score); // set the boost
				query.Add(tq, BooleanClause.Occur.SHOULD); // add to query
			}
			
			return query;
		}
Esempio n. 3
0
			public override Query Rewrite(IndexReader reader, MultiTermQuery query)
			{
				// Get the enum and start visiting terms.  If we
				// exhaust the enum before hitting either of the
				// cutoffs, we use ConstantBooleanQueryRewrite; else,
				// ConstantFilterRewrite:
				System.Collections.ArrayList pendingTerms = new System.Collections.ArrayList();
				int docCountCutoff = (int) ((docCountPercent / 100.0) * reader.MaxDoc());
				int termCountLimit = System.Math.Min(BooleanQuery.GetMaxClauseCount(), termCountCutoff);
				int docVisitCount = 0;
				
				FilteredTermEnum enumerator = query.GetEnum(reader);
				try
				{
					while (true)
					{
						Term t = enumerator.Term();
						if (t != null)
						{
							pendingTerms.Add(t);
							// Loading the TermInfo from the terms dict here
							// should not be costly, because 1) the
							// query/filter will load the TermInfo when it
							// runs, and 2) the terms dict has a cache:
							docVisitCount += reader.DocFreq(t);
						}
						
						if (pendingTerms.Count >= termCountLimit || docVisitCount >= docCountCutoff)
						{
							// Too many terms -- make a filter.
							Query result = new ConstantScoreQuery(new MultiTermQueryWrapperFilter(query));
							result.SetBoost(query.GetBoost());
							return result;
						}
						else if (!enumerator.Next())
						{
							// Enumeration is done, and we hit a small
							// enough number of terms & docs -- just make a
							// BooleanQuery, now
							System.Collections.IEnumerator it = pendingTerms.GetEnumerator();
							BooleanQuery bq = new BooleanQuery(true);
							while (it.MoveNext())
							{
								TermQuery tq = new TermQuery((Term) it.Current);
								bq.Add(tq, BooleanClause.Occur.SHOULD);
							}
							// Strip scores
							Query result = new ConstantScoreQuery(new QueryWrapperFilter(bq));
							result.SetBoost(query.GetBoost());
							query.IncTotalNumberOfTerms(pendingTerms.Count);
							return result;
						}
					}
				}
				finally
				{
					enumerator.Close();
				}
			}
Esempio n. 4
0
		/// <summary>Expert: merges the clauses of a set of BooleanQuery's into a single
		/// BooleanQuery.
		/// 
		/// <p/>A utility for use by {@link #Combine(Query[])} implementations.
		/// </summary>
		public static Query MergeBooleanQueries(BooleanQuery[] queries)
		{
            System.Collections.Hashtable allClauses = new System.Collections.Hashtable();
			for (int i = 0; i < queries.Length; i++)
			{
				BooleanClause[] clauses = queries[i].GetClauses();
				for (int j = 0; j < clauses.Length; j++)
				{
					SupportClass.CollectionsHelper.AddIfNotContains(allClauses, clauses[j]);
				}
			}
			
			bool coordDisabled = queries.Length == 0?false:queries[0].IsCoordDisabled();
			BooleanQuery result = new BooleanQuery(coordDisabled);
			System.Collections.IEnumerator i2 = allClauses.GetEnumerator();
			while (i2.MoveNext())
			{
				result.Add((BooleanClause) i2.Current);
			}
			return result;
		}
Esempio n. 5
0
			public override Query Rewrite(IndexReader reader, MultiTermQuery query)
			{
				
				FilteredTermEnum enumerator = query.GetEnum(reader);
				BooleanQuery result = new BooleanQuery(true);
				int count = 0;
				try
				{
					do 
					{
						Term t = enumerator.Term();
						if (t != null)
						{
							TermQuery tq = new TermQuery(t); // found a match
							tq.SetBoost(query.GetBoost() * enumerator.Difference()); // set the boost
							result.Add(tq, BooleanClause.Occur.SHOULD); // add to query
							count++;
						}
					}
					while (enumerator.Next());
				}
				finally
				{
					enumerator.Close();
				}
				query.IncTotalNumberOfTerms(count);
				return result;
			}
Esempio n. 6
0
		/// <summary>Expert: called when re-writing queries under MultiSearcher.
		/// 
		/// Create a single query suitable for use by all subsearchers (in 1-1
		/// correspondence with queries). This is an optimization of the OR of
		/// all queries. We handle the common optimization cases of equal
		/// queries and overlapping clauses of boolean OR queries (as generated
		/// by MultiTermQuery.rewrite()).
		/// Be careful overriding this method as queries[0] determines which
		/// method will be called and is not necessarily of the same type as
		/// the other queries.
		/// </summary>
		public virtual Query Combine(Query[] queries)
		{
            System.Collections.Hashtable uniques = new System.Collections.Hashtable();
			for (int i = 0; i < queries.Length; i++)
			{
				Query query = queries[i];
				BooleanClause[] clauses = null;
				// check if we can split the query into clauses
				bool splittable = (query is BooleanQuery);
				if (splittable)
				{
					BooleanQuery bq = (BooleanQuery) query;
					splittable = bq.IsCoordDisabled();
					clauses = bq.GetClauses();
					for (int j = 0; splittable && j < clauses.Length; j++)
					{
						splittable = (clauses[j].GetOccur() == BooleanClause.Occur.SHOULD);
					}
				}
				if (splittable)
				{
					for (int j = 0; j < clauses.Length; j++)
					{
						SupportClass.CollectionsHelper.AddIfNotContains(uniques, clauses[j].GetQuery());
					}
				}
				else
				{
					SupportClass.CollectionsHelper.AddIfNotContains(uniques, query);
				}
			}
			// optimization: if we have just one query, just return it
			if (uniques.Count == 1)
			{
                foreach (object key in uniques.Keys)
                {
                    return (Query) key;
                }
			}
			BooleanQuery result = new BooleanQuery(true);
            foreach (object key in uniques.Keys)
            {
                result.Add((Query) key, BooleanClause.Occur.SHOULD);
            }
			return result;
		}
Esempio n. 7
0
			internal AnonymousClassSimilarityDelegator(BooleanQuery enclosingInstance, Mono.Lucene.Net.Search.Similarity Param1):base(Param1)
			{
				InitBlock(enclosingInstance);
			}
Esempio n. 8
0
			private void  InitBlock(BooleanQuery enclosingInstance)
			{
				this.enclosingInstance = enclosingInstance;
			}
Esempio n. 9
0
			public BooleanWeight(BooleanQuery enclosingInstance, Searcher searcher)
			{
				InitBlock(enclosingInstance);
				this.similarity = Enclosing_Instance.GetSimilarity(searcher);
				weights = new System.Collections.ArrayList(Enclosing_Instance.clauses.Count);
				for (int i = 0; i < Enclosing_Instance.clauses.Count; i++)
				{
					BooleanClause c = (BooleanClause) Enclosing_Instance.clauses[i];
					weights.Add(c.GetQuery().CreateWeight(searcher));
				}
			}
Esempio n. 10
0
		public override Query Rewrite(IndexReader reader)
		{
			if (termArrays.Count == 1)
			{
				// optimize one-term case
				Term[] terms = (Term[]) termArrays[0];
				BooleanQuery boq = new BooleanQuery(true);
				for (int i = 0; i < terms.Length; i++)
				{
					boq.Add(new TermQuery(terms[i]), BooleanClause.Occur.SHOULD);
				}
				boq.SetBoost(GetBoost());
				return boq;
			}
			else
			{
				return this;
			}
		}