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)));  

  • 相关阅读:
    jQuery
    jquery操作滚动条滚动到指定元素位置 scrollTop
    javascript的offset、client、scroll使用方法
    js javascript:void(0) 真正含义
    Js中数组Array的用法
    javascript中typeof、undefined 和 null
    函数的参数是函数,函数中Ajax返回的回调函数中的函数运行
    xampp中php手动升级
    jQuery Mobile里xxx怎么用呀? (事件篇)
    eval解析JSON中的注意点
  • 原文地址:https://www.cnblogs.com/guoyuanwei/p/2423072.html
Copyright © 2011-2022 走看看