如何使用指南针 lucene 生成的 cfs 索引?

问题描述

使用(最新的)lucene 8.7 是否可以在我无法修改的遗留应用程序中使用 lucene 实用程序“Luke”打开由 2009 年左右的 lucene 2.2 生成的 .cfs 复合索引文件? 或者是否可以从 .cfs 为 Luke 生成 .idx 文件? .cfs 是由 lucene 2.2 上的指南针生成的,而不是直接由 lucene 生成的 是否可以使用指南生成的包含以下内容的索引:
_b.cfs
段.gen
段_d

可能与 solr 一起使用?

是否有任何示例如何在任何地方使用指南针打开基于文件的 .cfs 索引?

转换工具无法使用,因为索引版本太旧:

来自 lucene\build\demo :

java -cp ../core/lucene-core-8.7.0-SNAPSHOT.jar;../backward-codecs/lucene-backward-codecs-8.7.0-SNAPSHOT.jar org.apache.lucene.index .IndexUpgrader -verbose path_of_old_index

搜索文件演示:

java -classpath ../core/lucene-core-8.7.0-SNAPSHOT.jar;../queryparser/lucene-queryparser-8.7.0-SNAPSHOT.jar;./lucene-demo-8.7.0- SNAPSHOT.jar org.apache.lucene.demo.SearchFiles -index path_of_old_index

都失败:

org.apache.lucene.index.IndexFormatTooOldException:不支持格式版本 此版本的 Lucene 仅支持使用 6.0 及更高版本创建的索引。

可以以某种方式使用带有 lucene 的旧索引吗?如何使用旧的“编解码器”? 如果可能,也来自 lucene.net?

当前的 lucene 8.7 生成一个包含这些文件的索引:

segments_1
写锁
_0.cfe
_0.cfs
_0.si

================================================ ============================ 更新:令人惊讶的是,它似乎使用来自 nuget 的 lucene.net v. 3.0.3 打开了那个非常旧的格式索引!

这似乎是为了从索引中提取所有术语:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Globalization;
    
    using Lucene.Net.Analysis.Standard;
    using Lucene.Net.Documents;
    using Lucene.Net.Index;
    using Lucene.Net.QueryParsers;
    using Lucene.Net.Search;
    using Lucene.Net.Store;
    using Version = Lucene.Net.Util.Version;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main()
            {
    
                var reader = IndexReader.Open(FSDirectory.Open("C:\\Temp\\ftsemib_opzioni\\v210126135604\\index\\search_0"),true);
                Console.WriteLine("number of documents: "+reader.NumDocs() + "\n");
                Console.ReadLine();
    
                TermEnum terms = reader.Terms();
                while (terms.Next())
                {
                    Term term = terms.Term;
                    String termField = term.Field;
                    String termText = term.Text;
                    int frequency = reader.DocFreq(term);
                    Console.WriteLine(termField +" "+termText);
                }
                var fieldNames = reader.GetFieldNames(IndexReader.FieldOption.ALL);
                int numFields = fieldNames.Count;
                Console.WriteLine("number of fields: " + numFields + "\n");
                for (IEnumerator<String> iter = fieldNames.GetEnumerator(); iter.MoveNext();)
                {
                    String fieldName = iter.Current;
                    Console.WriteLine("field: " + fieldName);
                }
                reader.Close();
    
                Console.ReadLine();
            }
        }
    
    }

出于好奇,是否有可能找出它是什么索引版本? 有没有基于文件系统的索引的(旧)指南针示例?

解决方法

很遗憾,您不能使用旧的 Codec 来访问 Lucene 2.2 中的索引文件。这是因为在 Lucene 4.0 中引入了编解码器。在此之前,用于读取和写入索引文件的代码并未组合到编解码器中,而只是整个 Lucene 库的固有部分。

因此在 Lucene 4.0 之前的版本中没有编解码器,只是将文件读取和写入代码烘焙到库中。追踪所有这些代码并创建一个可以插入现代 Lucene 版本的编解码器将是非常困难的。这不是不可能完成的任务,但它需要专家 Lucene 开发人员和大量的努力(即极其昂贵的努力)。

鉴于所有这些,这个 SO 问题的答案可能会有用:How to upgrade lucene files from 2.2 to 4.3.1

更新

您最好的选择是使用 java lucene 的旧 3.x 副本或 Lucene.net ver 3.0.3 打开索引,然后添加并提交一个文档(这将创建第二个段)并执行优化这将导致两个段合并为一个新段。新段将是版本 3 段。然后你可以再次使用 Lucene.Net 4.8 Beta 或 Java Lucene 4.X 做同样的事情(但 Commit 从版本 4 开始更名为 ForceMerge)将索引转换为 4.x 索引。

然后您可以使用当前的 Lucene 8.x 版本再次执行此操作,将索引一直移动到 8,因为当前版本的 Java Lucene 的编解码器一直回到 5.0,请参阅:{ {3}}

但是,如果您再次收到您报告的错误:

此版本的 Lucene 仅支持使用 6.0 及更高版本创建的索引。

那么您将不得不使用 6.x Java Lucene 再玩一次这个游戏,以便从 5.x 索引变为 6.x 索引。 :-)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...