2012-08-03 76 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