Esempio n. 1
0
 public TermWeight(TermQuery outerInstance, IndexSearcher searcher, TermContext termStates)
 {
     this.OuterInstance = outerInstance;
     Debug.Assert(termStates != null, "TermContext must not be null");
     this.TermStates = termStates;
     this.Similarity = searcher.Similarity;
     this.Stats = Similarity.ComputeWeight(outerInstance.Boost, searcher.CollectionStatistics(outerInstance.Term_Renamed.Field()), searcher.TermStatistics(outerInstance.Term_Renamed, termStates));
 }
Esempio n. 2
0
        public override Query Rewrite(IndexReader reader)
        {
            if (this.m_terms.Count == 0)
            {
                return(new BooleanQuery());
            }
            else if (this.m_terms.Count == 1)
            {
                Query tq = NewTermQuery(this.m_terms[0], null);
                tq.Boost = Boost;
                return(tq);
            }
            var leaves       = reader.Leaves;
            int maxDoc       = reader.MaxDoc;
            var contextArray = new TermContext[m_terms.Count];
            var queryTerms   = this.m_terms.ToArray();

            CollectTermContext(reader, leaves, contextArray, queryTerms);
            return(BuildQuery(maxDoc, contextArray, queryTerms));
        }
Esempio n. 3
0
            public MultiPhraseWeight(MultiPhraseQuery outerInstance, IndexSearcher searcher)
            {
                this.outerInstance = outerInstance;
                this.similarity    = searcher.Similarity;
                IndexReaderContext context = searcher.TopReaderContext;

                // compute idf
                var allTermStats = new JCG.List <TermStatistics>();

                foreach (Term[] terms in outerInstance.termArrays)
                {
                    foreach (Term term in terms)
                    {
                        if (!termContexts.TryGetValue(term, out TermContext termContext) || termContext is null)
                        {
                            termContext        = TermContext.Build(context, term);
                            termContexts[term] = termContext;
                        }
                        allTermStats.Add(searcher.TermStatistics(term, termContext));
                    }
                }
                stats = similarity.ComputeWeight(outerInstance.Boost, searcher.CollectionStatistics(outerInstance.field), allTermStats.ToArray());
            }
Esempio n. 4
0
 public static void Initialize(IServiceProvider serviceProvider)
 {
     using (var context = new TermContext(serviceProvider.GetRequiredService <DbContextOptions <TermContext> >()))
     {
         // Look for any movies.
         if (context.Term.Any())
         {
             return;   // DB has been seeded
         }
         context.Term.AddRange(
             new Term
         {
             TermID      = 1,
             Terms       = "Physics",
             Subject     = "General Information",
             Definitions = "Physics is the branch of science concerned with the nature and properties of matter and energy",
             Questions   = "What is Physics?",
             Answers     = "Physics is the branch of science concerned with the nature and properties of matter and energy"
         }
             );
         context.SaveChanges();
     }
 }
Esempio n. 5
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 is 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);
        }
Esempio n. 6
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));
        }
            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);
                    }
                }
            }
		public IdentifierContext(TermContext context) { CopyFrom(context); }
Esempio n. 9
0
 public FalseContext(TermContext context)
 {
     CopyFrom(context);
 }
Esempio n. 10
0
 public IndexModel(TermContext context)
 {
     _context = context;
 }
Esempio n. 11
0
 /// <summary>
 /// Returns <see cref="Search.TermStatistics"/> for a term.
 /// <para/>
 /// This can be overridden for example, to return a term's statistics
 /// across a distributed collection.
 /// <para/>
 /// @lucene.experimental
 /// </summary>
 public virtual TermStatistics TermStatistics(Term term, TermContext context)
 {
     return(new TermStatistics(term.Bytes, context.DocFreq, context.TotalTermFreq));
 }
Esempio n. 12
0
 public ScoreTerm(IComparer <BytesRef> termComp, TermContext termState)
 {
     this.TermComp  = termComp;
     this.TermState = termState;
     this.Bytes     = new BytesRef();
 }
