예제 #1
0
        public IList<LuceneResult> Search(Query query, SortField sortField, int limit, int offset, float threshold, LuceneProfiler profiler, Plug authPlug) {
            return _disposalLock.ExecuteWithReadLock(() => {
                using(profiler.ProfileQueryInternals()) {
                    EnsureInstanceNotDisposed();
                    var searcher = GetSearcher();
                    int numHits;
                    if(authPlug == null) {
                        numHits = Math.Min(searcher.MaxDoc(), limit == int.MaxValue ? int.MaxValue : limit + offset);
                    } else {
                        var setSizeGuessLong = ((long)offset + limit) * 5;
                        setSizeGuessLong = Math.Max(1000, setSizeGuessLong);
                        var setSizeGuess = setSizeGuessLong.ToInt();
                        numHits = Math.Min(searcher.MaxDoc(), setSizeGuess);
                    }
                    var collector = TopFieldCollector.create(
                        sortField == null ? new Sort() : new Sort(sortField),
                        numHits,
                        false, // fillFields, not required
                        true, // trackDocScores, required
                        true, // trackMaxScore, related to trackDocScores
                        sortField == null // should docs be in docId order?
                    );
                    searcher.Search(query, collector);
                    var docs = collector.TopDocs();
                    var maxscore = docs.GetMaxScore();

                    // Note: cheap way to avoid div/zero
                    if(maxscore == 0) {
                        maxscore = 1;
                    }
                    var items = from hit in docs.scoreDocs
                                let score = hit.score / maxscore
                                where score >= threshold
                                select new LuceneResult(searcher.Doc(hit.doc), score);
                    IList<LuceneResult> resultSet;
                    if(authPlug == null) {
                        if(offset > 0 || limit != int.MaxValue) {
                            items = items.Skip(offset).Take(limit);
                        }
                        resultSet = items.ToList();
                    } else {
                        resultSet = LuceneResultFilter.Filter(authPlug, items, offset, limit, new Result<IList<LuceneResult>>()).Wait();
                    }
                    return resultSet;
                }
            });
        }
예제 #2
0
        public IList <LuceneResult> Search(Query query, SortField sortField, int limit, int offset, float threshold, LuceneProfiler profiler, Plug authPlug)
        {
            return(_disposalLock.ExecuteWithReadLock(() => {
                using (profiler.ProfileQueryInternals()) {
                    EnsureInstanceNotDisposed();
                    var searcher = GetSearcher();
                    int numHits;
                    if (authPlug == null)
                    {
                        numHits = Math.Min(searcher.MaxDoc(), limit == int.MaxValue ? int.MaxValue : limit + offset);
                    }
                    else
                    {
                        var setSizeGuessLong = ((long)offset + limit) * 5;
                        setSizeGuessLong = Math.Max(1000, setSizeGuessLong);
                        var setSizeGuess = setSizeGuessLong.ToInt();
                        numHits = Math.Min(searcher.MaxDoc(), setSizeGuess);
                    }
                    var collector = TopFieldCollector.create(
                        sortField == null ? new Sort() : new Sort(sortField),
                        numHits,
                        false,            // fillFields, not required
                        true,             // trackDocScores, required
                        true,             // trackMaxScore, related to trackDocScores
                        sortField == null // should docs be in docId order?
                        );
                    searcher.Search(query, collector);
                    var docs = collector.TopDocs();
                    var maxscore = docs.GetMaxScore();

                    // Note: cheap way to avoid div/zero
                    if (maxscore == 0)
                    {
                        maxscore = 1;
                    }
                    var items = from hit in docs.scoreDocs
                                let score = hit.score / maxscore
                                            where score >= threshold
                                            select new LuceneResult(searcher.Doc(hit.doc), score);
                    IList <LuceneResult> resultSet;
                    if (authPlug == null)
                    {
                        if (offset > 0 || limit != int.MaxValue)
                        {
                            items = items.Skip(offset).Take(limit);
                        }
                        resultSet = items.ToList();
                    }
                    else
                    {
                        resultSet = LuceneResultFilter.Filter(authPlug, items, offset, limit, new Result <IList <LuceneResult> >()).Wait();
                    }
                    return resultSet;
                }
            }));
        }