コード例 #1
0
 /// <summary>
 /// Recupera o total de hits.
 /// </summary>
 /// <returns></returns>
 public virtual int GetTotalHits()
 {
     if (_instance != null)
     {
         return(_instance.GetTotalHits());
     }
     else
     {
         return(_searchResult.DataRepository.Count);
     }
 }
コード例 #2
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]);
        }
コード例 #3
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);
            }
        }
コード例 #4
0
        public virtual PagedList <Models.ResultObject> Search(string key, int pageIndex, int pageSize, params string[] folders)
        {
            var indexDirectory = FSDirectory.Open(new DirectoryInfo(indexDir));

            if (!IndexReader.IndexExists(indexDirectory) || string.IsNullOrEmpty(key) && (folders == null || folders.Length == 0))
            {
                return(new PagedList <ResultObject>(new ResultObject[0], pageIndex, pageSize, 0));
            }

            var query = new BooleanQuery();

            key = QueryParser.Escape(key.Trim().ToLower());

            if (string.IsNullOrEmpty(key))
            {
                key = "*:*";
            }

            QueryParser titleParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, Converter.TitleFieldName, this.Analyzer);
            var         titleQuery  = titleParser.Parse(key);

            titleQuery.SetBoost(2);
            query.Add(titleQuery, BooleanClause.Occur.SHOULD);

            QueryParser bodyParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, Converter.BodyFieldName, this.Analyzer);
            var         bodyQuery  = bodyParser.Parse(key);

            bodyQuery.SetBoost(1);
            query.Add(bodyQuery, BooleanClause.Occur.SHOULD);

            QueryWrapperFilter filter = null;

            if (folders != null && folders.Length > 0)
            {
                var folderQuery = new BooleanQuery();
                //QueryParser folderParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "FolderName", this.Analyzer);
                foreach (var folder in folders)
                {
                    var termQuery = new TermQuery(new Term("FolderName", folder));
                    termQuery.SetBoost(3);
                    folderQuery.Add(termQuery, BooleanClause.Occur.SHOULD);
                }

                filter = new QueryWrapperFilter(folderQuery);
            }

            var searcher = new IndexSearcher(indexDirectory, true);
            TopDocsCollector collecltor = TopScoreDocCollector.create(searcher.MaxDoc(), false);

            if (filter == null)
            {
                searcher.Search(query, collecltor);
            }
            else
            {
                searcher.Search(query, filter, collecltor);
            }


            Lucene.Net.Highlight.Highlighter lighter =
                new Highlighter(new SimpleHTMLFormatter("<strong class='highlight'>", "</strong>"), new Lucene.Net.Highlight.QueryScorer((Query)query));


            var startIndex = (pageIndex - 1) * pageSize;

            List <ResultObject> results = new List <ResultObject>();

            foreach (var doc in collecltor.TopDocs(startIndex, pageSize).ScoreDocs)
            {
                var          document = searcher.Doc(doc.doc);
                ResultObject result   = Converter.ToResultObject(lighter, document);
                if (result != null)
                {
                    results.Add(result);
                }
            }

            return(new PagedList <ResultObject>(results, pageIndex, pageSize, collecltor.GetTotalHits()));
        }