#define Search

using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Tokenattributes;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using TestApp.Bll;
using TestApp.Model; namespace TestApp
{
class Program
{
static void Main()
{
#if CreateIndex
Console.WriteLine("开始创建索引");
var bll = new ItemBll();
CreateIndex(bll.GetItemInfos());
#endif
#if Search
#region 查词
StringBuilder sb = new StringBuilder();
//索引库目录
Lucene.Net.Store.Directory dir_search = FSDirectory.Open(new System.IO.DirectoryInfo("ItemIndexDir"), new NoLockFactory());
IndexReader reader = IndexReader.Open(dir_search, true);
IndexSearcher search = null;
try
{
search = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "ItemName", new PanGuAnalyzer());
Query query = parser.Parse(LuceneHelper.GetKeyWordSplid("甲醇"));
//执行搜索,获取查询结果集对象
TopDocs ts = search.Search(query, null, );
///获取命中的文档信息对象
ScoreDoc[] docs = ts.ScoreDocs;
Console.WriteLine(docs.Length);
for (int i = ; i < docs.Length; i++)
{
int docId = docs[i].Doc;
Document doc = search.Doc(docId);
var id = doc.Get("id");
Console.WriteLine(id);
var itemName = doc.Get("ItemName");
Console.WriteLine(itemName);
var purity = doc.Get("Purity");
Console.WriteLine(purity);
var size = doc.Get("Size");
Console.WriteLine(size);
var unit = doc.Get("Unit");
Console.WriteLine(unit);
var venderName = doc.Get("VenderName");
Console.WriteLine(venderName);
}
}
catch (Exception ex)
{
throw;
}
finally
{
if (search != null)
search.Dispose();
if (dir_search != null)
dir_search.Dispose();
}
#endregion #endif } //帮助类,对搜索的关键词进行分词
public static class LuceneHelper
{
public static string GetKeyWordSplid(string keywords)
{
StringBuilder sb = new StringBuilder();
Analyzer analyzer = new PanGuAnalyzer();
TokenStream stream = analyzer.TokenStream(keywords, new StringReader(keywords));
ITermAttribute ita = null;
bool hasNext = stream.IncrementToken();
while (hasNext)
{
ita = stream.GetAttribute<ITermAttribute>();
sb.Append(ita.Term + " ");
hasNext = stream.IncrementToken();
}
return sb.ToString();
}
} /// <summary>
/// 创建索引文件
/// </summary>
private static void CreateIndex(List<ItemInfo> list)
{
IndexWriter writer = null;
Analyzer analyzer = new PanGuAnalyzer();
Lucene.Net.Store.Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo("ItemIndexDir"));
int i = ;
try
{
////IndexReader:对索引进行读取的类。
//该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。
bool isCreate = !IndexReader.IndexExists(dir);
writer = new IndexWriter(dir, analyzer, isCreate, IndexWriter.MaxFieldLength.UNLIMITED);
//添加索引
foreach (var item in list)
{
Document doc = new Document();
if (item.ItemId % == )
Console.WriteLine($"开始写入{item.ItemId}"); doc.Add(new Field("id", item.ItemId.ToString(), Field.Store.YES, Field.Index.ANALYZED));
i = ;
doc.Add(new Field("ItemName", item.ItemName?.ToString(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
i = ;
doc.Add(new Field("Purity", item.Purity?.ToString(), Field.Store.YES, Field.Index.ANALYZED));
i = ;
doc.Add(new Field("Size", item.Size.ToString(), Field.Store.YES, Field.Index.ANALYZED));
i = ;
doc.Add(new Field("Unit", item.Unit?.ToString(), Field.Store.YES, Field.Index.ANALYZED));
i = ;
doc.Add(new Field("VenderName", item.VenderName?.ToString(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
i = ;
doc.Add(new Field("Price", item.Price.ToString(), Field.Store.YES, Field.Index.ANALYZED));
i = ; writer.AddDocument(doc, analyzer);
}
writer.Optimize();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine($"error step {i}");
throw;
}
finally
{
if (writer != null)
writer.Dispose();
if (dir != null)
dir.Dispose();
}
}
}
}

最新文章

  1. 【填坑】bzoj3224 splay裸题
  2. Atitit 研发体系建立 数据存储与数据知识点体系知识图谱attilax 总结
  3. 如何在batch脚本中嵌入python代码
  4. 视觉差双排listview效果
  5. 新手在sae部署程序容易忽略的一个细节
  6. tomcat 配置
  7. java集合框架示例图
  8. iOS开发——友盟分享
  9. JavaScript修改表中的内容
  10. 利用js获取时间并输出值
  11. Maven项目中提示:Eclipse “cannot be resolved to a type” error
  12. 网站SEO,HTTP请求的关键数字----6
  13. 面试时怎样回答:你对原生ajax的理解
  14. python全栈开发day103-python垃圾回收机制、mro和c3算法解析、跨域jsonp\CORS、Content-Type组件
  15. nginx做rails项目web服务器缓存配置方法
  16. JVM内存管理(转)
  17. hihoCoder 1116 计算(线段树)
  18. VS出现异常?!和十进制转二进制比是小事
  19. swift和OC中frame的小差异
  20. mysql 列转行处理

热门文章

  1. iOS - OC - JSON 解析 - NSJSONSerialization
  2. Linux符号连接的层数过多
  3. ViewPager 带动画的欢迎界面
  4. 文件Move操作
  5. Paxos Made Simple
  6. OAuth2.0 Owin 授权问题
  7. db2 批处理
  8. PTA第五次作业
  9. 前端之css笔记1
  10. 浅析10种常见的黑帽seo手法