Beispiel #1
0
        /*(non-Javadoc) @see Mono.Lucene.Net.Search.Query#rewrite(Mono.Lucene.Net.Index.IndexReader) */
        public override Query Rewrite(IndexReader reader)
        {
            CustomScoreQuery clone = null;

            Query sq = subQuery.Rewrite(reader);

            if (sq != subQuery)
            {
                clone          = (CustomScoreQuery)Clone();
                clone.subQuery = sq;
            }

            for (int i = 0; i < valSrcQueries.Length; i++)
            {
                ValueSourceQuery v = (ValueSourceQuery)valSrcQueries[i].Rewrite(reader);
                if (v != valSrcQueries[i])
                {
                    if (clone == null)
                    {
                        clone = (CustomScoreQuery)Clone();
                    }
                    clone.valSrcQueries[i] = v;
                }
            }

            return((clone == null) ? this : clone);
        }
Beispiel #2
0
        /// <summary> Query should be rewritten for wild/fuzzy support.
        ///
        /// </summary>
        /// <param name="query">
        /// </param>
        /// <returns> payloads Collection
        /// </returns>
        /// <throws>  IOException </throws>
        public virtual ICollection <byte[]> GetPayloadsForQuery(Query query)
        {
            ICollection <byte[]> payloads = new List <byte[]>();

            QueryToSpanQuery(query, payloads);
            return(payloads);
        }
Beispiel #3
0
		/// <summary> Create a CustomScoreQuery over input subQuery and a {@link ValueSourceQuery}.</summary>
		/// <param name="subQuery">the sub query whose score is being customized. Must not be null.
		/// </param>
		/// <param name="valSrcQueries">value source queries whose scores are used in the custom score
		/// computation. For most simple/convenient use case these would be 
		/// {@link Mono.Lucene.Net.Search.Function.FieldScoreQuery FieldScoreQueries}.
		/// This parameter is optional - it can be null or even an empty array.
		/// </param>
		public CustomScoreQuery(Query subQuery, ValueSourceQuery[] valSrcQueries)
		{
			this.subQuery = subQuery;
			this.valSrcQueries = valSrcQueries != null?valSrcQueries:new ValueSourceQuery[0];
			if (subQuery == null)
				throw new System.ArgumentException("<subquery> must not be null!");
		}