Esempio n. 13
0
 /// <summary>
 /// Expert: constructs a TermQuery that will use the
 ///  provided docFreq instead of looking up the docFreq
 ///  against the searcher.
 /// </summary>
 public TermQuery(Term t, TermContext states)
 {
     Debug.Assert(states != null);
     Term_Renamed = t;
     DocFreq = states.DocFreq;
     PerReaderTermState = states;
 }
Esempio n. 14
0
 public AcronymController(TermContext db)
 {
     _db = db;
 }
Esempio n. 15
0
	public TermContext term() {
		TermContext _localctx = new TermContext(Context, State);
		EnterRule(_localctx, 122, RULE_term);
		int _la;
		try {
			EnterOuterAlt(_localctx, 1);
			{
			State = 792; factor();
			State = 805;
			ErrorHandler.Sync(this);
			_la = TokenStream.La(1);
			while (((((_la - 46)) & ~0x3f) == 0 && ((1L << (_la - 46)) & ((1L << (STAR - 46)) | (1L << (DIV - 46)) | (1L << (MOD - 46)) | (1L << (IDIV - 46)) | (1L << (AT - 46)))) != 0)) {
				{
				State = 803;
				switch (TokenStream.La(1)) {
				case STAR:
					{
					State = 793; Match(STAR);
					State = 794; factor();
					}
					break;
				case DIV:
					{
					State = 795; Match(DIV);
					State = 796; factor();
					}
					break;
				case MOD:
					{
					State = 797; Match(MOD);
					State = 798; factor();
					}
					break;
				case IDIV:
					{
					State = 799; Match(IDIV);
					State = 800; factor();
					}
					break;
				case AT:
					{
					State = 801; Match(AT);
					State = 802; factor();
					}
					break;
				default:
					throw new NoViableAltException(this);
				}
				}
				State = 807;
				ErrorHandler.Sync(this);
				_la = TokenStream.La(1);
			}
			}
		}
		catch (RecognitionException re) {
			_localctx.exception = re;
			ErrorHandler.ReportError(this, re);
			ErrorHandler.Recover(this, re);
		}
		finally {
			ExitRule();
		}
		return _localctx;
	}
 public TermController(TermContext db)
 {
     _db = db;
 }
	public TermContext term() {
		TermContext _localctx = new TermContext(Context, State);
		EnterRule(_localctx, 0, RULE_term);
		try {
			State = 35;
			switch ( Interpreter.AdaptivePredict(TokenStream,0,Context) ) {
			case 1:
				EnterOuterAlt(_localctx, 1);
				{
				State = 18; _localctx._INT = Match(INT);
				 _localctx.value =  PumpingLemma.LinearIntegerExpression.Constant((_localctx._INT!=null?int.Parse(_localctx._INT.Text):0)); 
				}
				break;
			case 2:
				EnterOuterAlt(_localctx, 2);
				{
				State = 20; _localctx._IDENT = Match(IDENT);
				 
						_localctx.value =  PumpingLemma.LinearIntegerExpression.Variable((_localctx._IDENT!=null?_localctx._IDENT.Text:null));
						
				}
				break;
			case 3:
				EnterOuterAlt(_localctx, 3);
				{
				State = 22; _localctx._INT = Match(INT);
				State = 23; Match(TIMES);
				State = 24; _localctx._IDENT = Match(IDENT);
				 
						_localctx.value =  PumpingLemma.LinearIntegerExpression.SingleTerm(
							(_localctx._INT!=null?int.Parse(_localctx._INT.Text):0),
							PumpingLemma.VariableType.Variable((_localctx._IDENT!=null?_localctx._IDENT.Text:null))
							);
						
				}
				break;
			case 4:
				EnterOuterAlt(_localctx, 4);
				{
				State = 26; _localctx._IDENT = Match(IDENT);
				State = 27; Match(TIMES);
				State = 28; _localctx._INT = Match(INT);
				 
						_localctx.value =  PumpingLemma.LinearIntegerExpression.SingleTerm(
							(_localctx._INT!=null?int.Parse(_localctx._INT.Text):0),
							PumpingLemma.VariableType.Variable((_localctx._IDENT!=null?_localctx._IDENT.Text:null))
							);
						
				}
				break;
			case 5:
				EnterOuterAlt(_localctx, 5);
				{
				State = 30; Match(LPAREN);
				State = 31; _localctx.a = terms();
				State = 32; Match(RPAREN);
				 _localctx.value =  _localctx.a.value; 
				}
				break;
			}
		}
		catch (RecognitionException re) {
			_localctx.exception = re;
			ErrorHandler.ReportError(this, re);
			ErrorHandler.Recover(this, re);
		}
		finally {
			ExitRule();
		}
		return _localctx;
	}
Esempio n. 18
0
    public TermContext term()
    {
        TermContext _localctx = new TermContext(Context, State);

        EnterRule(_localctx, 4, RULE_term);
        int _la;

        try {
            State = 42;
            ErrorHandler.Sync(this);
            switch (Interpreter.AdaptivePredict(TokenStream, 5, Context))
            {
            case 1:
                EnterOuterAlt(_localctx, 1);
                {
                    State = 26; Match(IDENTIFIER);
                }
                break;

            case 2:
                EnterOuterAlt(_localctx, 2);
                {
                    State = 27; Match(INTEGER);
                }
                break;

            case 3:
                EnterOuterAlt(_localctx, 3);
                {
                    State = 28; predicate_atom();
                }
                break;

            case 4:
                EnterOuterAlt(_localctx, 4);
                {
                    State = 29; Match(T__3);
                    State = 38;
                    ErrorHandler.Sync(this);
                    _la = TokenStream.LA(1);
                    if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__3) | (1L << IDENTIFIER) | (1L << INTEGER) | (1L << STRING))) != 0))
                    {
                        {
                            State = 30; term();
                            State = 35;
                            ErrorHandler.Sync(this);
                            _la = TokenStream.LA(1);
                            while (_la == T__1)
                            {
                                {
                                    {
                                        State = 31; Match(T__1);
                                        State = 32; term();
                                    }
                                }
                                State = 37;
                                ErrorHandler.Sync(this);
                                _la = TokenStream.LA(1);
                            }
                        }
                    }

                    State = 40; Match(T__4);
                }
                break;

            case 5:
                EnterOuterAlt(_localctx, 5);
                {
                    State = 41; Match(STRING);
                }
                break;
            }
        }
        catch (RecognitionException re) {
            _localctx.exception = re;
            ErrorHandler.ReportError(this, re);
            ErrorHandler.Recover(this, re);
        }
        finally {
            ExitRule();
        }
        return(_localctx);
    }
Esempio n. 19
0
    private TermContext term(int _p)
    {
        ParserRuleContext _parentctx = Context;
        int         _parentState     = State;
        TermContext _localctx        = new TermContext(Context, _parentState);
        TermContext _prevctx         = _localctx;
        int         _startState      = 6;

        EnterRecursionRule(_localctx, 6, RULE_term, _p);
        try {
            int _alt;
            EnterOuterAlt(_localctx, 1);
            {
                State = 53;
                ErrorHandler.Sync(this);
                switch (TokenStream.LA(1))
                {
                case T__1:
                {
                    _localctx = new ParContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;

                    State = 25; Match(T__1);
                    State = 26; term(0);
                    State = 27; Match(T__2);
                }
                break;

                case VAR:
                {
                    _localctx = new VarContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;
                    State     = 29; Match(VAR);
                }
                break;

                case T__3:
                {
                    _localctx = new IftContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;
                    State     = 30; Match(T__3);
                    State     = 31; term(0);
                    State     = 32; Match(T__4);
                    State     = 33; term(0);
                    State     = 34; Match(T__5);
                    State     = 35; term(5);
                }
                break;

                case T__6:
                {
                    _localctx = new TrueContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;
                    State     = 37; Match(T__6);
                }
                break;

                case T__7:
                {
                    _localctx = new FalseContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;
                    State     = 38; Match(T__7);
                }
                break;

                case T__8:
                {
                    _localctx = new LetContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;
                    State     = 39; Match(T__8);
                    State     = 40; Match(VAR);
                    State     = 41; Match(T__9);
                    State     = 42; term(0);
                    State     = 43; Match(T__10);
                    State     = 44; term(2);
                }
                break;

                case LAMBDA:
                {
                    _localctx = new AbsContext(_localctx);
                    Context   = _localctx;
                    _prevctx  = _localctx;
                    State     = 46; Match(LAMBDA);
                    State     = 47; Match(VAR);
                    State     = 48; Match(T__11);
                    State     = 49; type(0);
                    State     = 50; Match(DOT);
                    State     = 51; term(1);
                }
                break;

                default:
                    throw new NoViableAltException(this);
                }
                Context.Stop = TokenStream.LT(-1);
                State        = 59;
                ErrorHandler.Sync(this);
                _alt = Interpreter.AdaptivePredict(TokenStream, 3, Context);
                while (_alt != 2 && _alt != global::Antlr4.Runtime.Atn.ATN.INVALID_ALT_NUMBER)
                {
                    if (_alt == 1)
                    {
                        if (ParseListeners != null)
                        {
                            TriggerExitRuleEvent();
                        }
                        _prevctx = _localctx;
                        {
                            {
                                _localctx = new AppContext(new TermContext(_parentctx, _parentState));
                                PushNewRecursionContext(_localctx, _startState, RULE_term);
                                State = 55;
                                if (!(Precpred(Context, 6)))
                                {
                                    throw new FailedPredicateException(this, "Precpred(Context, 6)");
                                }
                                State = 56; term(7);
                            }
                        }
                    }
                    State = 61;
                    ErrorHandler.Sync(this);
                    _alt = Interpreter.AdaptivePredict(TokenStream, 3, Context);
                }
            }
        }
        catch (RecognitionException re) {
            _localctx.exception = re;
            ErrorHandler.ReportError(this, re);
            ErrorHandler.Recover(this, re);
        }
        finally {
            UnrollRecursionContexts(_parentctx);
        }
        return(_localctx);
    }
Esempio n. 20
0
 public LetContext(TermContext context)
 {
     CopyFrom(context);
 }
		public ParenthesisContext(TermContext context) { CopyFrom(context); }
Esempio n. 22
0
 /// <summary>
 /// Expert: constructs a TermQuery that will use the
 ///  provided docFreq instead of looking up the docFreq
 ///  against the searcher.
 /// </summary>
 public TermQuery(Term t, int docFreq)
 {
     Term_Renamed = t;
     this.DocFreq = docFreq;
     PerReaderTermState = null;
 }
	public TermContext term() {
		TermContext _localctx = new TermContext(Context, State);
		EnterRule(_localctx, 12, RULE_term);
		try {
			State = 77;
			switch (TokenStream.La(1)) {
			case ID:
				_localctx = new IdentifierContext(_localctx);
				EnterOuterAlt(_localctx, 1);
				{
				State = 70; Match(ID);
				}
				break;
			case T__6:
				_localctx = new NegationContext(_localctx);
				EnterOuterAlt(_localctx, 2);
				{
				State = 71; Match(T__6);
				State = 72; term();
				}
				break;
			case T__7:
				_localctx = new ParenthesisContext(_localctx);
				EnterOuterAlt(_localctx, 3);
				{
				State = 73; Match(T__7);
				State = 74; expression();
				State = 75; Match(T__8);
				}
				break;
			default:
				throw new NoViableAltException(this);
			}
		}
		catch (RecognitionException re) {
			_localctx.exception = re;
			ErrorHandler.ReportError(this, re);
			ErrorHandler.Recover(this, re);
		}
		finally {
			ExitRule();
		}
		return _localctx;
	}
		public NegationContext(TermContext context) { CopyFrom(context); }
Esempio n. 25
0
 public CreateModel(TermContext context)
 {
     _context = context;
 }
Esempio n. 26
0
 public DetailsModel(TermContext context)
 {
     _context = context;
 }
