Take the 2-minute tour ×

I am currently using lucene.net 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 displayed.

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.

   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", objAnalyzer);
            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);
                        //Extract the data
                        string strPath = objDocument.Get("path");
                        string strFileName = objDocument.Get("name");
                        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>" + strFileName + "</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;

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

No FileName KeyWord(s)Found

1 Test.Doc karthik

2 Test2.Doc steven

i hope u guys undesrtood the question,

share|improve this question
code please !! atleast a snippet !! –  ashutosh raina Dec 29 '11 at 12:35
Try Highlighter.Net You can use it to highlight the results and to inspect the source. –  L.B Dec 29 '11 at 16:32
i have modified the question with code snippet find above thanks –  karthikdavanam Dec 30 '11 at 11:24
add comment

1 Answer

This depends on how your documents were indexed. You'll need to extract the original content, pass it through the analyzer to get the indexed tokens, and check which matches the generated query.

Just go with the Highlighter.Net package, part of contrib, which does this and more.

share|improve this answer
add comment

Your Answer


