Remove unsafe index options api (#1787)

The IndexOptions api has several problems:
- It is possible to open the same index with different options, which could lead to scoring problems
- If the index is already opened from another task, the options are ignored and the one from the opening task are used
- The analyzer which is derived from the options is used for every field which has not configured a specific analyzer
- This change removes the options api completely.

Co-authored-by: Konstantin Schaper <konstantin.schaper@cloudogu.com>
This commit is contained in:
Sebastian Sdorra
2021-08-31 14:03:16 +02:00
committed by GitHub
parent 61c2ebe80e
commit 765a39e4ce
15 changed files with 23 additions and 229 deletions

View File

@@ -43,46 +43,6 @@ class AnalyzerFactoryTest {
private final AnalyzerFactory analyzerFactory = new AnalyzerFactory();
@Nested
class FromIndexOptionsTests {
@Test
void shouldReturnStandardAnalyzer() {
Analyzer analyzer = analyzerFactory.create(IndexOptions.defaults());
assertThat(analyzer).isInstanceOf(StandardAnalyzer.class);
}
@Test
void shouldReturnStandardAnalyzerForUnknownLocale() {
Analyzer analyzer = analyzerFactory.create(IndexOptions.naturalLanguage(Locale.CHINESE));
assertThat(analyzer).isInstanceOf(StandardAnalyzer.class);
}
@Test
void shouldReturnEnglishAnalyzer() {
Analyzer analyzer = analyzerFactory.create(IndexOptions.naturalLanguage(Locale.ENGLISH));
assertThat(analyzer).isInstanceOf(EnglishAnalyzer.class);
}
@Test
void shouldReturnGermanAnalyzer() {
Analyzer analyzer = analyzerFactory.create(IndexOptions.naturalLanguage(Locale.GERMAN));
assertThat(analyzer).isInstanceOf(GermanAnalyzer.class);
}
@Test
void shouldReturnGermanAnalyzerForLocaleGermany() {
Analyzer analyzer = analyzerFactory.create(IndexOptions.naturalLanguage(Locale.GERMANY));
assertThat(analyzer).isInstanceOf(GermanAnalyzer.class);
}
@Test
void shouldReturnSpanishAnalyzer() {
Analyzer analyzer = analyzerFactory.create(IndexOptions.naturalLanguage(new Locale("es", "ES")));
assertThat(analyzer).isInstanceOf(SpanishAnalyzer.class);
}
}
@Nested
class FromSearchableTypeTests {
@@ -98,8 +58,7 @@ class AnalyzerFactoryTest {
private void analyze(Class<?> type, String text, String field, String... expectedTokens) throws IOException {
LuceneSearchableType searchableType = SearchableTypes.create(type);
IndexOptions defaults = IndexOptions.defaults();
Analyzer analyzer = analyzerFactory.create(searchableType, defaults);
Analyzer analyzer = analyzerFactory.create(searchableType);
List<String> tokens = Analyzers.tokenize(analyzer, field, text);
assertThat(tokens).containsOnly(expectedTokens);
}

View File

@@ -73,7 +73,7 @@ class IndexManagerTest {
void createIndexWriterFactory(@TempDir Path tempDirectory) {
this.directory = tempDirectory;
when(context.resolve(Paths.get("index"))).thenReturn(tempDirectory.resolve("index"));
when(analyzerFactory.create(any(LuceneSearchableType.class), any(IndexOptions.class))).thenReturn(new SimpleAnalyzer());
when(analyzerFactory.create(any(LuceneSearchableType.class))).thenReturn(new SimpleAnalyzer());
when(pluginLoader.getUberClassLoader()).thenReturn(IndexManagerTest.class.getClassLoader());
indexManager = new IndexManager(context, pluginLoader, analyzerFactory);
}
@@ -139,10 +139,10 @@ class IndexManagerTest {
}
@SuppressWarnings({"rawtypes", "unchecked"})
private IndexWriter open(Class type, String indexName) throws IOException {
private IndexWriter open(Class type, String indexName) {
lenient().when(searchableType.getType()).thenReturn(type);
when(searchableType.getName()).thenReturn(type.getSimpleName().toLowerCase(Locale.ENGLISH));
return indexManager.openForWrite(new IndexParams(indexName, searchableType, IndexOptions.defaults()));
return indexManager.openForWrite(new IndexParams(indexName, searchableType));
}
@Test

View File

@@ -82,6 +82,6 @@ class LuceneIndexFactoryTest {
}
private IndexParams params(String indexName, Class<?> type) {
return new IndexParams(indexName, SearchableTypes.create(type), IndexOptions.defaults());
return new IndexParams(indexName, SearchableTypes.create(type));
}
}

View File

@@ -263,7 +263,7 @@ class LuceneIndexTest {
private <T> LuceneIndex<T> createIndex(Class<T> type, Supplier<IndexWriter> writerFactor) {
SearchableTypeResolver resolver = new SearchableTypeResolver(type);
return new LuceneIndex<>(
new IndexParams("default", resolver.resolve(type), IndexOptions.defaults()), writerFactor
new IndexParams("default", resolver.resolve(type)), writerFactor
);
}

View File

@@ -59,7 +59,7 @@ class LuceneInjectingIndexTaskTest {
Injector injector = createInjector();
LuceneSearchableType searchableType = resolver.resolve(User.class);
IndexParams params = new IndexParams("default", searchableType, IndexOptions.defaults());
IndexParams params = new IndexParams("default", searchableType);
LuceneInjectingIndexTask task = new LuceneInjectingIndexTask(params, InjectingTask.class);
injector.injectMembers(task);

View File

@@ -128,7 +128,7 @@ class LuceneSearchEngineTest {
LuceneQueryBuilder<Repository> mockedBuilder = mock(LuceneQueryBuilder.class);
when(resolver.resolve(Repository.class)).thenReturn(searchableType);
IndexParams params = new IndexParams("default", searchableType, IndexOptions.defaults());
IndexParams params = new IndexParams("default", searchableType);
when(queryBuilderFactory.<Repository>create(params)).thenReturn(mockedBuilder);
QueryBuilder<Repository> queryBuilder = searchEngine.forType(Repository.class).search();
@@ -139,15 +139,13 @@ class LuceneSearchEngineTest {
@Test
@SuppressWarnings("unchecked")
void shouldDelegateSearch() {
IndexOptions options = IndexOptions.naturalLanguage(Locale.GERMAN);
LuceneQueryBuilder<Repository> mockedBuilder = mock(LuceneQueryBuilder.class);
when(resolver.resolve(Repository.class)).thenReturn(searchableType);
IndexParams params = new IndexParams("idx", searchableType, options);
IndexParams params = new IndexParams("idx", searchableType);
when(queryBuilderFactory.<Repository>create(params)).thenReturn(mockedBuilder);
QueryBuilder<Repository> queryBuilder = searchEngine.forType(Repository.class).withIndex("idx").withOptions(options).search();
QueryBuilder<Repository> queryBuilder = searchEngine.forType(Repository.class).withIndex("idx").search();
assertThat(queryBuilder).isSameAs(mockedBuilder);
}

View File

@@ -56,7 +56,7 @@ class LuceneSimpleIndexTaskTest {
LuceneSearchableType searchableType = resolver.resolve(Repository.class);
IndexParams params = new IndexParams("default", searchableType, IndexOptions.defaults());
IndexParams params = new IndexParams("default", searchableType);
AtomicReference<Index<?>> ref = new AtomicReference<>();
LuceneSimpleIndexTask task = new LuceneSimpleIndexTask(params, ref::set);