Ejemplo n.º 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;
                }
            }));
        }
Ejemplo n.º 2
0
        public static List <T> RawQuery <T>(Query query, SortOptions sortOptions = null)
            where T : class
        {
            List <T> results = new List <T>();

            IndexSearcher searcher = GetIndexSearcher();

            TopDocsCollector collector;

            if (sortOptions == null)
            {
                collector = TopScoreDocCollector.create(searcher.MaxDoc(), true);
            }
            else
            {
                collector = TopFieldCollector.create(
                    new Sort(new SortField(sortOptions.FieldName, (int)sortOptions.FieldType, sortOptions.Ascending)),
                    searcher.MaxDoc(),
                    false,
                    false,
                    false,
                    true
                    );
            }

            searcher.Search(query, collector);
            var topDocs   = collector.TopDocs();
            var scoreDocs = topDocs.ScoreDocs;

            var maxRecord = scoreDocs.Length;

            for (int index = 0; index < maxRecord && index < scoreDocs.Length; index++)
            {
                ScoreDoc scoreDoc = scoreDocs[index];
                Document doc      = searcher.Doc(scoreDoc.doc);

                var data = doc.Get("Data");

                var result = JsonConvert.DeserializeObject(data, _jsonSerializerSettings) as T;

                if (result == null)
                {
                    continue;
                }

                results.Add(result);
            }

            return(results);
        }
Ejemplo n.º 3
0
        private void DoSearch(Query query, IEnumerable <SortField> sortField, int maxResults)
        {
            //This try catch is because analyzers strip out stop words and sometimes leave the query
            //with null values. This simply tries to extract terms, if it fails with a null
            //reference then its an invalid null query, NotSupporteException occurs when the query is
            //valid but the type of query can't extract terms.
            //This IS a work-around, theoretically Lucene itself should check for null query parameters
            //before throwing exceptions.
            try
            {
                var set = new Hashtable();
                query.ExtractTerms(set);
            }
            catch (NullReferenceException)
            {
                //this means that an analyzer has stipped out stop words and now there are
                //no words left to search on
                TotalItemCount = 0;
                return;
            }
            catch (NotSupportedException)
            {
                //swallow this exception, we should continue if this occurs.
            }

            maxResults = maxResults >= 1 ? maxResults : LuceneSearcher.MaxDoc();

            Collector topDocsCollector;

            if (sortField.Any())
            {
                topDocsCollector = TopFieldCollector.create(
                    new Sort(sortField.ToArray()), maxResults, false, false, false, false);
            }
            else
            {
                topDocsCollector = TopScoreDocCollector.create(maxResults, true);
            }

            LuceneSearcher.Search(query, topDocsCollector);

            _topDocs = sortField.Any()
                ? ((TopFieldCollector)topDocsCollector).TopDocs()
                : ((TopScoreDocCollector)topDocsCollector).TopDocs();

            TotalItemCount = _topDocs.TotalHits;
        }
Ejemplo n.º 4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private org.apache.lucene.search.TopDocs getTopDocs(org.apache.lucene.search.Sort sort, int size) throws java.io.IOException
        private TopDocs GetTopDocs(Sort sort, int size)
        {
            TopDocs topDocs;

            if (sort == Sort.RELEVANCE)
            {
                TopScoreDocCollector collector = TopScoreDocCollector.create(size);
                ReplayTo(collector);
                topDocs = collector.topDocs();
            }
            else
            {
                TopFieldCollector collector = TopFieldCollector.create(sort, size, false, true, false);
                ReplayTo(collector);
                topDocs = collector.topDocs();
            }
            return(topDocs);
        }
