gpt4 book ai didi

java - 如何删除或更新 apache Lucene 中的文档

转载 作者:行者123 更新时间:2023-12-01 09:32:29 24 4
gpt4 key购买 nike

目前,我可以将文档列表以及单个文档添加到 apache lucene 索引中。但我在从索引更新文档时遇到了问题:

我的方法是在文件上传后立即进行,因此在写入磁盘之前,我会检查驱动器/文件夹中是否存在文件,并根据文件名删除索引。

其次,我将上传的文件添加到 Lucene 索引中。

但是我遇到的问题是新添加的文档和旧文档都以不同的内容显示在搜索结果中。

例如:文件名为 Sample_One.txt,文本为:

This is the sample text for first time.

从索引中删除上述文件,然后将新文件内容添加到索引中。

现在文件内容已更新为具有相同文件名的另一个文本:

This is the sample text with updated content.

在搜索诸如“sample”之类的文本时,结果显示 Sample_One.txt 文件两次,其中包含旧内容和新内容。

我想知道我是否遗漏了某些内容以及如何将文档更新/删除到索引中。

代码片段是:

//Deleting the Document from the Index
public void deleteDocumentsFromIndexUsingTerm(Document doc) throws IOException, ParseException {
Term fileTerm = new Term("file_name",doc.get("file_name"));
Term contentTerm = new Term("content", doc.get("content"));
Term docIDTerm = new Term("document_id", doc.get("document_id"));

File indexDir = new File(INDEX_DIRECTORY);

Directory directory = FSDirectory.open(indexDir.toPath());

Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(directory, conf);

System.out.println("Deleting the term with - "+doc.get("file_name"));
System.out.println("Deleting the term with contents - "+doc.get("content"));

indexWriter.deleteDocuments(fileTerm);
indexWriter.deleteDocuments(contentTerm);
indexWriter.deleteDocuments(docIDTerm);
indexWriter.commit();
indexWriter.close();
}

//将文档添加到索引的代码段

final String INDEX_DIRECTORY = "D:\\Development\\Lucene_Indexer";
long startTime = System.currentTimeMillis();
List<ContentHandler> contentHandlerList = new ArrayList<ContentHandler>();

String fileNames = (String)request.getAttribute("message");

File file = new File("D:\\Development\\Resume_Sample\\"+fileNames);

ArrayList<File> fileList = new ArrayList<File>();
fileList.add(file);

Metadata metadata = new Metadata();

// BodyContentHandler set the value as -1 to evade the Text Limit Exception
ContentHandler handler = new BodyContentHandler(-1);
ParseContext context = new ParseContext();
Parser parser = new AutoDetectParser();
InputStream stream = new FileInputStream(file);

try {
parser.parse(stream, handler, metadata, context);
contentHandlerList.add(handler);
}catch (TikaException e) {
e.printStackTrace();
}catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

FieldType fieldType = new FieldType();
fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
fieldType.setStoreTermVectors(true);
fieldType.setStoreTermVectorPositions(true);
fieldType.setStoreTermVectorPayloads(true);
fieldType.setStoreTermVectorOffsets(true);
fieldType.setStored(true);


Analyzer analyzer = new StandardAnalyzer();
Directory directory = FSDirectory.open(new File(INDEX_DIRECTORY).toPath());
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
IndexWriter writer = new IndexWriter(directory, conf);

Iterator<ContentHandler> handlerIterator = contentHandlerList.iterator();
Iterator<File> fileIterator = fileList.iterator();

while (handlerIterator.hasNext() && fileIterator.hasNext()) {
Document doc = new Document();

String text = handlerIterator.next().toString();
String textFileName = fileIterator.next().getName();

String idOne = UUID.randomUUID().toString();

Field idField = new Field("document_id",idOne,fieldType);
Field fileNameField = new Field("file_name", textFileName, fieldType);
Field contentField = new Field("content",text,fieldType);


doc.add(idField);
doc.add(contentField);
doc.add(fileNameField);

writer.addDocument(doc);

analyzer.close();
}

writer.commit();
writer.deleteUnusedFiles();
long endTime = System.currentTimeMillis();

writer.close();

首先,我会在文件上传后立即删除文档,然后对更新的文档建立索引。

最佳答案

问题是您的字段在编入索引时会被分析,但您尝试删除的术语不会被分析。

最好的解决方案是将您想要用作此目的标识符的字段设置为 StringField,这将导致它在不进行分析的情况下被索引。如:

Field idField = new StringField("document_id", idOne);
doc.add(idField);

或者,您可以使用 IndexWriter.deleteDocuments(Query...) ,并传入一个经过分析的查询(由 QueryParser 生成),但在这种情况下,您应该小心,不要删除比预期更多的文档(查询找到的任何文档都将被删除,而不是只是最好的结果)。

关于java - 如何删除或更新 apache Lucene 中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39292911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com