2012-08-03 111 views
0

以下方式我使用lucene.net進行了搜索。這個例程搜索多個詞對所有索引字段「標題」,「描述」,「網址」,「國家」使用lucene.net進行條件搜索

我需要知道我怎麼給等,其中國家=「UK」或國家的條件=「美國」

我想,多字應該是尋找像下面,但我想增加一個條款,當國家是英國時。所以請指導我在代碼中添加什麼。

if (!string.IsNullOrEmpty(multiWordPhrase)) 
    { 

     string[] fieldList = { "Title", "Description", "Url" }; 
     List<BooleanClause.Occur> occurs = new List<BooleanClause.Occur>(); 
     foreach (string field in fieldList) 
     { 
      occurs.Add(BooleanClause.Occur.SHOULD); 
     } 

     searcher = new IndexSearcher(_directory, false); 
     Query qry = MultiFieldQueryParser.Parse(Version.LUCENE_29, multiWordPhrase, fieldList, occurs.ToArray(), new StandardAnalyzer(Version.LUCENE_29)); 
     TopDocs topDocs = searcher.Search(qry, null, ((PageIndex + 1) * PageSize), Sort.RELEVANCE); 
     ScoreDoc[] scoreDocs = topDocs.ScoreDocs; 
     int resultsCount = topDocs.TotalHits; 
     list.HasData = resultsCount; 
     StartRecPos = (PageIndex * PageSize) + 1; 
     if (topDocs != null) 
     { 
      for (int i = (PageIndex * PageSize); i <= (((PageIndex + 1) * PageSize)-1) && i < topDocs.ScoreDocs.Length; i++) 
      { 
       Document doc = searcher.Doc(topDocs.ScoreDocs[i].doc); 
       oSr = new Result(); 
       oSr.ID = doc.Get("ID"); 
       oSr.Title = doc.Get("Title"); 
       oSr.Description = doc.Get("Description"); 
       //oSr.WordCount = AllExtension.WordCount(oSr.Description, WordExist(oSr.Title, multiWordPhrase)); 
       string preview = 
       oSr.Description = BBAReman.AllExtension.HighlightKeywords(oSr.Description, multiWordPhrase); //sr.Description; 
       oSr.Url = doc.Get("Url"); 
       TmpEndRecpos++; 
       list.Result.Add(oSr); 
      } 
     } 

感謝

回答

2

查找BooleanQuery

if (!string.IsNullOrEmpty(multiWordPhrase)) 
{ 
    BooleanQuery bq = new BooleanQuery(); 

    string[] fieldList = { "Title", "Description", "Url" }; 
    List<BooleanClause.Occur> occurs = new List<BooleanClause.Occur>(); 
    foreach (string field in fieldList) 
    { 
     occurs.Add(BooleanClause.Occur.SHOULD); 
    } 
    Query qry = MultiFieldQueryParser.Parse(Version.LUCENE_29, multiWordPhrase, fieldList, occurs.ToArray(), new StandardAnalyzer(Version.LUCENE_29)); 


    bq.Add(qry,BooleanClause.Occur.Must); 

    //this is the country query (modify the Country field name to whatever you have) 
    string country = "UK"; 
    Query q2 = new QueryParser(Version.LUCENE_CURRENT, "Country", analyzer).parse(country); 
    bq.Add(q2,BooleanClause.Occur.Must); 
    searcher = new IndexSearcher(_directory, false); 

    TopDocs topDocs = searcher.Search(bq, null, ((PageIndex + 1) * PageSize), Sort.RELEVANCE); 
    ScoreDoc[] scoreDocs = topDocs.ScoreDocs; 
    int resultsCount = topDocs.TotalHits; 
    list.HasData = resultsCount; 
    StartRecPos = (PageIndex * PageSize) + 1; 
    if (topDocs != null) 
    { 
    //loop through your results 

    } 
+0

感謝這不是我所期待的。我的要求是,我會在我的問題上面進行搜索,但在搜索時我想添加一個像country ='UK'這樣的子句。所以只需在我的上述代碼中添加幾行即可添加該子句,因爲搜索結果將僅以英國爲基礎。 plzz問我,如果我不清楚我想要什麼。請幫助我...非常感謝。 – Thomas 2012-08-04 18:01:30

+0

修改代碼示例... HTH – Mikos 2012-08-04 18:45:05

+0

是強制性的,我需要添加兩個查詢。我們不能用單個查詢來操縱。非常感謝。 – Thomas 2012-08-05 08:18:43