Esempio n. 27
0
                protected internal override void AddClause(SpanOrQuery topLevel, Term term, int docFreq, float boost, TermContext states)
                {
                    SpanTermQuery q = new SpanTermQuery(term);

                    q.Boost = boost;
                    topLevel.AddClause(q);
                }
 protected override void AddClause(BooleanQuery topLevel, Term term, int docFreq, float boost, TermContext states) //ignored
 {
     topLevel.Add(new TermQuery(term, states), Occur.SHOULD);
 }
Esempio n. 29
0
 public EditModel(TermContext context)
 {
     _context = context;
 }
Esempio n. 30
0
 /// <summary>
 /// Expert: constructs a <see cref="TermQuery"/> that will use the
 /// provided <paramref name="docFreq"/> instead of looking up the docFreq
 /// against the searcher.
 /// </summary>
 public TermQuery(Term t, int docFreq)
 {
     term               = t;
     this.docFreq       = docFreq;
     perReaderTermState = null;
 }
Esempio n. 31
0
            protected override void AddClause(BooleanQuery topLevel, Term term, int docCount, float boost, TermContext states)
            {
                TermQuery tq = new TermQuery(term, states);

                tq.Boost = boost;
                topLevel.Add(tq, Occur.SHOULD);
            }
Esempio n. 32
0
    public TermContext term()
    {
        TermContext _localctx = new TermContext(Context, State);

        EnterRule(_localctx, 12, RULE_term);
        int _la;

        try {
            State = 114;
            ErrorHandler.Sync(this);
            switch (Interpreter.AdaptivePredict(TokenStream, 13, Context))
            {
            case 1:
                EnterOuterAlt(_localctx, 1);
                {
                    State = 98; Match(IDENTIFIER);
                }
                break;

            case 2:
                EnterOuterAlt(_localctx, 2);
                {
                    State = 99; Match(INTEGER_CONSTANT);
                }
                break;

            case 3:
                EnterOuterAlt(_localctx, 3);
                {
                    State = 100; predicate();
                }
                break;

            case 4:
                EnterOuterAlt(_localctx, 4);
                {
                    State = 101; Match(OPEN_SQUARE_BRACKET);
                    State = 110;
                    ErrorHandler.Sync(this);
                    _la = TokenStream.LA(1);
                    if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << OPEN_SQUARE_BRACKET) | (1L << IDENTIFIER) | (1L << INTEGER_CONSTANT) | (1L << STRING_CONSTANT))) != 0))
                    {
                        {
                            State = 102; term();
                            State = 107;
                            ErrorHandler.Sync(this);
                            _la = TokenStream.LA(1);
                            while (_la == COMMA)
                            {
                                {
                                    {
                                        State = 103; Match(COMMA);
                                        State = 104; term();
                                    }
                                }
                                State = 109;
                                ErrorHandler.Sync(this);
                                _la = TokenStream.LA(1);
                            }
                        }
                    }

                    State = 112; Match(CLOSE_SQUARE_BRACKET);
                }
                break;

            case 5:
                EnterOuterAlt(_localctx, 5);
                {
                    State = 113; Match(STRING_CONSTANT);
                }
                break;
            }
        }
        catch (RecognitionException re) {
            _localctx.exception = re;
            ErrorHandler.ReportError(this, re);
            ErrorHandler.Recover(this, re);
        }
        finally {
            ExitRule();
        }
        return(_localctx);
    }
                public override TermStatistics TermStatistics(Term term, TermContext context)
                {
                    Debug.Assert(term != null);
                    long docFreq = 0;
                    long totalTermFreq = 0;
                    for (int nodeID = 0; nodeID < NodeVersions.Length; nodeID++)
                    {
                        TermStatistics subStats;
                        if (nodeID == MyNodeID)
                        {
                            subStats = base.TermStatistics(term, context);
                        }
                        else
                        {
                            TermAndShardVersion key = new TermAndShardVersion(nodeID, NodeVersions[nodeID], term);
                            subStats = OuterInstance.TermStatsCache[key];
                            // We pre-cached during rewrite so all terms
                            // better be here...
                            Debug.Assert(subStats != null);
                        }

                        long nodeDocFreq = subStats.DocFreq();
                        if (docFreq >= 0 && nodeDocFreq >= 0)
                        {
                            docFreq += nodeDocFreq;
                        }
                        else
                        {
                            docFreq = -1;
                        }

                        long nodeTotalTermFreq = subStats.TotalTermFreq();
                        if (totalTermFreq >= 0 && nodeTotalTermFreq >= 0)
                        {
                            totalTermFreq += nodeTotalTermFreq;
                        }
                        else
                        {
                            totalTermFreq = -1;
                        }
                    }

                    return new TermStatistics(term.Bytes(), docFreq, totalTermFreq);
                }
