如何查看Lucene索引

Nag*_*Nag 7 lucene luke

我正在尝试学习和理解 lucene 是如何工作的,lucene 索引里面有什么。基本上我想看看数据在 lucene 索引中是如何表示的?

我用作lucene-core 8.6.0依赖项

下面是我非常基本的 Lucene 代码

    private Document create(File file) throws IOException {
        Document document = new Document();

        Field field = new Field("contents", new FileReader(file), TextField.TYPE_NOT_STORED);
        Field fieldPath = new Field("path", file.getAbsolutePath(), TextField.TYPE_STORED);
        Field fieldName = new Field("name", file.getName(), TextField.TYPE_STORED);

        document.add(field);
        document.add(fieldPath);
        document.add(fieldName);

        //Create analyzer
        Analyzer analyzer = new StandardAnalyzer();

        //Create IndexWriter pass the analyzer

        Path indexPath = Files.createTempDirectory("tempIndex");
        Directory directory = FSDirectory.open(indexPath);
        IndexWriterConfig indexWriterCOnfig = new IndexWriterConfig(analyzer);
        IndexWriter iwriter = new IndexWriter(directory, indexWriterCOnfig);
        iwriter.addDocument(document);
        iwriter.close();
        return document;
    }
Run Code Online (Sandbox Code Playgroud)

注意:我了解 Lucene 背后的知识 - 倒排索引,但我缺乏对 lucene 库使用此概念以及如何创建文件以便使用 lucene 使搜索变得简单可行的了解。

我尝试过 Limo,但没有用。尽管我在 web.xml 中给出了索引位置,但它还是不起作用

and*_*mes 8

如果您想查看一个很好的介绍性代码示例,使用当前版本的 Lucene(构建索引然后使用它),您可以从基本演示开始(选择您的版本 - 此链接适用于 Lucene 8.6)。

\n

演示的源代码(使用最新版本的 Lucene)可以在 Github 上找到。

\n

如果您想探索索引数据,在创建索引数据后,您可以使用 Luke。如果您以前没有使用过它:要运行 Luke,您需要从主下载页面下载二进制版本。解压缩该文件,然后导航到该目录。然后运行相关脚本(或)。lukeluke.batluke.sh

\n

LIMO(我能找到的该工具的唯一版本是Sourceforge 上的这个版本。鉴于它是 2007 年的版本,几乎可以肯定它不再与最新的 Lucene 索引文件兼容。也许某处有更新的版本。)

\n

如果您想了解典型 Lucene 索引中的文件的概述,可以从这里开始

\n

通过查看相关包和类的API 文档,可以回答许多具体问题。

\n

就我个人而言,我还发现SolrElasticSearch文档对于解释特定概念非常有用,这些概念通常与 Lucene 直接相关。

\n

除此之外,我不太担心 Lucene 如何管理其内部索引数据结构。相反,我专注于可用于访问该数据的不同类型的分析器和查询。

\n
\n

更新:SimpleTextCodec

\n

现在已经是几个月后的事了,但这里还有另一种探索 Lucene 索引数据的方法:SimpleTextCodec。标准 Lucene 编解码器(如何将数据写入索引文件并从中读取数据)使用二进制格式 - 因此人类不可读。您不能只打开索引文件并查看其中的内容。

\n

但是,如果将编解码器更改为SimpleTextCodec,那么 Lucene 将创建纯文本索引文件,您可以在其中更清楚地看到结构。

\n

该编解码器纯粹用于信息/教育,不应在生产中使用。

\n

要使用编解码器,您首先需要包含相关的依赖项 - 例如,如下所示:

\n
<dependency>\n    <groupId>org.apache.lucene</groupId>\n    <artifactId>lucene-codecs</artifactId>\n    <version>8.7.0</version>\n</dependency>\n
Run Code Online (Sandbox Code Playgroud)\n

现在您可以按如下方式使用这个新的编解码器:

\n
iwc.setCodec(new SimpleTextCodec());\n
Run Code Online (Sandbox Code Playgroud)\n

因此,例如:

\n
final String indexPath = "/path/to/index_dir";\nfinal String docsPath = "/path/to/inputs_dir";\nfinal Path docDir = Paths.get(docsPath);\nDirectory dir = FSDirectory.open(Paths.get(indexPath));\nAnalyzer analyzer = new StandardAnalyzer();\nIndexWriterConfig iwc = new IndexWriterConfig(analyzer);\niwc.setOpenMode(OpenMode.CREATE);\niwc.setCodec(new SimpleTextCodec());\nSystem.out.println(iwc.getCodec().getName());\ntry ( IndexWriter writer = new IndexWriter(dir, iwc)) {\n    // read documents, and write index data:\n    indexDocs(writer, docDir);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

您现在可以在文本阅读器(例如 Notepad++)中自由检查生成的索引文件。

\n

就我而言,索引数据生成了几个文件 - 但我在这里感兴趣的是我的*.scf文件 - 一个“复合”文件,包含各种 \xe2\x80\x9c 虚拟文件 \xe2\x80\x9d 部分,其中人类- 存储可读索引数据。

\n