Esempio n. 1
0
        public static IEnumerable <ISearchItem> Search(string pattern, DirectoryInfo dataFolder, int page)
        {
            using (Analyzer analyzer = new SimpleAnalyzer(LuceneVersion.LUCENE_48))
                using (IndexReader reader = new PagesReader(dataFolder))
                {
                    Query             query     = new QueryParser(LuceneVersion.LUCENE_48, string.Empty, analyzer).Parse(pattern);
                    IndexSearcher     searcher  = new IndexSearcher(reader);
                    TopFieldCollector collector =
                        TopFieldCollector.Create(Sort.INDEXORDER, NumHits, false, false, false, false);
                    searcher.Search(query, collector);

                    /*
                     * IFormatter formatter = new SimpleHTMLFormatter();
                     * IScorer scorer = new QueryScorer(query);
                     * Highlighter highlighter = new Highlighter(formatter, scorer)
                     * {
                     *  TextFragmenter = new SimpleFragmenter(3)
                     * };
                     */

                    ScoreDoc[] docs = collector.GetTopDocs(page * PageSize, PageSize).ScoreDocs;
                    return(docs.Select(doc => new SearchItem(doc.Doc.ToString(), doc.Doc)));

                    /*
                     * Document document = searcher.Doc(doc.Doc);
                     * string body = document.Get("body");
                     * TokenStream stream = TokenSources.GetAnyTokenStream(reader, doc.Doc, "body", analyzer);
                     * //TokenStream stream = analyzer.GetTokenStream("test123", new StringReader("test456"));
                     * string best = highlighter.GetBestFragments(stream, body, 1, " ");
                     */
                }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            ConsoleKey keyPressed;
            var        pageReader = new PagesReader();

            pageReader.Read();
            var pageNumber = pageReader.GetNextPageNumber();

            do
            {
                Console.Clear();

                var pageModel = pageReader.GetPage(pageNumber);

                Console.Write("{0,50}", "-");
                Console.Write(pageModel.PageNumber);
                Console.Write("-");
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine(pageModel.Content);
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.Write("P - Poprzedni");
                Console.Write("{0,100}", "N - Nastepny");

                Console.WriteLine();
                keyPressed = Console.ReadKey().Key;

                switch (keyPressed)
                {
                case ConsoleKey.N:
                    pageNumber = pageReader.GetNextPageNumber();
                    break;

                case ConsoleKey.P:
                    pageNumber = pageReader.GetPrevPageNumber();
                    break;
                }
            } while (keyPressed != ConsoleKey.Enter);

            Console.ReadKey();
        }
Esempio n. 3
0
 public static ISet <int> HighlightedOffsets(int pagenumber, string pattern, DirectoryInfo dataFolder)
 {
     using (Analyzer analyzer = new SimpleAnalyzer(LuceneVersion.LUCENE_48))
         using (IndexReader reader = new PagesReader(dataFolder))
         {
             Query             query            = new QueryParser(LuceneVersion.LUCENE_48, string.Empty, analyzer).Parse(pattern);
             TermsEnum         termsEnum        = reader.GetTermVector(pagenumber, string.Empty).GetIterator(TermsEnum.EMPTY);
             IEnumerable <int> highlightOffsets = ExtractTerms(query).SelectMany(term =>
             {
                 termsEnum.SeekExact(term.Bytes);
                 DocsAndPositionsEnum docsAndPositionsEnum =
                     termsEnum.DocsAndPositions(null, null, DocsAndPositionsFlags.OFFSETS);
                 docsAndPositionsEnum.Advance(pagenumber);
                 return(Enumerable.Range(0, docsAndPositionsEnum.Freq).Select(i =>
                 {
                     docsAndPositionsEnum.NextPosition();
                     return docsAndPositionsEnum.StartOffset;
                 }));
             });
             return(new HashSet <int>(highlightOffsets));
         }
 }