代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Analysis.Cn;
namespace Lucene01
{
class Program
{
static void Main(string[] args)
{
CreateLucenetIndex();
ReadLucenetIndex();
}
/// <summary>
/// 创建搜索
/// </summary>
protected static void CreateLucenetIndex()
{
//FSDirectory 建立基于文档索引 ,RAMDirectory 建立基于内存索引
// Directory dir = new FSDirectory(); //new RAMDirectory();
Analyzer analyzer = new ChineseAnalyzer();
IndexWriter writer = new IndexWriter("rhythmKIndex", analyzer, true); //new IndexWriter(dir,analyzer,true);
//IndexReader.IndexExists("rhythmKIndex"); 可以判断是否存在索引文件
//故上面也可以用 new IndexWriter("rhythmkIndex",analyzer,true)
Document doc = new Document();
doc.Add(new Field("title", "这只是一个标题", Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("content", "我是一个中国人!I am a chinese boy~", Field.Store.YES, Field.Index.TOKENIZED));
// doc.Add(new Field("dd",DateField.DateToString(); ;
writer.AddDocument(doc);
doc = new Document();
doc.Add(new Field("title", "学习Lucene.Net", Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("content", "今天是我学习Lucene.Net第一天!", Field.Store.YES, Field.Index.TOKENIZED));
doc.SetBoost(2.5f);//通过设置权重 优先级,让其排名靠前
writer.AddDocument(doc);
writer.Optimize();
writer.Close();
}
/// <summary>
/// 读取索引
/// </summary>
protected static void ReadLucenetIndex()
{
//Directory dir = FSDirectory.GetDirectory("path");//
//dir = RAMDirectory(FSDirectory.GetDirectory("path"));//将指定文件下的索引读入内存
IndexSearcher searcher = new IndexSearcher("rhythmKIndex");
MultiFieldQueryParser mfq = new MultiFieldQueryParser(new string[] { "title", "content" }, new ChineseAnalyzer());
Query query = mfq.Parse("是");
Console.WriteLine(query.ToString());//打印索引语句
Hits hit = searcher.Search(query);
Console.WriteLine("总共获取到相关数据:{0}条,索引库总数:", hit.Length(), searcher.Reader.NumDocs());
for (int i = 0; i < hit.Length(); i++)
{
int docId = hit.Id(i);
string title = hit.Doc(i).Get("title");
string content = hit.Doc(i).Get("content");
Console.WriteLine("当前ID:{0}搜索到的标题:{1}内容:{2}", docId, title, content);
}
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Analysis.Cn;
namespace Lucene01
{
class Program
{
static void Main(string[] args)
{
CreateLucenetIndex();
ReadLucenetIndex();
}
/// <summary>
/// 创建搜索
/// </summary>
protected static void CreateLucenetIndex()
{
//FSDirectory 建立基于文档索引 ,RAMDirectory 建立基于内存索引
// Directory dir = new FSDirectory(); //new RAMDirectory();
Analyzer analyzer = new ChineseAnalyzer();
IndexWriter writer = new IndexWriter("rhythmKIndex", analyzer, true); //new IndexWriter(dir,analyzer,true);
//IndexReader.IndexExists("rhythmKIndex"); 可以判断是否存在索引文件
//故上面也可以用 new IndexWriter("rhythmkIndex",analyzer,true)
Document doc = new Document();
doc.Add(new Field("title", "这只是一个标题", Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("content", "我是一个中国人!I am a chinese boy~", Field.Store.YES, Field.Index.TOKENIZED));
// doc.Add(new Field("dd",DateField.DateToString(); ;
writer.AddDocument(doc);
doc = new Document();
doc.Add(new Field("title", "学习Lucene.Net", Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("content", "今天是我学习Lucene.Net第一天!", Field.Store.YES, Field.Index.TOKENIZED));
doc.SetBoost(2.5f);//通过设置权重 优先级,让其排名靠前
writer.AddDocument(doc);
writer.Optimize();
writer.Close();
}
/// <summary>
/// 读取索引
/// </summary>
protected static void ReadLucenetIndex()
{
//Directory dir = FSDirectory.GetDirectory("path");//
//dir = RAMDirectory(FSDirectory.GetDirectory("path"));//将指定文件下的索引读入内存
IndexSearcher searcher = new IndexSearcher("rhythmKIndex");
MultiFieldQueryParser mfq = new MultiFieldQueryParser(new string[] { "title", "content" }, new ChineseAnalyzer());
Query query = mfq.Parse("是");
Console.WriteLine(query.ToString());//打印索引语句
Hits hit = searcher.Search(query);
Console.WriteLine("总共获取到相关数据:{0}条,索引库总数:", hit.Length(), searcher.Reader.NumDocs());
for (int i = 0; i < hit.Length(); i++)
{
int docId = hit.Id(i);
string title = hit.Doc(i).Get("title");
string content = hit.Doc(i).Get("content");
Console.WriteLine("当前ID:{0}搜索到的标题:{1}内容:{2}", docId, title, content);
}
Console.ReadKey();
}
}
}