/// <summary> /// Adds an Object to a PriorityQueue in log(size) time. If one tries to add /// more objects than maxSize from initialize an /// {@link ArrayIndexOutOfBoundsException} is thrown. /// </summary> /// <param name="element"></param> /// <returns>the new 'bottom' element in the queue.</returns> public ScoreDoc Add(ScoreDoc element) { size++; heap[size] = element; UpHeap(size); return heap[1]; }
public void constructor_should_convert_document_and_scoredoc_to_properties_and_parse_createdon_date() { // Arrange LuceneDocument document = new LuceneDocument(); document.Add(CreateField("id","123")); document.Add(CreateField("title", "the title")); document.Add(CreateField("contentsummary", "the summary")); document.Add(CreateField("tags", "tag1 tag2")); document.Add(CreateField("createdby", "gandhi")); document.Add(CreateField("contentlength", "999")); document.Add(CreateField("createdon", DateTime.Today.ToString())); ScoreDoc scoreDoc = new ScoreDoc(0, 9.50f); // Act SearchResultViewModel model = new SearchResultViewModel(document, scoreDoc); // Assert Assert.That(model.Id, Is.EqualTo(123)); Assert.That(model.Title, Is.EqualTo("the title")); Assert.That(model.ContentSummary, Is.EqualTo("the summary")); Assert.That(model.Tags, Is.EqualTo("tag1 tag2")); Assert.That(model.CreatedBy, Is.EqualTo("gandhi")); Assert.That(model.ContentLength, Is.EqualTo(999)); Assert.That(model.CreatedOn, Is.EqualTo(DateTime.Today)); // only the date should be parsed Assert.That(model.Score, Is.EqualTo(9.50f)); }
/// <summary> Populates the results array with the ScoreDoc instaces. This can be /// overridden in case a different ScoreDoc type should be returned. /// </summary> protected internal virtual void PopulateResults(ScoreDoc[] results, int howMany) { for (int i = howMany - 1; i >= 0; i--) { results[i] = (ScoreDoc) pq.Pop(); } }
// prevents instantiation private TopScoreDocCollector(int numHits) : base(new HitQueue(numHits, true)) { // HitQueue implements getSentinelObject to return a ScoreDoc, so we know // that at this point top() is already initialized. pqTop = (ScoreDoc) pq.Top(); }
private static DataTable ConvertToDataTable(IndexSearcher indexSearcher, ScoreDoc[] result) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn(LuceneConfig.Field_Path)); dt.Columns.Add(new DataColumn(LuceneConfig.Field_FileName)); dt.Columns.Add(new DataColumn(LuceneConfig.Field_PageNumber)); dt.Columns.Add(new DataColumn(LuceneConfig.Field_ContentByPage)); dt.Columns.Add(new DataColumn(LuceneConfig.Field_Score)); foreach (ScoreDoc scoreDoc in result) { Document doc = indexSearcher.Doc(scoreDoc.Doc); DataRow dr = dt.NewRow(); dr[LuceneConfig.Field_Path] = doc.Get(LuceneConfig.Field_Path); dr[LuceneConfig.Field_FileName] = doc.Get(LuceneConfig.Field_FileName); dr[LuceneConfig.Field_PageNumber] = doc.Get(LuceneConfig.Field_PageNumber); dr[LuceneConfig.Field_ContentByPage] = doc.Get(LuceneConfig.Field_ContentByPage); dr[LuceneConfig.Field_Score] = scoreDoc.Score; dt.Rows.Add(dr); } return dt; }
public virtual int Compare(ScoreDoc i, ScoreDoc j) { if (i.score > j.score) return - 1; if (i.score < j.score) return 1; return 0; }
public virtual int Compare(ScoreDoc i, ScoreDoc j) { if (i.doc < j.doc) return - 1; if (i.doc > j.doc) return 1; return 0; }
/// <summary> /// Tests that a Hits has an expected order of documents </summary> public static void CheckDocIds(string mes, int[] results, ScoreDoc[] hits) { Assert.AreEqual(hits.Length, results.Length, mes + " nr of hits"); for (int i = 0; i < results.Length; i++) { Assert.AreEqual(results[i], hits[i].Doc, mes + " doc nrs for hit " + i); } }
protected void CompareRanks(ScoreDoc[] hits, int[] ranks) { Assert.AreEqual(ranks.Length, hits.Length); for (int i = 0; i < ranks.Length; i++) { Assert.AreEqual(ranks[i], hits[i].Doc); } }
public override int Compare(ScoreDoc doc1, ScoreDoc doc2) { for (int i = 0; i < _comparators.Length; ++i) { int v = _comparators[i].Compare(doc1, doc2); if (v != 0) return v; } return 0; }
public void Should_Throw_ArgumentNullException_When_Document_Is_Null() { // Arrange LuceneDocument document = null; ScoreDoc scoreDoc = new ScoreDoc(0, 9.50f); // Act + Assert SearchResultViewModel model = new SearchResultViewModel(document, scoreDoc); }
public PreviousSearchState(Query query, Sort sort, ScoreDoc searchAfterLocal, ScoreDoc searchAfterShard, long[] versions, int numHitsPaged) { this.Versions = (long[])versions.Clone(); this.SearchAfterLocal = searchAfterLocal; this.SearchAfterShard = searchAfterShard; this.Sort = sort; this.Query = query; this.NumHitsPaged = numHitsPaged; SearchTimeNanos = TimeHelper.NanoTime(); }
// javadoc inherited public override TopDocs TopDocs() { FieldSortedHitQueue fshq = (FieldSortedHitQueue) hq; ScoreDoc[] scoreDocs = new ScoreDoc[fshq.Size()]; for (int i = fshq.Size() - 1; i >= 0; i--) // put docs in array scoreDocs[i] = fshq.FillFields((FieldDoc) fshq.Pop()); return new TopFieldDocs(totalHits, scoreDocs, fshq.GetFields(), fshq.GetMaxScore()); }
public int Compare(ScoreDoc i, ScoreDoc j) { int fi = fieldOrder[i.doc]; int fj = fieldOrder[j.doc]; if (fi < fj) return - 1; if (fi > fj) return 1; return 0; }
/// <summary>The top-scoring hits. </summary> public virtual TopDocs TopDocs() { ScoreDoc[] scoreDocs = new ScoreDoc[hq.Size()]; for (int i = hq.Size() - 1; i >= 0; i--) // put docs in array scoreDocs[i] = (ScoreDoc) hq.Pop(); float maxScore = (totalHits == 0) ? System.Single.NegativeInfinity : scoreDocs[0].score; return new TopDocs(totalHits, scoreDocs, maxScore); }
public AllHitsCollector(ScoreDoc[] docs) { this.outOfOrder = true; this.shouldScore = true; hits = new List<AllHit>(docs.Length); foreach (var doc in docs) { this.hits.Add(new AllHit(doc.doc, doc.score)); } }
private int Compare(ScoreDoc doc1, ScoreDoc doc2) { int cmp = comparator.Compare(doc1, doc2); if (cmp != 0) { return -cmp; } else { return doc2.Doc - doc1.Doc; } }
private void CheckHits(ScoreDoc[] hits, int expectedCount, IndexSearcher searcher) { Assert.AreEqual(expectedCount, hits.Length, "total results"); for (int i = 0 ; i < hits.Length; i++) { if (i < 10 || (i > 94 && i < 105)) { Document d = searcher.Doc(hits[i].Doc); Assert.AreEqual(Convert.ToString(i), d.Get(ID_FIELD), "check " + i); } } }
/// <summary> /// Takes O(size) time. /// </summary> /// <param name="newEle"></param> /// <param name="oldEle"></param> /// <returns>the 'bottom' element in the queue.</returns> public virtual ScoreDoc Replace(ScoreDoc newEle, ScoreDoc oldEle) { for (int i = 1; i <= size; ++i) { if (heap[i] == oldEle) { heap[i] = newEle; UpHeap(i); DownHeap(i); break; } } return heap[1]; }
private void AssertHits(Query q, float[] scores) { ScoreDoc[] expected = new ScoreDoc[scores.Length]; int[] expectedDocs = new int[scores.Length]; for (int i = 0; i < expected.Length; i++) { expectedDocs[i] = i; expected[i] = new ScoreDoc(i, scores[i]); } TopDocs docs = @is.Search(q, 10, new Sort(new SortField("id", SortField.Type_e.STRING))); CheckHits.DoCheckHits(Random(), q, "", @is, expectedDocs); CheckHits.CheckHitsQuery(q, expected, docs.ScoreDocs, expectedDocs); CheckHits.CheckExplanations(q, "", @is); }
public IEnumerable <Document> Search(string propertyNameToSearch, string searchTerm, int topCount = int.MaxValue) { LuceneSearch.IndexSearcher searcher = new LuceneSearch.IndexSearcher(IndexDirectory); QueryParser parser = new QueryParser(LuceneVersion, propertyNameToSearch, AnalyzerProvider()); LuceneSearch.Query query = parser.Parse(searchTerm); LuceneSearch.TopDocs hits = searcher.Search(query, topCount); IndexReader reader = IndexReader.Open(IndexDirectory, true); for (int i = 0; i < hits.ScoreDocs.Length; i++) { LuceneSearch.ScoreDoc scoreDoc = hits.ScoreDocs[i]; Document doc = reader.Document(scoreDoc.Doc); yield return(doc); } }
public override int Compare(ScoreDoc doc1, ScoreDoc doc2) { // cannot return v1-v2 because it could overflow if (values[doc1.Doc] < values[doc2.Doc]) { return -1; } else if (values[doc1.Doc] > values[doc2.Doc]) { return 1; } else { return 0; } }
public virtual void TestHugeN() { TaskScheduler service = new LimitedConcurrencyLevelTaskScheduler(4); IndexSearcher[] searchers = new IndexSearcher[] { new IndexSearcher(Reader), new IndexSearcher(Reader, service) }; Query[] queries = new Query[] { new MatchAllDocsQuery(), new TermQuery(new Term("field", "1")) }; Sort[] sorts = new Sort[] { null, new Sort(new SortField("field2", SortField.Type_e.STRING)) }; Filter[] filters = new Filter[] { null, new QueryWrapperFilter(new TermQuery(new Term("field2", "true"))) }; ScoreDoc[] afters = new ScoreDoc[] { null, new FieldDoc(0, 0f, new object[] { new BytesRef("boo!") }) }; foreach (IndexSearcher searcher in searchers) { foreach (ScoreDoc after in afters) { foreach (Query query in queries) { foreach (Sort sort in sorts) { foreach (Filter filter in filters) { searcher.Search(query, int.MaxValue); searcher.SearchAfter(after, query, int.MaxValue); searcher.Search(query, filter, int.MaxValue); searcher.SearchAfter(after, query, filter, int.MaxValue); if (sort != null) { searcher.Search(query, int.MaxValue, sort); searcher.Search(query, filter, int.MaxValue, sort); searcher.Search(query, filter, int.MaxValue, sort, true, true); searcher.Search(query, filter, int.MaxValue, sort, true, false); searcher.Search(query, filter, int.MaxValue, sort, false, true); searcher.Search(query, filter, int.MaxValue, sort, false, false); searcher.SearchAfter(after, query, filter, int.MaxValue, sort); searcher.SearchAfter(after, query, filter, int.MaxValue, sort, true, true); searcher.SearchAfter(after, query, filter, int.MaxValue, sort, true, false); searcher.SearchAfter(after, query, filter, int.MaxValue, sort, false, true); searcher.SearchAfter(after, query, filter, int.MaxValue, sort, false, false); } } } } } } TestUtil.ShutdownExecutorService(service); }
public virtual void TestHugeN() { TaskScheduler service = new LimitedConcurrencyLevelTaskScheduler(4); IndexSearcher[] searchers = new IndexSearcher[] { new IndexSearcher(Reader), new IndexSearcher(Reader, service) }; Query[] queries = new Query[] { new MatchAllDocsQuery(), new TermQuery(new Term("field", "1")) }; Sort[] sorts = new Sort[] { null, new Sort(new SortField("field2", SortFieldType.STRING)) }; Filter[] filters = new Filter[] { null, new QueryWrapperFilter(new TermQuery(new Term("field2", "true"))) }; ScoreDoc[] afters = new ScoreDoc[] { null, new FieldDoc(0, 0f, new object[] { new BytesRef("boo!") }) }; foreach (IndexSearcher searcher in searchers) { foreach (ScoreDoc after in afters) { foreach (Query query in queries) { foreach (Sort sort in sorts) { foreach (Filter filter in filters) { searcher.Search(query, int.MaxValue); searcher.SearchAfter(after, query, int.MaxValue); searcher.Search(query, filter, int.MaxValue); searcher.SearchAfter(after, query, filter, int.MaxValue); if (sort != null) { searcher.Search(query, int.MaxValue, sort); searcher.Search(query, filter, int.MaxValue, sort); searcher.Search(query, filter, int.MaxValue, sort, true, true); searcher.Search(query, filter, int.MaxValue, sort, true, false); searcher.Search(query, filter, int.MaxValue, sort, false, true); searcher.Search(query, filter, int.MaxValue, sort, false, false); searcher.SearchAfter(after, query, filter, int.MaxValue, sort); searcher.SearchAfter(after, query, filter, int.MaxValue, sort, true, true); searcher.SearchAfter(after, query, filter, int.MaxValue, sort, true, false); searcher.SearchAfter(after, query, filter, int.MaxValue, sort, false, true); searcher.SearchAfter(after, query, filter, int.MaxValue, sort, false, false); } } } } } } TestUtil.ShutdownExecutorService(service); }
public static void CheckEqual(Query query, ScoreDoc[] hits1, ScoreDoc[] hits2) { const float scoreTolerance = 1.0e-6f; if (hits1.Length != hits2.Length) { Assert.Fail("Unequal lengths: hits1=" + hits1.Length + ",hits2=" + hits2.Length); } for (int i = 0; i < hits1.Length; i++) { if (hits1[i].Doc != hits2[i].Doc) { Assert.Fail("Hit " + i + " docnumbers don't match\n" + Hits2str(hits1, hits2, 0, 0) + "for query:" + query.ToString()); } if ((hits1[i].Doc != hits2[i].Doc) || Math.Abs(hits1[i].Score - hits2[i].Score) > scoreTolerance) { Assert.Fail("Hit " + i + ", doc nrs " + hits1[i].Doc + " and " + hits2[i].Doc + "\nunequal : " + hits1[i].Score + "\n and: " + hits2[i].Score + "\nfor query:" + query.ToString()); } } }
/// <summary> Returns whether <code>a</code> is less relevant than <code>b</code>.</summary> /// <param name="a">ScoreDoc /// </param> /// <param name="b">ScoreDoc /// </param> /// <returns> <code>true</code> if document <code>a</code> should be sorted after document <code>b</code>. /// </returns> public override bool LessThan(object a, object b) { ScoreDoc docA = (ScoreDoc)a; ScoreDoc docB = (ScoreDoc)b; // run comparators int n = comparators.Length; int c = 0; for (int i = 0; i < n && c == 0; ++i) { c = (fields[i].reverse) ? comparators[i].Compare(docB, docA) : comparators[i].Compare(docA, docB); } // avoid random sort order that could lead to duplicates (bug #31241): if (c == 0) { return(docA.doc > docB.doc); } return(c > 0); }
protected virtual void AssertHits(Query q, float[] scores) { ScoreDoc[] expected = new ScoreDoc[scores.Length]; int[] expectedDocs = new int[scores.Length]; for (int i = 0; i < expected.Length; i++) { expectedDocs[i] = i; expected[i] = new ScoreDoc(i, scores[i]); } TopDocs docs = searcher.Search(q, 2, new Sort(new SortField("id", SortField.Type_e.STRING))); /* for (int i=0;i<docs.scoreDocs.length;i++) { System.out.println(searcher.explain(q, docs.scoreDocs[i].doc)); } */ CheckHits.DoCheckHits(Random(), q, "", searcher, expectedDocs); CheckHits.CheckHitsQuery(q, expected, docs.ScoreDocs, expectedDocs); CheckHits.CheckExplanations(q, "", searcher); }
public int Compare(ScoreDoc i, ScoreDoc j) { System.String is_Renamed = index[i.doc]; System.String js = index[j.doc]; if ((object)is_Renamed == (object)js) { return(0); } else if (is_Renamed == null) { return(-1); } else if (js == null) { return(1); } else { return(collator.Compare(is_Renamed.ToString(), js.ToString())); } }
public override TopFieldDocs Search(Weight weight, Filter filter, int n, Sort sort) { FieldDocSortedHitQueue hq = null; int totalHits = 0; float maxScore = System.Single.NegativeInfinity; for (int i = 0; i < searchables.Length; i++) { // search each searcher TopFieldDocs docs = searchables[i].Search(weight, filter, n, sort); if (hq == null) { hq = new FieldDocSortedHitQueue(docs.fields, n); } totalHits += docs.totalHits; // update totalHits maxScore = System.Math.Max(maxScore, docs.GetMaxScore()); ScoreDoc[] scoreDocs = docs.scoreDocs; for (int j = 0; j < scoreDocs.Length; j++) { // merge scoreDocs into hq ScoreDoc scoreDoc = scoreDocs[j]; scoreDoc.doc += starts[i]; // convert doc if (!hq.Insert(scoreDoc)) { break; // no more scores > minScore } } } ScoreDoc[] scoreDocs2 = new ScoreDoc[hq.Size()]; for (int i = hq.Size() - 1; i >= 0; i--) { // put docs in array scoreDocs2[i] = (ScoreDoc)hq.Pop(); } return(new TopFieldDocs(totalHits, scoreDocs2, hq.GetFields(), maxScore)); }
private static List<FarmLocationDto> ConvertHitsToFarmLocationDtos(ScoreDoc[] hits) { var searcher = WebApiApplication.Searcher; var result = new List<FarmLocationDto>(); foreach (ScoreDoc scoreDoc in hits) { var doc = searcher.Doc(scoreDoc.Doc); double lat; double lng; if (double.TryParse(doc.Get("latitude"), out lat) && double.TryParse(doc.Get("longitude"), out lng)) { result.Add(new FarmLocationDto { Name = doc.Get("name"), Latitude = lat, Longitude = lng }); } } return result; }
internal virtual void AssertPage(int pageStart, TopDocs all, TopDocs paged) { Assert.AreEqual(all.TotalHits, paged.TotalHits); for (int i = 0; i < paged.ScoreDocs.Length; i++) { ScoreDoc sd1 = all.ScoreDocs[pageStart + i]; ScoreDoc sd2 = paged.ScoreDocs[i]; if (isVerbose) { Console.WriteLine(" hit " + (pageStart + i)); Console.WriteLine(" expected id=" + searcher.Doc(sd1.Doc).Get("id") + " " + sd1); Console.WriteLine(" actual id=" + searcher.Doc(sd2.Doc).Get("id") + " " + sd2); } Assert.AreEqual(sd1.Doc, sd2.Doc); Assert.AreEqual(sd1.Score, sd2.Score, 0f); if (sd1 is FieldDoc) { Assert.IsTrue(sd2 is FieldDoc); Assert.AreEqual(((FieldDoc)sd1).Fields, ((FieldDoc)sd2).Fields); } } }
/// <summary> A search implementation allowing sorting which spans a new thread for each /// Searchable, waits for each search to complete and merges /// the results back together. /// </summary> public override TopFieldDocs Search(Weight weight, Filter filter, int nDocs, Sort sort, IState state) { if (sort == null) { throw new ArgumentNullException("sort"); } FieldDocSortedHitQueue hq = new FieldDocSortedHitQueue(nDocs); object lockObj = new object(); Task <TopFieldDocs>[] tasks = new Task <TopFieldDocs> [searchables.Length]; for (int i = 0; i < searchables.Length; i++) // search each searchable { int cur = i; tasks[i] = Task <TopFieldDocs> .Factory.StartNew( () => MultiSearcherCallableWithSort(ThreadLock.MonitorLock, lockObj, searchables[cur], weight, filter, nDocs, hq, sort, cur, starts, state)); } int totalHits = 0; float maxScore = float.NegativeInfinity; Task.WaitAll(tasks); foreach (TopFieldDocs topFieldDocs in tasks.Select(x => x.Result)) { totalHits += topFieldDocs.TotalHits; maxScore = Math.Max(maxScore, topFieldDocs.MaxScore); } ScoreDoc[] scoreDocs = new ScoreDoc[hq.Size()]; for (int i = hq.Size() - 1; i >= 0; i--) { scoreDocs[i] = hq.Pop(); } return(new TopFieldDocs(totalHits, scoreDocs, hq.GetFields(), maxScore)); }
public MergeSortQueue(Sort sort, TopDocs[] shardHits) : base(shardHits.Length) { this.shardHits = new ScoreDoc[shardHits.Length][]; for (int shardIDX = 0; shardIDX < shardHits.Length; shardIDX++) { ScoreDoc[] shard = shardHits[shardIDX].ScoreDocs; //System.out.println(" init shardIdx=" + shardIDX + " hits=" + shard); if (shard != null) { this.shardHits[shardIDX] = shard; // Fail gracefully if API is misused: for (int hitIDX = 0; hitIDX < shard.Length; hitIDX++) { ScoreDoc sd = shard[hitIDX]; if (!(sd is FieldDoc)) { throw new System.ArgumentException("shard " + shardIDX + " was not sorted by the provided Sort (expected FieldDoc but got ScoreDoc)"); } FieldDoc fd = (FieldDoc)sd; if (fd.Fields == null) { throw new System.ArgumentException("shard " + shardIDX + " did not set sort field values (FieldDoc.fields is null); you must pass fillFields=true to IndexSearcher.search on each shard"); } } } } SortField[] sortFields = sort.GetSort(); comparers = new FieldComparer[sortFields.Length]; reverseMul = new int[sortFields.Length]; for (int compIDX = 0; compIDX < sortFields.Length; compIDX++) { SortField sortField = sortFields[compIDX]; comparers[compIDX] = sortField.GetComparer(1, compIDX); reverseMul[compIDX] = sortField.IsReverse ? -1 : 1; } }
public /*protected internal*/ override TopDocs NewTopDocs(ScoreDoc[] results, int start) { if (results == null) { return EMPTY_TOPDOCS; } float maxScore = System.Single.NaN; if (start == 0) { maxScore = results[0].score; } else { for (int i = pq.Size(); i > 1; i--) { pq.Pop(); } maxScore = ((ScoreDoc) pq.Pop()).score; } return new TopDocs(totalHits, results, maxScore); }
override public void Run() { try { docs = (sort == null) ? searchable.Search(weight, filter, nDocs) : searchable.Search(weight, filter, nDocs, sort); } // Store the IOException for later use by the caller of this thread catch (System.IO.IOException ioe) { this.ioe = ioe; } if (this.ioe == null) { // if we are sorting by fields, we need to tell the field sorted hit queue // the actual type of fields, in case the original list contained AUTO. // if the searchable returns null for fields, we'll have problems. if (sort != null) { ((FieldDocSortedHitQueue)hq).SetFields(((TopFieldDocs)docs).fields); } ScoreDoc[] scoreDocs = docs.scoreDocs; for (int j = 0; j < scoreDocs.Length; j++) { // merge scoreDocs into hq ScoreDoc scoreDoc = scoreDocs[j]; scoreDoc.doc += starts[i]; // convert doc //it would be so nice if we had a thread-safe insert lock (hq) { if (!hq.Insert(scoreDoc)) { break; } } // no more scores > minScore } } }
public SearchResultViewModel(Document document, ScoreDoc scoreDoc) { if (document == null) throw new ArgumentNullException("document"); if (scoreDoc == null) throw new ArgumentNullException("scoreDoc"); EnsureFieldsExist(document); Id = int.Parse(document.GetField("id").StringValue); Title = document.GetField("title").StringValue; ContentSummary = document.GetField("contentsummary").StringValue; Tags = document.GetField("tags").StringValue; CreatedBy = document.GetField("createdby").StringValue; ContentLength = int.Parse(document.GetField("contentlength").StringValue); Score = scoreDoc.Score; DateTime createdOn = DateTime.UtcNow; if (!DateTime.TryParse(document.GetField("createdon").StringValue, out createdOn)) createdOn = DateTime.UtcNow; CreatedOn = createdOn; }
// javadoc inherited public override void Collect(int doc, float score) { if (score > 0.0f) { totalHits++; if (reusableSD == null) { reusableSD = new ScoreDoc(doc, score); } else if (score >= reusableSD.score) { // reusableSD holds the last "rejected" entry, so, if // this new score is not better than that, there's no // need to try inserting it reusableSD.doc = doc; reusableSD.score = score; } else { return ; } reusableSD = (ScoreDoc) hq.InsertWithOverflow(reusableSD); } }
public TopDocs Call() { TopDocs docs = Searcher.Search(Arrays.AsList(Slice.Leaves), Weight, After, NDocs); ScoreDoc[] scoreDocs = docs.ScoreDocs; //it would be so nice if we had a thread-safe insert @lock.Lock(); try { for (int j = 0; j < scoreDocs.Length; j++) // merge scoreDocs into hq { ScoreDoc scoreDoc = scoreDocs[j]; if (scoreDoc == Hq.InsertWithOverflow(scoreDoc)) { break; } } } finally { @lock.Unlock(); } return(docs); }
// javadoc inherited public override void Collect(int doc, float score) { if (score > 0.0f) { totalHits++; if (reusableSD == null) { reusableSD = new ScoreDoc(doc, score); } else if (score >= reusableSD.score) { // reusableSD holds the last "rejected" entry, so, if // this new score is not better than that, there's no // need to try inserting it reusableSD.doc = doc; reusableSD.score = score; } else { return; } reusableSD = (ScoreDoc)hq.InsertWithOverflow(reusableSD); } }
internal InOrderPagingScoreDocCollector(ScoreDoc after, int numHits) : base(numHits) { this.after = after; }
public virtual System.IComparable SortValue(ScoreDoc i) { return((System.Int32)i.doc); }
public virtual System.IComparable SortValue(ScoreDoc i) { return((float)i.score); }
public virtual System.IComparable SortValue(ScoreDoc i) { return(index.Lookup[index.Order[i.doc]]); }
/// <summary> /// Finds the top <code>n</code> /// hits for <code>query</code>, applying <code>filter</code> if non-null, /// where all results are after a previous result (<code>after</code>). /// <p> /// By passing the bottom result from a previous page as <code>after</code>, /// this method can be used for efficient 'deep-paging' across potentially /// large result sets. /// </summary> /// <exception cref="BooleanQuery.TooManyClauses"> If a query would exceed /// <seealso cref="BooleanQuery#getMaxClauseCount()"/> clauses. </exception> public virtual TopDocs SearchAfter(ScoreDoc after, Query query, Filter filter, int n) { return(Search(CreateNormalizedWeight(WrapFilter(query, filter)), after, n)); }
/// <summary> /// Finds the top <code>n</code> /// hits for <code>query</code> where all results are after a previous /// result (<code>after</code>). /// <p> /// By passing the bottom result from a previous page as <code>after</code>, /// this method can be used for efficient 'deep-paging' across potentially /// large result sets. /// </summary> /// <exception cref="BooleanQuery.TooManyClauses"> If a query would exceed /// <seealso cref="BooleanQuery#getMaxClauseCount()"/> clauses. </exception> public virtual TopDocs SearchAfter(ScoreDoc after, Query query, int n) { return(Search(CreateNormalizedWeight(query), after, n)); }
/// <summary> /// Expert: Low-level search implementation. Finds the top <code>n</code> /// hits for <code>query</code>. /// /// <p>Applications should usually call <seealso cref="IndexSearcher#search(Query,int)"/> or /// <seealso cref="IndexSearcher#search(Query,Filter,int)"/> instead. </summary> /// <exception cref="BooleanQuery.TooManyClauses"> If a query would exceed /// <seealso cref="BooleanQuery#getMaxClauseCount()"/> clauses. </exception> protected internal virtual TopDocs Search(IList <AtomicReaderContext> leaves, Weight weight, ScoreDoc after, int nDocs) { // single thread int limit = Reader.MaxDoc; if (limit == 0) { limit = 1; } nDocs = Math.Min(nDocs, limit); TopScoreDocCollector collector = TopScoreDocCollector.Create(nDocs, after, !weight.ScoresDocsOutOfOrder()); Search(leaves, weight, collector); return(collector.TopDocs()); }
public virtual System.IComparable SortValue(ScoreDoc i) { return(index[i.doc]); }
[AwaitsFix(BugUrl = "https://github.com/apache/lucenenet/issues/269")] // LUCENENET TODO: this test fails on x86 on .NET Framework in Release mode only #endif public virtual void TestBS2DisjunctionNextVsAdvance() { Directory d = NewDirectory(); RandomIndexWriter w = new RandomIndexWriter( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, d); int numDocs = AtLeast(300); for (int docUpto = 0; docUpto < numDocs; docUpto++) { string contents = "a"; if (Random.Next(20) <= 16) { contents += " b"; } if (Random.Next(20) <= 8) { contents += " c"; } if (Random.Next(20) <= 4) { contents += " d"; } if (Random.Next(20) <= 2) { contents += " e"; } if (Random.Next(20) <= 1) { contents += " f"; } Document doc = new Document(); doc.Add(new TextField("field", contents, Field.Store.NO)); w.AddDocument(doc); } w.ForceMerge(1); IndexReader r = w.GetReader(); IndexSearcher s = NewSearcher(r); w.Dispose(); for (int iter = 0; iter < 10 * RandomMultiplier; iter++) { if (Verbose) { Console.WriteLine("iter=" + iter); } IList <string> terms = new List <string> { "a", "b", "c", "d", "e", "f" }; int numTerms = TestUtil.NextInt32(Random, 1, terms.Count); while (terms.Count > numTerms) { terms.RemoveAt(Random.Next(terms.Count)); } if (Verbose) { Console.WriteLine(" terms=" + terms); } BooleanQuery q = new BooleanQuery(); foreach (string term in terms) { q.Add(new BooleanClause(new TermQuery(new Term("field", term)), Occur.SHOULD)); } Weight weight = s.CreateNormalizedWeight(q); Scorer scorer = weight.GetScorer(s.m_leafContexts[0], null); // First pass: just use .NextDoc() to gather all hits IList <ScoreDoc> hits = new List <ScoreDoc>(); while (scorer.NextDoc() != DocIdSetIterator.NO_MORE_DOCS) { hits.Add(new ScoreDoc(scorer.DocID, scorer.GetScore())); } if (Verbose) { Console.WriteLine(" " + hits.Count + " hits"); } // Now, randomly next/advance through the list and // verify exact match: for (int iter2 = 0; iter2 < 10; iter2++) { weight = s.CreateNormalizedWeight(q); scorer = weight.GetScorer(s.m_leafContexts[0], null); if (Verbose) { Console.WriteLine(" iter2=" + iter2); } int upto = -1; while (upto < hits.Count) { int nextUpto; int nextDoc; int left = hits.Count - upto; if (left == 1 || Random.nextBoolean()) { // next nextUpto = 1 + upto; nextDoc = scorer.NextDoc(); } else { // advance int inc = TestUtil.NextInt32(Random, 1, left - 1); nextUpto = inc + upto; nextDoc = scorer.Advance(hits[nextUpto].Doc); } if (nextUpto == hits.Count) { Assert.AreEqual(DocIdSetIterator.NO_MORE_DOCS, nextDoc); } else { ScoreDoc hit = hits[nextUpto]; Assert.AreEqual(hit.Doc, nextDoc); // Test for precise float equality: Assert.IsTrue(hit.Score == scorer.GetScore(), "doc " + hit.Doc + " has wrong score: expected=" + hit.Score + " actual=" + scorer.GetScore()); } upto = nextUpto; } } } r.Dispose(); d.Dispose(); }
public virtual int Compare(ScoreDoc i, ScoreDoc j) { return(cachedValues[i.Doc].CompareTo(cachedValues[j.Doc])); }
private PreviousSearchState AssertSame(IndexSearcher mockSearcher, NodeState.ShardIndexSearcher shardSearcher, Query q, Sort sort, PreviousSearchState state) { int numHits = TestUtil.NextInt(Random(), 1, 100); if (state != null && state.SearchAfterLocal == null) { // In addition to what we last searched: numHits += state.NumHitsPaged; } if (VERBOSE) { Console.WriteLine("TEST: query=" + q + " sort=" + sort + " numHits=" + numHits); if (state != null) { Console.WriteLine(" prev: searchAfterLocal=" + state.SearchAfterLocal + " searchAfterShard=" + state.SearchAfterShard + " numHitsPaged=" + state.NumHitsPaged); } } // Single (mock local) searcher: TopDocs hits; if (sort == null) { if (state != null && state.SearchAfterLocal != null) { hits = mockSearcher.SearchAfter(state.SearchAfterLocal, q, numHits); } else { hits = mockSearcher.Search(q, numHits); } } else { hits = mockSearcher.Search(q, numHits, sort); } // Shard searcher TopDocs shardHits; if (sort == null) { if (state != null && state.SearchAfterShard != null) { shardHits = shardSearcher.SearchAfter(state.SearchAfterShard, q, numHits); } else { shardHits = shardSearcher.Search(q, numHits); } } else { shardHits = shardSearcher.Search(q, numHits, sort); } int numNodes = shardSearcher.NodeVersions.Length; int[] @base = new int[numNodes]; IList <IndexReaderContext> subs = mockSearcher.TopReaderContext.Children; Assert.AreEqual(numNodes, subs.Count); for (int nodeID = 0; nodeID < numNodes; nodeID++) { @base[nodeID] = subs[nodeID].DocBaseInParent; } if (VERBOSE) { /* * for(int shardID=0;shardID<shardSearchers.Length;shardID++) { * System.out.println(" shard=" + shardID + " maxDoc=" + shardSearchers[shardID].searcher.getIndexReader().MaxDoc); * } */ Console.WriteLine(" single searcher: " + hits.TotalHits + " totalHits maxScore=" + hits.MaxScore); for (int i = 0; i < hits.ScoreDocs.Length; i++) { ScoreDoc sd = hits.ScoreDocs[i]; Console.WriteLine(" doc=" + sd.Doc + " score=" + sd.Score); } Console.WriteLine(" shard searcher: " + shardHits.TotalHits + " totalHits maxScore=" + shardHits.MaxScore); for (int i = 0; i < shardHits.ScoreDocs.Length; i++) { ScoreDoc sd = shardHits.ScoreDocs[i]; Console.WriteLine(" doc=" + sd.Doc + " (rebased: " + (sd.Doc + @base[sd.ShardIndex]) + ") score=" + sd.Score + " shard=" + sd.ShardIndex); } } int numHitsPaged; if (state != null && state.SearchAfterLocal != null) { numHitsPaged = hits.ScoreDocs.Length; if (state != null) { numHitsPaged += state.NumHitsPaged; } } else { numHitsPaged = hits.ScoreDocs.Length; } bool moreHits; ScoreDoc bottomHit; ScoreDoc bottomHitShards; if (numHitsPaged < hits.TotalHits) { // More hits to page through moreHits = true; if (sort == null) { bottomHit = hits.ScoreDocs[hits.ScoreDocs.Length - 1]; ScoreDoc sd = shardHits.ScoreDocs[shardHits.ScoreDocs.Length - 1]; // Must copy because below we rebase: bottomHitShards = new ScoreDoc(sd.Doc, sd.Score, sd.ShardIndex); if (VERBOSE) { Console.WriteLine(" save bottomHit=" + bottomHit); } } else { bottomHit = null; bottomHitShards = null; } } else { Assert.AreEqual(hits.TotalHits, numHitsPaged); bottomHit = null; bottomHitShards = null; moreHits = false; } // Must rebase so Assert.AreEqual passes: for (int hitID = 0; hitID < shardHits.ScoreDocs.Length; hitID++) { ScoreDoc sd = shardHits.ScoreDocs[hitID]; sd.Doc += @base[sd.ShardIndex]; } TestUtil.AssertEquals(hits, shardHits); if (moreHits) { // Return a continuation: return(new PreviousSearchState(q, sort, bottomHit, bottomHitShards, shardSearcher.NodeVersions, numHitsPaged)); } else { return(null); } }
public virtual System.IComparable SortValue(ScoreDoc i) { return(cachedValues[i.Doc]); }
public PreviousSearchState(Query query, Sort sort, ScoreDoc searchAfterLocal, ScoreDoc searchAfterShard, long[] versions, int numHitsPaged) { this.Versions = (long[])versions.Clone(); this.SearchAfterLocal = searchAfterLocal; this.SearchAfterShard = searchAfterShard; this.Sort = sort; this.Query = query; this.NumHitsPaged = numHitsPaged; SearchTimeNanos = Time.NanoTime(); }
private static void AssertTopDocsEquals(ScoreDoc[] scoreDocs1, ScoreDoc[] scoreDocs2) { assertEquals(scoreDocs1.Length, scoreDocs2.Length); for (int i = 0; i < scoreDocs1.Length; ++i) { ScoreDoc scoreDoc1 = scoreDocs1[i]; ScoreDoc scoreDoc2 = scoreDocs2[i]; assertEquals(scoreDoc1.Doc, scoreDoc2.Doc); assertEquals(scoreDoc1.Score, scoreDoc2.Score, 0.001f); } }
public int Compare(ScoreDoc i, ScoreDoc j) { return(collator.Compare(index[i.doc].ToString(), index[j.doc].ToString())); }
// prevents instantiation private TopScoreDocCollector(int numHits) : base(new HitQueue(numHits, true)) { // HitQueue implements getSentinelObject to return a ScoreDoc, so we know // that at this point top() is already initialized. pqTop = (ScoreDoc)pq.Top(); }
public SearcherCallableNoSort(ReentrantLock @lock, IndexSearcher searcher, LeafSlice slice, Weight weight, ScoreDoc after, int nDocs, HitQueue hq) { this.@lock = @lock; this.Searcher = searcher; this.Weight = weight; this.After = after; this.NDocs = nDocs; this.Hq = hq; this.Slice = slice; }
public virtual System.IComparable SortValue(ScoreDoc i) { return(fieldOrder[i.doc]); }
// TODO: broadcastNodeExpire? then we can purge the // known-stale cache entries... // MOCK: in a real env you have to hit the wire // (send this query to all remote nodes // concurrently): internal virtual TopDocs SearchNode(int nodeID, long[] nodeVersions, Query q, Sort sort, int numHits, ScoreDoc searchAfter) { NodeState.ShardIndexSearcher s = m_nodes[nodeID].Acquire(nodeVersions); try { if (sort is null) { if (searchAfter != null) { return(s.LocalSearchAfter(searchAfter, q, numHits)); } else { return(s.LocalSearch(q, numHits)); } } else { if (Debugging.AssertsEnabled) { Debugging.Assert(searchAfter is null); // not supported yet } return(s.LocalSearch(q, numHits, sort)); } } finally { NodeState.Release(s); // LUCENENET: made static per CA1822 and eliminated array lookup } }
protected internal virtual void PrintHits(System.String test, ScoreDoc[] h, Searcher searcher) { System.Console.Error.WriteLine("------- " + test + " -------"); for (int i = 0; i < h.Length; i++) { Document d = searcher.Doc(h[i].Doc); float score = h[i].Score; System.Console.Error.WriteLine("#" + i + ": {0.000000}" + score + " - " + d.Get("id") + " - " + d.Get("data")); } }
public virtual TopDocs LocalSearchAfter(ScoreDoc after, Query query, int numHits) { return(base.SearchAfter(after, query, numHits)); }