Beispiel #4
0
 /// <summary> Create a CustomScoreQuery over input subQuery and a {@link ValueSourceQuery}.</summary>
 /// <param name="subQuery">the sub query whose score is being customized. Must not be null.
 /// </param>
 /// <param name="valSrcQueries">value source queries whose scores are used in the custom score
 /// computation. For most simple/convenient use case these would be
 /// {@link Mono.Lucene.Net.Search.Function.FieldScoreQuery FieldScoreQueries}.
 /// This parameter is optional - it can be null or even an empty array.
 /// </param>
 public CustomScoreQuery(Query subQuery, ValueSourceQuery[] valSrcQueries)
 {
     this.subQuery      = subQuery;
     this.valSrcQueries = valSrcQueries != null?valSrcQueries:new ValueSourceQuery[0];
     if (subQuery == null)
     {
         throw new System.ArgumentException("<subquery> must not be null!");
     }
 }
 private void  ApplySlop(Query q, int slop)
 {
     if (q is PhraseQuery)
     {
         ((PhraseQuery)q).SetSlop(slop);
     }
     else if (q is MultiPhraseQuery)
     {
         ((MultiPhraseQuery)q).SetSlop(slop);
     }
 }
        /// <summary> Parses a query, searching on the fields specified. Use this if you need
        /// to specify certain fields as required, and others as prohibited.
        /// <p/>
        ///
        /// <pre>
        /// Usage:
        /// &lt;code&gt;
        /// String[] query = {&quot;query1&quot;, &quot;query2&quot;, &quot;query3&quot;};
        /// String[] fields = {&quot;filename&quot;, &quot;contents&quot;, &quot;description&quot;};
        /// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
        /// BooleanClause.Occur.MUST,
        /// BooleanClause.Occur.MUST_NOT};
        /// MultiFieldQueryParser.parse(query, fields, flags, analyzer);
        /// &lt;/code&gt;
        /// </pre>
        /// <p/>
        /// The code above would construct a query:
        ///
        /// <pre>
        /// &lt;code&gt;
        /// (filename:query1) +(contents:query2) -(description:query3)
        /// &lt;/code&gt;
        /// </pre>
        ///
        /// </summary>
        /// <param name="matchVersion">Lucene version to match; this is passed through to
        /// QueryParser.
        /// </param>
        /// <param name="queries">Queries string to parse
        /// </param>
        /// <param name="fields">Fields to search on
        /// </param>
        /// <param name="flags">Flags describing the fields
        /// </param>
        /// <param name="analyzer">Analyzer to use
        /// </param>
        /// <throws>  ParseException </throws>
        /// <summary>             if query parsing fails
        /// </summary>
        /// <throws>  IllegalArgumentException </throws>
        /// <summary>             if the length of the queries, fields, and flags array differ
        /// </summary>
        public static Query Parse(Version matchVersion, System.String[] queries, System.String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer)
        {
            if (!(queries.Length == fields.Length && queries.Length == flags.Length))
            {
                throw new System.ArgumentException("queries, fields, and flags array have have different length");
            }
            BooleanQuery bQuery = new BooleanQuery();

            for (int i = 0; i < fields.Length; i++)
            {
                QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
                Query       q  = qp.Parse(queries[i]);
                if (q != null && (!(q is BooleanQuery) || ((BooleanQuery)q).GetClauses().Length > 0))
                {
                    bQuery.Add(q, flags[i]);
                }
            }
            return(bQuery);
        }
        protected internal override Query GetFieldQuery(System.String field, System.String queryText, int slop)
        {
            if (field == null)
            {
                System.Collections.IList clauses = new System.Collections.ArrayList();
                for (int i = 0; i < fields.Length; i++)
                {
                    Query q = base.GetFieldQuery(fields[i], queryText);
                    if (q != null)
                    {
                        //If the user passes a map of boosts
                        if (boosts != null)
                        {
                            //Get the boost from the map and apply them
                            if (boosts.Contains(fields[i]))
                            {
                                System.Single boost = (System.Single)boosts[fields[i]];
                                q.SetBoost((float)boost);
                            }
                        }
                        ApplySlop(q, slop);
                        clauses.Add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
                    }
                }
                if (clauses.Count == 0)
                {
                    // happens for stopwords
                    return(null);
                }
                return(GetBooleanQuery(clauses, true));
            }
            Query q2 = base.GetFieldQuery(field, queryText);

            ApplySlop(q2, slop);
            return(q2);
        }
Beispiel #8
0
		/// <summary> Create a CustomScoreQuery over input subQuery and a {@link ValueSourceQuery}.</summary>
		/// <param name="subQuery">the sub query whose score is being customed. Must not be null.
		/// </param>
		/// <param name="valSrcQuery">a value source query whose scores are used in the custom score
		/// computation. For most simple/convineient use case this would be a 
		/// {@link Mono.Lucene.Net.Search.Function.FieldScoreQuery FieldScoreQuery}.
        /// This parameter is optional - it can be null or even an empty array.
		/// </param>
		public CustomScoreQuery(Query subQuery, ValueSourceQuery valSrcQuery):this(subQuery, valSrcQuery != null?new ValueSourceQuery[]{valSrcQuery}:new ValueSourceQuery[0])
		{
		}
Beispiel #9
0
 /// <summary> Create a CustomScoreQuery over input subQuery and a {@link ValueSourceQuery}.</summary>
 /// <param name="subQuery">the sub query whose score is being customed. Must not be null.
 /// </param>
 /// <param name="valSrcQuery">a value source query whose scores are used in the custom score
 /// computation. For most simple/convineient use case this would be a
 /// {@link Mono.Lucene.Net.Search.Function.FieldScoreQuery FieldScoreQuery}.
 /// This parameter is optional - it can be null or even an empty array.
 /// </param>
 public CustomScoreQuery(Query subQuery, ValueSourceQuery valSrcQuery) : this(subQuery, valSrcQuery != null?new ValueSourceQuery[] { valSrcQuery }:new ValueSourceQuery[0])
 {
 }
Beispiel #10
0
		internal bool BufferDeleteQueries(Query[] queries)
		{
			lock (this)
			{
				WaitReady(null);
				for (int i = 0; i < queries.Length; i++)
					AddDeleteQuery(queries[i], numDocsInRAM);
				return TimeToFlushDeletes();
			}
		}
