#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. log4j每天,每小时产生一日志文件

    log4j每天,每小时产生一日志文件 2016年08月05日 14:14:33 阅读数:6254 一.之前的文章中有log4j的相关配置以及属性的介绍,下面我们先把配置列出来:   log4j.roo ...

  2. Spring依赖注入servlet会话监听器

    Spring提供了一个 “ContextLoaderListener” 监听器,以使 Spring 依赖注入到会话监听器. 在本教程中,通过添加一个 Spring 依赖注入一个bean 到会话监听器修 ...

  3. Oracle LOOP循环控制语句

    在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列.常用的LOOP循环语句包含3种形式:基本的LOOP.WHILE...LOOP和FOR...LOOP. LO ...

  4. 《JavaScript高级程序设计》笔记

    1. 当在函数内部定义了其他函数时,就创建了闭包.闭包有权访问包含函数内部的所有变量. 2. 闭包可以分隔变量空间,不会占用全局空间而造成相互间的干拢.使用闭包可以在JavaScript中模仿块级作用 ...

  5. Angular 通过注入 $location 获取与修改当前页面URL

    //1.获取当前完整的url路径 var absurl = $location.absUrl(); //http://172.16.0.88:8100/#/homePage?id=10&a=1 ...

  6. java实现从url路径中下载pdf文档到本地

    package com.cellstrain.icell.util; import java.io.*;import java.net.*; public class DownloadPdf { /* ...

  7. 2018.09.09 poj2949Word Rings(01分数规划+spfa判环)

    传送门 这题要先巧妙的转化一下. 对于每个字符串,我们把头尾的两个小字符串对应的点连边,边权是这个字符串的长度. 这样最多会出现26*26个点. 这个时候就只用求出边权和跟边数的最大比值了. 这个显然 ...

  8. hdu-1070(水题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1070 题意:一个人喝牛奶,有三个原则: 1.牛奶的日期不超过6天,就是最多5天. 2.每次只喝200m ...

  9. hdu-1698(线段树,区间修改)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 注意:用位运算会更快,不然超时. #include<iostream> #inclu ...

  10. hdu-1166(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 思路:线段树模板 #include<iostream> #include<cs ...