发表评论
using System;
using System.IO;
using Lucene.Net.Index;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Analysis.Cn;
namespace CreateIndex
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
DateTime start = DateTime.Now;
IndexWriter writer = new IndexWriter("index", new ChineseAnalyzer(), true);
indexDocs(writer, new DirectoryInfo(@"E:\18show"));
writer.Optimize();
writer.Close();
DateTime end = DateTime.Now;
Console.Write(end.Millisecond - start.Millisecond);
Console.WriteLine(" total milliseconds");
Console.ReadLine();
}
public static void indexDocs(IndexWriter writer, DirectoryInfo m_DirectoryInfo)
{
foreach(FileInfo m_OneFileInfo in m_DirectoryInfo.GetFiles())
{
writer.AddDocument(FileDocument.Document(m_OneFileInfo));
}
}
}
public class FileDocument
{
public static Document Document(FileInfo f)
{
Document doc = new Document();
doc.Add(Field.Text("path", f.FullName));
doc.Add(Field.Keyword("modified",f.LastWriteTime.ToString()));
TextReader m_TextReader=File.OpenText(f.FullName);
doc.Add(Field.Text("contents", m_TextReader));
return doc;
}
}
}
using System;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Lucene.Net.Analysis.Cn;
namespace TestQuery
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Searcher searcher = new IndexSearcher(@"index的路径");
Analyzer analyzer = new ChineseAnalyzer();
while (true)
{
Console.WriteLine("Query: ");
String line = Console.ReadLine();
if (line.Length == -1)
break;
Query query = QueryParser.Parse(line, "contents", analyzer);
Console.WriteLine("Searching for: " + query.ToString("contents"));
Hits hits = searcher.Search(query);
Console.WriteLine(hits.Length() + " total matching documents");
const int HITS_PER_PAGE = 10;
for (int start = 0; start < hits.Length(); start += HITS_PER_PAGE)
{
int end = Math.Min(hits.Length(), start + HITS_PER_PAGE);
for (int i = start; i < end; i++)
{
Document doc = hits.Doc(i);
String path = doc.Get("path");
if (path != null)
{
Console.WriteLine(i + ". " + path);
}
else
{
String url = doc.Get("url");
if (url != null)
{
Console.WriteLine(i + ". " + url);
Console.WriteLine(" - " + doc.Get("title"));
}
else
{
Console.WriteLine(i + ". " + "No path nor URL for this document");
}
}
}
if (hits.Length() > end)
{
Console.WriteLine("more (y/n) ? ");
line = Console.ReadLine();
if (line.Length == 0 || line.ToCharArray()[0] == 'n')
break;
}
}
}
searcher.Close();
}
}
}
有没有人安装过luncene.net?
怎么安装呀?
帖易(search.teein.com)-中文社区搜索引擎招聘lucene相关开发人员,诚邀熟悉dotlucene结构,对分布式搜索引擎有一定研究的TX加盟。
联系MSN:haoyan#teein.com (替换#成@)
工作地点在上海,要求全职。
dudu,能不能给个用Lucene.Net搜索sql server数据的例子啊,我搜遍整个网站,也没有找到,只是看到一些搜索本地文件的例子,郁闷啊。
#12楼 [
楼主]2006-08-11 15:27 |
@有容乃大
你可以下载CNBlogs Dottext 1.0 Beta 2 看看,下载地址:http://downloads.cnblogs.com/Dottext/CNBlogsDottext10Beta2.rar 。
请问这个中文分词怎么不支持数字?也就是我要用电话号码来搜索,结果搜索不到...
为什么我引用不到Lucene.Net.Search.HighLight???
上面的类库找到了~
请问Lucene.Net.Analysis.Cn用这个做分析器建立的索引,只支持中文的搜索吗?其他的英文或数字都不支持吗?