Lucene.Net学习
学习夕阳轨迹的Lucene.Net开发介绍,做个测试
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace TestLucene
{
using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Index();
p.Searcher("测试");
}
/// <summary>
/// 搜索
/// </summary>
/// <param name="querystring">搜索输入</param>
private void Searcher(string querystring)
{
Analyzer analyzer = new StandardAnalyzer();
IndexSearcher searcher = new IndexSearcher("index");
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.Parse(querystring);
Hits hits = searcher.Search(query);
for (int i = 0; i < hits.Length(); i++)
{
Document doc = hits.Doc(i);
Console.WriteLine(doc.Get("content") + "得分:" + hits.Score(i).ToString("f2"));
}
}
/// <summary>
/// 索引数据
/// </summary>
private void Index()
{
Analyzer analyzer = new StandardAnalyzer();
IndexWriter writer = new IndexWriter("index", analyzer, true);
AddDocument(writer, "测试标题一", "测试内容一111", 1.9f);
AddDocument(writer, "测试标题二", "测试内容二11", 1.8f);
AddDocument(writer, "测试标题三", "测试内容三1", 1.0f);
AddDocument(writer, "测试标题四", "测试内容四", 1.0f);
writer.Optimize();
writer.Close();
}
/// <summary>
/// 为索引准备数据
/// </summary>
/// <param name="write">索引实例</param>
/// <param name="title">标题</param>
/// <param name="content">需要索引的数据</param>
/// <param name="boost">权值</param>
void AddDocument(IndexWriter write, string title, string content, float boost)
{
Document document = new Document();
document.Add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
document.Add(new Field("content", content, Field.Store.YES, Field.Index.TOKENIZED));
document.SetBoost(boost);
write.AddDocument(document);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TestLucene
{
using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Index();
p.Searcher("测试");
}
/// <summary>
/// 搜索
/// </summary>
/// <param name="querystring">搜索输入</param>
private void Searcher(string querystring)
{
Analyzer analyzer = new StandardAnalyzer();
IndexSearcher searcher = new IndexSearcher("index");
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.Parse(querystring);
Hits hits = searcher.Search(query);
for (int i = 0; i < hits.Length(); i++)
{
Document doc = hits.Doc(i);
Console.WriteLine(doc.Get("content") + "得分:" + hits.Score(i).ToString("f2"));
}
}
/// <summary>
/// 索引数据
/// </summary>
private void Index()
{
Analyzer analyzer = new StandardAnalyzer();
IndexWriter writer = new IndexWriter("index", analyzer, true);
AddDocument(writer, "测试标题一", "测试内容一111", 1.9f);
AddDocument(writer, "测试标题二", "测试内容二11", 1.8f);
AddDocument(writer, "测试标题三", "测试内容三1", 1.0f);
AddDocument(writer, "测试标题四", "测试内容四", 1.0f);
writer.Optimize();
writer.Close();
}
/// <summary>
/// 为索引准备数据
/// </summary>
/// <param name="write">索引实例</param>
/// <param name="title">标题</param>
/// <param name="content">需要索引的数据</param>
/// <param name="boost">权值</param>
void AddDocument(IndexWriter write, string title, string content, float boost)
{
Document document = new Document();
document.Add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
document.Add(new Field("content", content, Field.Store.YES, Field.Index.TOKENIZED));
document.SetBoost(boost);
write.AddDocument(document);
}
}
}