Example #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));
        }
        protected override Result <LightweightHitData> CreateResults(N2.Persistence.Search.Query query, IndexSearcher s, TopDocs hits)
        {
            logger.InfoFormat("Creating results for query {0} and {1} hits", query.Text, hits.TotalHits);

            var result = new Result <LightweightHitData>();

            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"));
                return(new Hit <LightweightHitData>
                {
                    Content = new LightweightHitData
                    {
                        ID = id,
                        AlteredPermissions = (Security.Permission) int.Parse(doc.Get("AlteredPermissions")),
                        State = (ContentState)int.Parse(doc.Get("State")),
                        Visible = Convert.ToBoolean(doc.Get("Visible")),
                        AuthorizedRoles = doc.Get("Roles").Split(' '),
                        Path = doc.Get("Path")
                    },
                    Title = doc.Get("Title"),
                    Url = doc.Get("Url"),
                    Score = hit.Score
                });
            }).ToList();

            result.Hits  = resultHits;
            result.Count = resultHits.Count;
            return(result);
        }
Example #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);
                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();
            }
        }
Example #4
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();
            }
        }
Example #5
0
 protected abstract Result <T> CreateResults(N2.Persistence.Search.Query query, IndexSearcher s, TopDocs hits);
Example #6
0
        protected virtual Lucene.Net.Search.Query CreateQuery(N2.Persistence.Search.Query query)
        {
            var q = "";

            if (!string.IsNullOrEmpty(query.Text))
            {
                q = query.OnlyPages.HasValue
                                         ? string.Format("+(Title:({0})^4 Text:({0}) PartsText:({0}))", query.Text)
                                         : string.Format("+(Title:({0})^4 Text:({0}))", query.Text);
            }

            if (query.Ancestor != null)
            {
                q += string.Format(" +Trail:{0}*", Utility.GetTrail(query.Ancestor));
            }
            if (query.OnlyPages.HasValue)
            {
                q += string.Format(" +IsPage:{0}", query.OnlyPages.Value.ToString().ToLower());
            }
            if (query.Roles != null)
            {
                q += string.Format(" +Roles:(Everyone {0})", string.Join(" ", query.Roles.ToArray()));
            }
            if (query.Types != null)
            {
                q += string.Format(" +Types:({0})", string.Join(" ", query.Types.Select(t => t.Name).ToArray()));
            }
            if (query.LanguageCode != null)
            {
                q += string.Format(" +Language:({0})", query.LanguageCode);
            }
            if (query.Exclution != null)
            {
                q += string.Format(" -({0})", CreateQuery(query.Exclution));
            }
            if (query.Intersection != null)
            {
                q = string.Format("+({0}) +({1})", q, CreateQuery(query.Intersection));
            }
            if (query.Union != null)
            {
                q = string.Format("({0}) ({1})", q, CreateQuery(query.Union));
            }
            if (query.Details.Count > 0)
            {
                foreach (var kvp in query.Details)
                {
                    if (LuceneIndexer.Properties.All.Contains(kvp.Key))
                    {
                        q += string.Format(" +{0}:({1})", kvp.Key, kvp.Value);
                    }
                    else
                    {
                        q += string.Format(" +Detail.{0}:({1})", kvp.Key, kvp.Value);
                    }
                }
            }
            logger.Debug("CreateQuery: " + q);

            return(accessor.GetQueryParser().Parse(q));
        }