Beispiel #1
0
        public IEnumerable <IDictionary <string, string> > Search(string q)
        {
            q = q.ToLower();

            // by default, results will not include dropped objects, but you can add dropped:? to over-ride this
            if (!q.Contains("dropped:"))
            {
                q = string.Format("({0}) -dropped:true", q);
            }

            var query = _parser.Parse(q);

            var topFieldCollector = TopFieldCollector.Create(
                sort: Sort.RELEVANCE,
                numHits: _resultsLimit,
                fillFields: false,
                trackDocScores: true,
                trackMaxScore: false,
                docsScoredInOrder: false
                );

            _searcher.Search(query, topFieldCollector);

            var topDocs = topFieldCollector.TopDocs();

            if (topDocs == null)
            {
                return(Enumerable.Repeat(new Dictionary <string, string>(), 0));
            }

            var results = topDocs.ScoreDocs.Select(hit => Docs.DocToDict(_searcher.Doc(hit.Doc), hit.Score)).ToArray();

            _logger.Debug("Search of '{0}' return {1} results.", q, results.Count());
            return(results);
        }
        public IEnumerable <IDictionary <string, string> > Search(string q)
        {
            var query = _parser.Parse(q);

            var topFieldCollector = TopFieldCollector.Create(
                sort: Sort.RELEVANCE,
                numHits: 1000,
                fillFields: false,
                trackDocScores: true,
                trackMaxScore: false,
                docsScoredInOrder: false
                );

            _searcher.Search(query, topFieldCollector);

            var topDocs = topFieldCollector.TopDocs();

            if (topDocs == null)
            {
                return(new List <IDictionary <string, string> >());
            }

            var missingIndices = topDocs.ScoreDocs.Select(hit => Docs.DocToDict(_searcher.Doc(hit.Doc), hit.Score)).ToArray();

            _logger.Debug("Search of '{0}' return {1} results.", q, missingIndices.Count());

            return(missingIndices.OrderByDescending(dict => dict["score"]).ThenByDescending(dict => dict["rank"]).Take(_resultsLimit));
        }
        public IEnumerable <IDictionary <string, string> > ReadHighScores(int limit)
        {
            var results = new List <IDictionary <string, string> >();

            if (_directory != null)
            {
                try {
                    var reader  = IndexReader.Open(_directory, true);
                    var numDocs = reader.NumDocs();
                    for (var i = 0; i < numDocs; i++)
                    {
                        if (reader.IsDeleted(i))
                        {
                            continue;
                        }
                        results.Add(Docs.DocToDict(reader.Document(i)));
                    }

                    _logger.Debug("Closing");
                    reader.Dispose();
                }
                catch (Exception) {
                    _logger.Info("Attempted to read empty search index.");
                }
            }

            _logger.Info("Found {0}.", results.Count);

            return(results.OrderByDescending(dict => dict["score"]).Take(limit));
        }
Beispiel #4
0
        public IDictionary <string, string> Find(string id)
        {
            var keywordAnalyzer = new KeywordAnalyzer();
            var parser          = new QueryParser(Version.LUCENE_30, "id", keywordAnalyzer);
            var query           = parser.Parse(id);
            var scoreDocs       = _searcher.Search(query, 1).ScoreDocs;

            if (scoreDocs != null && scoreDocs.Length > 0)
            {
                return(Docs.DocToDict(_searcher.Doc(scoreDocs[0].Doc), 1f));
            }

            return(null);
        }