Esempio n. 34
0
                protected internal override void AddClause(SpanOrQuery topLevel, Term term, int docCount, float boost, TermContext states)
                {
                    // TODO: would be nice to not lose term-state here.
                    // we could add a hack option to SpanOrQuery, but the hack would only work if this is the top-level Span
                    // (if you put this thing in another span query, it would extractTerms/double-seek anyway)
                    SpanTermQuery q = new SpanTermQuery(term);

                    q.Boost = boost;
                    topLevel.AddClause(q);
                }
Esempio n. 35
0
        protected internal virtual Query BuildQuery(int maxDoc, TermContext[] contextArray, Term[] queryTerms)
        {
            var lowFreq  = new BooleanQuery(disableCoord);
            var highFreq = new BooleanQuery(disableCoord)
            {
                Boost = highFreqBoost
            };

            lowFreq.Boost = lowFreqBoost;
            var query = new BooleanQuery(true);

            for (int i = 0; i < queryTerms.Length; i++)
            {
                TermContext termContext = contextArray[i];
                if (termContext == null)
                {
                    lowFreq.Add(NewTermQuery(queryTerms[i], null), lowFreqOccur);
                }
                else
                {
                    if ((maxTermFrequency >= 1f && termContext.DocFreq > maxTermFrequency) || (termContext.DocFreq > (int)Math.Ceiling(maxTermFrequency * (float)maxDoc)))
                    {
                        highFreq.Add(NewTermQuery(queryTerms[i], termContext), highFreqOccur);
                    }
                    else
                    {
                        lowFreq.Add(NewTermQuery(queryTerms[i], termContext), lowFreqOccur);
                    }
                }
            }
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int numLowFreqClauses = lowFreq.clauses().size();
            int numLowFreqClauses = lowFreq.Clauses.Length;
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int numHighFreqClauses = highFreq.clauses().size();
            int numHighFreqClauses = highFreq.Clauses.Length;

            if (lowFreqOccur == BooleanClause.Occur.SHOULD && numLowFreqClauses > 0)
            {
                int minMustMatch = CalcLowFreqMinimumNumberShouldMatch(numLowFreqClauses);
                lowFreq.MinimumNumberShouldMatch = minMustMatch;
            }
            if (highFreqOccur == BooleanClause.Occur.SHOULD && numHighFreqClauses > 0)
            {
                int minMustMatch = CalcHighFreqMinimumNumberShouldMatch(numHighFreqClauses);
                highFreq.MinimumNumberShouldMatch = minMustMatch;
            }
            if (lowFreq.Clauses.Length == 0)
            {
                /*
                 * if lowFreq is empty we rewrite the high freq terms in a conjunction to
                 * prevent slow queries.
                 */
                if (highFreq.MinimumNumberShouldMatch == 0 && highFreqOccur != BooleanClause.Occur.MUST)
                {
                    foreach (BooleanClause booleanClause in highFreq)
                    {
                        booleanClause.Occur_ = BooleanClause.Occur.MUST;
                    }
                }
                highFreq.Boost = Boost;
                return(highFreq);
            }
            else if (highFreq.Clauses.Length == 0)
            {
                // only do low freq terms - we don't have high freq terms
                lowFreq.Boost = Boost;
                return(lowFreq);
            }
            else
            {
                query.Add(highFreq, BooleanClause.Occur.SHOULD);
                query.Add(lowFreq, BooleanClause.Occur.MUST);
                query.Boost = Boost;
                return(query);
            }
        }
