Commit c4d19193 authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

1323: Implement metasearch for SRU endpoints

Task-Url: https://pm.winseda.de/issues/1323
parent 10627869
Pipeline #12094 failed with stages
in 10 seconds
package eu.dariah.de.search.controller.search;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dariah.de.search.exceptions.QueryExecutionException;
import eu.dariah.de.search.query.ExtendedQueryImpl;
import eu.dariah.de.search.query.Query;
import eu.dariah.de.search.query.SimpleQueryImpl;
import eu.dariah.de.search.query.meta.SruQueryExecutionServiceImpl;
public abstract class BaseQueryExecutionController {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired protected ObjectMapper objMapper;
@Autowired protected SruQueryExecutionServiceImpl sruQueryExecutionService;
protected Query deserializeQuery(String jsonQuery) throws QueryExecutionException {
Query query = null;
try {
JsonNode node = objMapper.readValue(jsonQuery, JsonNode.class);
// For wrapped queries
if (node.has("query")) {
node = node.get("query");
}
if (node.has("queryString")) {
query = objMapper.treeToValue(node, SimpleQueryImpl.class);
} else {
query = objMapper.treeToValue(node, ExtendedQueryImpl.class);
}
return query;
} catch (IOException e) {
throw new QueryExecutionException("Failed to deserialize provided query", e);
}
}
}
package eu.dariah.de.search.controller.search;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import eu.dariah.de.search.exceptions.QueryExecutionException;
import eu.dariah.de.search.query.Query;
import eu.dariah.de.search.query.SimpleQuery;
import eu.dariah.de.search.query.SimpleQueryImpl;
import eu.dariah.de.search.query.results.QueryResult;
@Controller
@RequestMapping("/metaquery")
public class MetaqueryExecutionController extends BaseQueryExecutionController {
@GetMapping
public @ResponseBody List<QueryResult> queryAsGet(@RequestParam String q, HttpServletResponse response, Locale locale) {
SimpleQuery query = new SimpleQueryImpl();
query.setQueryString(q);
List<QueryResult> results = new ArrayList<>();
results.addAll(sruQueryExecutionService.executeQuery(query, locale));
return results;
}
@PostMapping
public @ResponseBody List<QueryResult> queryAsPost(@RequestBody String q, HttpServletResponse response, Locale locale) throws QueryExecutionException {
Query query = this.deserializeQuery(q);
List<QueryResult> results = new ArrayList<>();
results.addAll(sruQueryExecutionService.executeQuery(query, locale));
return results;
}
}
package eu.dariah.de.search.controller.search;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -19,37 +16,30 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import eu.dariah.de.search.exceptions.QueryExecutionException;
import eu.dariah.de.search.model.CustomSearch;
import eu.dariah.de.search.query.ExtendedQueryImpl;
import eu.dariah.de.search.query.Query;
import eu.dariah.de.search.query.SimpleQuery;
import eu.dariah.de.search.query.SimpleQueryImpl;
import eu.dariah.de.search.query.execution.AggregationService;
import eu.dariah.de.search.query.execution.QueryExecutionServiceImpl;
import eu.dariah.de.search.query.meta.SruQueryExecutionServiceImpl;
import eu.dariah.de.search.query.results.QueryResult;
import eu.dariah.de.search.service.CustomSearchService;
import eu.dariah.de.search.service.DatamodelService;
@Controller
@RequestMapping("/query")
public class QueryExecutionController {
protected static final Logger logger = LoggerFactory.getLogger(QueryExecutionController.class);
public class QueryExecutionController extends BaseQueryExecutionController {
@Autowired private QueryExecutionServiceImpl queryExecutionService;
@Autowired private SruQueryExecutionServiceImpl sruQueryExecutionService;
@Autowired private AggregationService aggregationService;
@Autowired private CustomSearchService customSearchService;
@Autowired protected DatamodelService datamodelService;
@Autowired protected ObjectMapper objMapper;
@GetMapping
public @ResponseBody List<QueryResult> queryAsGet(@RequestParam String q, HttpServletResponse response, Locale locale) {
SimpleQuery query = new SimpleQueryImpl();
......@@ -101,23 +91,4 @@ public class QueryExecutionController {
result.set("tags", objMapper.valueToTree(aggregationService.aggregateTags(query, query.getTagAggregators(), query.getSize())));
return result;
}
private Query deserializeQuery(String jsonQuery) throws QueryExecutionException {
Query query = null;
try {
JsonNode node = objMapper.readValue(jsonQuery, JsonNode.class);
// For wrapped queries
if (node.has("query")) {
node = node.get("query");
}
if (node.has("queryString")) {
query = objMapper.treeToValue(node, SimpleQueryImpl.class);
} else {
query = objMapper.treeToValue(node, ExtendedQueryImpl.class);
}
return query;
} catch (IOException e) {
throw new QueryExecutionException("Failed to deserialize provided query", e);
}
}
}
......@@ -44,15 +44,10 @@ import eu.dariah.de.search.transformation.ResourceEnrichmentService;
public class SruQueryExecutionServiceImpl extends BaseResultService implements InitializingBean {
protected final Logger logger = LoggerFactory.getLogger(SruQueryExecutionServiceImpl.class);
@Autowired private ApplicationContext appContext;
@Autowired private CollectionService collectionService;
@Autowired protected RestTemplate restTemplate;
@Autowired private DatamodelService datamodelService;
@Value("${datamodels.integration}")
......@@ -60,8 +55,7 @@ public class SruQueryExecutionServiceImpl extends BaseResultService implements I
@Value("${datamodels.presentation}")
private String presentationModelEntityId;
public List<QueryResult> prepareQuery(Query query, Locale locale) {
List<QueryResult> result = new ArrayList<>();
......@@ -70,8 +64,10 @@ public class SruQueryExecutionServiceImpl extends BaseResultService implements I
QueryResult prepResult;
for (QueryResultDatasource sruDatasource : sruCollectionEndpointMap.keySet()) {
prepResult = new QueryResult();
prepResult.setResultType(SearchTypes.META);
prepResult.setResultDatasources(new ArrayList<>());
prepResult.getResultDatasources().add(sruDatasource);
result.add(prepResult);
}
return result;
}
......@@ -151,13 +147,10 @@ public class SruQueryExecutionServiceImpl extends BaseResultService implements I
} catch (Exception e) {
logger.error("Error", e);
}
}
return result;
}
private Map<QueryResultDatasource, Endpoint> collectSruSourceEndpointMap(Query q, Locale locale) {
List<Collection> coll;
if (q.getSourceIds()!=null && !q.getSourceIds().isEmpty()) {
......@@ -165,7 +158,6 @@ public class SruQueryExecutionServiceImpl extends BaseResultService implements I
} else {
coll = collectionService.getAll();
}
Map<QueryResultDatasource, Endpoint> sruSourceEndpointMap = new HashMap<>();
QueryResultDatasource qrd;
for (Collection c : coll) {
......@@ -182,8 +174,4 @@ public class SruQueryExecutionServiceImpl extends BaseResultService implements I
}
return sruSourceEndpointMap;
}
}
Supports Markdown
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