Example #1
0
            public override Query Rewrite(IndexReader reader, MultiTermQuery query)
            {
                // Get the enum and start visiting terms.  If we
                // exhaust the enum before hitting either of the
                // cutoffs, we use ConstantBooleanQueryRewrite; else,
                // ConstantFilterRewrite:
                System.Collections.ArrayList pendingTerms = new System.Collections.ArrayList();
                int docCountCutoff = (int)((docCountPercent / 100.0) * reader.MaxDoc());
                int termCountLimit = System.Math.Min(BooleanQuery.GetMaxClauseCount(), termCountCutoff);
                int docVisitCount  = 0;

                FilteredTermEnum enumerator = query.GetEnum(reader);

                try
                {
                    while (true)
                    {
                        Term t = enumerator.Term();
                        if (t != null)
                        {
                            pendingTerms.Add(t);
                            // Loading the TermInfo from the terms dict here
                            // should not be costly, because 1) the
                            // query/filter will load the TermInfo when it
                            // runs, and 2) the terms dict has a cache:
                            docVisitCount += reader.DocFreq(t);
                        }

                        if (pendingTerms.Count >= termCountLimit || docVisitCount >= docCountCutoff)
                        {
                            // Too many terms -- make a filter.
                            Query result = new ConstantScoreQuery(new MultiTermQueryWrapperFilter(query));
                            result.SetBoost(query.GetBoost());
                            return(result);
                        }
                        else if (!enumerator.Next())
                        {
                            // Enumeration is done, and we hit a small
                            // enough number of terms & docs -- just make a
                            // BooleanQuery, now
                            System.Collections.IEnumerator it = pendingTerms.GetEnumerator();
                            BooleanQuery bq = new BooleanQuery(true);
                            while (it.MoveNext())
                            {
                                TermQuery tq = new TermQuery((Term)it.Current);
                                bq.Add(tq, BooleanClause.Occur.SHOULD);
                            }
                            // Strip scores
                            Query result = new ConstantScoreQuery(new QueryWrapperFilter(bq));
                            result.SetBoost(query.GetBoost());
                            query.IncTotalNumberOfTerms(pendingTerms.Count);
                            return(result);
                        }
                    }
                }
                finally
                {
                    enumerator.Close();
                }
            }
Example #2
0
            public override Query Rewrite(IndexReader reader, MultiTermQuery query)
            {
                FilteredTermEnum enumerator = query.GetEnum(reader);
                BooleanQuery     result     = new BooleanQuery(true);
                int count = 0;

                try
                {
                    do
                    {
                        Term t = enumerator.Term();
                        if (t != null)
                        {
                            TermQuery tq = new TermQuery(t);                             // found a match
                            tq.SetBoost(query.GetBoost() * enumerator.Difference());     // set the boost
                            result.Add(tq, BooleanClause.Occur.SHOULD);                  // add to query
                            count++;
                        }
                    }while (enumerator.Next());
                }
                finally
                {
                    enumerator.Close();
                }
                query.IncTotalNumberOfTerms(count);
                return(result);
            }
        private void testEnum(int lower, int upper)
        {
            NumericRangeQuery <int> q        = NumericRangeQuery.NewIntRange("field4", 4, lower, upper, true, true);
            FilteredTermEnum        termEnum = q.GetEnum(searcher.IndexReader, null);

            try
            {
                int count = 0;
                do
                {
                    Term t = termEnum.Term;
                    if (t != null)
                    {
                        int val = NumericUtils.PrefixCodedToInt(t.Text);
                        Assert.True(val >= lower && val <= upper, "value not in bounds");
                        count++;
                    }
                    else
                    {
                        break;
                    }
                } while (termEnum.Next(null));
                Assert.False(termEnum.Next(null));
                Console.WriteLine("TermEnum on 'field4' for range [" + lower + "," + upper + "] contained " + count +
                                  " terms.");
            }
            finally
            {
                termEnum.Close();
            }
        }
