Пример #1
0
        public Result <T> Search(N2.Persistence.Search.Query query)
        {
            if (!query.IsValid())
            {
                logger.Warn("Invalid query");
                return(Result <T> .Empty);
            }

            var luceneQuery = query.ToLuceneQuery();

            logger.Warn("Prepared lucene query " + luceneQuery);

            var q = accessor.GetQueryParser().Parse(luceneQuery);
            var s = accessor.GetSearcher();

            TopDocs hits;

            if (string.IsNullOrEmpty(query.SortField))
            {
                hits = s.Search(q, query.SkipHits + query.TakeHits);
            }
            else
            {
                hits = s.Search(
                    query: q,
                    filter: null,
                    n: query.SkipHits + query.TakeHits,
                    sort: new Sort(
                        query.SortFields.Select(
                            field => new SortField(field.SortField, GetSortFieldType(field.SortField), field.SortDescending)).ToArray()));
            }

            return(CreateResults(query, s, hits));
        }
Пример #2
0
        public Result Search(N2.Persistence.Search.Query query)
        {
            if (!query.IsValid())
            {
                return(Result.Empty);
            }

            var s = accessor.GetSearcher();

            try
            {
                var q    = CreateQuery(query);
                var hits = s.Search(q, query.SkipHits + query.TakeHits);

                var result = new Result();
                result.Total = hits.totalHits;
                var resultHits = hits.scoreDocs.Skip(query.SkipHits).Take(query.TakeHits).Select(hit =>
                {
                    var doc          = s.Doc(hit.doc);
                    int id           = int.Parse(doc.Get("ID"));
                    ContentItem item = persister.Get(id);
                    return(new Hit {
                        Content = item, Score = hit.score
                    });
                }).Where(h => h.Content != null).ToList();
                result.Hits  = resultHits;
                result.Count = resultHits.Count;
                return(result);
            }
            finally
            {
                //s.Close();
            }
        }
Пример #3
0
        public Result Search(N2.Persistence.Search.Query query)
        {
            if (!query.IsValid())
            {
                return(Result.Empty);
            }

            var s = accessor.GetSearcher();

            try
            {
                var q = CreateQuery(query);
                //new TopFieldDocCollector(s.GetIndexReader(), new Sort(
                TopDocs hits;
                if (string.IsNullOrEmpty(query.SortField))
                {
                    hits = s.Search(q, query.SkipHits + query.TakeHits);
                }
                else
                {
                    hits = s.Search(
                        query: q,
                        filter: null,
                        n: query.SkipHits + query.TakeHits,
                        sort: new Sort(
                            query.SortFields.Select(
                                field => new SortField(field.SortField, GetSortFieldType(field.SortField), field.SortDescending)).ToArray()));
                }

                var result = new Result();
                result.Total = hits.TotalHits;
                var resultHits = hits.ScoreDocs.Skip(query.SkipHits).Take(query.TakeHits).Select(hit =>
                {
                    var doc          = s.Doc(hit.Doc);
                    int id           = int.Parse(doc.Get("ID"));
                    ContentItem item = persister.Get(id);
                    return(new Hit {
                        Content = item, Score = hit.Score
                    });
                }).Where(h => h.Content != null).ToList();
                result.Hits  = resultHits;
                result.Count = resultHits.Count;
                return(result);
            }
            finally
            {
                //s.Close();
            }
        }
Пример #4
0
        /// <summary>Delets an item from the index and any descendants.</summary>
        /// <param name="itemID">The id of the item to delete.</param>
        public virtual void Delete(int itemID)
        {
            logger.Debug("Deleting item #" + itemID);

            lock (accessor)
            {
                var    iw    = accessor.GetWriter();
                var    s     = accessor.GetSearcher();
                string trail = GetTrail(s, new Term(Properties.ID, itemID.ToString()));
                if (trail == null)
                {
                    return;                     // not indexed
                }
                var query = new PrefixQuery(new Term(Properties.Trail, trail));
                iw.DeleteDocuments(query);
                iw.Commit();
                accessor.RecreateSearcher();
            }
        }