Lucene.net 全文检索数据库
#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();
}
}
}
}
Lucene.net 全文检索数据库的更多相关文章
- JAVAEE——Lucene基础:什么是全文检索、Lucene实现全文检索的流程、配置开发环境、索引库创建与管理
1. 学习计划 第一天:Lucene的基础知识 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3.配置开发环境 4.创建索引库 5 ...
- lucene解决全文检索word2003,word2007的办法
在上一篇文章中 ,lucene只能全文检索word2003,无法检索2007,并且只能加载部分内容,无法加载全文内容.为解决此问题,找到了如下方法 POI 读取word (word 2003 和 wo ...
- 用Lucene.net对数据库建立索引及搜索<转>
用Lucene.net对数据库建立索引及搜索 最近我一直在研究 Lucene.net ,发现Lucene.net对数据库方面建索引的文章在网上很少见,其实它是可以对数据库进行索引的,我闲着没事,写了个 ...
- Lucene的全文检索学习
Lucene的官方网站(Apache的顶级项目):http://lucene.apache.org/ 1.什么是Lucene? Lucene 是 apache 软件基金会的一个子项目,由 Doug C ...
- 基于Lucene的全文检索实践
由于项目的需要,使用到了全文检索技术,这里将前段时间所做的工作进行一个实践总结,方便以后查阅.在实际的工作中,需要灵活的使用lucene里面的查询技术,以达到满足业务要求与搜索性能提升的目的. 一.全 ...
- lucene教程--全文检索技术
1 Lucene 示例代码 https://blog.csdn.net/qzqanzc/article/details/80916430 2 Lucene 实例教程(一)初识L ...
- 黑马_10 Lucene:全文检索
10 Lucene:01.全文检索基本介绍 10 Lucene:02.创建索引库和查询索引 10 Lucene:03.中文分析器 10 Lucene:04.索引库维护CURD
- Lucene.net 全文检索 盘古分词
lucene.net + 盘古分词 引用: 1.Lucene.Net.dll 2.PanGu.Lucene.Analyzer.dll 3.PanGu.HighLight.dll 4.PanGu.dll ...
- Lucene.net 全文检索文件
using Lucene.Net.Analysis; using Lucene.Net.Analysis.Tokenattributes; using Lucene.Net.Documents; us ...
随机推荐
- Django的models介绍
我们一般会在创建表的类中写一个__str__方法,就会为为了打印这个对象不会打印一大堆的对象的内存地址,而是我们想要他返回的信息,方便我们更直观的知道这个对象是谁,方便显示.比如下面的例子 from ...
- LocalBroadcastManager 的使用
一.使用本地广播发送一条广播(本例为自己发送自己接收,本地广播也可以是其他应用接收)然后接收到广播时回调Receiver类中的回调方法onReceive()在此方法中自定义发出通知 代码 packag ...
- discuz模板引擎语法
论坛的首页模板:forum/discuz.htm 版块的内容模板:forum/forumdisplay.htm 主题的查看模板:forum/viewthread.htm 帖子的内容模板:forum/p ...
- 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)
四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast) 转载 2011年10月03日 23:59:05 标签: stru ...
- 我的UI启蒙之路
为什么叫UI启蒙之路呢? 我没有学过美术,也不懂设计,但是有的时候也许就是一种命中注定吧,让我知道了UI,并且一发不可收拾的爱上了它. 具体情况是这样的: 我毕业于电力学校,是一名不折不扣的工科生,专 ...
- 2018.09.07 loj#10166 数字游戏(数位dp)
传送门 数位dp板子题. f[i][mod]" role="presentation" style="position: relative;"> ...
- 2018.08.06 bzoj1500: [NOI2005]维修数列(非旋treap)
传送门 平衡树好题. 我仍然是用的fhqtreap,感觉速度还行. 维护也比线段树splay什么的写起来简单. %%%非旋treap大法好. 代码: #include<bits/stdc++.h ...
- jquery ajax 为什么会 多次请求
因你绑定的时间会随着你调用的地方增加而增加的,jquery 就是有这样的现象,举个例子让你解决吧,如果有个地方$('#Id').click(function(){ $.ajax({})})这样用对吧, ...
- Django 必会面试题总结
1 列举Http请求中常见的请求方式 HTTP请求的方法: HTTP/1.1协议中共定义了八种方法(有时也叫“动作”),来表明Request-URL指定的资源不同的操作方式 注意: 1)方法名称是 ...
- VMware + LInux + Xshell 连接环境设置(心得体会)
准备好VMware软件,和Linux 和xshell三款软件,下载和安装好,这里VMware是十二,Linux是CentOs 6 ,xshell是5 其实没有什么区别只要版本兼容就行,我们就可以实现远 ...