Example #4
0
        public override Query Rewrite(IndexReader reader)
        {
            FilteredTermEnum enumerator   = GetEnum(reader);
            int            maxClauseCount = BooleanQuery.GetMaxClauseCount();
            ScoreTermQueue stQueue        = new ScoreTermQueue(maxClauseCount);
            ScoreTerm      reusableST     = null;

            try
            {
                do
                {
                    float score = 0.0f;
                    Term  t     = enumerator.Term();
                    if (t != null)
                    {
                        score = enumerator.Difference();
                        if (reusableST == null)
                        {
                            reusableST = new ScoreTerm(t, score);
                        }
                        else if (score >= reusableST.score)
                        {
                            // reusableST holds the last "rejected" entry, so, if
                            // this new score is not better than that, there's no
                            // need to try inserting it
                            reusableST.score = score;
                            reusableST.term  = t;
                        }
                        else
                        {
                            continue;
                        }

                        reusableST = (ScoreTerm)stQueue.InsertWithOverflow(reusableST);
                    }
                }while (enumerator.Next());
            }
            finally
            {
                enumerator.Close();
            }

            BooleanQuery query = new BooleanQuery(true);
            int          size  = stQueue.Size();

            for (int i = 0; i < size; i++)
            {
                ScoreTerm st = (ScoreTerm)stQueue.Pop();
                TermQuery tq = new TermQuery(st.term);              // found a match
                tq.SetBoost(GetBoost() * st.score);                 // set the boost
                query.Add(tq, BooleanClause.Occur.SHOULD);          // add to query
            }

            return(query);
        }
Example #5
0
        public override Query Rewrite(IndexReader reader)
        {
            FilteredTermEnum enumerator   = GetEnum(reader);
            int            maxClauseCount = BooleanQuery.GetMaxClauseCount();
            ScoreTermQueue stQueue        = new ScoreTermQueue(maxClauseCount);

            try
            {
                do
                {
                    float minScore = 0.0f;
                    float score    = 0.0f;
                    Term  t        = enumerator.Term();
                    if (t != null)
                    {
                        score = enumerator.Difference();
                        // terms come in alphabetical order, therefore if queue is full and score
                        // not bigger than minScore, we can skip
                        if (stQueue.Size() < maxClauseCount || score > minScore)
                        {
                            stQueue.Insert(new ScoreTerm(t, score));
                            minScore = ((ScoreTerm)stQueue.Top()).score;                              // maintain minScore
                        }
                    }
                }while (enumerator.Next());
            }
            finally
            {
                enumerator.Close();
            }

            BooleanQuery query = new BooleanQuery(true);
            int          size  = stQueue.Size();

            for (int i = 0; i < size; i++)
            {
                ScoreTerm st = (ScoreTerm)stQueue.Pop();
                TermQuery tq = new TermQuery(st.term);              // found a match
                tq.SetBoost(GetBoost() * st.score);                 // set the boost
                query.Add(tq, BooleanClause.Occur.SHOULD);          // add to query
            }

            return(query);
        }
        public override Query Rewrite(IndexReader reader)
        {
            FilteredTermEnum enumerator = GetEnum(reader);
            BooleanQuery     query      = new BooleanQuery(true);

            try
            {
                do
                {
                    Term t = enumerator.Term();
                    if (t != null)
                    {
                        TermQuery tq = new TermQuery(t);                         // found a match
                        tq.SetBoost(GetBoost() * enumerator.Difference());       // set the boost
                        query.Add(tq, BooleanClause.Occur.SHOULD);               // add to query
                    }
                }while (enumerator.Next());
            }
            finally
            {
                enumerator.Close();
            }
            return(query);
        }
Example #7
0
        public override Query Rewrite(IndexReader reader, IState state)
        {
            if (!termLongEnough)
            {
                // can only match if it's exact
                return(new TermQuery(Term));
            }

            int maxSize = BooleanQuery.MaxClauseCount;

            // TODO: Java uses a PriorityQueue.  Using Linq, we can emulate it,
            //       however it's considerable slower than the java counterpart.
            //       this should be a temporary thing, fixed before release
            SortedList <ScoreTerm, ScoreTerm> stQueue = new SortedList <ScoreTerm, ScoreTerm>();
            FilteredTermEnum enumerator = GetEnum(reader, state);

            try
            {
                ScoreTerm st = new ScoreTerm();
                do
                {
                    Term t = enumerator.Term;
                    if (t == null)
                    {
                        break;
                    }
                    float score = enumerator.Difference();
                    //ignore uncompetetive hits
                    if (stQueue.Count >= maxSize && score <= stQueue.Keys.First().score)
                    {
                        continue;
                    }
                    // add new entry in PQ
                    st.term  = t;
                    st.score = score;
                    stQueue.Add(st, st);
                    // possibly drop entries from queue
                    if (stQueue.Count > maxSize)
                    {
                        st = stQueue.Keys.First();
                        stQueue.Remove(st);
                    }
                    else
                    {
                        st = new ScoreTerm();
                    }
                }while (enumerator.Next(state));
            }
            finally
            {
                enumerator.Close();
            }

            BooleanQuery query = new BooleanQuery(true);

            foreach (ScoreTerm st in stQueue.Keys)
            {
                TermQuery tq = new TermQuery(st.term);       // found a match
                tq.Boost = Boost * st.score;                 // set the boost
                query.Add(tq, Occur.SHOULD);                 // add to query
            }

            return(query);
        }