Esempio n. 36
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);
                        }
                    }
                }
            }
        }
Esempio n. 37
0
        protected virtual Query BuildQuery(int maxDoc, TermContext[] contextArray, Term[] queryTerms)
        {
            var lowFreq  = new BooleanQuery(m_disableCoord);
            var highFreq = new BooleanQuery(m_disableCoord)
            {
                Boost = m_highFreqBoost
            };

            lowFreq.Boost = m_lowFreqBoost;
            var query = new BooleanQuery(true);

            for (int i = 0; i < queryTerms.Length; i++)
            {
                TermContext termContext = contextArray[i];
                if (termContext == null)
                {
                    lowFreq.Add(NewTermQuery(queryTerms[i], null), m_lowFreqOccur);
                }
                else
                {
                    if ((m_maxTermFrequency >= 1f && termContext.DocFreq > m_maxTermFrequency) || (termContext.DocFreq > (int)Math.Ceiling(m_maxTermFrequency * (float)maxDoc)))
                    {
                        highFreq.Add(NewTermQuery(queryTerms[i], termContext), m_highFreqOccur);
                    }
                    else
                    {
                        lowFreq.Add(NewTermQuery(queryTerms[i], termContext), m_lowFreqOccur);
                    }
                }
            }
            int numLowFreqClauses  = lowFreq.GetClauses().Length;
            int numHighFreqClauses = highFreq.GetClauses().Length;

            if (m_lowFreqOccur == Occur.SHOULD && numLowFreqClauses > 0)
            {
                int minMustMatch = CalcLowFreqMinimumNumberShouldMatch(numLowFreqClauses);
                lowFreq.MinimumNumberShouldMatch = minMustMatch;
            }
            if (m_highFreqOccur == Occur.SHOULD && numHighFreqClauses > 0)
            {
                int minMustMatch = CalcHighFreqMinimumNumberShouldMatch(numHighFreqClauses);
                highFreq.MinimumNumberShouldMatch = minMustMatch;
            }
            if (lowFreq.GetClauses().Length == 0)
            {
                /*
                 * if lowFreq is empty we rewrite the high freq terms in a conjunction to
                 * prevent slow queries.
                 */
                if (highFreq.MinimumNumberShouldMatch == 0 && m_highFreqOccur != Occur.MUST)
                {
                    foreach (BooleanClause booleanClause in highFreq)
                    {
                        booleanClause.Occur = Occur.MUST;
                    }
                }
                highFreq.Boost = Boost;
                return(highFreq);
            }
            else if (highFreq.GetClauses().Length == 0)
            {
                // only do low freq terms - we don't have high freq terms
                lowFreq.Boost = Boost;
                return(lowFreq);
            }
            else
            {
                query.Add(highFreq, Occur.SHOULD);
                query.Add(lowFreq, Occur.MUST);
                query.Boost = Boost;
                return(query);
            }
        }
 protected abstract void AddClause(Q topLevel, Term term, int docCount, float boost, TermContext states);
Esempio n. 39
0
 /// <summary>
 /// Builds a new <see cref="TermQuery"/> instance.
 /// <para>This is intended for subclasses that wish to customize the generated queries.</para> </summary>
 /// <param name="term"> term </param>
 /// <param name="context"> the <see cref="TermContext"/> to be used to create the low level term query. Can be <c>null</c>. </param>
 /// <returns> new <see cref="TermQuery"/> instance </returns>
 protected virtual Query NewTermQuery(Term term, TermContext context)
 {
     return(context == null ? new TermQuery(term) : new TermQuery(term, context));
 }
Esempio n. 40
0
            protected override void AddClause(BooleanQuery topLevel, Term term, int docFreq, float boost, TermContext states)
            {
                Query q = new ConstantScoreQuery(new TermQuery(term, states));

                q.Boost = boost;
                topLevel.Add(q, Occur.SHOULD);
            }
		public virtual void CopyFrom(TermContext context) {
			base.CopyFrom(context);
		}