Skip to content

Commit

Permalink
Fix lucence segment error by checking for existing index (#8401)
Browse files Browse the repository at this point in the history
* Fix lucence segment error by checking for exisitng index

fix some potential resource leaks

* Remove empty lines and improve Logger call

Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
  • Loading branch information
Siedlerchr and koppor authored Jan 9, 2022
1 parent 070aac7 commit 7d4916e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Fixed

- We fixed an issue where the fulltext search on an empty library with no documents would lead to an exception [koppor#522](https://github.com/koppor/jabref/issues/522)
- We fixed an issue where clicking on "Accept changes" in the merge dialog would lead to an exception [forum#2418](https://discourse.jabref.org/t/the-library-has-been-modified-by-another-program/2418/8)
- We fixed an issue where clicking on headings in the entry preview could lead to an exception. [#8292](https://github.com/JabRef/jabref/issues/8292)
- We fixed an issue where IntegrityCheck used the system's character encoding instead of the one set by the library or in preferences [#8022](https://github.com/JabRef/jabref/issues/8022)
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/org/jabref/logic/pdf/search/indexing/PdfIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ public static PdfIndexer of(BibDatabaseContext databaseContext, FilePreferences
*/
public void createIndex() {
// Create new index by creating IndexWriter but not writing anything.
try {
IndexWriter indexWriter = new IndexWriter(directoryToIndex, new IndexWriterConfig(new EnglishStemAnalyzer()).setOpenMode(IndexWriterConfig.OpenMode.CREATE));
indexWriter.close();
try (IndexWriter indexWriter = new IndexWriter(directoryToIndex, new IndexWriterConfig(new EnglishStemAnalyzer()).setOpenMode(IndexWriterConfig.OpenMode.CREATE))) {
// empty comment for checkstyle
} catch (IOException e) {
LOGGER.warn("Could not create new Index!", e);
}
Expand Down Expand Up @@ -119,8 +118,7 @@ public void removeFromIndex(BibEntry entry, LinkedFile linkedFile) {
try (IndexWriter indexWriter = new IndexWriter(
directoryToIndex,
new IndexWriterConfig(
new EnglishStemAnalyzer()).setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND))
) {
new EnglishStemAnalyzer()).setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND))) {
if (!entry.getFiles().isEmpty()) {
indexWriter.deleteDocuments(new Term(SearchFieldConstants.PATH, linkedFile.getLink()));
}
Expand Down Expand Up @@ -189,8 +187,7 @@ private void writeToIndex(BibEntry entry, LinkedFile linkedFile) {
}
try {
// Check if a document with this path is already in the index
try {
IndexReader reader = DirectoryReader.open(directoryToIndex);
try (IndexReader reader = DirectoryReader.open(directoryToIndex)) {
IndexSearcher searcher = new IndexSearcher(reader);
TermQuery query = new TermQuery(new Term(SearchFieldConstants.PATH, linkedFile.getLink()));
TopDocs topDocs = searcher.search(query, 1);
Expand All @@ -205,19 +202,18 @@ private void writeToIndex(BibEntry entry, LinkedFile linkedFile) {
return;
}
}
reader.close();
} catch (IndexNotFoundException e) {
// if there is no index yet, don't need to check anything!
}
// If no document was found, add the new one
Optional<List<Document>> pages = new DocumentReader(entry, filePreferences).readLinkedPdf(this.databaseContext, linkedFile);
if (pages.isPresent()) {
IndexWriter indexWriter = new IndexWriter(directoryToIndex,
new IndexWriterConfig(
new EnglishStemAnalyzer()).setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND));
indexWriter.addDocuments(pages.get());
indexWriter.commit();
indexWriter.close();
try (IndexWriter indexWriter = new IndexWriter(directoryToIndex,
new IndexWriterConfig(
new EnglishStemAnalyzer()).setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND))) {
indexWriter.addDocuments(pages.get());
indexWriter.commit();
}
}
} catch (IOException e) {
LOGGER.warn("Could not add the document {} to the index!", linkedFile.getLink(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,22 @@ public static PdfSearcher of(BibDatabaseContext databaseContext) throws IOExcept
* @return a result set of all documents that have matches in any fields
*/
public PdfSearchResults search(final String searchString, final int maxHits)
throws IOException {
throws IOException {
if (StringUtil.isBlank(Objects.requireNonNull(searchString, "The search string was null!"))) {
return new PdfSearchResults();
}
if (maxHits <= 0) {
throw new IllegalArgumentException("Must be called with at least 1 maxHits, was" + maxHits);
}

try {
List<SearchResult> resultDocs = new LinkedList<>();
List<SearchResult> resultDocs = new LinkedList<>();

IndexReader reader = DirectoryReader.open(indexDirectory);
if (!DirectoryReader.indexExists(indexDirectory)) {
LOGGER.debug("Index directory {} does not yet exist", indexDirectory);
return new PdfSearchResults();
}

try (IndexReader reader = DirectoryReader.open(indexDirectory)) {
IndexSearcher searcher = new IndexSearcher(reader);
Query query = new MultiFieldQueryParser(PDF_FIELDS, new EnglishStemAnalyzer()).parse(searchString);
TopDocs results = searcher.search(query, maxHits);
Expand All @@ -69,7 +73,7 @@ public PdfSearchResults search(final String searchString, final int maxHits)
}
return new PdfSearchResults(resultDocs);
} catch (ParseException e) {
LOGGER.warn("Could not parse query: '" + searchString + "'! \n" + e.getMessage());
LOGGER.warn("Could not parse query: '{}'!\n{}", searchString, e.getMessage());
return new PdfSearchResults();
}
}
Expand Down

0 comments on commit 7d4916e

Please sign in to comment.