lucenenet-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Franklin Simmons <>
Subject RE: [Lucene.Net] Urgent Help required in (Replay ASAP) !!!!!!!!
Date Tue, 03 Jan 2012 19:04:32 GMT
I suggest you look into strategies used for highlighting, e.g. term vectors, as your problem
is fundamentally the same.


-----Original Message-----
From: Karthik Davanam [] 
Sent: Tuesday, January 03, 2012 3:00 AM
Subject: [Lucene.Net] Urgent Help required in (Replay ASAP) !!!!!!!!

I am currently using to search the content of files for keyword search. I am able
to get the results correctly but I have a scenario where I need to display the keywords found
in a particular file.
There are two different files containing "karthik" and "steven", and if I search for "karthik
and steven" I am able to get both the files displayed. 
If I search only for "karthik" and "steven" separately, only the respective files are getting
When I search for "karthik and steven" simultaneously I get both the files in the result as
I am displaying the filename alone, and now I need to display the particular keyword found
in that particular file as a record in the listview.
enter code here      
Public bool StartSearch()      
bool bResult = false;            
Searcher objSearcher = new IndexSearcher(mstrIndexLocation);            
Analyzer objAnalyzer = new StandardAnalyzer();               
//Perform Search                  
DateTime dteStart = DateTime.Now;               
   Query objQuery = QueryParser.Parse(mstrSearchFor, "contents", 
   Hits objHits = objSearcher.Search(objQuery, objFilter);               
DateTime dteEnd = DateTime.Now;                  
mlngTotalTime = (Date.GetTime(dteEnd) - Date.GetTime(dteStart));         
mlngNumHitsFound = objHits.Length();                  
//GeneratePreviewText(objQuery, mstrSearchFor,objHits);                  
//Generate results - convert to XML                  
mstrResultsXML = "";                  
if (mlngNumHitsFound > 0)
                    mstrResultsXML = "<?xml version=\"1.0\" 
encoding=\"UTF-8\" ?><Results>";
                    //Loop through results
                    for (int i = 0; i < objHits.Length(); i++)
                            //Get the next result
                            Document objDocument = objHits.Doc(i);

                            //Field ff = objDocument.GetField(SearchFor);
                            //Extract the data
                            string strPath = objDocument.Get("path");
                            string strFileName = objDocument.Get( "filename");

                            string strcontents = objDocument.Get( "contents");

                            string preview = GeneratePreviewText(obj, strcontents);

                            strcontents = preview;

                            if (strPath == null) { strPath = ""; }
                            string strLastWrite = objDocument.Get( "last_write_time");
                            if (strLastWrite == null)
                                strLastWrite = "unavailable";
                                strLastWrite = DateField .StringToDate(strLastWrite).ToShortDateString();
                            double dblScore = objHits.Score(i) * 100;
                            string strScore = String.Format("{0:00.00}", dblScore);
                            //Add results as an XML row
                            mstrResultsXML += "<Row>";
                            //mstrResultsXML += "<Sequence>" + (i +
1).ToString() + "</Sequence>";
                            mstrResultsXML += "<Path>" + strPath + "</Path>";
                            mstrResultsXML += "<FileName>" + strcontents + "</FileName>";
                            //mstrResultsXML += "<Score>" + strScore + "%" 
+ "</Score>";
                            mstrResultsXML += "</Row>";
                    }                      //Finish off XML            
            mstrResultsXML += "</Results>";                        
//Build Dataview (to bind to datagrid                        
DataSet objDS = new DataSet();                        
StringReader objSR = new StringReader(mstrResultsXML);                  
objSR = null;                        
mobjResultsDataView = new DataView();         
               mobjResultsDataView = objDS.Tables[0].DefaultView;         
//Finish up                  
bResult = true;            
catch (Exception e)            
mstrError = "Exception: " + e.Message;            
objSearcher = null; 
                  objAnalyzer = null;         
return bResult;      

 public string GeneratePreviewText(Query q, string text)
            QueryScorer scorer = new QueryScorer(q);
            Lucene.Net.Highlight.Formatter formatter =
                new Lucene.Net.Highlight.SimpleHTMLFormatter("<span
            Highlighter highlighter = new Highlighter(formatter, scorer);
            highlighter.SetTextFragmenter(new SimpleFragmenter(250));
            TokenStream stream = new StandardAnalyzer().TokenStream( "contents", new StringReader(text));
            return highlighter.GetBestFragments(stream, text, 4, "<br/>");

Above is the code i am using for search and the xml i am binding to the 
listview, now i need to tag the particular keywords found in the 
respective document and display it in the listview as recordsss,simlar to 
the below listview. In the GeneratePreviewText i am able to highlight the 
keywords in the content but how do i tag it as separate Record and tag to 
each file as below 
No   FileName    KeyWord(s)Found

1     Test.Doc         karthik

2     Test2.Doc        steven

i hope u guys understood the question,

Thanks and Regards
Karthik Davanam
Tata Consultancy Services
Pioneer Building, International Tech Park,
Padandhur Agrahara, Whitefield Road,
Bangalore - 560066,Karnataka
Cell:- +919731600998
Experience certainty.   IT Services
                        Business Solutions
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you

View raw message