2011-11-26 136 views
0

希望有人能儘快幫助我:-) 我想知道我們如何使用Lucene搜索多個站點??! (所有網站都在一個索引中)。如何使用Lucene搜索引擎API搜索多個站點?

我已經成功搜索一個網站,索引多個站點,但是我不能夠搜索到所有網站。

考慮這種方法,我有:

private void PerformSearch() 
    { 
     DateTime start = DateTime.Now; 

     //Create the Searcher object 
     string strIndexDir = Server.MapPath("index") + @"\" + mstrURL; 
     IndexSearcher objSearcher = new IndexSearcher(strIndexDir); 

     //Parse the query, "text" is the default field to search 
     Query objQuery = QueryParser.Parse(mstrQuery, "text", new StandardAnalyzer()); 

     //Create the result DataTable 
     mobjDTResults.Columns.Add("title", typeof(string)); 
     mobjDTResults.Columns.Add("path", typeof(string)); 
     mobjDTResults.Columns.Add("score", typeof(string)); 
     mobjDTResults.Columns.Add("sample", typeof(string)); 
     mobjDTResults.Columns.Add("explain", typeof(string)); 

     //Perform search and get hit count 
     Hits objHits = objSearcher.Search(objQuery); 
     mintTotal = objHits.Length(); 

     //Create Highlighter 
     QueryHighlightExtractor highlighter = new QueryHighlightExtractor(objQuery, new StandardAnalyzer(), "<B>", "</B>"); 

     //Initialize "Start At" variable 
     mintStartAt = GetStartAt(); 

     //How many items we should show? 
     int intResultsCt = GetSmallerOf(mintTotal, mintMaxResults + mintStartAt); 

     //Loop through results and display 
     for (int intCt = mintStartAt; intCt < intResultsCt; intCt++) 
     { 
      //Get the document from resuls index 
      Document doc = objHits.Doc(intCt); 

      //Get the document's ID and set the cache location 
      string strID = doc.Get("id"); 
      string strLocation = ""; 
      if (mstrURL.Substring(0,3) == "www") 
       strLocation = Server.MapPath("cache") + 
        @"\" + mstrURL + @"\" + strID + ".htm"; 
      else 
       strLocation = doc.Get("path") + doc.Get("filename"); 

      //Load the HTML page from cache 
      string strPlainText; 
      using (StreamReader sr = new StreamReader(strLocation, System.Text.Encoding.Default)) 
      { 
       strPlainText = ParseHTML(sr.ReadToEnd()); 
      } 

      //Add result to results datagrid 
      DataRow row = mobjDTResults.NewRow(); 
      if (mstrURL.Substring(0,3) == "www") 
       row["title"] = doc.Get("title"); 
      else 
       row["title"] = doc.Get("filename"); 
      row["path"] = doc.Get("path"); 
      row["score"] = String.Format("{0:f}", (objHits.Score(intCt) * 100)) + "%"; 
      row["sample"] = highlighter.GetBestFragments(strPlainText, 200, 2, "..."); 
      Explanation objExplain = objSearcher.Explain(objQuery, intCt); 
      row["explain"] = objExplain.ToHtml(); 
      mobjDTResults.Rows.Add(row); 
     } 
     objSearcher.Close(); 

     //Finalize results information 
     mTsDuration = DateTime.Now - start; 
     mintFromItem = mintStartAt + 1; 
     mintToItem = GetSmallerOf(mintStartAt + mintMaxResults, mintTotal); 
    } 

正如你可以看到,我使用的網站URL mstrURL當我創建的搜索對象

string strIndexDir = Server.MapPath("index") + @"\" + mstrURL; 

我怎麼可以這樣做,當我想要搜索多個網站?

其實我使用this代碼。

回答