Beispiel #11
0
        private void  QueryToSpanQuery(Query query, ICollection <byte[]> payloads)
        {
            if (query is BooleanQuery)
            {
                BooleanClause[] queryClauses = ((BooleanQuery)query).GetClauses();

                for (int i = 0; i < queryClauses.Length; i++)
                {
                    if (!queryClauses[i].IsProhibited())
                    {
                        QueryToSpanQuery(queryClauses[i].GetQuery(), payloads);
                    }
                }
            }
            else if (query is PhraseQuery)
            {
                Term[]      phraseQueryTerms = ((PhraseQuery)query).GetTerms();
                SpanQuery[] clauses          = new SpanQuery[phraseQueryTerms.Length];
                for (int i = 0; i < phraseQueryTerms.Length; i++)
                {
                    clauses[i] = new SpanTermQuery(phraseQueryTerms[i]);
                }

                int  slop    = ((PhraseQuery)query).GetSlop();
                bool inorder = false;

                if (slop == 0)
                {
                    inorder = true;
                }

                SpanNearQuery sp = new SpanNearQuery(clauses, slop, inorder);
                sp.SetBoost(query.GetBoost());
                GetPayloads(payloads, sp);
            }
            else if (query is TermQuery)
            {
                SpanTermQuery stq = new SpanTermQuery(((TermQuery)query).GetTerm());
                stq.SetBoost(query.GetBoost());
                GetPayloads(payloads, stq);
            }
            else if (query is SpanQuery)
            {
                GetPayloads(payloads, (SpanQuery)query);
            }
            else if (query is FilteredQuery)
            {
                QueryToSpanQuery(((FilteredQuery)query).GetQuery(), payloads);
            }
            else if (query is DisjunctionMaxQuery)
            {
                for (System.Collections.IEnumerator iterator = ((DisjunctionMaxQuery)query).Iterator(); iterator.MoveNext();)
                {
                    QueryToSpanQuery((Query)iterator.Current, payloads);
                }
            }
            else if (query is MultiPhraseQuery)
            {
                MultiPhraseQuery         mpq        = (MultiPhraseQuery)query;
                System.Collections.IList termArrays = mpq.GetTermArrays();
                int[] positions = mpq.GetPositions();
                if (positions.Length > 0)
                {
                    int maxPosition = positions[positions.Length - 1];
                    for (int i = 0; i < positions.Length - 1; ++i)
                    {
                        if (positions[i] > maxPosition)
                        {
                            maxPosition = positions[i];
                        }
                    }

                    System.Collections.ArrayList[] disjunctLists = new System.Collections.ArrayList[maxPosition + 1];
                    int distinctPositions = 0;

                    for (int i = 0; i < termArrays.Count; ++i)
                    {
                        Term[] termArray = (Term[])termArrays[i];
                        System.Collections.IList disjuncts = disjunctLists[positions[i]];
                        if (disjuncts == null)
                        {
                            disjuncts = (disjunctLists[positions[i]] = new System.Collections.ArrayList(termArray.Length));
                            ++distinctPositions;
                        }
                        for (int j = 0; j < termArray.Length; ++j)
                        {
                            disjuncts.Add(new SpanTermQuery(termArray[j]));
                        }
                    }

                    int         positionGaps = 0;
                    int         position     = 0;
                    SpanQuery[] clauses      = new SpanQuery[distinctPositions];
                    for (int i = 0; i < disjunctLists.Length; ++i)
                    {
                        System.Collections.ArrayList disjuncts = disjunctLists[i];
                        if (disjuncts != null)
                        {
                            clauses[position++] = new SpanOrQuery((SpanQuery[])(disjuncts.ToArray(typeof(SpanQuery[]))));
                        }
                        else
                        {
                            ++positionGaps;
                        }
                    }

                    int  slop    = mpq.GetSlop();
                    bool inorder = (slop == 0);

                    SpanNearQuery sp = new SpanNearQuery(clauses, slop + positionGaps, inorder);
                    sp.SetBoost(query.GetBoost());
                    GetPayloads(payloads, sp);
                }
            }
        }
