Exemplo n.º 1
0
        internal LuceneSearchCriteria(string type, Analyzer analyzer, string[] fields, bool allowLeadingWildcards, BooleanOperation occurance)
        {
            Enforcer.ArgumentNotNull(fields, "fields");

            SearchIndexType = type;
            Query = new BooleanQuery();
            this.BooleanOperation = occurance;
            this.QueryParser = new MultiFieldQueryParser(_luceneVersion, fields, analyzer);
            this.QueryParser.SetAllowLeadingWildcard(allowLeadingWildcards);
            this._occurance = occurance.ToLuceneOccurance();
        }
        /// <summary>
        /// verify parsing of query using a stopping analyzer  
        /// </summary>
        /// <param name="qtxt"></param>
        /// <param name="expectedRes"></param>
        private void AssertStopQueryEquals(string qtxt, string expectedRes)
        {
            string[] fields = { "b", "t" };
            BooleanClause.Occur[] occur = new BooleanClause.Occur[] { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
            TestQueryParser.QPTestAnalyzer a = new TestQueryParser.QPTestAnalyzer();
            MultiFieldQueryParser mfqp = new MultiFieldQueryParser(TEST_VERSION_CURRENT, fields, a);

            Query q = mfqp.Parse(qtxt);
            assertEquals(expectedRes, q.toString());

            q = MultiFieldQueryParser.Parse(TEST_VERSION_CURRENT, qtxt, fields, occur, a);
            assertEquals(expectedRes, q.toString());
        }
Exemplo n.º 3
0
 private void InitPendingClause()
 {
     _occur = BooleanClause.Occur.SHOULD;
     _exactMatch = false;
     _query = null;
     _boost = 0;
     _asFilter = false;
     _sort = String.Empty;
     _comparer = 0;
 }
Exemplo n.º 4
0
 public ISearchBuilder Mandatory()
 {
     _occur = BooleanClause.Occur.MUST;
     return this;
 }
Exemplo n.º 5
0
 public ISearchBuilder Forbidden()
 {
     _occur = BooleanClause.Occur.MUST_NOT;
     return this;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Gets the default child occur value
 /// </summary>
 /// <returns></returns>
 protected virtual BooleanClause.Occur GetDefaultChildrenOccur()
 {
     return defaultChildrenOccur ?? (defaultChildrenOccur = BooleanClause.Occur.MUST);
 }
Exemplo n.º 7
0
 /// <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/>
 /// Usage:
 /// <code>
 /// 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);
 /// </code>
 /// <p/>
 /// The code above would construct a query:
 /// 
 /// <code>
 /// (filename:query1) +(contents:query2) -(description:query3)
 /// </code>
 /// 
 /// </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, 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;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LuceneQuery"/> class.
 /// </summary>
 /// <param name="search">The search.</param>
 /// <param name="occurance">The occurance.</param>
 internal LuceneQuery(LuceneSearchCriteria search, BooleanClause.Occur occurance)
 {
     this.search = search;
     this.occurance = occurance;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Create a new FilterClause </summary>
 /// <param name="filter"> A Filter object containing a BitSet </param>
 /// <param name="occur"> A parameter implementation indicating SHOULD, MUST or MUST NOT </param>
 public FilterClause(Filter filter, BooleanClause.Occur occur)
 {
     this.occur = occur;
     this.filter = filter;
 }
Exemplo n.º 10
0
        /// <summary>
        /// 多Field,多条件,多Occur查询分析
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="queries"></param>
        /// <param name="flags"></param>
        /// <returns></returns>
        public static Query ParseMultiField(String[] fields, String[] queries, Occur[] flags)
        {
            if (fields == null)
            {
                throw new ArgumentException("parameter \"fields\" is null");
            }
            if (queries == null)
            {
                throw new ArgumentException("parameter \"queries\" is null");
            }
            if (flags == null)
            {
                throw new ArgumentException("parameter \"flags\" is null");
            }

            if (!(queries.Length == fields.Length && queries.Length == flags.Length))
            {
                throw new ArgumentException("queries, fields, and flags array have have different length");
            }

            BooleanQuery resultQuery = new BooleanQuery();
            for (int i = 0; i < fields.Length; i++)
            {
                if (fields[i] != null)
                {
                    Query partQuery = Parse(fields[i], queries[i]);
                    if (partQuery != null &&
                              (!(partQuery is BooleanQuery) || ((BooleanQuery)partQuery).GetClauses().Length > 0))
                    {
                        resultQuery.Add(partQuery, flags[i]);
                    }
                }
            }
            return resultQuery;
        }
Exemplo n.º 11
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, Occur occur)
 {
     return new BooleanClause(q, occur);
 }
Exemplo n.º 12
0
 /// <summary>
 /// Creates a new <seealso cref="CommonTermsQuery"/>
 /// </summary>
 /// <param name="highFreqOccur">
 ///          <seealso cref="BooleanClause.Occur"/> used for high frequency terms </param>
 /// <param name="lowFreqOccur">
 ///          <seealso cref="BooleanClause.Occur"/> used for low frequency terms </param>
 /// <param name="maxTermFrequency">
 ///          a value in [0..1) (or absolute number >=1) representing the
 ///          maximum threshold of a terms document frequency to be considered a
 ///          low frequency term. </param>
 /// <param name="disableCoord">
 ///          disables <seealso cref="Similarity#coord(int,int)"/> in scoring for the low
 ///          / high frequency sub-queries </param>
 /// <exception cref="ArgumentException">
 ///           if <seealso cref="BooleanClause.Occur#MUST_NOT"/> is pass as lowFreqOccur or
 ///           highFreqOccur </exception>
 public CommonTermsQuery(BooleanClause.Occur highFreqOccur, BooleanClause.Occur lowFreqOccur,
     float maxTermFrequency, bool disableCoord)
 {
     if (highFreqOccur == BooleanClause.Occur.MUST_NOT)
     {
         throw new System.ArgumentException("highFreqOccur should be MUST or SHOULD but was MUST_NOT");
     }
     if (lowFreqOccur == BooleanClause.Occur.MUST_NOT)
     {
         throw new System.ArgumentException("lowFreqOccur should be MUST or SHOULD but was MUST_NOT");
     }
     this.disableCoord = disableCoord;
     this.highFreqOccur = highFreqOccur;
     this.lowFreqOccur = lowFreqOccur;
     this.maxTermFrequency = maxTermFrequency;
     LowFreqMinimumNumberShouldMatch = 0;
     HighFreqMinimumNumberShouldMatch = 0;
 }
Exemplo n.º 13
0
        protected internal IBooleanOperation GroupedFlexibleInternal(string[] fields, BooleanOperation[] operations, IExamineValue[] fieldVals, BooleanClause.Occur occurance)
        {
            //if there's only 1 query text we want to build up a string like this:
            //(field1:query field2:query field3:query)
            //but Lucene will bork if you provide an array of length 1 (which is != to the field length)

            var flags = new BooleanClause.Occur[operations.Count()];
            for (int i = 0; i < flags.Length; i++)
                flags[i] = operations.ElementAt(i).ToLuceneOccurance();

            var queryVals = new IExamineValue[fields.Length];
            if (fieldVals.Length == 1)
            {
                for (int i = 0; i < queryVals.Length; i++)
                    queryVals[i] = fieldVals[0];
            }
            else
            {
                queryVals = fieldVals;
            }

            var qry = new BooleanQuery();
            for (int i = 0; i < fields.Length; i++)
            {
                var q = GetFieldInternalQuery(fields[i], queryVals[i], true);
                if (q != null)
                {
                    qry.Add(q, flags[i]);
                }                
            }

            this.Query.Add(qry, occurance);

            return new LuceneBooleanOperation(this);
        }
Exemplo n.º 14
0
 /// <summary>
 /// Create a new BooleanFilterClause
 /// </summary>
 /// <param name="filter">A Filter object</param>
 /// <param name="occur">A parameter implementation indicating SHOULD, MUST or MUST NOT</param>
 public BooleanFilterClause(Filter filter, BooleanClause.Occur occur)
 {
     this.Occur  = occur;
     this.Filter = filter;
 }
Exemplo n.º 15
0
 /// <summary>Adds a clause to a boolean query.
 ///
 /// </summary>
 /// <throws>  TooManyClauses if the new number of clauses exceeds the maximum clause number </throws>
 /// <seealso cref="GetMaxClauseCount()">
 /// </seealso>
 public virtual void  Add(Query query, BooleanClause.Occur occur)
 {
     Add(new BooleanClause(query, occur));
 }