Cause IndexInvalidDataError when search index is invalid

This commit is contained in:
Kitaiti Makoto 2020-07-18 09:40:47 +09:00
parent 010eac6c4a
commit c5d03d300b

View File

@ -5,10 +5,11 @@ use crate::{
use chrono::Datelike; use chrono::Datelike;
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
use itertools::Itertools; use itertools::Itertools;
use std::{cmp, fs::create_dir_all, path::Path, sync::Mutex}; use std::{cmp, fs::create_dir_all, io, path::Path, sync::Mutex};
use tantivy::{ use tantivy::{
collector::TopDocs, directory::MmapDirectory, schema::*, Index, IndexReader, IndexWriter, collector::TopDocs, directory::MmapDirectory, schema::*, Index, IndexReader, IndexWriter,
ReloadPolicy, Term, ReloadPolicy, Term,
TantivyError,
}; };
use whatlang::{detect as detect_lang, Lang}; use whatlang::{detect as detect_lang, Lang};
@ -18,6 +19,7 @@ pub enum SearcherError {
WriteLockAcquisitionError, WriteLockAcquisitionError,
IndexOpeningError, IndexOpeningError,
IndexEditionError, IndexEditionError,
InvalidIndexDataError,
} }
pub struct Searcher { pub struct Searcher {
@ -135,7 +137,19 @@ impl Searcher {
.reader_builder() .reader_builder()
.reload_policy(ReloadPolicy::Manual) .reload_policy(ReloadPolicy::Manual)
.try_into() .try_into()
.map_err(|_| SearcherError::IndexCreationError)?, .map_err(|e| {
if let TantivyError::IOError(err) = e {
let err: io::Error = err.into();
if err.kind() == io::ErrorKind::InvalidData {
// Search index was created in older Tantivy format.
SearcherError::InvalidIndexDataError
} else {
SearcherError::IndexCreationError
}
} else {
SearcherError::IndexCreationError
}
})?,
index, index,
}) })
} }