Beispiel #12
0
		protected internal virtual void  AddClause(System.Collections.IList 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.IsProhibited())
					c.SetOccur(BooleanClause.Occur.MUST);
			}
			
			if (clauses.Count > 0 && operator_Renamed == AND_OPERATOR && 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.IsProhibited())
					c.SetOccur(BooleanClause.Occur.SHOULD);
			}
			
			// 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 == OR_OPERATOR)
			{
				// 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);
			}
			if (required && !prohibited)
				clauses.Add(NewBooleanClause(q, BooleanClause.Occur.MUST));
			else if (!required && !prohibited)
				clauses.Add(NewBooleanClause(q, BooleanClause.Occur.SHOULD));
			else if (!required && prohibited)
				clauses.Add(NewBooleanClause(q, BooleanClause.Occur.MUST_NOT));
			else
				throw new System.SystemException("Clause cannot be both required and prohibited");
		}
Beispiel #13
0
		/// <summary> Builds a new BooleanClause instance</summary>
		/// <param name="q">sub query
		/// </param>
		/// <param name="occur">how this clause should occur when matching documents
		/// </param>
		/// <returns> new BooleanClause instance
		/// </returns>
		protected internal virtual BooleanClause NewBooleanClause(Query q, BooleanClause.Occur occur)
		{
			return new BooleanClause(q, occur);
		}
Beispiel #14
0
        private bool strict = false;              // if true, valueSource part of query does not take part in weights normalization.

        /// <summary> Create a CustomScoreQuery over input subQuery.</summary>
        /// <param name="subQuery">the sub query whose scored is being customed. Must not be null.
        /// </param>
        public CustomScoreQuery(Query subQuery) : this(subQuery, new ValueSourceQuery[0])
        {
        }
Beispiel #15
0
		protected internal virtual void  AddClause(System.Collections.ArrayList clauses, int conj, int mods, Query q)
		{
			AddClause((System.Collections.IList) clauses, conj, mods, q);
		}
Beispiel #16
0
		/// <summary> Query should be rewritten for wild/fuzzy support.
		/// 
		/// </summary>
		/// <param name="query">
		/// </param>
		/// <returns> payloads Collection
		/// </returns>
		/// <throws>  IOException </throws>
		public virtual ICollection<byte[]> GetPayloadsForQuery(Query query)
		{
			ICollection<byte[]> payloads = new List<byte[]>();
			QueryToSpanQuery(query, payloads);
			return payloads;
		}
Beispiel #17
0
		private bool strict = false; // if true, valueSource part of query does not take part in weights normalization.  
		
		/// <summary> Create a CustomScoreQuery over input subQuery.</summary>
		/// <param name="subQuery">the sub query whose scored is being customed. Must not be null. 
		/// </param>
		public CustomScoreQuery(Query subQuery):this(subQuery, new ValueSourceQuery[0])
		{
		}
Beispiel #18
0
		private void  ApplySlop(Query q, int slop)
		{
			if (q is PhraseQuery)
			{
				((PhraseQuery) q).SetSlop(slop);
			}
			else if (q is MultiPhraseQuery)
			{
				((MultiPhraseQuery) q).SetSlop(slop);
			}
		}
Beispiel #19
0
		private void  AddDeleteQuery(Query query, int docID)
		{
			lock (this)
			{
				deletesInRAM.queries[query] = (System.Int32) (flushedDocCount + docID);
				deletesInRAM.AddBytesUsed(BYTES_PER_DEL_QUERY);
			}
		}
Beispiel #20
0
		internal bool BufferDeleteQuery(Query query)
		{
			lock (this)
			{
				WaitReady(null);
				AddDeleteQuery(query, numDocsInRAM);
				return TimeToFlushDeletes();
			}
		}
