// Test that queries based on reverse/ordFieldScore returns docs with expected score. private void DoTestExactScore(System.String field, bool inOrder) { IndexSearcher s = new IndexSearcher(dir, true); ValueSource vs; if (inOrder) { vs = new OrdFieldSource(field); } else { vs = new ReverseOrdFieldSource(field); } Query q = new ValueSourceQuery(vs); TopDocs td = s.Search(q, null, 1000); Assert.AreEqual(N_DOCS, td.TotalHits, "All docs should be matched!"); ScoreDoc[] sd = td.ScoreDocs; for (int i = 0; i < sd.Length; i++) { float score = sd[i].Score; System.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; Assert.AreEqual(expectedScore, score, TEST_SCORE_TOLERANCE_DELTA, "score of result " + i + " shuould be " + expectedScore + " != " + score); System.String expectedId = inOrder?Id2String(N_DOCS - i):Id2String(i + 1); // reverse ==> smaller values first Assert.IsTrue(expectedId.Equals(id), "id of result " + i + " shuould be " + expectedId + " != " + score); } }
/// <summary> Create a CustomScoreQuery over input subQuery and a {@link ValueSourceQuery}.</summary> /// <param name="subQuery">the sub query whose score is being customized. Must not be null. /// </param> /// <param name="valSrcQueries">value source queries whose scores are used in the custom score /// computation. For most simple/convenient use case these would be /// {@link Lucene.Net.Search.Function.FieldScoreQuery FieldScoreQueries}. /// This parameter is optional - it can be null or even an empty array. /// </param> public CustomScoreQuery(Query subQuery, ValueSourceQuery[] valSrcQueries) { this.subQuery = subQuery; this.valSrcQueries = valSrcQueries != null?valSrcQueries:new ValueSourceQuery[0]; if (subQuery == null) throw new System.ArgumentException("<subquery> must not be null!"); }
/*(non-Javadoc) <see cref="Lucene.Net.Search.Query.rewrite(Lucene.Net.Index.IndexReader) */ 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 < valSrcQueries.Length; i++) { ValueSourceQuery v = (ValueSourceQuery)valSrcQueries[i].Rewrite(reader); if (v != valSrcQueries[i]) { if (clone == null) { clone = (CustomScoreQuery)Clone(); } clone.valSrcQueries[i] = v; } } return((clone == null) ? this : clone); }
// constructor internal ValueSourceScorer(ValueSourceQuery enclosingInstance, Similarity similarity, IndexReader reader, ValueSourceWeight w) : base(similarity) { InitBlock(enclosingInstance); this.weight = w; this.qWeight = w.GetValue(); // this is when/where the values are first created. vals = Enclosing_Instance.valSrc.GetValues(reader); termDocs = reader.TermDocs(null); }
/// <summary>Returns true if <c>o</c> is equal to this. </summary> public override bool Equals(System.Object o) { if (GetType() != o.GetType()) { return(false); } ValueSourceQuery other = (ValueSourceQuery)o; return(this.Boost == other.Boost && this.valSrc.Equals(other.valSrc)); }
// Test that queries based on reverse/ordFieldScore scores correctly private void DoTestRank(System.String field, bool inOrder) { IndexSearcher s = new IndexSearcher(dir, true); ValueSource vs; if (inOrder) { vs = new OrdFieldSource(field); } else { vs = new ReverseOrdFieldSource(field); } Query q = new ValueSourceQuery(vs); Log("test: " + q); QueryUtils.Check(q, s); ScoreDoc[] h = s.Search(q, null, 1000).ScoreDocs; Assert.AreEqual(N_DOCS, h.Length, "All docs should be matched!"); System.String prevID = inOrder?"IE":"IC"; // smaller than all ids of docs in this test ("ID0001", etc.) for (int i = 0; i < h.Length; i++) { System.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) { Assert.IsTrue(String.CompareOrdinal(resID, prevID) < 0, "res id " + resID + " should be < prev res id " + prevID); } else { Assert.IsTrue(String.CompareOrdinal(resID, prevID) > 0, "res id " + resID + " should be > prev res id " + prevID); } prevID = resID; } }
// constructor internal ValueSourceScorer(ValueSourceQuery enclosingInstance, Similarity similarity, IndexReader reader, ValueSourceWeight w):base(similarity) { InitBlock(enclosingInstance); this.weight = w; this.qWeight = w.GetValue(); // this is when/where the values are first created. vals = Enclosing_Instance.valSrc.GetValues(reader); termDocs = reader.TermDocs(null); }
// Test that values loaded for FieldScoreQuery are cached properly and consumes the proper RAM resources. private void DoTestCaching(System.String field, bool inOrder) { IndexSearcher s = new IndexSearcher(dir, true); System.Object innerArray = null; bool warned = false; // print warning once for (int i = 0; i < 10; i++) { ValueSource vs; if (inOrder) { vs = new OrdFieldSource(field); } else { vs = new ReverseOrdFieldSource(field); } ValueSourceQuery q = new ValueSourceQuery(vs); ScoreDoc[] h = s.Search(q, null, 1000).ScoreDocs; try { Assert.AreEqual(N_DOCS, h.Length, "All docs should be matched!"); IndexReader[] readers = s.IndexReader.GetSequentialSubReaders(); for (int j = 0; j < readers.Length; j++) { IndexReader reader = readers[j]; if (i == 0) { innerArray = q.valSrc.GetValues(reader).InnerArray; } else { Log(i + ". compare: " + innerArray + " to " + q.valSrc.GetValues(reader).InnerArray); Assert.AreSame(innerArray, q.valSrc.GetValues(reader).InnerArray, "field values should be cached and reused!"); } } } catch (System.NotSupportedException) { if (!warned) { System.Console.Error.WriteLine("WARNING: " + TestName() + " cannot fully test values of " + q); warned = true; } } } ValueSource vs2; ValueSourceQuery q2; ScoreDoc[] h2; // verify that different values are loaded for a different field System.String field2 = INT_FIELD; Assert.IsFalse(field.Equals(field2)); // otherwise this test is meaningless. if (inOrder) { vs2 = new OrdFieldSource(field2); } else { vs2 = new ReverseOrdFieldSource(field2); } q2 = new ValueSourceQuery(vs2); h2 = s.Search(q2, null, 1000).ScoreDocs; Assert.AreEqual(N_DOCS, h2.Length, "All docs should be matched!"); IndexReader[] readers2 = s.IndexReader.GetSequentialSubReaders(); for (int j = 0; j < readers2.Length; j++) { IndexReader reader = readers2[j]; try { Log("compare (should differ): " + innerArray + " to " + q2.valSrc.GetValues(reader).InnerArray); Assert.AreNotSame(innerArray, q2.valSrc.GetValues(reader).InnerArray, "different values shuold be loaded for a different field!"); } catch (System.NotSupportedException) { if (!warned) { System.Console.Error.WriteLine("WARNING: " + TestName() + " cannot fully test values of " + q2); warned = true; } } } // verify new values are reloaded (not reused) for a new reader s = new IndexSearcher(dir, true); if (inOrder) { vs2 = new OrdFieldSource(field); } else { vs2 = new ReverseOrdFieldSource(field); } q2 = new ValueSourceQuery(vs2); h2 = s.Search(q2, null, 1000).ScoreDocs; Assert.AreEqual(N_DOCS, h2.Length, "All docs should be matched!"); readers2 = s.IndexReader.GetSequentialSubReaders(); for (int j = 0; j < readers2.Length; j++) { IndexReader reader = readers2[j]; try { Log("compare (should differ): " + innerArray + " to " + q2.valSrc.GetValues(reader).InnerArray); Assert.AreNotSame(innerArray, q2.valSrc.GetValues(reader).InnerArray, "cached field values should not be reused if reader as changed!"); } catch (System.NotSupportedException) { if (!warned) { System.Console.Error.WriteLine("WARNING: " + TestName() + " cannot fully test values of " + q2); warned = true; } } } }
// constructor internal CustomMulAddQuery(Query q, ValueSourceQuery qValSrc1, ValueSourceQuery qValSrc2) : base(q, new ValueSourceQuery[] { qValSrc1, qValSrc2 }) { }
/// <summary> Create a CustomScoreQuery over input subQuery and a {@link ValueSourceQuery}.</summary> /// <param name="subQuery">the sub query whose score is being customed. Must not be null. /// </param> /// <param name="valSrcQuery">a value source query whose scores are used in the custom score /// computation. For most simple/convineient use case this would be a /// {@link Lucene.Net.Search.Function.FieldScoreQuery FieldScoreQuery}. /// This parameter is optional - it can be null or even an empty array. /// </param> public CustomScoreQuery(Query subQuery, ValueSourceQuery valSrcQuery):this(subQuery, valSrcQuery != null?new ValueSourceQuery[]{valSrcQuery}:new ValueSourceQuery[0]) { }
private void InitBlock(ValueSourceQuery enclosingInstance) { this.enclosingInstance = enclosingInstance; }
private static void VisitQuery(ValueSourceQuery query, AzureQueryLogger.IndentedTextWriter writer) { }
public ValueSourceWeight(ValueSourceQuery enclosingInstance, Searcher searcher) { InitBlock(enclosingInstance); this.similarity = Enclosing_Instance.GetSimilarity(searcher); }
/// <summary> Create a CustomScoreQuery over input subQuery and a {@link ValueSourceQuery}.</summary> /// <param name="subQuery">the sub query whose score is being customed. Must not be null. /// </param> /// <param name="valSrcQuery">a value source query whose scores are used in the custom score /// computation. For most simple/convineient use case this would be a /// {@link Lucene.Net.Search.Function.FieldScoreQuery FieldScoreQuery}. /// This parameter is optional - it can be null. /// </param> public CustomScoreQuery(Query subQuery, ValueSourceQuery valSrcQuery) : this(subQuery, valSrcQuery != null ? new ValueSourceQuery[] { valSrcQuery } : new ValueSourceQuery[0]) { }
// constructor internal CustomMulAddQuery(Query q, ValueSourceQuery qValSrc1, ValueSourceQuery qValSrc2):base(q, new ValueSourceQuery[]{qValSrc1, qValSrc2}) { }
// constructor internal CustomAddQuery(Query q, ValueSourceQuery qValSrc):base(q, qValSrc) { }
// constructor internal CustomAddQuery(Query q, ValueSourceQuery qValSrc) : base(q, qValSrc) { }
public virtual Query VisitValueSourceQuery(ValueSourceQuery valueSourceq) { throw new NotImplementedException(); }