public FunctionWeight(FunctionQuery outerInstance, IndexSearcher searcher) { this.outerInstance = outerInstance; this.searcher = searcher; this.context = ValueSource.NewContext(searcher); outerInstance.func.CreateWeight(context, searcher); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public AllScorer(org.apache.lucene.index.AtomicReaderContext context, org.apache.lucene.util.Bits acceptDocs, FunctionWeight w, float qWeight) throws java.io.IOException public AllScorer(FunctionQuery outerInstance, AtomicReaderContext context, Bits acceptDocs, FunctionWeight w, float qWeight) : base(w) { this.outerInstance = outerInstance; this.weight = w; this.qWeight = qWeight; this.reader = context.Reader; this.maxDoc = reader.MaxDoc; this.acceptDocs = acceptDocs; vals = outerInstance.func.GetValues(weight.context, context); }
/// <summary> /// Test that FieldScoreQuery returns docs in expected order. /// </summary> /// <param name="valueSource"></param> private void DoTestRank(ValueSource valueSource) { FunctionQuery functionQuery = new FunctionQuery(valueSource); IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); Log("test: " + functionQuery); QueryUtils.Check(Random(), functionQuery, s, Similarity); ScoreDoc[] h = s.Search(functionQuery, null, 1000).ScoreDocs; assertEquals("All docs should be matched!", N_DOCS, h.Length); string prevID = "ID" + (N_DOCS + 1); // greater than all ids of docs in this test for (int i = 0; i < h.Length; i++) { string resID = s.Doc(h[i].Doc).Get(ID_FIELD); Log(i + ". score=" + h[i].Score + " - " + resID); Log(s.Explain(functionQuery, h[i].Doc)); assertTrue("res id " + resID + " should be < prev res id " + prevID, resID.CompareTo(prevID) < 0); prevID = resID; } r.Dispose(); }
/// <summary> /// Test that queries based on reverse/ordFieldScore scores correctly /// </summary> /// <param name="field"></param> /// <param name="inOrder"></param> private void DoTestRank(string field, bool inOrder) { IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); ValueSource vs; if (inOrder) { vs = new OrdFieldSource(field); } else { vs = new ReverseOrdFieldSource(field); } Query q = new FunctionQuery(vs); Log("test: " + q); QueryUtils.Check(Random(), q, s, Similarity); ScoreDoc[] h = s.Search(q, null, 1000).ScoreDocs; assertEquals("All docs should be matched!", N_DOCS, h.Length); string prevID = inOrder ? "IE" : "IC"; // smaller than all ids of docs in this test ("ID0001", etc.) - greater than all ids of docs in this test ("ID0001", etc.) for (int i = 0; i < h.Length; i++) { string resID = s.Doc(h[i].Doc).Get(ID_FIELD); Log(i + ". score=" + h[i].Score + " - " + resID); Log(s.Explain(q, h[i].Doc)); if (inOrder) { assertTrue("res id " + resID + " should be < prev res id " + prevID, resID.CompareTo(prevID) < 0); } else { assertTrue("res id " + resID + " should be > prev res id " + prevID, resID.CompareTo(prevID) > 0); } prevID = resID; } r.Dispose(); }
// constructor internal CustomMulAddQuery(Query q, FunctionQuery qValSrc1, FunctionQuery qValSrc2) : base(q, qValSrc1, qValSrc2) { }
// constructor internal CustomAddQuery(Query q, FunctionQuery qValSrc) : base(q, qValSrc) { }
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(); }
/// <summary> /// Create a CustomScoreQuery over input subQuery and a <seealso cref="FunctionQuery"/>. </summary> /// <param name="subQuery"> the sub query whose score is being customized. Must not be null. </param> /// <param name="scoringQuery"> a value source query whose scores are used in the custom score /// computation. This parameter is optional - it can be null. </param> public CustomScoreQuery(Query subQuery, FunctionQuery scoringQuery) : this(subQuery, scoringQuery != null ? new FunctionQuery[] { scoringQuery } : new FunctionQuery[0]) // don't want an array that contains a single null.. { }
/// <summary> /// Test that queries based on reverse/ordFieldScore returns docs with expected score. /// </summary> /// <param name="field"></param> /// <param name="inOrder"></param> private void DoTestExactScore(string field, bool inOrder) { IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); ValueSource vs; if (inOrder) { vs = new OrdFieldSource(field); } else { vs = new ReverseOrdFieldSource(field); } Query q = new FunctionQuery(vs); TopDocs td = s.Search(q, null, 1000); assertEquals("All docs should be matched!", N_DOCS, td.TotalHits); ScoreDoc[] sd = td.ScoreDocs; for (int i = 0; i < sd.Length; i++) { float score = sd[i].Score; string id = s.IndexReader.Document(sd[i].Doc).Get(ID_FIELD); Log("-------- " + i + ". Explain doc " + id); Log(s.Explain(q, sd[i].Doc)); float expectedScore = N_DOCS - i - 1; assertEquals("score of result " + i + " shuould be " + expectedScore + " != " + score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA); string expectedId = inOrder ? Id2String(N_DOCS - i) : Id2String(i + 1); // reverse ==> smaller values first - in-order ==> larger values first assertTrue("id of result " + i + " shuould be " + expectedId + " != " + score, expectedId.Equals(id)); } r.Dispose(); }
// don't want an array that contains a single null.. /// <summary> /// Create a CustomScoreQuery over input subQuery and a <seealso cref="FunctionQuery"/>. </summary> /// <param name="subQuery"> the sub query whose score is being customized. Must not be null. </param> /// <param name="scoringQuery"> a value source query whose scores are used in the custom score /// computation. This parameter is optional - it can be null. </param> public CustomScoreQuery(Query subQuery, FunctionQuery scoringQuery) : this(subQuery, scoringQuery != null ? new FunctionQuery[] { scoringQuery } : new FunctionQuery[0]) { }
/** scores[] are in docId order */ protected virtual void CheckValueSource(ValueSource vs, float[] scores, float delta) { FunctionQuery q = new FunctionQuery(vs); // //TODO is there any point to this check? // int expectedDocs[] = new int[scores.length];//fill with ascending 0....length-1 // for (int i = 0; i < expectedDocs.length; i++) { // expectedDocs[i] = i; // } // CheckHits.checkHits(random(), q, "", indexSearcher, expectedDocs); TopDocs docs = indexSearcher.Search(q, 1000);//calculates the score for (int i = 0; i < docs.ScoreDocs.Length; i++) { ScoreDoc gotSD = docs.ScoreDocs[i]; float expectedScore = scores[gotSD.Doc]; assertEquals("Not equal for doc " + gotSD.Doc, expectedScore, gotSD.Score, delta); } CheckHits.CheckExplanations(q, "", indexSearcher); }
// Test that FieldScoreQuery returns docs with expected score. //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private void doTestExactScore(ValueSource valueSource) throws Exception private void doTestExactScore(ValueSource valueSource) { FunctionQuery functionQuery = new FunctionQuery(valueSource); IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); TopDocs td = s.Search(functionQuery, null, 1000); assertEquals("All docs should be matched!", N_DOCS, td.TotalHits); ScoreDoc[] sd = td.ScoreDocs; foreach (ScoreDoc aSd in sd) { float score = aSd.Score; Log(s.Explain(functionQuery, aSd.Doc)); string id = s.IndexReader.Document(aSd.Doc).Get(ID_FIELD); float expectedScore = ExpectedFieldScore(id); // "ID7" --> 7.0 assertEquals("score of " + id + " shuould be " + expectedScore + " != " + score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA); } r.Dispose(); }