public override Query Rewrite(IndexReader reader) { CustomScoreQuery clone = null; Query sq = subQuery.Rewrite(reader); if (sq != subQuery) { clone = (CustomScoreQuery)Clone(); clone.subQuery = sq; } for (int i = 0; i < scoringQueries.Length; i++) { Query v = scoringQueries[i].Rewrite(reader); if (v != scoringQueries[i]) { if (clone == null) { clone = (CustomScoreQuery)Clone(); } clone.scoringQueries[i] = v; } } return(clone ?? this); }
private readonly float[] vScores; // reused in score() to avoid allocating this array for each doc // constructor internal CustomScorer(CustomScoreQuery outerInstance, CustomScoreProvider provider, CustomWeight w, float qWeight, Scorer subQueryScorer, Scorer[] valSrcScorers) : base(w) { this.outerInstance = outerInstance; this.qWeight = qWeight; this.subQueryScorer = subQueryScorer; this.valSrcScorers = valSrcScorers; this.vScores = new float[valSrcScorers.Length]; this.provider = provider; }
public CustomWeight(CustomScoreQuery outerInstance, IndexSearcher searcher) { this.outerInstance = outerInstance; this.subQueryWeight = outerInstance.subQuery.CreateWeight(searcher); this.valSrcWeights = new Weight[outerInstance.scoringQueries.Length]; for (int i = 0; i < outerInstance.scoringQueries.Length; i++) { this.valSrcWeights[i] = outerInstance.scoringQueries[i].CreateWeight(searcher); } this.qStrict = outerInstance.strict; }
/*(non-Javadoc) @see org.apache.lucene.search.Query#clone() */ public override CustomScoreQuery Clone() { CustomScoreQuery clone = (CustomScoreQuery)base.Clone(); clone.subQuery = subQuery.Clone(); clone.scoringQueries = new Query[scoringQueries.Length]; for (int i = 0; i < scoringQueries.Length; i++) { clone.scoringQueries[i] = scoringQueries[i].Clone(); } return(clone); }
public virtual void TestRewrite() { IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); Query q = new TermQuery(new Term(TEXT_FIELD, "first")); CustomScoreQuery original = new CustomScoreQuery(q); CustomScoreQuery rewritten = (CustomScoreQuery)original.Rewrite(s.IndexReader); assertTrue("rewritten query should be identical, as TermQuery does not rewrite", original == rewritten); assertTrue("no hits for query", s.Search(rewritten, 1).TotalHits > 0); assertEquals(s.Search(q, 1).TotalHits, s.Search(rewritten, 1).TotalHits); q = new TermRangeQuery(TEXT_FIELD, null, null, true, true); // everything original = new CustomScoreQuery(q); rewritten = (CustomScoreQuery)original.Rewrite(s.IndexReader); assertTrue("rewritten query should not be identical, as TermRangeQuery rewrites", original != rewritten); assertTrue("no hits for query", s.Search(rewritten, 1).TotalHits > 0); assertEquals(s.Search(q, 1).TotalHits, s.Search(original, 1).TotalHits); assertEquals(s.Search(q, 1).TotalHits, s.Search(rewritten, 1).TotalHits); r.Dispose(); }
private void DoTestCustomScore(ValueSource valueSource, double dboost) { float boost = (float)dboost; FunctionQuery functionQuery = new FunctionQuery(valueSource); IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); // regular (boolean) query. BooleanQuery q1 = new BooleanQuery(); q1.Add(new TermQuery(new Term(TEXT_FIELD, "first")), BooleanClause.Occur.SHOULD); q1.Add(new TermQuery(new Term(TEXT_FIELD, "aid")), BooleanClause.Occur.SHOULD); q1.Add(new TermQuery(new Term(TEXT_FIELD, "text")), BooleanClause.Occur.SHOULD); Log(q1); // custom query, that should score the same as q1. BooleanQuery q2CustomNeutral = new BooleanQuery(true); Query q2CustomNeutralInner = new CustomScoreQuery(q1); q2CustomNeutral.Add(q2CustomNeutralInner, BooleanClause.Occur.SHOULD); // a little tricky: we split the boost across an outer BQ and CustomScoreQuery // this ensures boosting is correct across all these functions (see LUCENE-4935) q2CustomNeutral.Boost = (float)Math.Sqrt(dboost); q2CustomNeutralInner.Boost = (float)Math.Sqrt(dboost); Log(q2CustomNeutral); // custom query, that should (by default) multiply the scores of q1 by that of the field CustomScoreQuery q3CustomMul = new CustomScoreQuery(q1, functionQuery); q3CustomMul.Strict = true; q3CustomMul.Boost = boost; Log(q3CustomMul); // custom query, that should add the scores of q1 to that of the field CustomScoreQuery q4CustomAdd = new CustomAddQuery(q1, functionQuery); q4CustomAdd.Strict = true; q4CustomAdd.Boost = boost; Log(q4CustomAdd); // custom query, that multiplies and adds the field score to that of q1 CustomScoreQuery q5CustomMulAdd = new CustomMulAddQuery(q1, functionQuery, functionQuery); q5CustomMulAdd.Strict = true; q5CustomMulAdd.Boost = boost; Log(q5CustomMulAdd); // do al the searches TopDocs td1 = s.Search(q1, null, 1000); TopDocs td2CustomNeutral = s.Search(q2CustomNeutral, null, 1000); TopDocs td3CustomMul = s.Search(q3CustomMul, null, 1000); TopDocs td4CustomAdd = s.Search(q4CustomAdd, null, 1000); TopDocs td5CustomMulAdd = s.Search(q5CustomMulAdd, null, 1000); // put results in map so we can verify the scores although they have changed IDictionary<int, float> h1 = TopDocsToMap(td1); IDictionary<int, float> h2CustomNeutral = TopDocsToMap(td2CustomNeutral); IDictionary<int, float> h3CustomMul = TopDocsToMap(td3CustomMul); IDictionary<int, float> h4CustomAdd = TopDocsToMap(td4CustomAdd); IDictionary<int, float> h5CustomMulAdd = TopDocsToMap(td5CustomMulAdd); VerifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd); r.Dispose(); }