Skip to content

Commit

Permalink
feat: default no-op indexer and searcher added, no enforcement to dow…
Browse files Browse the repository at this point in the history
…nload a search plugin #4459
  • Loading branch information
ymarcon committed Sep 28, 2024
1 parent 66e3016 commit 7dc7e02
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ public class PluginsService implements EnvironmentAware, InitializingBean {

private static final String MICA_TAXONOMIES_PLUGIN_TYPE = "mica-taxonomies";



private static final String MICA_SEARCH_PLUGIN_NAME = "plugins.micaSearchPlugin";

private static final String DEFAULT_MICA_SEARCH_PLUGIN_NAME = "mica-search-es8";

private static final String DEFAULT_PLUGINS_UPDATE_SITE = "https://plugins.obiba.org";

private static final String[] ES_CONFIGURATION = new String[]{"elasticsearch.dataNode", "elasticsearch.clusterName", "elasticsearch.shards", "elasticsearch.replicas",
Expand Down Expand Up @@ -87,6 +83,10 @@ public void setEnvironment(Environment environment) {
this.environment = environment;
}

public boolean hasSearchEngineService() {
return !getServicePlugins(SearchEngineService.class).isEmpty();
}

public SearchEngineService getSearchEngineService() {
return (SearchEngineService) getServicePlugins(SearchEngineService.class).iterator().next();
}
Expand Down Expand Up @@ -305,7 +305,7 @@ public void afterPropertiesSet() throws Exception {
}

public String getSearchPluginName() {
return environment.getProperty(MICA_SEARCH_PLUGIN_NAME, DEFAULT_MICA_SEARCH_PLUGIN_NAME);
return environment.getProperty(MICA_SEARCH_PLUGIN_NAME);
}

/**
Expand All @@ -315,19 +315,22 @@ private void initPlugins() {
Collection<PluginResources> plugins = getPlugins(true);
String searchPluginName = getSearchPluginName();

try {
String pluginLatestVersion = getPluginRepositoryCache().getPluginLatestVersion(searchPluginName);
// ensure there is a mica-search plugin installed
if (plugins.stream().noneMatch(p -> MICA_SEARCH_PLUGIN_TYPE.equals(p.getType()))
|| plugins.stream()
.filter(plugin -> searchPluginName.equals(plugin.getName()))
.filter(plugin -> plugin.getVersion().compareTo(new Version(pluginLatestVersion)) >= 0).count() == 0) {
installPlugin(searchPluginName, null);
// rescan plugins
plugins = getPlugins(true);
// will use embedded default
if (!Strings.isNullOrEmpty(searchPluginName)) {
try {
String pluginLatestVersion = getPluginRepositoryCache().getPluginLatestVersion(searchPluginName);
// ensure there is a mica-search plugin installed
if (plugins.stream().noneMatch(p -> MICA_SEARCH_PLUGIN_TYPE.equals(p.getType()))
|| plugins.stream()
.filter(plugin -> searchPluginName.equals(plugin.getName()))
.filter(plugin -> plugin.getVersion().compareTo(new Version(pluginLatestVersion)) >= 0).count() == 0) {
installPlugin(searchPluginName, null);
// rescan plugins
plugins = getPlugins(true);
}
} catch (PluginRepositoryException e) {
log.error("Cannot initialize plugins properly", e);
}
} catch (PluginRepositoryException e) {
log.error("Cannot initialize plugins properly", e);
}

boolean micaSearchFound = false; // mica-search plugin is a singleton
Expand All @@ -336,7 +339,7 @@ private void initPlugins() {
|| MICA_TABLES_PLUGIN_TYPE.equals(plugin.getType())
|| MICA_TAXONOMIES_PLUGIN_TYPE.equals(plugin.getType()))
.sorted(Comparator.comparing(PluginResources::getVersion))
.collect(Collectors.toList());
.toList();

for (PluginResources plugin : filteredPlugins) {
if (MICA_SEARCH_PLUGIN_TYPE.equals(plugin.getType()) && !micaSearchFound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.obiba.mica.search;

import org.obiba.mica.micaConfig.service.PluginsService;
import org.obiba.mica.search.basic.DefaultSearcher;
import org.obiba.mica.spi.search.QueryScope;
import org.obiba.mica.spi.search.Searcher;
import org.obiba.mica.spi.search.support.JoinQuery;
Expand All @@ -31,8 +32,15 @@ public class SearchEngineClient implements Searcher {
@Inject
private PluginsService pluginsService;

private Searcher defaultSearcher;

private Searcher getSearcher() {
return pluginsService.getSearchEngineService().getSearcher();
if (pluginsService.hasSearchEngineService())
return pluginsService.getSearchEngineService().getSearcher();
if (defaultSearcher == null) {
defaultSearcher = new DefaultSearcher();
}
return defaultSearcher;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.obiba.mica.search;

import org.obiba.mica.micaConfig.service.PluginsService;
import org.obiba.mica.search.basic.DefaultIndexer;
import org.obiba.mica.spi.search.IndexFieldMapping;
import org.obiba.mica.spi.search.Indexable;
import org.obiba.mica.spi.search.Indexer;
Expand All @@ -27,8 +28,15 @@ public class SearchEngineIndexer implements Indexer {
@Inject
private PluginsService pluginsService;

private Indexer defaultIndexer;

private Indexer getIndexer() {
return pluginsService.getSearchEngineService().getIndexer();
if (pluginsService.hasSearchEngineService())
return pluginsService.getSearchEngineService().getIndexer();
if (defaultIndexer == null) {
defaultIndexer = new DefaultIndexer();
}
return defaultIndexer;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2024 OBiBa. All rights reserved.
*
* This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.obiba.mica.search.basic;

import org.jetbrains.annotations.Nullable;
import org.obiba.mica.spi.search.IndexFieldMapping;
import org.obiba.mica.spi.search.Indexable;
import org.obiba.mica.spi.search.Indexer;
import org.springframework.data.domain.Persistable;

import java.util.Map;

public class DefaultIndexer implements Indexer {
@Override
public void index(String indexName, Persistable<String> persistable) {

}

@Override
public void index(String indexName, Persistable<String> persistable, Persistable<String> parent) {

}

@Override
public void index(String indexName, Indexable indexable) {

}

@Override
public void index(String indexName, Indexable indexable, Indexable parent) {

}

@Override
public void reIndexAllIndexables(String indexName, Iterable<? extends Indexable> persistables) {

}

@Override
public void reindexAll(String indexName, Iterable<? extends Persistable<String>> persistables) {

}

@Override
public void indexAll(String indexName, Iterable<? extends Persistable<String>> persistables) {

}

@Override
public void indexAll(String indexName, Iterable<? extends Persistable<String>> persistables, Persistable<String> parent) {

}

@Override
public void indexAllIndexables(String indexName, Iterable<? extends Indexable> indexables) {

}

@Override
public void indexAllIndexables(String indexName, Iterable<? extends Indexable> indexables, @Nullable String parentId) {

}

@Override
public void delete(String indexName, Persistable<String> persistable) {

}

@Override
public void delete(String indexName, Indexable indexable) {

}

@Override
public void delete(String indexName, String[] types, Map.Entry<String, String> termQuery) {

}

@Override
public void delete(String indexName, String type, Map.Entry<String, String> termQuery) {

}

@Override
public boolean hasIndex(String indexName) {
return false;
}

@Override
public void dropIndex(String indexName) {

}

@Override
public IndexFieldMapping getIndexfieldMapping(String indexName, String type) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2024 OBiBa. All rights reserved.
*
* This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.obiba.mica.search.basic;

import org.jetbrains.annotations.Nullable;
import org.obiba.mica.spi.search.QueryScope;
import org.obiba.mica.spi.search.Searcher;
import org.obiba.mica.spi.search.support.JoinQuery;
import org.obiba.mica.spi.search.support.Query;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;


public class DefaultSearcher implements Searcher {

@Override
public JoinQuery makeJoinQuery(String rql) {
return null;
}

@Override
public Query makeQuery(String rql) {
return null;
}

@Override
public Query andQuery(Query... queries) {
return null;
}

@Override
public DocumentResults find(String indexName, String type, String rql, IdFilter idFilter) {
return new EmptyDocumentResults();
}

@Override
public DocumentResults count(String indexName, String type, String rql, IdFilter idFilter) {
return new EmptyDocumentResults();
}

@Override
public List<String> suggest(String indexName, String type, int limit, String locale, String queryString, String defaultFieldName) {
return List.of();
}

@Override
public InputStream getDocumentById(String indexName, String type, String id) {
return null;
}

@Override
public InputStream getDocumentByClassName(String indexName, String type, Class clazz, String id) {
return null;
}

@Override
public DocumentResults getDocumentsByClassName(String indexName, String type, Class clazz, int from, int limit, @Nullable String sort, @Nullable String order, @Nullable String queryString, @Nullable TermFilter termFilter, @Nullable IdFilter idFilter) {
return new EmptyDocumentResults();
}

@Override
public DocumentResults getDocuments(String indexName, String type, int from, int limit, @Nullable String sort, @Nullable String order, @Nullable String queryString, @Nullable TermFilter termFilter, @Nullable IdFilter idFilter, @Nullable List<String> fields, @Nullable List<String> excludedFields) {
return new EmptyDocumentResults();
}

@Override
public long countDocumentsWithField(String indexName, String type, String field) {
return 0;
}

@Override
public DocumentResults query(String indexName, String type, Query query, QueryScope scope, List<String> mandatorySourceFields, Properties aggregationProperties, @Nullable IdFilter idFilter) throws IOException {
return new EmptyDocumentResults();
}

@Override
public DocumentResults aggregate(String indexName, String type, Query query, Properties aggregationProperties, IdFilter idFilter) {
return new EmptyDocumentResults();
}

@Override
public DocumentResults cover(String indexName, String type, Query query, Properties aggregationProperties, @Nullable IdFilter idFilter) {
return new EmptyDocumentResults();
}

@Override
public DocumentResults cover(String indexName, String type, Query query, Properties aggregationProperties, Map<String, Properties> subAggregationProperties, @Nullable IdFilter idFilter) {
return new EmptyDocumentResults();
}

@Override
public Map<Object, Object> harmonizationStatusAggregation(String datasetId, int size, String aggregationFieldName, String statusFieldName) {
return Map.of();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 OBiBa. All rights reserved.
*
* This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.obiba.mica.search.basic;

import org.obiba.mica.spi.search.Searcher;

import java.util.List;
import java.util.Map;

public class EmptyDocumentResults implements Searcher.DocumentResults {
@Override
public long getTotal() {
return 0;
}

@Override
public List<Searcher.DocumentResult> getDocuments() {
return List.of();
}

@Override
public Map<String, Long> getAggregation(String field) {
return Map.of();
}

@Override
public List<Searcher.DocumentAggregation> getAggregations() {
return List.of();
}
}
2 changes: 1 addition & 1 deletion mica-webapp/src/main/conf/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ shiro:
#

plugins:
micaSearchPlugin: mica-search-es8
micaSearchPlugin:
updateSite: https://plugins.obiba.org

opalTaxonomies:
Expand Down

0 comments on commit 7dc7e02

Please sign in to comment.