lucene.net学习 -ag凯发k8国际
很早就知道lucene.net但一直都没时间认真实践一下.最近项目需要.所以考虑学习一下lucene.net引擎.终于做出了第一个demo感觉挺实用的.还不错.代码贴上.各位博友可参考学习一下.
protected void btnaddindex_click(object sender, eventargs e)
{
writer = new indexwriter(server.mappath("index"), new lucene.net.analysis.simpleanalyzer(), false);
indexdirectory(writer, new fileinfo(server.mappath("doc")));
writer.close();
}
public void indexdirectory(indexwriter writer, fileinfo file)
{
if (system.io.directory.exists(file.fullname))
{
string[] files = system.io.directory.getfilesystementries(file.fullname);
if (files != null)
{
for (int i = 0; i < files.length; i )
{
indexdirectory(writer, new fileinfo(files[i]));
}
}
}
else if (file.extension == ".txt" || file.extension == ".htm" || file.extension == ".html")
{
indexfile(file, writer);
}
}
private void indexfile(fileinfo file, indexwriter writer)
{
try
{
document doc = new document();
txtlistindex.text = txtlistindex.text "\n文件路径:" file.fullname;
doc.add(field.keyword("filename", path.getfilename(file.fullname).replace(file.extension,"")));
//doc.add(field.text("contents", new streamreader(file.fullname, system.text.encoding.default)));
doc.add(field.text("contents", file.fullname));
writer.adddocument(doc);
}
catch (filenotfoundexception fnfe)
{
response.write(fnfe.message);
}
}
protected void button1_click(object sender, eventargs e)
{
indexsearcher searcher = new indexsearcher(server.mappath("index"));
query query = queryparser.parse("linux", "filename", new lucene.net.analysis.whitespaceanalyzer());
hits hits = searcher.search(query);
if (hits.length() > 0)
{
for (int num = 0; num <= hits.length() - 1; num )
{
document docnew = hits.doc(num);
txtlistindex.text = txtlistindex.text "\n" docnew.get("contents");
}
}
}
转载于:https://www.cnblogs.com/witer666/archive/2008/04/26/1172597.html
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是ag凯发k8国际为你收集整理的lucene.net学习的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: