[Lucene.Net] Multiple Search

  • 1219
  • 0
  • C#
  • 2017-01-10

[Lucene.Net] Multiple Search

為了提高搜尋效能,一般我們會切割索引資料庫(可依照大小、分類..等)並搭配平行搜尋(ParallelMultiSearcher),

這裡簡單紀錄。

 

新增Blob 儲存體和索引資料

image

索引資料庫1(E0001~E0006),索引資料庫2(E0007~E00012)

 

使用ParallelMultiSearcher

public ActionResult SearchResult(string kw)
        {
            List<IndexSearcher> searchers = new List<IndexSearcher>();
            var storageAccount = CloudStorageAccount.Parse(AzureBlob);
            var azureDir = new AzureDirectory(storageAccount, AzureContainName);
            IndexSearcher searcher = new IndexSearcher(azureDir);
            searchers.Add(searcher);

            storageAccount = CloudStorageAccount.Parse(AzureBlob2);
            //AzureContainName 不可重複
            azureDir = new AzureDirectory(storageAccount, AzureContainName2);
            IndexSearcher searcher2 = new IndexSearcher(azureDir);
            searchers.Add(searcher2);

            Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT);
            var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, "Content", analyzer);
                
            Query query = parser.Parse(kw);
            ParallelMultiSearcher parallelmultisearcher = new ParallelMultiSearcher(searchers.ToArray());
            TopDocs topDocs = parallelmultisearcher.Search(query, null, 100);
            int totalnum = topDocs.TotalHits;
            int max = topDocs.ScoreDocs.Length;
            SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font color=\"red\">", "</font>");
            QueryScorer scorer = new QueryScorer(query, "Content");
            var highlighter = new Highlighter(formatter, scorer);
            highlighter.TextFragmenter = new SimpleFragmenter(500);//500字元
            List<LuceneModels> _LuceneModels = new List<LuceneModels>();
            for (int i = 0; i < max; i++)
            {
                LuceneModels _LuceneModel = new LuceneModels();
                ScoreDoc scoreDoc = topDocs.ScoreDocs[i];
                int docId = scoreDoc.Doc;
                Document doc = parallelmultisearcher.Doc(docId);
                string strSnippet = highlighter.GetBestFragment(analyzer, "Content", doc.Get("Content"));
                _LuceneModel.Content = strSnippet;
                _LuceneModel.CreateDate = doc.Get("CreateDate");
                _LuceneModel.CreateUser = doc.Get("CreateUser");
                _LuceneModel.EdocId = doc.Get("EdocId"); 
                _LuceneModels.Add(_LuceneModel);
            }
            //ViewData["MaxCount"] = totalnum.ToString();
            return PartialView("_FullText", _LuceneModels);         
        }

 

 

結果

image