From c5d03d300b689fa09b75a82dad1b835cca51bb35 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Sat, 18 Jul 2020 09:40:47 +0900 Subject: [PATCH] Cause IndexInvalidDataError when search index is invalid --- plume-models/src/search/searcher.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/plume-models/src/search/searcher.rs b/plume-models/src/search/searcher.rs index a1f4c813..2e509a0d 100644 --- a/plume-models/src/search/searcher.rs +++ b/plume-models/src/search/searcher.rs @@ -5,10 +5,11 @@ use crate::{ use chrono::Datelike; use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; 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::{ collector::TopDocs, directory::MmapDirectory, schema::*, Index, IndexReader, IndexWriter, ReloadPolicy, Term, + TantivyError, }; use whatlang::{detect as detect_lang, Lang}; @@ -18,6 +19,7 @@ pub enum SearcherError { WriteLockAcquisitionError, IndexOpeningError, IndexEditionError, + InvalidIndexDataError, } pub struct Searcher { @@ -135,7 +137,19 @@ impl Searcher { .reader_builder() .reload_policy(ReloadPolicy::Manual) .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, }) }