0

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

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

1 Answer 1

2

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

   }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for this not what i am looking for. my requirement is like that i will do the search above way in my question but at the time of search i want to add a clause like country='UK'. so just add few lines in my above code by which i can add the clause as a result search result will be only UK based. plzz ask me if i am not clear what i want. please help me...thanks a lot.
is it mandatory that i need to add two query. can't we manipulate with single query. any thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.