使用lucene.net进行条件搜索

时间:2012-08-03 06:58:04

标签: c# lucene.net

以下方式我用lucene.net进行了搜索。此例程在所有索引字段中搜索多个单词,称为“标题”,“描述”,“网址”,“国家/地区”

我需要知道如何给出像country ='UK'或country ='US'

的条件?

我希望多个单词应该像下面一样进行搜索,但我想添加一个当国家是英国时的条款。所以请指导我在我的代码中添加什么。

 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);
            }
        }

感谢

1 个答案:

答案 0 :(得分: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

   }