Example #1
0
        /// <summary>Prints a user-readable version of this query. </summary>
        public override System.String ToString(System.String field)
        {
            System.Text.StringBuilder buffer = new System.Text.StringBuilder();
            bool needParens = (GetBoost() != 1.0) || (GetMinimumNumberShouldMatch() > 0);

            if (needParens)
            {
                buffer.Append("(");
            }

            for (int i = 0; i < clauses.Count; i++)
            {
                BooleanClause c = (BooleanClause)clauses[i];
                if (c.IsProhibited())
                {
                    buffer.Append("-");
                }
                else if (c.IsRequired())
                {
                    buffer.Append("+");
                }

                Query subQuery = c.GetQuery();
                if (subQuery is BooleanQuery)
                {
                    // wrap sub-bools in parens
                    buffer.Append("(");
                    buffer.Append(c.GetQuery().ToString(field));
                    buffer.Append(")");
                }
                else
                {
                    buffer.Append(c.GetQuery().ToString(field));
                }

                if (i != clauses.Count - 1)
                {
                    buffer.Append(" ");
                }
            }

            if (needParens)
            {
                buffer.Append(")");
            }

            if (GetMinimumNumberShouldMatch() > 0)
            {
                buffer.Append('~');
                buffer.Append(GetMinimumNumberShouldMatch());
            }

            if (GetBoost() != 1.0f)
            {
                buffer.Append(ToStringUtils.Boost(GetBoost()));
            }

            return(buffer.ToString());
        }
Example #2
0
        public override Query Rewrite(IndexReader reader)
        {
            if (minNrShouldMatch == 0 && clauses.Count == 1)
            {
                // optimize 1-clause queries
                BooleanClause c = (BooleanClause)clauses[0];
                if (!c.IsProhibited())
                {
                    // just return clause

                    Query query = c.GetQuery().Rewrite(reader);                     // rewrite first

                    if (GetBoost() != 1.0f)
                    {
                        // incorporate boost
                        if (query == c.GetQuery())
                        {
                            // if rewrite was no-op
                            query = (Query)query.Clone();                              // then clone before boost
                        }
                        query.SetBoost(GetBoost() * query.GetBoost());
                    }

                    return(query);
                }
            }

            BooleanQuery clone = null;             // recursively rewrite

            for (int i = 0; i < clauses.Count; i++)
            {
                BooleanClause c     = (BooleanClause)clauses[i];
                Query         query = c.GetQuery().Rewrite(reader);
                if (query != c.GetQuery())
                {
                    // clause rewrote: must clone
                    if (clone == null)
                    {
                        clone = (BooleanQuery)this.Clone();
                    }
                    clone.clauses[i] = new BooleanClause(query, c.GetOccur());
                }
            }
            if (clone != null)
            {
                return(clone);                // some clauses rewrote
            }
            else
            {
                return(this);                // no clauses rewrote
            }
        }
Example #3
0
 // inherit javadoc
 public override void  ExtractTerms(System.Collections.Hashtable terms)
 {
     for (System.Collections.IEnumerator i = clauses.GetEnumerator(); i.MoveNext();)
     {
         BooleanClause clause = (BooleanClause)i.Current;
         clause.GetQuery().ExtractTerms(terms);
     }
 }
Example #4
0
 public BooleanWeight(BooleanQuery enclosingInstance, Searcher searcher)
 {
     InitBlock(enclosingInstance);
     this.similarity = Enclosing_Instance.GetSimilarity(searcher);
     for (int i = 0; i < Enclosing_Instance.clauses.Count; i++)
     {
         BooleanClause c = (BooleanClause)Enclosing_Instance.clauses[i];
         weights.Add(c.GetQuery().CreateWeight(searcher));
     }
 }
