Esempio n. 1
0
        public static SearchResults Query(HttpContext ctx, string query, int page = 1) {
            if (string.IsNullOrWhiteSpace(query)) {
                return new SearchResults {
                    Documents = new SearchResult[0],
                    TotalCount = 0
                };
            }
            var indexPath = ctx.Server.MapPath("~/App_Data/Index");
            var indexSearcher = new DirectoryIndexSearcher(new DirectoryInfo(indexPath));
            using (var searchService = new SearchService(indexSearcher)) {
                var parser = new MultiFieldQueryParser(
                    Lucene.Net.Util.Version.LUCENE_29,
                    new[] { "Text" },
                    new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));

                Query multiQuery = GetSafeQuery(parser, query);

                var result = searchService.SearchIndex(multiQuery);
                return new SearchResults {
                    Documents = result.Results
                    .Skip(PageSize*(page - 1))
                    .Take(PageSize)
                    .Select(d => new SearchResult {
                        Url = d.Get("Url"),
                        Title = d.Get("Title"),
                        Summary = d.Get("Summary")
                    }),
                    TotalCount = result.Results.Count()
                };
            }
        }
Esempio n. 2
0
 public static List<Creative> SearchCreatives(string IndexPath, string searchString, ICreativesRepository creativesRepository)
 {
     var indexSearcher = new DirectoryIndexSearcher(new DirectoryInfo(IndexPath), true);
     Directory directory = FSDirectory.Open(new DirectoryInfo(IndexPath));
     Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
     IndexReader indexReader = IndexReader.Open(directory, true);
     Searcher indexSearch = new IndexSearcher(indexReader);
     string[] fields = { "Titel", "About" };
     var queryParser = new Lucene.Net.QueryParsers.MultiFieldQueryParser(Version.LUCENE_30, fields, analyzer);
     var query = queryParser.Parse(searchString.ToLower() + "*");
     var hits = indexSearch.Search(query, indexReader.MaxDoc).ScoreDocs;
     List<Creative> creatives = new List<Creative>();
     foreach (var hit in hits)
     {
         Document documentFromSearcher = indexSearch.Doc(hit.Doc);
         creatives.Add(creativesRepository.GetCreativeById(int.Parse(documentFromSearcher.Get("CreativeId"))));
     }
     indexSearch.Dispose();
     directory.Dispose();
     return creatives;
 }
Esempio n. 3
0
 public static DirectoryIndexSearcher GetIndexSearcher()
 {
     var indexPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Index");
     var indexSearcher = new DirectoryIndexSearcher(new DirectoryInfo(indexPath), true);
     return indexSearcher;
 }