Beispiel #21
0
		private void  QueryToSpanQuery(Query query, ICollection<byte[]> payloads)
		{
			if (query is BooleanQuery)
			{
				BooleanClause[] queryClauses = ((BooleanQuery) query).GetClauses();
				
				for (int i = 0; i < queryClauses.Length; i++)
				{
					if (!queryClauses[i].IsProhibited())
					{
						QueryToSpanQuery(queryClauses[i].GetQuery(), payloads);
					}
				}
			}
			else if (query is PhraseQuery)
			{
				Term[] phraseQueryTerms = ((PhraseQuery) query).GetTerms();
				SpanQuery[] clauses = new SpanQuery[phraseQueryTerms.Length];
				for (int i = 0; i < phraseQueryTerms.Length; i++)
				{
					clauses[i] = new SpanTermQuery(phraseQueryTerms[i]);
				}
				
				int slop = ((PhraseQuery) query).GetSlop();
				bool inorder = false;
				
				if (slop == 0)
				{
					inorder = true;
				}
				
				SpanNearQuery sp = new SpanNearQuery(clauses, slop, inorder);
				sp.SetBoost(query.GetBoost());
				GetPayloads(payloads, sp);
			}
			else if (query is TermQuery)
			{
				SpanTermQuery stq = new SpanTermQuery(((TermQuery) query).GetTerm());
				stq.SetBoost(query.GetBoost());
				GetPayloads(payloads, stq);
			}
			else if (query is SpanQuery)
			{
				GetPayloads(payloads, (SpanQuery) query);
			}
			else if (query is FilteredQuery)
			{
				QueryToSpanQuery(((FilteredQuery) query).GetQuery(), payloads);
			}
			else if (query is DisjunctionMaxQuery)
			{
				
				for (System.Collections.IEnumerator iterator = ((DisjunctionMaxQuery) query).Iterator(); iterator.MoveNext(); )
				{
					QueryToSpanQuery((Query) iterator.Current, payloads);
				}
			}
			else if (query is MultiPhraseQuery)
			{
				MultiPhraseQuery mpq = (MultiPhraseQuery) query;
				System.Collections.IList termArrays = mpq.GetTermArrays();
				int[] positions = mpq.GetPositions();
				if (positions.Length > 0)
				{
					
					int maxPosition = positions[positions.Length - 1];
					for (int i = 0; i < positions.Length - 1; ++i)
					{
						if (positions[i] > maxPosition)
						{
							maxPosition = positions[i];
						}
					}
					
					System.Collections.ArrayList[] disjunctLists = new System.Collections.ArrayList[maxPosition + 1];
					int distinctPositions = 0;
					
					for (int i = 0; i < termArrays.Count; ++i)
					{
						Term[] termArray = (Term[]) termArrays[i];
						System.Collections.IList disjuncts = disjunctLists[positions[i]];
						if (disjuncts == null)
						{
							disjuncts = (disjunctLists[positions[i]] = new System.Collections.ArrayList(termArray.Length));
							++distinctPositions;
						}
						for (int j = 0; j < termArray.Length; ++j)
						{
							disjuncts.Add(new SpanTermQuery(termArray[j]));
						}
					}
					
					int positionGaps = 0;
					int position = 0;
					SpanQuery[] clauses = new SpanQuery[distinctPositions];
					for (int i = 0; i < disjunctLists.Length; ++i)
					{
						System.Collections.ArrayList disjuncts = disjunctLists[i];
						if (disjuncts != null)
						{
                            clauses[position++] = new SpanOrQuery((SpanQuery[]) (disjuncts.ToArray(typeof(SpanQuery[]))));
						}
						else
						{
							++positionGaps;
						}
					}
					
					int slop = mpq.GetSlop();
					bool inorder = (slop == 0);
					
					SpanNearQuery sp = new SpanNearQuery(clauses, slop + positionGaps, inorder);
					sp.SetBoost(query.GetBoost());
					GetPayloads(payloads, sp);
				}
			}
		}
Beispiel #22
0
		/// <summary> Deletes the document(s) matching any of the provided queries.
		/// All deletes are flushed at the same time.
		/// 
		/// <p/><b>NOTE</b>: if this method hits an OutOfMemoryError
		/// you should immediately close the writer.  See <a
		/// href="#OOME">above</a> for details.<p/>
		/// 
		/// </summary>
		/// <param name="queries">array of queries to identify the documents
		/// to be deleted
		/// </param>
		/// <throws>  CorruptIndexException if the index is corrupt </throws>
		/// <throws>  IOException if there is a low-level IO error </throws>
		public virtual void  DeleteDocuments(Query[] queries)
		{
			EnsureOpen();
			bool doFlush = docWriter.BufferDeleteQueries(queries);
			if (doFlush)
				Flush(true, false, false);
		}