今天突然想来看一下全文检索,于是就了解了一下Lucene.Net,然后把公司目前的产品表拿来练手,写了这么个Demo。

  先看一下Demo的代码  

    public class ProductRepository
{
private Logger logger = new Logger(typeof(ProductRepository)); /// <summary>
/// 分页获取商品数据
/// </summary>
/// <param name="tableNum"></param>
/// <param name="pageIndex">从1开始</param>
/// <param name="pageSize"></param>
/// <returns></returns>
public List<Product> QueryList(int tableNum, int pageIndex, int pageSize)
{
string sql = string.Format("SELECT top {2} ProductID,ProductCode,ProductName,PreferentialPrice AS Price,ProductImage AS ImageUrl FROM Product WHERE ProductID>{1};", tableNum.ToString(""), pageSize * Math.Max(, pageIndex - ), pageSize);
return SqlHelper.QueryList<Product>(sql);
}
}
internal class LuceneTest
{
public static void Show(string searchInput)
{
FSDirectory dir = FSDirectory.Open(StaticConstant.TestIndexPath);
IndexSearcher searcher = new IndexSearcher(dir);//查找器 QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "title", new PanGuAnalyzer());//解析器
{
string keyword = searchInput;
{
Query query = parser.Parse(keyword);
TopDocs docs = searcher.Search(query, null, );//找到的数据 int i = ;
foreach (ScoreDoc sd in docs.ScoreDocs)
{
if (i++ < )
{
Document doc = searcher.Doc(sd.Doc);
Console.WriteLine("***************************************");
Console.Write(string.Format(" id={0}", doc.Get("id")));
Console.Write(string.Format(" title={0}", doc.Get("title")));
Console.Write(string.Format(" time={0}", doc.Get("time")));
Console.Write(string.Format(" price={0}", doc.Get("price")));
Console.WriteLine("***************************************");
}
}
Console.WriteLine($"一共命中了{docs.TotalHits}个");
}
}
} /// <summary>
/// 初始化索引
/// </summary>
public static void InitIndex()
{
List<Product> productList = GetList(); FSDirectory directory = FSDirectory.Open(StaticConstant.TestIndexPath);//FSDirectory表示存放在文件中
using (IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED))//索引写入器
{
foreach (Product product in productList)
{
Document doc = new Document();//一条数据              
doc.Add(new Field("id", product.ProductID.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED));//一个字段 列名 值 是否保存值 是否分词
doc.Add(new Field("title", product.ProductName, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("imageurl", product.ImageUrl, Field.Store.NO, Field.Index.NOT_ANALYZED));
doc.Add(new Field("content", "this is lucene working,powerful tool ", Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new NumericField("price", Field.Store.YES, true).SetDoubleValue((double)(product.Price)));
doc.Add(new NumericField("time", Field.Store.YES, true).SetIntValue(int.Parse(DateTime.Now.ToString("yyyyMMdd"))));
writer.AddDocument(doc);//写进去
}
writer.Optimize();//优化合并
}
}
/// <summary>
/// 数据库取5000条数据测试
/// </summary>
/// <returns></returns>
private static List<Product> GetList()
{
ProductRepository repository = new ProductRepository();
List<Product> productList = repository.QueryList(, , );
return productList;
}
}
class Program
{
static void Main(string[] args)
{
LuceneTest.InitIndex(); string input;
while ((input = Console.ReadLine()).ToUpper() != "EXIT")
{
if (input == "") continue; LuceneTest.Show(input);
}
}
}

  现在来看一下LuceneTest的InitIndex方法。

  FSDirectory directory = FSDirectory.Open(StaticConstant.TestIndexPath); 该语句表示将创建的索引保存到文件中;

  IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED)  接着我们创建了一个writer,并指定存放索引的目录为刚创建的directory,使用的分析器为PanGuAnalyzer,第三个参数说明如果已经有索引文件在索引目录下,我们将覆盖它们。

  我们循环遍历productList,创建Document,然后往Document添加Filed。这里说明一下Document是表示含文档的数据结构,定义了存储文档的数据结构,Field类定义了document一个域。Field有两个属性可选:存储和索引。通过存储属性你可以控制是否对这个Field进行存储;通过索引属性你可以控制是否对该Field进行索引。最后将document加入到IndexWriter里,使用IndexWriter.Optimize()进行优化合并。

  之后我们可以看到在我们制定的目录下生成了下面的文件

  最后我们在Show方法中使用 IndexSearcher 进行检索。

最新文章

  1. vim保存文件时,生成.un~文件
  2. sublime text2 bracketHighLighter 配置
  3. Qt里的slot
  4. win10 chrome浏览器字体小,模糊
  5. c++中的指针
  6. java 1.7
  7. 【BZOJ 1507】【NOI 2003】&amp;【Tyvj P2388】Editor 块状链表模板题
  8. 在eclipse中下载包含子模块(Submodules)的git项目
  9. Linux开发工具之Makefile(上)
  10. POJ 1019 Number Sequence 解读
  11. 201521123009《Java程序设计》第3周学习总结
  12. 深度学习之注意力机制(Attention Mechanism)和Seq2Seq
  13. JavaScript 获得客户端IP
  14. C#矩阵求逆
  15. UI5-文档-4.25-Sorting and Grouping
  16. android安装前期遇到的问题
  17. 贪心--cf、education62-C
  18. &lt;mvc:annotation-driven /&gt;注解详解
  19. [零基础学JAVA]Java SE基础部分-03. 运算符和表达式
  20. SSH面试题目

热门文章

  1. ThreadPoolExecutor之二:jdk实现的线程池介绍
  2. MySQL主从详细安装步骤
  3. InstallShield 12 豪华版+破解版 下载
  4. 个人项目开发PSP实践-MyWCprj
  5. python接口自动化(三十八)-python操作mysql数据库(详解)
  6. Cg(C for Graphic)语言语义绑定方法(转)
  7. (六)SpringBoot整合Swagger2框架
  8. 语句 if
  9. Scipy-数值计算库
  10. Tcp实现省略编码