コード例 #1
0
        public override Weight CreateWeight(IndexSearcher searcher)
        {
            IndexReaderContext context = searcher.TopReaderContext;
            TermContext        termState;

            if (perReaderTermState == null || perReaderTermState.TopReaderContext != context)
            {
                // make TermQuery single-pass if we don't have a PRTS or if the context differs!
                termState = TermContext.Build(context, term);
            }
            else
            {
                // PRTS was pre-build for this IS
                termState = this.perReaderTermState;
            }

            // we must not ignore the given docFreq - if set use the given value (lie)
            if (docFreq != -1)
            {
                termState.DocFreq = docFreq;
            }

            return(new TermWeight(this, searcher, termState));
        }
コード例 #2
0
        // Mock: in a real env, this would hit the wire and get
        // term stats from remote node
        internal virtual IDictionary <Term, TermStatistics> GetNodeTermStats(ISet <Term> terms, int nodeID, long version)
        {
            NodeState node = m_nodes[nodeID];
            IDictionary <Term, TermStatistics> stats = new Dictionary <Term, TermStatistics>();
            IndexSearcher s = node.Searchers.Acquire(version);

            if (s == null)
            {
                throw new SearcherExpiredException("node=" + nodeID + " version=" + version);
            }
            try
            {
                foreach (Term term in terms)
                {
                    TermContext termContext = TermContext.Build(s.IndexReader.Context, term);
                    stats[term] = s.TermStatistics(term, termContext);
                }
            }
            finally
            {
                node.Searchers.Release(s);
            }
            return(stats);
        }
コード例 #3
0
            internal SlowMinShouldMatchScorer(BooleanWeight weight, AtomicReader reader, IndexSearcher searcher)
                : base(weight)
            {
                this.dv     = reader.GetSortedSetDocValues("dv");
                this.maxDoc = reader.MaxDoc;
                BooleanQuery bq = (BooleanQuery)weight.Query;

                this.minNrShouldMatch = bq.MinimumNumberShouldMatch;
                this.sims             = new SimScorer[(int)dv.ValueCount];
                foreach (BooleanClause clause in bq.GetClauses())
                {
                    if (Debugging.AssertsEnabled)
                    {
                        Debugging.Assert(!clause.IsProhibited);
                    }
                    if (Debugging.AssertsEnabled)
                    {
                        Debugging.Assert(!clause.IsRequired);
                    }
                    Term term = ((TermQuery)clause.Query).Term;
                    long ord  = dv.LookupTerm(term.Bytes);
                    if (ord >= 0)
                    {
                        bool success = ords.Add(ord);
                        if (Debugging.AssertsEnabled)
                        {
                            Debugging.Assert(success);                           // no dups
                        }
                        TermContext context = TermContext.Build(reader.Context, term);
                        SimWeight   w       = weight.Similarity.ComputeWeight(1f, searcher.CollectionStatistics("field"), searcher.TermStatistics(term, context));
                        var         dummy   = w.GetValueForNormalization(); // ignored
                        w.Normalize(1F, 1F);
                        sims[(int)ord] = weight.Similarity.GetSimScorer(w, (AtomicReaderContext)reader.Context);
                    }
                }
            }
コード例 #4
0
        /// <summary>
        /// Fills a <see cref="T:IDictionary{string, WeightedSpanTerm}"/> with <see cref="WeightedSpanTerm"/>s using the terms from the supplied <see cref="SpanQuery"/>.
        /// </summary>
        /// <param name="terms"><see cref="T:IDictionary{string, WeightedSpanTerm}"/> to place created <see cref="WeightedSpanTerm"/>s in</param>
        /// <param name="spanQuery"><see cref="SpanQuery"/> to extract Terms from</param>
        /// <exception cref="IOException">If there is a low-level I/O error</exception>
        protected virtual void ExtractWeightedSpanTerms(IDictionary <string, WeightedSpanTerm> terms, SpanQuery spanQuery)
        {
            ISet <string> fieldNames;

            if (fieldName == null)
            {
                fieldNames = new JCG.HashSet <string>();
                CollectSpanQueryFields(spanQuery, fieldNames);
            }
            else
            {
                fieldNames = new JCG.HashSet <string>
                {
                    fieldName
                };
            }
            // To support the use of the default field name
            if (defaultField != null)
            {
                fieldNames.Add(defaultField);
            }

            IDictionary <string, SpanQuery> queries = new JCG.Dictionary <string, SpanQuery>();

            var  nonWeightedTerms = new JCG.HashSet <Term>();
            bool mustRewriteQuery = MustRewriteQuery(spanQuery);

            if (mustRewriteQuery)
            {
                foreach (string field in fieldNames)
                {
                    SpanQuery rewrittenQuery = (SpanQuery)spanQuery.Rewrite(GetLeafContext().Reader);
                    queries[field] = rewrittenQuery;
                    rewrittenQuery.ExtractTerms(nonWeightedTerms);
                }
            }
            else
            {
                spanQuery.ExtractTerms(nonWeightedTerms);
            }

            List <PositionSpan> spanPositions = new List <PositionSpan>();

            foreach (string field in fieldNames)
            {
                SpanQuery q;
                q = mustRewriteQuery ? queries[field] : spanQuery;

                AtomicReaderContext context = GetLeafContext();
                var         termContexts    = new JCG.Dictionary <Term, TermContext>();
                ISet <Term> extractedTerms  = new JCG.SortedSet <Term>();
                q.ExtractTerms(extractedTerms);
                foreach (Term term in extractedTerms)
                {
                    termContexts[term] = TermContext.Build(context, term);
                }
                IBits       acceptDocs = context.AtomicReader.LiveDocs;
                Spans.Spans spans      = q.GetSpans(context, acceptDocs, termContexts);

                // collect span positions
                while (spans.MoveNext())
                {
                    spanPositions.Add(new PositionSpan(spans.Start, spans.End - 1));
                }
            }

            if (spanPositions.Count == 0)
            {
                // no spans found
                return;
            }

            foreach (Term queryTerm in nonWeightedTerms)
            {
                if (FieldNameComparer(queryTerm.Field))
                {
                    if (!terms.TryGetValue(queryTerm.Text, out WeightedSpanTerm weightedSpanTerm) || weightedSpanTerm == null)
                    {
                        weightedSpanTerm = new WeightedSpanTerm(spanQuery.Boost, queryTerm.Text);
                        weightedSpanTerm.AddPositionSpans(spanPositions);
                        weightedSpanTerm.IsPositionSensitive = true;
                        terms[queryTerm.Text] = weightedSpanTerm;
                    }
                    else
                    {
                        if (spanPositions.Count > 0)
                        {
                            weightedSpanTerm.AddPositionSpans(spanPositions);
                        }
                    }
                }
            }
        }