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.
enter code here
==Code====
Public bool StartSearch()
{
bool bResult = false;
Searcher objSearcher = new IndexSearcher(mstrIndexLocation);
Analyzer objAnalyzer = new StandardAnalyzer();
try
{
//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++)
{
try
{
//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";
else
{
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>";
}
catch
{
break;
}
} //Finish off XML
mstrResultsXML += "</Results>";
//Build Dataview (to bind to datagrid
DataSet objDS = new DataSet();
StringReader objSR = new StringReader(mstrResultsXML);
objDS.ReadXml(objSR);
objSR = null;
mobjResultsDataView = new DataView();
mobjResultsDataView = objDS.Tables[0].DefaultView;
}
//Finish up
objSearcher.Close();
bResult = true;
}
catch (Exception e)
{
mstrError = "Exception: " + e.Message;
}
finally
{
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
style='background:red;'>",
"</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
Rolls-Royce,
Tata Consultancy Services
Pioneer Building, International Tech Park,
Padandhur Agrahara, Whitefield Road,
Bangalore - 560066,Karnataka
India
Cell:- +919731600998
Mailto: [email protected]
Website: http://www.tcs.com
____________________________________________
Experience certainty. IT Services
Business Solutions
Outsourcing
____________________________________________
=====-----=====-----=====
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