与lucene.net条件搜索 [英] Conditional search with lucene.net

查看:137
本文介绍了与lucene.net条件搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下方式我做了lucene.net搜索。对所有索引字段这个程序搜索多个词叫标题,说明,URL,国家

the below way i did the search with lucene.net. this routine search multiple word against all index field called "Title", "Description", "Url", "Country" .

我需要知道我怎么可以给想去哪国='UK'或国家的条件=US

i need to know how can i give a condition like where country='UK' or country='US'

我想,多字应该是寻找像下面,但我想增加一个条款,当国家为英国。所以请指导我怎么在我的代码添加。

i want that multiple word should be search like below but i want to add one more clause that when country is UK. so please guide me what to add in my code.

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



感谢

thanks

推荐答案

查找BooleanQuery

Look up 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

   }

这篇关于与lucene.net条件搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