private int maxDocs = 200; // max to cache internal Hits(Searcher s, Query q, Filter f) { query = q; searcher = s; filter = f; GetMoreDocs(50); // retrieve 100 initially }
/// <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(Query query, Filter filter, int nDocs) { HitQueue hq = new HitQueue(nDocs); int totalHits = 0; MultiSearcherThread[] msta = new MultiSearcherThread[searchables.Length]; for (int i = 0; i < searchables.Length; i++) { // search each searcher // Assume not too many searchables and cost of creating a thread is by far inferior to a search msta[i] = new MultiSearcherThread(searchables[i], query, 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) { ; // TODO: what should we do with this??? } 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(); return new TopDocs(totalHits, scoreDocs); }
protected internal virtual void AddClause(System.Collections.ArrayList clauses, int conj, int mods, Query q) { bool required, prohibited; // If this term is introduced by AND, make the preceding term required, // unless it's already prohibited if (clauses.Count > 0 && conj == CONJ_AND) { BooleanClause c = (BooleanClause) clauses[clauses.Count - 1]; if (!c.prohibited) c.required = true; } if (clauses.Count > 0 && operator_Renamed == DEFAULT_OPERATOR_AND && conj == CONJ_OR) { // If this term is introduced by OR, make the preceding term optional, // unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b) // notice if the input is a OR b, first term is parsed as required; without // this modification a OR b would parsed as +a OR b BooleanClause c = (BooleanClause) clauses[clauses.Count - 1]; if (!c.prohibited) c.required = false; } // We might have been passed a null query; the term might have been // filtered away by the analyzer. if (q == null) return ; if (operator_Renamed == DEFAULT_OPERATOR_OR) { // We set REQUIRED if we're introduced by AND or +; PROHIBITED if // introduced by NOT or -; make sure not to set both. prohibited = (mods == MOD_NOT); required = (mods == MOD_REQ); if (conj == CONJ_AND && !prohibited) { required = true; } } else { // We set PROHIBITED if we're introduced by NOT or -; We set REQUIRED // if not PROHIBITED and not introduced by OR prohibited = (mods == MOD_NOT); required = (!prohibited && conj != CONJ_OR); } clauses.Add(new BooleanClause(q, required, prohibited)); }
public override Query Combine(Query[] queries) { return Query.MergeBooleanQueries(queries); }
public override Explanation Explain(Query query, int doc) { return query.Weight(this).Explain(reader, doc); }
// inherit javadoc public override void Search(Query query, Filter filter, HitCollector results) { HitCollector collector = results; if (filter != null) { System.Collections.BitArray bits = filter.Bits(reader); collector = new AnonymousClassHitCollector2(bits, results, this); } Scorer scorer = query.Weight(this).Scorer(reader); if (scorer == null) return ; scorer.Score(collector); }
/// <summary>Expert: called when re-writing queries under MultiSearcher. /// /// <p>Only implemented by derived queries, with no /// {@link #CreateWeight(Searcher)} implementatation. /// </summary> public virtual Query Combine(Query[] queries) { throw new System.NotSupportedException(); }
public override Query Rewrite(Query original) { Query[] queries = new Query[searchables.Length]; for (int i = 0; i < searchables.Length; i++) { queries[i] = searchables[i].Rewrite(original); } return original.Combine(queries); }
public MultiSearcherThread(Monodoc.Lucene.Net.Search.Searchable searchable, Query query, Filter filter, int nDocs, FieldDocSortedHitQueue hq, Sort sort, int i, int[] starts, System.String name):base(name) { this.searchable = searchable; this.query = query; this.filter = filter; this.nDocs = nDocs; this.hq = hq; this.i = i; this.starts = starts; this.sort = sort; }
public virtual Explanation Explain(Query query, int doc) { return local.Explain(query, doc); }
public virtual Query Rewrite(Query original) { return local.Rewrite(original); }
public virtual TopFieldDocs Search(Query query, Filter filter, int n, Sort sort) { return local.Search(query, filter, n, sort); }
public virtual TopDocs Search(Query query, Filter filter, int n) { return local.Search(query, filter, n); }
public virtual void Search(Query query, Filter filter, HitCollector results) { local.Search(query, filter, results); }
public override TopFieldDocs Search(Query query, Filter filter, int n, Sort sort) { FieldDocSortedHitQueue hq = null; int totalHits = 0; for (int i = 0; i < searchables.Length; i++) { // search each searcher TopFieldDocs docs = searchables[i].Search(query, filter, n, sort); if (hq == null) hq = new FieldDocSortedHitQueue(docs.fields, n); 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(); return new TopFieldDocs(totalHits, scoreDocs2, hq.GetFields()); }
// inherit javadoc public override void Search(Query query, Filter filter, HitCollector results) { for (int i = 0; i < searchables.Length; i++) { int start = starts[i]; searchables[i].Search(query, filter, new AnonymousClassHitCollector(results, start, this)); } }
/// <summary>Constructs a filter which only matches documents matching /// <code>query</code>. /// </summary> public QueryFilter(Query query) { this.query = query; }
public override Explanation Explain(Query query, int doc) { int i = SubSearcher(doc); // find searcher index return searchables[i].Explain(query, doc - starts[i]); // dispatch to searcher }
/// <summary>Returns the documents matching <code>query</code>. </summary> public Hits Search(Query query) { return Search(query, (Filter) null); }
/// <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(Query[] queries) { System.Collections.Hashtable allClauses = new System.Collections.Hashtable(); for (int i = 0; i < queries.Length; i++) { BooleanClause[] clauses = ((BooleanQuery) queries[i]).GetClauses(); for (int j = 0; j < clauses.Length; j++) { allClauses.Add(clauses[j], clauses[j]); } } BooleanQuery result = new BooleanQuery(); foreach (BooleanClause booleanClause in allClauses.Keys) { result.Add(booleanClause); } return result; }
/// <summary>Returns the documents matching <code>query</code> and /// <code>filter</code>. /// </summary> public virtual Hits Search(Query query, Filter filter) { return new Hits(this, query, filter); }
// inherit javadoc public override TopFieldDocs Search(Query query, Filter filter, int nDocs, Sort sort) { Scorer scorer = query.Weight(this).Scorer(reader); if (scorer == null) return new TopFieldDocs(0, new ScoreDoc[0], sort.fields); System.Collections.BitArray bits = filter != null ? filter.Bits(reader) : null; FieldSortedHitQueue hq = new FieldSortedHitQueue(reader, sort.fields, nDocs); int[] totalHits = new int[1]; scorer.Score(new AnonymousClassHitCollector1(bits, totalHits, hq, this)); ScoreDoc[] scoreDocs = new ScoreDoc[hq.Size()]; for (int i = hq.Size() - 1; i >= 0; i--) // put docs in array scoreDocs[i] = hq.FillFields((FieldDoc) hq.Pop()); return new TopFieldDocs(totalHits[0], scoreDocs, hq.GetFields()); }
/// <summary>Returns documents matching <code>query</code> sorted by /// <code>sort</code>. /// </summary> public virtual Hits Search(Query query, Sort sort) { return new Hits(this, query, null, sort); }
public override Query Rewrite(Query original) { Query query = original; for (Query rewrittenQuery = query.Rewrite(reader); rewrittenQuery != query; rewrittenQuery = query.Rewrite(reader)) { query = rewrittenQuery; } return query; }
/// <summary>Returns documents matching <code>query</code> and <code>filter</code>, /// sorted by <code>sort</code>. /// </summary> public virtual Hits Search(Query query, Filter filter, Sort sort) { return new Hits(this, query, filter, sort); }
/// <summary> Constructs a new query which applies a filter to the results of the original query. /// Filter.bits() 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; }
/// <summary>Lower-level search API. /// /// <p>{@link HitCollector#Collect(int,float)} is called for every non-zero /// scoring document. /// /// <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)}) is usually more efficient, as it skips /// non-high-scoring hits. /// <p>Note: The <code>score</code> passed to this method is a raw score. /// In other words, the score will not necessarily be a float whose value is /// between 0 and 1. /// </summary> public virtual void Search(Query query, HitCollector results) { Search(query, (Filter) null, results); }
/// <summary>Adds a clause to a boolean query. Clauses may be: /// <ul> /// <li><code>required</code> which means that documents which <i>do not</i> /// match this sub-query will <i>not</i> match the boolean query; /// <li><code>prohibited</code> which means that documents which <i>do</i> /// match this sub-query will <i>not</i> match the boolean query; or /// <li>neither, in which case matched documents are neither prohibited from /// nor required to match the sub-query. However, a document must match at /// least 1 sub-query to match the boolean query. /// </ul> /// It is an error to specify a clause as both <code>required</code> and /// <code>prohibited</code>. /// /// </summary> /// <seealso cref="#GetMaxClauseCount()"> /// </seealso> public virtual void Add(Query query, bool required, bool prohibited) { Add(new BooleanClause(query, required, prohibited)); }
/// <summary>Constructs a BooleanClause with query <code>q</code>, required /// <code>r</code> and prohibited <code>p</code>. /// </summary> public BooleanClause(Query q, bool r, bool p) { query = q; required = r; prohibited = p; }