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 ...
随机推荐
- 【校招面试 之 C/C++】第9题 C++多态
C++的多态性用一句话概括就是:在基类的函数前加上virtual关键字,在派生类中重写该函数,运行时将会根据对象的实际类型来调用相应的函数.如果对象类型是派生类,就调用派生类的函数:如果对象类型是基类 ...
- maven不存在jar包解决
win7环境 下载:https://maven.apache.org/download.cgi 提取文件,并cmd 转到bin目录 假设要添加的jar包是jbarcode-0.2.8.jar, 可执行 ...
- [leetcode]381. Insert Delete GetRandom O(1) - Duplicates allowed常数时间插入删除取随机值
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- Linux SSH基于密钥交换的自动登陆原理简介及配置说明
一.原理简介 SSH证书认证登录的基础是一对唯一匹配密钥: 私钥(private key)和公钥(public key).公钥用于对数据进行加密,而且只能用于加密.而私钥只能对使用所匹配的公钥,所加密 ...
- 10-多写一个@Autowired导致程序崩了
再是javaweb实验六中,是让我们改代码,让它跑起来,结果我少注释了一个,导致一直报错,检查许久没有找到,最后通过代码替换逐步查找,才发现问题.
- .net core webapi 部署windows server 2008 r2 笔记
WebAPI部署文档 安装dotnet-dev-win-x64.1.0.4 安装DotNetCore.1.1.0-WindowsHosting 安装vc_redist.x64 安装Windows6.1 ...
- Linux gprof命令
一.简介 gprof是GNU工具之一,它在编译的时候在每个函数的出入口加入了profiling的代码,运行时统计程序在用户态的执行信息,可以得到每个函数的调用次数,执行时间,调用关系等信息,简单易懂. ...
- geoserver 源码介绍
上一章我们通过实现一个服务对如何扩展GeoServer有了一定的了解,但是,对于为何要这样做并没有说明,本章我们重点来说说GeoServer的结构,下图来自GeoServer官网(希望没有侵权),它很 ...
- 跨页传值c#
Application (4)URL地址中的参数 (5)通过隐藏字段来传递数据 (6)Server.Transfer (7)通过序列化对象 (8)........ 下面就分别一一介绍: (1)使用Se ...
- ip白名单 通过* ? 检测IP匹配 轻量级
#include "stdafx.h" #include <windows.h> #include <string.h> #include <asse ...