#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 全文检索数据库的更多相关文章

  1. JAVAEE——Lucene基础:什么是全文检索、Lucene实现全文检索的流程、配置开发环境、索引库创建与管理

    1. 学习计划 第一天:Lucene的基础知识 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3.配置开发环境 4.创建索引库 5 ...

  2. lucene解决全文检索word2003,word2007的办法

    在上一篇文章中 ,lucene只能全文检索word2003,无法检索2007,并且只能加载部分内容,无法加载全文内容.为解决此问题,找到了如下方法 POI 读取word (word 2003 和 wo ...

  3. 用Lucene.net对数据库建立索引及搜索<转>

    用Lucene.net对数据库建立索引及搜索 最近我一直在研究 Lucene.net ,发现Lucene.net对数据库方面建索引的文章在网上很少见,其实它是可以对数据库进行索引的,我闲着没事,写了个 ...

  4. Lucene的全文检索学习

    Lucene的官方网站(Apache的顶级项目):http://lucene.apache.org/ 1.什么是Lucene? Lucene 是 apache 软件基金会的一个子项目,由 Doug C ...

  5. 基于Lucene的全文检索实践

    由于项目的需要,使用到了全文检索技术,这里将前段时间所做的工作进行一个实践总结,方便以后查阅.在实际的工作中,需要灵活的使用lucene里面的查询技术,以达到满足业务要求与搜索性能提升的目的. 一.全 ...

  6. lucene教程--全文检索技术

    1    Lucene 示例代码        https://blog.csdn.net/qzqanzc/article/details/80916430 2   Lucene 实例教程(一)初识L ...

  7. 黑马_10 Lucene:全文检索

    10 Lucene:01.全文检索基本介绍 10 Lucene:02.创建索引库和查询索引 10 Lucene:03.中文分析器 10 Lucene:04.索引库维护CURD

  8. Lucene.net 全文检索 盘古分词

    lucene.net + 盘古分词 引用: 1.Lucene.Net.dll 2.PanGu.Lucene.Analyzer.dll 3.PanGu.HighLight.dll 4.PanGu.dll ...

  9. Lucene.net 全文检索文件

    using Lucene.Net.Analysis; using Lucene.Net.Analysis.Tokenattributes; using Lucene.Net.Documents; us ...

随机推荐

  1. win下php的memcached的安装与使用

    1.memcache的php扩展与memcached服务器的区别? php要操作memcached就必须要安装memcache的扩展, 在http://windows.php.net/download ...

  2. OpenGLES.gpus_ReturnNotPermittedKillClient

    在iOS中,使用OpenGLES不当引起的crash:“gpus_ReturnNotPermittedKillClient” https://developer.apple.com/library/i ...

  3. 02 请求库之 requests模块

    requests模块   一 介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requ ...

  4. 深入浅出 JMS(三) - ActiveMQ 消息传输

    深入浅出 JMS(三) - ActiveMQ 消息传输 一.消息协商器(Message Broker) broke:消息的交换器,就是对消息进行管理的容器.ActiveMQ 可以创建多个 Broker ...

  5. 为了记忆和方便翻阅 vue构建后的结构目录说明

    一. ├── build              // 项目构建(webpack)相关代码             记忆:(够贱)    9个 │ ├── build.js       // 生产环 ...

  6. python之数据类型1

    什么是数据类型及数据类型分类        python中的数据类型 python使用对象模型来存储数据,每一个数据类型都有一个内置的类,每新建一个数据,实际就是在初始化生成一个对象,即所有数据都是对 ...

  7. sqlserver实现分页的几种方式

    sqlserver实现分页的几种方式 第一种:使用org.springframework.data.domain.Page来进行分页 package com.cellstrain.icell.repo ...

  8. Map的常用操作

    public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map. ...

  9. UVa 1572 Self-Assembly (构造+拓扑排序。。。。。)

    题意:给定n个带标号的正方形,标号要么是一个大写字母加一个+或-,要么是00, 当且仅当大写字母相同并且符号相反时可以连接,问你给定的能不能拼成一个无限大的的东西. 析:说实话,真心没有看出来是拓扑排 ...

  10. wordpaster更新说明

    官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/wordpaster/index.aspx 在线演示:FCKEditor2x示例 ...