我正在致力于将 Lucene 与我们基于 Spring-MVC 的应用程序集成。目前我们已经可以使用它了,但是我们很少得到cannot obtain lock
错误。之后我必须手动删除锁定文件才能正常工作。
如何在 Java 中设置锁定索引的超时时间?我没有任何 Lucene 的 XML 配置。我通过POM.xml在maven中添加了项目库并实例化了所需的类。
Code :
public void saveIndexes(String text, String tagFileName, String filePath, long groupId, boolean type, int objectId) {
try {
// path is the indexing directory.
File testDir;
Path suggestionsPath;
Directory suggestionsDir;
Path phraseSuggestPath;
Directory phraseSuggestDir;
Directory directory = org.apache.lucene.store.FSDirectory.open(path);
IndexWriterConfig config = new IndexWriterConfig(new SimpleAnalyzer());
IndexWriter indexWriter = new IndexWriter(directory, config);
org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
if (filePath != null) {
File file = new File(filePath); // current directory
doc.add(new TextField("path", file.getPath(), Field.Store.YES));
}
doc.add(new StringField("id", String.valueOf(objectId), Field.Store.YES));
// doc.add(new TextField("id",String.valueOf(objectId),Field.Store.YES));
if (text == null) {
if (filePath != null) {
FileInputStream is = new FileInputStream(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder stringBuffer = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuffer.append(line).append("\n");
}
stringBuffer.append("\n").append(tagFileName);
reader.close();
doc.add(new TextField("contents", stringBuffer.toString(), Field.Store.YES));
}
} else {
FieldType fieldType = new FieldType(TextField.TYPE_STORED);
fieldType.setTokenized(false);
doc.add(new Field("contents", text+"\n"+tagFileName, fieldType));
}
indexWriter.addDocument(doc);
indexWriter.commit();
indexWriter.flush();
indexWriter.close();
directory.close();
StandardAnalyzer analyzer = new StandardAnalyzer();
AnalyzingInfixSuggester wordSuggester = new AnalyzingInfixSuggester(suggestionsDir, analyzer);
ArrayList<String> words = new ArrayList<>();
if (text != null) {
text = html2text(text);
Pattern pt = Pattern.compile("[^\\w\\s]");
Matcher match = pt.matcher(text);
while (match.find()) {
String s = match.group();
text = text.replaceAll("\\" + s, "");
}
if (text.contains(" ")) {
Collections.addAll(words, text.split(" "));
} else {
words.add(text);
}
SuggestionIterator suggestionIterator = new SuggestionIterator(words.iterator());
wordSuggester.build(suggestionIterator);
wordSuggester.close();
suggestionsDir.close();
}
AnalyzingInfixSuggester phraseSuggester = new AnalyzingInfixSuggester(phraseSuggestDir, analyzer);
if (text != null) {
text = html2text(text);
ArrayList<String> phrases = new ArrayList<>();
phrases.add(text);
SuggestionIterator suggestionIterator = new SuggestionIterator(phrases.iterator());
phraseSuggester.build(suggestionIterator);
phraseSuggester.close();
phraseSuggestDir.close();
}
} catch (Exception ignored) {
}
}
谢谢。