/*(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); }
/// <summary>Returns true if <code>o</code> is equal to this. </summary> public override bool Equals(System.Object o) { if (GetType() != o.GetType()) { return(false); } CustomScoreQuery other = (CustomScoreQuery)o; if (this.GetBoost() != other.GetBoost() || !this.subQuery.Equals(other.subQuery) || this.strict != other.strict || this.valSrcQueries.Length != other.valSrcQueries.Length) { return(false); } for (int i = 0; i < valSrcQueries.Length; i++) { //TODO simplify with Arrays.deepEquals() once moving to Java 1.5 if (!valSrcQueries[i].Equals(other.valSrcQueries[i])) { return(false); } } return(true); }
private IQueryable<Package> SearchCore(SearchFilter searchFilter, out int totalHits) { int numRecords = searchFilter.Skip + searchFilter.Take; var searcher = new IndexSearcher(_directory, readOnly: true); var query = ParseQuery(searchFilter); // IF searching by relevance, boost scores by download count. if (searchFilter.SortProperty == SortProperty.Relevance) { var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT); query = new CustomScoreQuery(query, downloadCountBooster); } var filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable"; var termQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString)); var filter = new QueryWrapperFilter(termQuery); var results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(GetSortField(searchFilter))); totalHits = results.totalHits; if (results.totalHits == 0 || searchFilter.CountOnly) { return Enumerable.Empty<Package>().AsQueryable(); } var packages = results.scoreDocs .Skip(searchFilter.Skip) .Select(sd => PackageFromDoc(searcher.Doc(sd.doc))) .ToList(); return packages.AsQueryable(); }
// Test that FieldScoreQuery returns docs with expected score. private void DoTestCustomScore(System.String field, FieldScoreQuery.Type tp, double dboost) { float boost = (float)dboost; IndexSearcher s = new IndexSearcher(dir, true); FieldScoreQuery qValSrc = new FieldScoreQuery(field, tp); // a query that would score by the field QueryParser qp = new QueryParser(Util.Version.LUCENE_CURRENT, TEXT_FIELD, anlzr); System.String qtxt = "first aid text"; // from the doc texts in FunctionQuerySetup. // regular (boolean) query. Query q1 = qp.Parse(qtxt); Log(q1); // custom query, that should score the same as q1. CustomScoreQuery q2CustomNeutral = new CustomScoreQuery(q1); q2CustomNeutral.Boost = boost; Log(q2CustomNeutral); // custom query, that should (by default) multiply the scores of q1 by that of the field CustomScoreQuery q3CustomMul = new CustomScoreQuery(q1, qValSrc); q3CustomMul.SetStrict(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, qValSrc); q4CustomAdd.SetStrict(true); q4CustomAdd.Boost = boost; Log(q4CustomAdd); // custom query, that multiplies and adds the field score to that of q1 CustomScoreQuery q5CustomMulAdd = new CustomMulAddQuery(q1, qValSrc, qValSrc); q5CustomMulAdd.SetStrict(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 System.Collections.Hashtable h1 = TopDocsToMap(td1); System.Collections.Hashtable h2CustomNeutral = TopDocsToMap(td2CustomNeutral); System.Collections.Hashtable h3CustomMul = TopDocsToMap(td3CustomMul); System.Collections.Hashtable h4CustomAdd = TopDocsToMap(td4CustomAdd); System.Collections.Hashtable h5CustomMulAdd = TopDocsToMap(td5CustomMulAdd); VerifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd); }
private float[] vScores; // reused in score() to avoid allocating this array for each doc // constructor internal CustomScorer(CustomScoreQuery enclosingInstance, Similarity similarity, IndexReader reader, CustomWeight w, Scorer subQueryScorer, Scorer[] valSrcScorers) : base(similarity) { InitBlock(enclosingInstance); this.qWeight = w.Value; this.subQueryScorer = subQueryScorer; this.valSrcScorers = valSrcScorers; //this.reader = reader; this.vScores = new float[valSrcScorers.Length]; this.provider = this.Enclosing_Instance.GetCustomScoreProvider(reader); }
private float[] vScores; // reused in score() to avoid allocating this array for each doc // constructor internal CustomScorer(CustomScoreQuery enclosingInstance, Similarity similarity, IndexReader reader, CustomWeight w, Scorer subQueryScorer, Scorer[] valSrcScorers) : base(similarity) { InitBlock(enclosingInstance); this.weight = w; this.qWeight = w.GetValue(); this.subQueryScorer = subQueryScorer; this.valSrcScorers = valSrcScorers; this.reader = reader; this.vScores = new float[valSrcScorers.Length]; }
/*(non-Javadoc) @see Lucene.Net.Search.Query#clone() */ public override System.Object Clone() { CustomScoreQuery clone = (CustomScoreQuery)base.Clone(); clone.subQuery = (Query)subQuery.Clone(); clone.valSrcQueries = new ValueSourceQuery[valSrcQueries.Length]; for (int i = 0; i < valSrcQueries.Length; i++) { clone.valSrcQueries[i] = (ValueSourceQuery)valSrcQueries[i].Clone(); } return(clone); }
public CustomWeight(CustomScoreQuery enclosingInstance, Searcher searcher) { InitBlock(enclosingInstance); this.similarity = Enclosing_Instance.GetSimilarity(searcher); this.subQueryWeight = Enclosing_Instance.subQuery.Weight(searcher); this.valSrcWeights = new Weight[Enclosing_Instance.valSrcQueries.Length]; for (int i = 0; i < Enclosing_Instance.valSrcQueries.Length; i++) { this.valSrcWeights[i] = Enclosing_Instance.valSrcQueries[i].CreateWeight(searcher); } this.qStrict = Enclosing_Instance.strict; }
private SearchResults SearchCore(SearchFilter searchFilter) { // Get index timestamp DateTime timestamp = File.GetLastWriteTimeUtc(LuceneCommon.GetIndexMetadataPath()); int numRecords = searchFilter.Skip + searchFilter.Take; var searcher = new IndexSearcher(_directory, readOnly: true); var query = ParseQuery(searchFilter); // IF searching by relevance, boost scores by download count. if (searchFilter.SortOrder == SortOrder.Relevance) { var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT); query = new CustomScoreQuery(query, downloadCountBooster); } var filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable"; Query filterQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString)); if (searchFilter.CuratedFeed != null) { var feedFilterQuery = new TermQuery(new Term("CuratedFeedKey", searchFilter.CuratedFeed.Key.ToString(CultureInfo.InvariantCulture))); BooleanQuery conjunctionQuery = new BooleanQuery(); conjunctionQuery.Add(filterQuery, Occur.MUST); conjunctionQuery.Add(feedFilterQuery, Occur.MUST); filterQuery = conjunctionQuery; } Filter filter = new QueryWrapperFilter(filterQuery); var results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(GetSortField(searchFilter))); if (results.TotalHits == 0 || searchFilter.CountOnly) { return new SearchResults(results.TotalHits, timestamp); } var packages = results.ScoreDocs .Skip(searchFilter.Skip) .Select(sd => PackageFromDoc(searcher.Doc(sd.Doc))) .ToList(); return new SearchResults( results.TotalHits, timestamp, packages.AsQueryable()); }
/// <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); } CustomScoreQuery other = (CustomScoreQuery)o; if (this.Boost != other.Boost || !this.subQuery.Equals(other.subQuery) || this.strict != other.strict || this.valSrcQueries.Length != other.valSrcQueries.Length) { return(false); } // SequenceEqual should properly mimic java's Array.equals() return(valSrcQueries.SequenceEqual(other.valSrcQueries)); }
private IQueryable<Package> SearchCore(SearchFilter searchFilter, out int totalHits) { int numRecords = searchFilter.Skip + searchFilter.Take; var searcher = new IndexSearcher(_directory, readOnly: true); var query = ParseQuery(searchFilter); // IF searching by relevance, boost scores by download count. if (searchFilter.SortProperty == SortProperty.Relevance) { var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT); query = new CustomScoreQuery(query, downloadCountBooster); } var filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable"; Query filterQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString)); if (searchFilter.CuratedFeedKey.HasValue) { var feedFilterQuery = new TermQuery(new Term("CuratedFeedKey", searchFilter.CuratedFeedKey.Value.ToString(CultureInfo.InvariantCulture))); BooleanQuery conjunctionQuery = new BooleanQuery(); conjunctionQuery.Add(filterQuery, Occur.MUST); conjunctionQuery.Add(feedFilterQuery, Occur.MUST); filterQuery = conjunctionQuery; } Filter filter = new QueryWrapperFilter(filterQuery); var results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(GetSortField(searchFilter))); totalHits = results.TotalHits; if (results.TotalHits == 0 || searchFilter.CountOnly) { return Enumerable.Empty<Package>().AsQueryable(); } var packages = results.ScoreDocs .Skip(searchFilter.Skip) .Select(sd => PackageFromDoc(searcher.Doc(sd.Doc))) .ToList(); return packages.AsQueryable(); }
private SearchResults SearchCore(SearchFilter searchFilter) { // Get index timestamp DateTime timestamp = File.GetLastWriteTimeUtc(LuceneCommon.IndexMetadataPath); int numRecords = searchFilter.Skip + searchFilter.Take; var searcher = new IndexSearcher(_directory, readOnly: true); var query = ParseQuery(searchFilter); // If searching by relevance, boost scores by download count. if (searchFilter.SortProperty == SortProperty.Relevance) { var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT); query = new CustomScoreQuery(query, downloadCountBooster); } var filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable"; Query filterQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString)); Filter filter = new QueryWrapperFilter(filterQuery); var results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(GetSortField(searchFilter))); if (results.TotalHits == 0 || searchFilter.CountOnly) { return new SearchResults(results.TotalHits, timestamp); } var packages = results.ScoreDocs .Skip(searchFilter.Skip) .Select(sd => PackageFromDoc(searcher.Doc(sd.Doc))) .ToList(); return new SearchResults( results.TotalHits, timestamp, packages.AsQueryable()); }
private void InitBlock(CustomScoreQuery enclosingInstance) { this.enclosingInstance = enclosingInstance; }
public CustomWeight(CustomScoreQuery enclosingInstance, Searcher searcher) { InitBlock(enclosingInstance); this.similarity = Enclosing_Instance.GetSimilarity(searcher); this.subQueryWeight = Enclosing_Instance.subQuery.Weight(searcher); this.valSrcWeights = new Weight[Enclosing_Instance.valSrcQueries.Length]; for (int i = 0; i < Enclosing_Instance.valSrcQueries.Length; i++) { this.valSrcWeights[i] = Enclosing_Instance.valSrcQueries[i].CreateWeight(searcher); } this.qStrict = Enclosing_Instance.strict; }
public AnonymousCustomScoreProvider(CustomScoreQuery parent, IndexReader reader) : base(reader) { this.parent = parent; }
private void InitBlock(CustomScoreQuery enclosingInstance) { this.enclosingInstance = enclosingInstance; }
/// <summary> /// Perform search /// </summary> /// <param name="query"></param> /// <param name="startIndex"></param> /// <param name="blockSize"></param> /// <param name="indexDirEs"></param> /// <param name="indexDirEn"></param> /// <param name="sortBy"></param> /// <returns></returns> public List<IssueDocument> MedesSearch(Query query, int startIndex, int blockSize, Directory indexDirEs, Directory indexDirEn, Directory indexDirHe, string sortBy) { #if DEBUG T.TraceMessage(string.Format("Begin search , query: '{0}'", query.ToString())); #endif List<IssueDocument> result = new List<IssueDocument>(); try { // build a multi searcher across the 2 indexes MultiSearcher mSearcher = CombineSearchers(indexDirEs, indexDirEn, indexDirHe); TopDocs tDocs = null; int iterateLast = startIndex + blockSize; string customScoreField = "article_id"; FieldScoreQuery dateBooster = new FieldScoreQuery(customScoreField, FieldScoreQuery.Type.FLOAT); CustomScoreQuery customQuery = new CustomScoreQuery(query, dateBooster); tDocs = mSearcher.Search(customQuery, 1000); //ScoreDoc[] hits = tpDcs.scoreDocs; if (startIndex + blockSize > tDocs.TotalHits) iterateLast = tDocs.TotalHits; for (int i = startIndex; i < iterateLast; i++) { // Document hitDoc = mSearcher.Doc(hits[i].doc); Document hitDoc = mSearcher.Doc(i); result.Add(new IssueDocument() { Id = Int32.Parse(hitDoc.Get("issue_id").ToString())}); } // close the searcher and indexes mSearcher.Dispose(); indexDirEs.Dispose(); indexDirEn.Dispose(); indexDirHe.Dispose(); } catch (Exception ex) { T.TraceError("Error MedesSearch, query '{0}'", query.ToString()); T.TraceError(ex); throw ex; } return result; }
// Test that FieldScoreQuery returns docs with expected score. private void DoTestCustomScore(System.String field, FieldScoreQuery.Type tp, double dboost) { float boost = (float) dboost; IndexSearcher s = new IndexSearcher(dir, true); FieldScoreQuery qValSrc = new FieldScoreQuery(field, tp); // a query that would score by the field QueryParser qp = new QueryParser(Util.Version.LUCENE_CURRENT, TEXT_FIELD, anlzr); System.String qtxt = "first aid text"; // from the doc texts in FunctionQuerySetup. // regular (boolean) query. Query q1 = qp.Parse(qtxt); Log(q1); // custom query, that should score the same as q1. CustomScoreQuery q2CustomNeutral = new CustomScoreQuery(q1); q2CustomNeutral.Boost = boost; Log(q2CustomNeutral); // custom query, that should (by default) multiply the scores of q1 by that of the field CustomScoreQuery q3CustomMul = new CustomScoreQuery(q1, qValSrc); q3CustomMul.SetStrict(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, qValSrc); q4CustomAdd.SetStrict(true); q4CustomAdd.Boost = boost; Log(q4CustomAdd); // custom query, that multiplies and adds the field score to that of q1 CustomScoreQuery q5CustomMulAdd = new CustomMulAddQuery(q1, qValSrc, qValSrc); q5CustomMulAdd.SetStrict(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 System.Collections.Hashtable h1 = TopDocsToMap(td1); System.Collections.Hashtable h2CustomNeutral = TopDocsToMap(td2CustomNeutral); System.Collections.Hashtable h3CustomMul = TopDocsToMap(td3CustomMul); System.Collections.Hashtable h4CustomAdd = TopDocsToMap(td4CustomAdd); System.Collections.Hashtable h5CustomMulAdd = TopDocsToMap(td5CustomMulAdd); VerifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd); }
private static void VisitQuery(CustomScoreQuery query, AzureQueryLogger.IndentedTextWriter writer) { writer.WriteLine("IsStrict: {0}", (query.IsStrict() ? 1 : 0)); writer.WriteLine("Name: {0}", (object)query.Name()); }
private float[] vScores; // reused in score() to avoid allocating this array for each doc // constructor internal CustomScorer(CustomScoreQuery enclosingInstance, Similarity similarity, IndexReader reader, CustomWeight w, Scorer subQueryScorer, Scorer[] valSrcScorers):base(similarity) { InitBlock(enclosingInstance); this.weight = w; this.qWeight = w.GetValue(); this.subQueryScorer = subQueryScorer; this.valSrcScorers = valSrcScorers; this.reader = reader; this.vScores = new float[valSrcScorers.Length]; this.provider = this.Enclosing_Instance.GetCustomScoreProvider(reader); }
public AnonymousCustomScoreProvider(CustomScoreQuery parent, IndexReader reader) : base(reader) { this.parent = parent; }
public virtual Query VisitCustomScoreQuery(CustomScoreQuery customScoreq) { throw new NotImplementedException(); }