zoukankan      html  css  js  c++  java
  • lucene.Net学习一

    建索引的代码,代码里面有注释

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    using Lucene.Net.Index;
    using Lucene.Net.Store;
    using Lucene.Net.Util;
    using Lucene.Net.Analysis.Standard;
    using Lucene.Net.Documents;



    namespace TestFileIndex
    {
    class Program
    {
    static void Main(string[] args)
    {
    FileInfo index_dir = new FileInfo(@"F:\lucene_index");//索引文件存放的路径
    FileInfo doc_dir = new FileInfo(@"F:\doc");//需要创建的索引的原始文件
    //实例化索引对象
    IndexWriter IW = new IndexWriter(FSDirectory.Open(index_dir), new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
    IndexDocs(IW,doc_dir); //创建索引
    IW.Optimize();
    IW.Close();
    }

    private static void IndexDocs(IndexWriter writer, FileInfo file)
    {
    if (System.IO.Directory.Exists(file.FullName))//判断指定的目录是否存在,注意不是文件的路径
    {
    System.String[] files = System.IO.Directory.GetFileSystemEntries(file.FullName);//返回指定目录中的所有文件和子目录
    if (files != null)
    {
    for (int i = 0; i < files.Length; i++)
    {
    IndexDocs(writer, new System.IO.FileInfo(files[i]));//递归调用
    }
    }
    }
    else
    {
    System.Console.Out.WriteLine("adding " + file.FullName);
    try
    {
    Document d = new Document();
    d.Add(new Field("path",file.FullName, Field.Store.YES, Field.Index.NOT_ANALYZED));
    d.Add(new Field("contents", new System.IO.StreamReader(file.FullName, System.Text.Encoding.Default)));
    writer.AddDocument(d);
    }
    catch (System.IO.FileNotFoundException fnfe)
    {
    Console.WriteLine(fnfe.Message);
    }
    }
    }


    }
    }

    查询索引的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.IO;
    using Lucene.Net.Store;
    using Lucene.Net.Index;
    using Lucene.Net.Search;
    using Lucene.Net.Documents;

    namespace TestSearchFileIndex
    {
    class Program
    {
    static void Main(string[] args)
    {
    string indexdir=@"F:\lucene_index";//索引文件存放的路径
    string q = "";
    searchindex(indexdir, q);
    }

    private static void searchindex(string f,string query)
    {
    //打开索引文件,并将索引文件的相关信息读入
    Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(f));
    IndexSearcher IS = new IndexSearcher(dir);//打开索引

    Term t = new Term("contents", query);//构建一个Term对象
    Query quer = new TermQuery(t);//构建一个Query对象

    TopDocs hits = IS.Search(quer, 10);//检索

    for (int i = 0; i < hits.scoreDocs.Length; i++)
    {
    Document doc = IS.Doc(hits.scoreDocs[i].doc);//检索匹配到的文档
    Console.WriteLine(doc.Get("path"));//显示文件路径
    }

    }
    }
    }

      注意这里面的字符编码,否则查询的时候查不到      d.Add(new Field("contents", new StreamReader(file.FullName, System.Text.Encoding.Default)));  

  • 相关阅读:
    事务(进程 ID 133)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品的解决方案
    Waiting for cache lock
    Win11系统将软件安装在C盘后,打开软件会有无法正常读写C盘下文件的问题
    SharePoint 2010 微软学习教程
    Oracle 远程连接配置文件
    如何优化操作大数据量数据库(几十万以上数据)(二。改善SQL语句)
    SQL BI Microsoft MSDN
    Oracle 错误:ORA06413: Connection not open 解决办法
    2008R2Win7管理一创建域和加入域
    学生表 课程表 成绩表 教师表 50个常用sql语句
  • 原文地址:https://www.cnblogs.com/guoyuanwei/p/2423072.html
Copyright © 2011-2022 走看看