mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-07-02 21:18:05 +02:00
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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user