Commit 41981efd authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

444: Prevent fallback to all collections if sourceId specified, but not

found (OPENED)

Task-Url: #444
parent d93c14a9
Pipeline #26397 passed with stage
in 40 seconds
...@@ -26,7 +26,6 @@ import eu.dariah.de.search.query.Query; ...@@ -26,7 +26,6 @@ import eu.dariah.de.search.query.Query;
import eu.dariah.de.search.query.SimpleQuery; import eu.dariah.de.search.query.SimpleQuery;
import eu.dariah.de.search.query.SimpleQueryImpl; import eu.dariah.de.search.query.SimpleQueryImpl;
import eu.dariah.de.search.query.execution.AggregationService; import eu.dariah.de.search.query.execution.AggregationService;
import eu.dariah.de.search.query.execution.QueryExecutionServiceImpl;
import eu.dariah.de.search.query.results.QueryResult; import eu.dariah.de.search.query.results.QueryResult;
import eu.dariah.de.search.service.CustomSearchService; import eu.dariah.de.search.service.CustomSearchService;
import eu.dariah.de.search.service.DatamodelService; import eu.dariah.de.search.service.DatamodelService;
......
...@@ -56,14 +56,14 @@ public abstract class BaseSearchController extends BaseController { ...@@ -56,14 +56,14 @@ public abstract class BaseSearchController extends BaseController {
protected String getCollectionSelectionDialog(@RequestBody JsonNode body, List<Collection> availableCollections, Locale locale, Model model, HttpServletRequest request) { protected String getCollectionSelectionDialog(@RequestBody JsonNode body, List<Collection> availableCollections, Locale locale, Model model, HttpServletRequest request) {
ArrayNode sourceIdsNode = (ArrayNode) body.path("sourceIds"); ArrayNode sourceIdsNode = (ArrayNode) body.path("sourceIds");
List<String> sourceIds = new ArrayList<String>(); List<String> sourceIds = new ArrayList<>();
for (int i=0; i<sourceIdsNode.size(); i++) { for (int i=0; i<sourceIdsNode.size(); i++) {
sourceIds.add(sourceIdsNode.get(i).textValue()); sourceIds.add(sourceIdsNode.get(i).textValue());
} }
model.addAttribute("collections", collectionConverter.convert(availableCollections, locale)); model.addAttribute("collections", collectionConverter.convert(availableCollections, locale));
model.addAttribute("preselectedCollIds", sourceIds); model.addAttribute("preselectedCollIds", sourceIds);
model.addAttribute("preselectAll", sourceIds.size()==0); model.addAttribute("preselectAll", sourceIds.isEmpty());
return "search/sourceSelectionForm"; return "search/sourceSelectionForm";
} }
...@@ -75,16 +75,15 @@ public abstract class BaseSearchController extends BaseController { ...@@ -75,16 +75,15 @@ public abstract class BaseSearchController extends BaseController {
Map<String, String> collectionIdMap = collections.stream().collect(Collectors.toMap(Collection::getColregEntityId, Collection::getId)); Map<String, String> collectionIdMap = collections.stream().collect(Collectors.toMap(Collection::getColregEntityId, Collection::getId));
for (int i=0; i<sourceIds.size(); i++) { for (int i=0; i<sourceIds.size(); i++) {
if (!collectionIdMap.containsKey(sourceIds.get(i))) {
if (!notifications.contains("~eu.dariah.de.minfba.search.view.notifications.selected_collection_unknown")) {
notifications.add("~eu.dariah.de.minfba.search.view.notifications.selected_collection_unknown");
}
continue;
}
if (strSourceId.length()>1) { if (strSourceId.length()>1) {
strSourceId.append(","); strSourceId.append(",");
} }
strSourceId.append("\"").append(collectionIdMap.get(sourceIds.get(i))).append("\"");
if (collectionIdMap.containsKey(sourceIds.get(i))) {
strSourceId.append("\"").append(collectionIdMap.get(sourceIds.get(i))).append("\"");
} else {
strSourceId.append("\"").append("cid:" + sourceIds.get(i)).append("\"");
}
} }
} }
......
...@@ -7,6 +7,8 @@ import java.util.HashMap; ...@@ -7,6 +7,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.text.Text; import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
...@@ -42,6 +44,7 @@ import eu.dariah.de.search.query.results.QueryResult; ...@@ -42,6 +44,7 @@ import eu.dariah.de.search.query.results.QueryResult;
import eu.dariah.de.search.query.results.QueryResultDatasource; import eu.dariah.de.search.query.results.QueryResultDatasource;
import eu.dariah.de.search.query.results.ResultElement; import eu.dariah.de.search.query.results.ResultElement;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice.This;
import eu.dariah.de.search.query.results.QueryResult.SearchTypes; import eu.dariah.de.search.query.results.QueryResult.SearchTypes;
@Slf4j @Slf4j
...@@ -301,7 +304,7 @@ public class QueryExecutionServiceImpl extends BaseQueryService implements Query ...@@ -301,7 +304,7 @@ public class QueryExecutionServiceImpl extends BaseQueryService implements Query
if (responses==null) { if (responses==null) {
return; return;
} }
Map<String, Long> datasourceCountMap = new HashMap<String, Long>(); Map<String, Long> datasourceCountMap = new HashMap<>();
for (SearchResponse resp : responses) { for (SearchResponse resp : responses) {
if (resp==null || resp.getAggregations()==null) { if (resp==null || resp.getAggregations()==null) {
...@@ -328,9 +331,14 @@ public class QueryExecutionServiceImpl extends BaseQueryService implements Query ...@@ -328,9 +331,14 @@ public class QueryExecutionServiceImpl extends BaseQueryService implements Query
} }
} }
List<QueryResultDatasource> resultProviders = new ArrayList<QueryResultDatasource>(); List<String> unknownSourceIds = new ArrayList<>();
if (q.getSourceIds()!=null) {
unknownSourceIds.addAll(q.getSourceIds());
}
List<QueryResultDatasource> resultProviders = new ArrayList<>();
QueryResultDatasource qrd; QueryResultDatasource qrd;
for (String datasourceId : datasourceCountMap.keySet()) { for (String datasourceId : datasourceCountMap.keySet()) {
qrd = new QueryResultDatasource(); qrd = new QueryResultDatasource();
qrd.setHitCount(datasourceCountMap.get(datasourceId)); qrd.setHitCount(datasourceCountMap.get(datasourceId));
qrd.setLoadTags(qrd.getHitCount()>0); qrd.setLoadTags(qrd.getHitCount()>0);
...@@ -341,14 +349,37 @@ public class QueryExecutionServiceImpl extends BaseQueryService implements Query ...@@ -341,14 +349,37 @@ public class QueryExecutionServiceImpl extends BaseQueryService implements Query
qrd.setProviderName(collectionService.findById(datasourceId).getName("deu")); qrd.setProviderName(collectionService.findById(datasourceId).getName("deu"));
} }
resultProviders.add(qrd); resultProviders.add(qrd);
unknownSourceIds.remove(datasourceId);
}
// Resolve entries that were unknown before (still are in UI) and can be replaced with actual datasources now
Map<String, String> resolvedIdsMap = this.resolveUnknownExternalIds(unknownSourceIds);
for (Entry<String, String> resolvedIdsEntry : resolvedIdsMap.entrySet()) {
qrd = new QueryResultDatasource();
if (!resolvedIdsEntry.getKey().equals(resolvedIdsEntry.getValue())) {
qrd.setHitCount(datasourceCountMap.get(resolvedIdsEntry.getValue()));
qrd.setLoadTags(qrd.getHitCount()>0);
qrd.setProviderId(resolvedIdsEntry.getKey() + "~" + resolvedIdsEntry.getValue());
Collection ds = collectionService.findById(resolvedIdsEntry.getValue());
if (ds!=null) {
qrd.setProviderName(collectionService.findById(resolvedIdsEntry.getValue()).getName("deu"));
}
} else {
qrd.setHitCount(0);
qrd.setLoadTags(false);
qrd.setProviderName("unknown");
qrd.setProviderId(resolvedIdsEntry.getValue());
}
resultProviders.add(qrd);
} }
Collections.sort(resultProviders); Collections.sort(resultProviders);
qr.setResultDatasources(resultProviders); qr.setResultDatasources(resultProviders);
} }
private void addConfigInfo(Resource r, List<Element> elems) { private void addConfigInfo(Resource r, List<Element> elems) {
// This should never be the case! Why is it happening // This should never be the case! Why is it happening
if (elems==null) { if (elems==null) {
...@@ -375,3 +406,4 @@ public class QueryExecutionServiceImpl extends BaseQueryService implements Query ...@@ -375,3 +406,4 @@ public class QueryExecutionServiceImpl extends BaseQueryService implements Query
return false; return false;
} }
} }
...@@ -17,6 +17,7 @@ import eu.dariah.de.search.Constants; ...@@ -17,6 +17,7 @@ import eu.dariah.de.search.Constants;
import eu.dariah.de.search.Constants.QueryTypes; import eu.dariah.de.search.Constants.QueryTypes;
import eu.dariah.de.search.Constants.RootElementKeys; import eu.dariah.de.search.Constants.RootElementKeys;
import eu.dariah.de.search.es.service.SearchService; import eu.dariah.de.search.es.service.SearchService;
import eu.dariah.de.search.model.Collection;
import eu.dariah.de.search.model.ExtendedMappingContainer; import eu.dariah.de.search.model.ExtendedMappingContainer;
import eu.dariah.de.search.query.ExtendedQuery; import eu.dariah.de.search.query.ExtendedQuery;
import eu.dariah.de.search.query.Query; import eu.dariah.de.search.query.Query;
...@@ -208,6 +209,24 @@ public abstract class BaseQueryService extends BaseResultService { ...@@ -208,6 +209,24 @@ public abstract class BaseQueryService extends BaseResultService {
return this.wrapQueryBuilder(bqb, q); return this.wrapQueryBuilder(bqb, q);
} }
protected Map<String, String> resolveUnknownExternalIds(List<String> sourceIds) {
Map<String, String> resolved = new HashMap<>(sourceIds.size());
Collection resolvedC;
for (String sourceId : sourceIds) {
if (sourceId.contains(":")) {
resolvedC = collectionService.findByColregEntityId(sourceId.substring(sourceId.indexOf(":")+1));
if (resolvedC!=null) {
resolved.put(sourceId, resolvedC.getId());
} else {
resolved.put(sourceId, sourceId);
}
} else {
resolved.put(sourceId, sourceId);
}
}
return resolved;
}
private QueryBuilder wrapQueryBuilder(QueryBuilder bldr, Query q) { private QueryBuilder wrapQueryBuilder(QueryBuilder bldr, Query q) {
BoolQueryBuilder boolQb = null; BoolQueryBuilder boolQb = null;
...@@ -227,7 +246,7 @@ public abstract class BaseQueryService extends BaseResultService { ...@@ -227,7 +246,7 @@ public abstract class BaseQueryService extends BaseResultService {
} }
if (q.getSourceIds()!=null && !q.getSourceIds().isEmpty()) { if (q.getSourceIds()!=null && !q.getSourceIds().isEmpty()) {
boolQb.filter(QueryBuilders.termsQuery(Constants.ELEMENT_KEY_COLLECTION_ID, q.getSourceIds())); boolQb.filter(QueryBuilders.termsQuery(Constants.ELEMENT_KEY_COLLECTION_ID, this.resolveUnknownExternalIds(q.getSourceIds()).values()));
} }
} }
......
Subproject commit 864d677d7792539ec5f5331a3903497e3774999d Subproject commit cab8358d7ef4525e5d79ce56a58b2ffc4e3aac8e
Subproject commit 32e1d5ded7a30b738500748f2e07baeffb3019d3 Subproject commit a761a6234188333500aecf32bb0dabe946e2705c
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment