Java org.apache.lucene.index.IndexNotFoundException:在org.apache.lucene.store.RAMDirectory中找不到segments*文件
我不熟悉Java和Lucene。我的代码从文件中获取一行并将其存储在Lucene索引中。但是当我创建一个Java org.apache.lucene.index.IndexNotFoundException:在org.apache.lucene.store.RAMDirectory中找不到segments*文件,java,netbeans,lucene,Java,Netbeans,Lucene,我不熟悉Java和Lucene。我的代码从文件中获取一行并将其存储在Lucene索引中。但是当我创建一个IndexReader来搜索和读取索引时,它抛出了一个异常 下面是我的java代码。在创建IndexReader时,它会抛出IndexNotFoundException static String itemsfreq[]; static StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35); static Ind
IndexReader
来搜索和读取索引时,它抛出了一个异常
下面是我的java代码。在创建IndexReader
时,它会抛出IndexNotFoundException
static String itemsfreq[];
static StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
static IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
public static void index_data(Directory indexed_document,int doc_num,IndexWriter w) throws IOException
{
for(int i = 0;i < itemsfreq.length;i++)
{
Document doc = new Document();
doc.add(new Field(Integer.toString(doc_num)+","+itemsfreq[i],itemsfreq[i++], Field.Store.YES, Field.Index.ANALYZED));
w.addDocument(doc);
}
}
//Gets string from a file and insert it in INDEX named indexed_document
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader("fullText100.txt"));
String line;
int i = 0;
Directory indexed_document = new RAMDirectory();
IndexWriter writer = new IndexWriter(indexed_document, config);
while((line=reader.readLine()) != null)
{
if(i == 1)
{
break;
}
itemsfreq = line.split(" ");
index_data(indexed_document,i,writer);
i++;
}
IndexReader r = IndexReader.open(indexed_document);
}
静态字符串itemsfreq[];
静态标准分析仪=新标准分析仪(版本.LUCENE_35);
静态IndexWriterConfig配置=新的IndexWriterConfig(Version.LUCENE_35,analyzer);
公共静态无效索引数据(目录索引文档、int doc num、IndexWriter w)引发IOException
{
for(int i=0;i
要将更改写入索引,必须关闭索引编写器,然后打开IndexReader
writer.close();
如果必须在写入完成之前打开IndexReader,则必须告诉IndexReader重新打开索引以查看更改。在使用读取器打开索引之前,请调用一次
writer.commit()
您需要做的是在打开IndexSearcher之前显式调用commit
directory = new RAMDirectory();
iwriter = new IndexWriter(directory, config);
iwriter.commit();
现在打开搜索器
ireader = DirectoryReader.open(directory);
isearcher = new IndexSearcher(ireader);
还要记住,添加文档后需要调用commit,否则搜索可能找不到它。提交后需要重新打开Searcher(当然要关闭旧的Searcher)
我(在Lucene.Net中,C#)出现了这个错误,因为我通过在文件系统和内存中创建适当的目录创建了一个索引,FSDirectory
,但实际上还没有添加任何文档
具体地说,添加新文档的代码正在检查以确保它没有使用读取器添加副本,但是在尝试添加第一个文档时引发了异常,因为还没有片段
我是这样处理的:
// Make a reader to check the index for duplicates
// The reader will only be aware of items that were in the index before it was created
IndexReader reader = null;
try {
reader = IndexReader.Open( index, true );
} catch( IOException ) {
// There is no segments file because the index is empty
reader = null;
}
... // inside a method called by that scope, with reader passed in
// See if it exists already
// If the reader is null, then the index is empty
if( reader != null ) {
var existsTerm = new Term( SingleFieldIndexManager.FieldName, formattedTerm );
var matches = reader.TermDocs( existsTerm );
if( matches.Next() ) {
// It's already in there, no need to add again
return;
}
}
... // back to the first method after a return
// dispose of the reader if we managed to create one
if( reader != null ) {
reader.Dispose();
}
我遇到了类似的问题,并使用
writer.commit()
修复了它。如果我使用了writer.close()
,我将不得不重新打开writer。你的建议比上一个好。事实上,这是Lucene docs推荐的正确解决方案,可以重复使用IndexWriter和IndexReader的单个实例;这样,我就不会被迫关闭writer并在需要时再次创建它。嘿,我和你有同样的问题。但我不明白你是怎么解决的。如果没有文件,那么它将进入catch块,就是这样,对吗?找到了问题。我在使用参数create=false lucent.NET 4.8(我使用的是4.8.0-beta0006版)创建IndexWriter时遇到了这个错误,它使用类IndexWriterConfig
,而该类又有一个属性OpenMode
,该属性还允许指定类似create
或create\u或\u APPEND
的内容。我的期望是它会为我创建索引,但它似乎忽略了该参数。很可能我还没有正确使用它。我以前使用过3.0.3,它能够处理索引为空的情况。
// Make a reader to check the index for duplicates
// The reader will only be aware of items that were in the index before it was created
IndexReader reader = null;
try {
reader = IndexReader.Open( index, true );
} catch( IOException ) {
// There is no segments file because the index is empty
reader = null;
}
... // inside a method called by that scope, with reader passed in
// See if it exists already
// If the reader is null, then the index is empty
if( reader != null ) {
var existsTerm = new Term( SingleFieldIndexManager.FieldName, formattedTerm );
var matches = reader.TermDocs( existsTerm );
if( matches.Next() ) {
// It's already in there, no need to add again
return;
}
}
... // back to the first method after a return
// dispose of the reader if we managed to create one
if( reader != null ) {
reader.Dispose();
}