Ejemplo n.º 5
0
        public List <LuceneResult> Search(Query query, Sort sort)
        {
            var searcher  = new IndexSearcher(_rd);
            var collector = TopFieldCollector.create(sort ?? new Sort(), searcher.MaxDoc(), false, true, true, sort == null);

            searcher.Search(query, collector);
            var docs     = collector.TopDocs();
            var maxscore = docs.GetMaxScore();

            // Note: cheap way to avoid div/zero
            if (maxscore == 0)
            {
                maxscore = 1;
            }
            return((from hit in docs.scoreDocs
                    let score = hit.score / maxscore
                                where score >= 0.001f
                                select new LuceneResult(searcher.Doc(hit.doc), score)).ToList());
        }
Ejemplo n.º 6
0
        public IEnumerable <ISearchHit> Search()
        {
            var query = CreateQuery();

            IndexSearcher searcher;

            try {
                searcher = new IndexSearcher(_directory, true);
            }
            catch {
                // index might not exist if it has been rebuilt
                Logger.Information("Attempt to read a none existing index");
                return(Enumerable.Empty <ISearchHit>());
            }

            try {
                var sort = String.IsNullOrEmpty(_sort)
                               ? Sort.RELEVANCE
                               : new Sort(new SortField(_sort, _comparer, _sortDescending));
                var collector = TopFieldCollector.create(
                    sort,
                    _count + _skip,
                    false,
                    true,
                    false,
                    true);

                Logger.Debug("Searching: {0}", query.ToString());
                searcher.Search(query, collector);

                var results = collector.TopDocs().ScoreDocs
                              .Skip(_skip)
                              .Select(scoreDoc => new LuceneSearchHit(searcher.Doc(scoreDoc.doc), scoreDoc.score))
                              .ToList();

                Logger.Debug("Search results: {0}", results.Count);

                return(results);
            }
            finally {
                searcher.Close();
            }
        }
Ejemplo n.º 7
0
        public IEnumerable <ContentSearchResult> Search(string searchTerm, int start, int count, bool newest, out int total)
        {
            int              limit = start + count;
            Searcher         searcher;
            TopDocsCollector collector = !newest
                ? (TopDocsCollector)TopScoreDocCollector.create(limit, true)
                : (TopDocsCollector)TopFieldCollector.create(
                new Sort(new SortField("modified", SortField.STRING, true)),
                limit, true, true, true, true);

            if (RunQuery(searchTerm, out searcher, collector))
            {
                total = collector.GetTotalHits();
                return(EnumSearchResults(start, searcher, collector, limit));
            }

            total = 0;
            return(new ContentSearchResult[0]);
        }