Example #5
0
            public override Explanation Explain(IndexReader reader, int doc)
            {
                int minShouldMatch         = Enclosing_Instance.GetMinimumNumberShouldMatch();
                ComplexExplanation sumExpl = new ComplexExplanation();

                sumExpl.SetDescription("sum of:");
                int   coord            = 0;
                int   maxCoord         = 0;
                float sum              = 0.0f;
                bool  fail             = false;
                int   shouldMatchCount = 0;

                for (System.Collections.IEnumerator wIter = weights.GetEnumerator(), cIter = Enclosing_Instance.clauses.GetEnumerator(); wIter.MoveNext();)
                {
                    cIter.MoveNext();

                    Weight        w = (Weight)wIter.Current;
                    BooleanClause c = (BooleanClause)cIter.Current;
                    if (w.Scorer(reader, true, true) == null)
                    {
                        continue;
                    }
                    Explanation e = w.Explain(reader, doc);
                    if (!c.IsProhibited())
                    {
                        maxCoord++;
                    }
                    if (e.IsMatch())
                    {
                        if (!c.IsProhibited())
                        {
                            sumExpl.AddDetail(e);
                            sum += e.GetValue();
                            coord++;
                        }
                        else
                        {
                            Explanation r = new Explanation(0.0f, "match on prohibited clause (" + c.GetQuery().ToString() + ")");
                            r.AddDetail(e);
                            sumExpl.AddDetail(r);
                            fail = true;
                        }
                        if (c.GetOccur() == Occur.SHOULD)
                        {
                            shouldMatchCount++;
                        }
                    }
                    else if (c.IsRequired())
                    {
                        Explanation r = new Explanation(0.0f, "no match on required clause (" + c.GetQuery().ToString() + ")");
                        r.AddDetail(e);
                        sumExpl.AddDetail(r);
                        fail = true;
                    }
                }
                if (fail)
                {
                    System.Boolean tempAux = false;
                    sumExpl.SetMatch(tempAux);
                    sumExpl.SetValue(0.0f);
                    sumExpl.SetDescription("Failure to meet condition(s) of required/prohibited clause(s)");
                    return(sumExpl);
                }
                else if (shouldMatchCount < minShouldMatch)
                {
                    System.Boolean tempAux2 = false;
                    sumExpl.SetMatch(tempAux2);
                    sumExpl.SetValue(0.0f);
                    sumExpl.SetDescription("Failure to match minimum number " + "of optional clauses: " + minShouldMatch);
                    return(sumExpl);
                }

                sumExpl.SetMatch(0 < coord?true:false);
                sumExpl.SetValue(sum);

                float coordFactor = similarity.Coord(coord, maxCoord);

                if (coordFactor == 1.0f)
                {
                    // coord is no-op
                    return(sumExpl);
                }
                // eliminate wrapper
                else
                {
                    ComplexExplanation result = new ComplexExplanation(sumExpl.IsMatch(), sum * coordFactor, "product of:");
                    result.AddDetail(sumExpl);
                    result.AddDetail(new Explanation(coordFactor, "coord(" + coord + "/" + maxCoord + ")"));
                    return(result);
                }
            }
Example #6
0
            /// <returns> A good old 1.4 Scorer
            /// </returns>
            public virtual Scorer Scorer(IndexReader reader)
            {
                // First see if the (faster) ConjunctionScorer will work.  This can be
                // used when all clauses are required.  Also, at this point a
                // BooleanScorer cannot be embedded in a ConjunctionScorer, as the hits
                // from a BooleanScorer are not always sorted by document number (sigh)
                // and hence BooleanScorer cannot implement skipTo() correctly, which is
                // required by ConjunctionScorer.
                bool allRequired = true;
                bool noneBoolean = true;

                for (int i = 0; i < weights.Count; i++)
                {
                    BooleanClause c = (BooleanClause)Enclosing_Instance.clauses[i];
                    if (!c.IsRequired())
                    {
                        allRequired = false;
                    }
                    if (c.GetQuery() is BooleanQuery)
                    {
                        noneBoolean = false;
                    }
                }

                if (allRequired && noneBoolean)
                {
                    // ConjunctionScorer is okay
                    ConjunctionScorer result = new ConjunctionScorer(similarity);
                    for (int i = 0; i < weights.Count; i++)
                    {
                        Weight w         = (Weight)weights[i];
                        Scorer subScorer = w.Scorer(reader);
                        if (subScorer == null)
                        {
                            return(null);
                        }
                        result.Add(subScorer);
                    }
                    return(result);
                }

                // Use good-old BooleanScorer instead.
                BooleanScorer result2 = new BooleanScorer(similarity);

                for (int i = 0; i < weights.Count; i++)
                {
                    BooleanClause c         = (BooleanClause)Enclosing_Instance.clauses[i];
                    Weight        w         = (Weight)weights[i];
                    Scorer        subScorer = w.Scorer(reader);
                    if (subScorer != null)
                    {
                        result2.Add(subScorer, c.IsRequired(), c.IsProhibited());
                    }
                    else if (c.IsRequired())
                    {
                        return(null);
                    }
                }

                return(result2);
            }
Example #7
0
 public override BooleanClause VisitBooleanClause(BooleanClause clause)
 {
     var occur = clause.GetOccur();
     var query = clause.GetQuery();
     var visited = Visit(query);
     if (query == visited)
         return clause;
     if (visited == null)
         return null;
     return new BooleanClause(visited, occur);
 }
Example #8
0
 // </V2.9.2>
 //--
 public virtual BooleanClause VisitBooleanClause(BooleanClause clause)
 {
     var occur = clause.GetOccur();
     var query = clause.GetQuery();
     var visited = Visit(query);
     if (query == visited)
         return clause;
     return new BooleanClause(visited, occur);
 }