예제 #1
0
        public Task <IEnumerable <SearchDocument> > SearchAsync(string term, CancellationToken cancellationToken = default)
        {
            var list = new List <SearchDocument>();

            if (!string.IsNullOrEmpty(term))
            {
                // create index writer
                using var dir = FSDirectory.Open(_indexPath);
                var indexConfig = new IndexWriterConfig(AppLuceneVersion, _analyzer);
                // Create an index writer
                using var writer = new IndexWriter(dir, indexConfig);
                // get reader
                using var reader = writer.GetReader(applyAllDeletes: true);
                var searcher = new IndexSearcher(reader);
                // Search with query
                var parser = new MultiFieldQueryParser(AppLuceneVersion, new[] { "field1", "field2" },
                                                       _analyzer);
//var query = parser.GetFuzzyQuery("fieldName", "featured", 0.7f);
                // var query = new TermQuery(new Term("content", term));
                var query = parser.CreatePhraseQuery("content", term);
                // get hits
                var hits = searcher.Search(query, 50).ScoreDocs;
                foreach (var hit in hits)
                {
                    var doc = searcher.Doc(hit.Doc);
                    list.Add(Convert(doc));
                }
            }

            return(Task.FromResult((IEnumerable <SearchDocument>)list));
        }