搜索文件内容lucene.net [英] searching file content lucene.net

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

问题描述

我当前正在使用lucene.net来搜索文件内容以进行关键字搜索.我能够正确获得结果,但是有一种情况需要显示在特定文件中找到的关键字.
有两个不同的文件,分别包含"karthik"和"steven",如果我搜索"karthik和steven",则可以同时显示两个文件.如果我仅分别搜索"karthik"和"steven",则仅显示相应的文件.
当我同时搜索"karthik和steven"时,由于仅显示文件名,结果中两个文件都得到了,现在我需要在该特定文件中找到的特定关键字显示为列表视图中的记录.
在此处输入代码

==代码====
公共布尔StartSearch()
{
bool bResult = false;
搜索器objSearcher =新的IndexSearcher(mstrIndexLocation);
分析器objAnalyzer =新的StandardAnalyzer();
尝试
{
//执行搜索
DateTime dteStart = DateTime.Now;
查询objQuery = QueryParser.Parse(mstrSearchFor,"contents",objAnalyzer);
点击objHits = objSearcher.Search(objQuery,objFilter);
DateTime dteEnd = DateTime.Now;
mlngTotalTime =(Date.GetTime(dteEnd)-Date.GetTime(dteStart));
mlngNumHitsFound = objHits.Length();
//GeneratePreviewText(objQuery,mstrSearchFor,objHits);
//生成结果-转换为XML
mstrResultsXML =";
如果(mlngNumHitsFound> 0)
{
mstrResultsXML =<?xml version = \" 1.0 \"encoding = \" UTF-8 \?>< Results>";
//遍历结果
for(int i = 0; i< objHits.Length(); i ++)
{
尝试
{
//获取下一个结果
文档objDocument = objHits.Doc(i);
//提取数据
字符串strPath = objDocument.Get("path");
字符串strFileName = objDocument.Get("name");
如果(strPath == null){strPath ="; }
字符串strLastWrite = objDocument.Get("last_write_time");
如果(strLastWrite == null)
strLastWrite =不可用";
其他
{
strLastWrite = DateField.StringToDate(strLastWrite).ToShortDateString(); }
double dblScore = objHits.Score(i)* 100;
字符串strScore = String.Format("{0:00.00}",dblScore);
//将结果添加为XML行
mstrResultsXML + =< Row>";
//mstrResultsXML + =< Sequence>" +(i + 1).ToString()+</Sequence>";
mstrResultsXML + =<路径>" + strPath +</Path>";
mstrResultsXML + =< FileName>" + strFileName +</FileName>";
//mstrResultsXML + =<分数>" + strScore +%" +</Score>";
mstrResultsXML + =</Row>"; }抓住{break;

}

}//结束XML
mstrResultsXML + =</Results>";
//构建Dataview(绑定到datagrid

DataSet objDS = new DataSet();

StringReader objSR =新的StringReader(mstrResultsXML);
objDS.ReadXml(objSR);
objSR = null;
mobjResultsDataView = new DataView();
mobjResultsDataView = objDS.Tables [0] .DefaultView;
}
//完成
objSearcher.Close();
bResult = true;
}
catch(异常e)
{
mstrError ="Exception:" + e.Message;
}
终于
{
objSearcher = null;
objAnalyzer = null;
}
返回bResult;
}


上面是我用于搜索的代码和我绑定到listview的xml,现在我需要标记在相应文档中找到的特定关键字,并将其作为记录显示在listview中,类似于下面的listview

找不到文件名关键字
1 Test.Doc karthik
2 Test2.Doc史蒂文

我希望你们能解决这个问题,

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);
//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";
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>" + strFileName + "</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;
}


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,

推荐答案

[

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

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