Ejemplo n.º 8
0
        public IEnumerable <ContentSearchResult> Similar(string uriExisting, int limit, bool newest)
        {
            var docs = IndexReader.TermDocs(new Term("uri", uriExisting));

            if (docs.Next())
            {
                int      docid = docs.Doc();
                Document info  = IndexReader.Document(docid);
                string   title = info.GetField("title").StringValue();

                TermFreqVector[] terms = IndexReader.GetTermFreqVectors(docid);
                if (terms.Length == 1)
                {
                    List <TermQuery> find = new List <TermQuery>();
                    int[]            freq = terms[0].GetTermFrequencies();
                    int      limitfreq    = Math.Max(10, (int)(freq.Max() * 0.5));
                    string[] word         = terms[0].GetTerms();
                    for (int i = 0; i < freq.Length && i < word.Length; i++)
                    {
                        if (freq[i] > 1 && freq[i] < limitfreq)
                        {
                            find.Add(new TermQuery(new Term("contents", word[i])));
                        }
                    }

                    TopDocsCollector collector = !newest
                                                     ? (TopDocsCollector)TopScoreDocCollector.create(limit, true)
                                                     : (TopDocsCollector)TopFieldCollector.create(
                        new Sort(new SortField("modified", SortField.STRING, true)),
                        limit, true, true, true, true);

                    Searcher searcher;
                    if (RunQuery(new BooleanQuery().Combine(find.ToArray()), out searcher, collector))
                    {
                        return(EnumSearchResults(0, searcher, collector, limit + 1)
                               .Where(r => r.Uri != uriExisting).Take(limit));
                    }
                }
            }

            return(new ContentSearchResult[0]);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Realiza a pesquisa a recupera a quantidade de itens do resultado.
        /// </summary>
        /// <param name="query"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        private int SearchCount(Query query, Filter filter)
        {
            var reader   = CreateReader();
            var searcher = new IndexSearcher(reader);

            try
            {
                if (query != null)
                {
                    var weight    = query.Weight(searcher);
                    var collector = TopFieldCollector.create(new global::Lucene.Net.Search.Sort(), 1, true, false, false, !weight.ScoresDocsOutOfOrder());
                    searcher.Search(weight, filter, collector);
                    return(collector.GetTotalHits());
                }
            }
            finally
            {
                searcher.Close();
                reader.Close();
            }
            return(0);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Executa a pesquisa
        /// </summary>
        /// <returns></returns>
        private Collector ExecuteSearch(int pageIndex)
        {
            Collector        collector = null;
            TopDocsCollector result    = null;
            var totalHits = _options.RandomResult ? 1 : (pageIndex + 1) * (_options.HitsPerPage > 0 ? _options.HitsPerPage : 50);

            if (totalHits == 0)
            {
                totalHits = 1;
            }
            global::Lucene.Net.Search.Query query = _query;
            if (_sort != null && query == null)
            {
                var parser = new global::Lucene.Net.QueryParsers.QueryParser(global::Lucene.Net.Util.Version.LUCENE_29, "test", new global::Lucene.Net.Analysis.Standard.StandardAnalyzer(global::Lucene.Net.Util.Version.LUCENE_29, Stopwords.PORTUGUESE_SET));
                query  = parser.Parse("test:1");
                result = TopFieldCollector.create(_sort, totalHits, true, false, false, true);
            }
            if (query != null)
            {
                var weight = query.Weight(Searcher);
                collector = result = TopFieldCollector.create(_sort, totalHits, true, false, false, !weight.ScoresDocsOutOfOrder());
                if (_options.RandomResult)
                {
                    collector = new RandomCollectorWrapper(this, this.Searcher, result);
                }
                else if (_channelFields == null || _channelFields.Length == 0)
                {
                    collector = new SummaryCollectorWrapper(this, this.Searcher, result);
                }
                this.Searcher.Search(weight, _filter, collector);
                if (collector is SummaryCollectorWrapper)
                {
                    var sCollector = ((SummaryCollectorWrapper)collector);
                    _summaries     = sCollector.GetSummaries().ToList();
                    _channelFields = sCollector.GetChannelsFields().ToArray();
                }
                if (collector is RandomCollectorWrapper)
                {
                    var wrapper       = (RandomCollectorWrapper)collector;
                    var wrapperResult = wrapper.GetResult();
                    _result = wrapperResult is Element[] ? (Element[])wrapperResult : wrapperResult.ToArray();
                    _count  = _result.Length;
                    OnLoad();
                    return(null);
                }
                _count = result.GetTotalHits();
                OnLoad();
                return(result);
            }
            else
            {
                SummaryCollectorWrapper wrapper = null;
                if (_options.RandomResult)
                {
                    wrapper = new RandomCollectorWrapper(this);
                    _result = ((RandomCollectorWrapper)wrapper).GetResult().ToArray();
                }
                else
                {
                    collector      = wrapper = new SummaryCollectorWrapper(this);
                    _channelFields = wrapper.GetChannelsFields().ToArray();
                }
                _count     = wrapper.GetTotalHits();
                _summaries = wrapper.GetSummaries().ToList();
                OnLoad();
                return(wrapper);
            }
        }
Ejemplo n.º 11
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: static org.apache.lucene.search.TopFieldCollector scoringCollector(org.apache.lucene.search.Sort sorting, int n) throws java.io.IOException
        internal static TopFieldCollector ScoringCollector(Sort sorting, int n)
        {
            return(TopFieldCollector.create(sorting, n, false, true, false));
        }