Lucene系列 - 索引(六) - 创建本地搜索的索引

13 篇文章 1 订阅
13 篇文章 0 订阅

整理记录了在Lucene 4版本中的遍历读取本地文件夹数据,并创建索引的过程。
大致步骤:

  1. 确定索引的存放目录和待索引文件的路径
  2. 本地生成磁盘索引,准备添加数据
  3. 读取目录下的文件信息,分析文本文件,并使用内存索引进行索引。之后将内存索引添加到本地磁盘索引中去。(为了提高索引和检索的性能指标)
  4. 读子目录进行递归遍历,直至所有文件遍历完成。
  5. 关闭本地磁盘索引目录,索引创建完成。
package com.gangwu.lucene.tools;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

/**
 * Lucene Index索引相关工具
 * @author root
 *
 */
public class IndexTools {
    private static Logger logger = Logger.getLogger(IndexTools.class);

    public static void main(String[] args) {
        int nums = indexBuilder(new File("Dest_Index_Path"), new File("Test_File_Path"));
        System.out.println("doc counts is : " + nums);
    }

    /**
     * 索引创建函数.<br>
     * 生成IndexWriter创建索引,调用子目录索引函数,并优化存储本地磁盘索引
     * @param indexPath 指定索引目录
     * @param dataPath 待分析目录
     * @return 返回的文档总数
     */
    public static int indexBuilder(File indexPath, File dataPath) {
        if (!dataPath.exists() || !dataPath.isDirectory() || !dataPath.canRead()) {
            try {
                throw new IOException(dataPath + " 不存在或不允许访问!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        int num = 0;
        try {
            Analyzer analyzer = new StandardAnalyzer();//文本分析器
            IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
            conf.setUseCompoundFile(true);//采用多文件索引结构,默认为复合索引
            Directory fsDir = FSDirectory.open(indexPath);
            IndexWriter fsdWriter = new IndexWriter(fsDir, conf);

            subIndexBuilder(fsdWriter, dataPath);

            num = fsdWriter.numDocs();

            fsdWriter.forceMerge(5);//优化压缩段,执行优化的方法,参数表示优化称几段索引
            fsdWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return num;
    }

    /**
     * 递归函数,递归分析目录.<br>
     * 如果找到子目录,继续递归;如果找到文件分析文件内容并建立索引
     * @param fsdWriter IndexWriter
     * @param subPath 待分析目录
     */
    private static void subIndexBuilder(IndexWriter fsdWriter, File subPath) {
        File[] fileLIst = subPath.listFiles();
        for (int i = 0; i < subPath.length(); i++) {
            File file = fileLIst[i];
            if (file.isDirectory()) {
                subIndexBuilder(fsdWriter, file);
            } else if (IsValidType(file.getName())) {
                fileIndexBUilder(fsdWriter, file);
            }
        }
    }

    /**
     * 创建RAM内存索引,生成并添加新文档,且合并到本地磁盘索引中
     * @param fsdWriter IndexWriter
     * @param subFile 待分析目录
     */
    private static void fileIndexBUilder(IndexWriter fsdWriter, File subFile) {
        if (subFile.isHidden() || !subFile.exists() || !subFile.canRead()) {
            return;
        }
        try {
            Directory ramDir = new RAMDirectory();
            Analyzer analyzer = new StandardAnalyzer();//文本分析器
            IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);
            conf.setUseCompoundFile(true);//采用多文件索引结构,默认为复合索引
            IndexWriter ramWriter = new IndexWriter(ramDir, conf);

            FileReader fileReader = new FileReader(subFile);
            System.out.println("-> 创建索引 : " + subFile.getCanonicalPath());
            Document document = new Document();
            Field fieldName = new TextField("name", subFile.getName(), Store.YES);
            document.add(fieldName);
            Field fieldPath = new TextField("path", subFile.getAbsolutePath(), Store.YES);
            document.add(fieldPath);
            Field fieldContent = new TextField("content", fileReader);
            document.add(fieldContent);

            ramWriter.addDocument(document);//文档添加到内存索引
            ramWriter.close();//关闭内存索引,保存添加的数据

            fsdWriter.addIndexes(new Directory[] {ramDir});//添加内存索引到磁盘索引
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断当前文件名是否符合文件后缀的要求
     * @param name 文件名
     * @return true 有效文件
     */
    private static boolean IsValidType(String name) {
        if (name.endsWith(".txt")) {
            return true;
        } else {
            return false;
        }
    }   

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C# Lucene.Net创建索引的步骤: 1.添加Lucene.Net库的引用 在Visual Studio中,右键单击项目并选择“管理NuGet程序包”。在搜索框中搜索Lucene.Net”,然后安装Lucene.Net库。 2.创建索引 ```csharp using Lucene.Net.Analysis.Standard; using Lucene.Net.Documents; using Lucene.Net.Index; using Lucene.Net.Store; using System.IO; // 创建索引 public void CreateIndex(string indexPath, string dataPath) { // 创建分析器 var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); // 创建索引存储目录 var directory = FSDirectory.Open(new DirectoryInfo(indexPath)); // 创建索引写入器 var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); // 读取数据文件 var lines = File.ReadAllLines(dataPath); // 遍历数据文件中的每一行 foreach (var line in lines) { // 创建文档 var doc = new Document(); // 添加字段 doc.Add(new Field("content", line, Field.Store.YES, Field.Index.ANALYZED)); // 将文档写入索引 writer.AddDocument(doc); } // 关闭索引写入器 writer.Dispose(); } ``` 3.使用索引 ```csharp using Lucene.Net.Analysis.Standard; using Lucene.Net.QueryParsers; using Lucene.Net.Search; using Lucene.Net.Store; using System.IO; // 使用索引 public void SearchIndex(string indexPath, string queryStr) { // 创建分析器 var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); // 创建索引存储目录 var directory = FSDirectory.Open(new DirectoryInfo(indexPath)); // 创建索引搜索器 var searcher = new IndexSearcher(directory, true); // 创建查询解析器 var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "content", analyzer); // 解析查询字符串 var query = parser.Parse(queryStr); // 执行查询 var hits = searcher.Search(query, null, 10, Sort.RELEVANCE).ScoreDocs; // 遍历查询结果 foreach (var hit in hits) { // 获取文档 var doc = searcher.Doc(hit.Doc); // 输出文档内容 Console.WriteLine(doc.Get("content")); } // 关闭索引搜索器 searcher.Dispose(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值