Exemplo n.º 1
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
            }
        }
Exemplo n.º 2
0
        /// <summary>Optimize our representation and our subqueries representations</summary>
        /// <param name="reader">the IndexReader we query
        /// </param>
        /// <returns> an optimized copy of us (which may not be a copy if there is nothing to optimize)
        /// </returns>
        public override Query Rewrite(IndexReader reader)
        {
            int numDisjunctions = disjuncts.Count;

            if (numDisjunctions == 1)
            {
                Query singleton = (Query)disjuncts[0];
                Query result    = singleton.Rewrite(reader);
                if (GetBoost() != 1.0f)
                {
                    if (result == singleton)
                    {
                        result = (Query)result.Clone();
                    }
                    result.SetBoost(GetBoost() * result.GetBoost());
                }
                return(result);
            }
            DisjunctionMaxQuery clone = null;

            for (int i = 0; i < numDisjunctions; i++)
            {
                Query clause  = (Query)disjuncts[i];
                Query rewrite = clause.Rewrite(reader);
                if (rewrite != clause)
                {
                    if (clone == null)
                    {
                        clone = (DisjunctionMaxQuery)this.Clone();
                    }
                    clone.disjuncts[i] = rewrite;
                }
            }
            if (clone != null)
            {
                return(clone);
            }
            else
            {
                return(this);
            }
        }