C# Lucene get all the index

前端 未结 2 1409
情话喂你
情话喂你 2020-12-18 03:31

I am working on a windows application using Lucene. I want to get all the indexed keywords and use them as a source for a auto-suggest on search field. How can I receive all

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 04:07

    For inspiration with Apache Lucene.Net version 4.8 you can look at GitHub msigut/LuceneNet48Demo. Use classes: SearcherManager, *QueryParser and IndexWriter for build index.

        // you favorite Query parser (MultiFieldQueryParser for example)
        _queryParser = new MultiFieldQueryParser(...
    
        // Execute the search with a fresh indexSearcher
        _searchManager.MaybeRefreshBlocking();
    
        var searcher = _searchManager.Acquire();
        try
        {
            var q = _queryParser.Parse(query);
    
            var topDocs = searcher.Search(q, 10);
    
            foreach (var scoreDoc in topDocs.ScoreDocs)
            {
                var document = searcher.Doc(scoreDoc.Doc);
    
                var hit = new QueryHit
                {
                    Title = document.GetField("title")?.GetStringValue(),
    
                    // ... you logic to read data from index ...
                };
            }
        }
        finally
        {
            _searchManager.Release(searcher);
            searcher = null;
        }
    

提交回复
热议问题