Skip to content

Commit

Permalink
Merge pull request #559 from EBISPOT/multisearch
Browse files Browse the repository at this point in the history
Multisearch
  • Loading branch information
henrietteharmse authored Nov 2, 2023
2 parents b854a7a + 5170874 commit e614a96
Show file tree
Hide file tree
Showing 20 changed files with 255 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -29,7 +30,9 @@

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
Expand All @@ -48,12 +51,12 @@ public HttpEntity<V2PagedAndFacetedResponse<V2Entity>> getClasses(
@RequestParam(value = "boostFields", required = false) String boostFields,
@RequestParam(value = "exactMatch", required = false, defaultValue = "false") boolean exactMatch,
@RequestParam(value = "includeObsoleteEntities", required = false, defaultValue = "false") boolean includeObsoleteEntities,
@RequestParam Map<String,String> searchProperties
@RequestParam MultiValueMap<String,String> searchProperties
) throws ResourceNotFoundException, IOException {

Map<String,String> properties = new HashMap<>();
Map<String, Collection<String>> properties = new HashMap<>();
if(!includeObsoleteEntities)
properties.put("isObsolete", "false");
properties.put("isObsolete", List.of("false"));
properties.putAll(searchProperties);

return new ResponseEntity<>(
Expand All @@ -74,12 +77,12 @@ public HttpEntity<V2PagedAndFacetedResponse<V2Entity>> getClasses(
@RequestParam(value = "boostFields", required = false) String boostFields,
@RequestParam(value = "exactMatch", required = false, defaultValue = "false") boolean exactMatch,
@RequestParam(value = "includeObsoleteEntities", required = false, defaultValue = "false") boolean includeObsoleteEntities,
@RequestParam Map<String,String> searchProperties
@RequestParam MultiValueMap<String,String> searchProperties
) throws ResourceNotFoundException, IOException {

Map<String,String> properties = new HashMap<>();
Map<String,Collection<String>> properties = new HashMap<>();
if(!includeObsoleteEntities)
properties.put("isObsolete", "false");
properties.put("isObsolete", List.of("false"));
properties.putAll(searchProperties);

return new ResponseEntity<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -22,7 +23,9 @@

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
Expand All @@ -42,12 +45,12 @@ public HttpEntity<V2PagedAndFacetedResponse<V2Entity>> getEntities(
@RequestParam(value = "facetFields", required = false) String facetFields,
@RequestParam(value = "exactMatch", required = false, defaultValue = "false") boolean exactMatch,
@RequestParam(value = "includeObsoleteEntities", required = false, defaultValue = "false") boolean includeObsoleteEntities,
@RequestParam Map<String,String> searchProperties
@RequestParam MultiValueMap<String,String> searchProperties
) throws ResourceNotFoundException, IOException {

Map<String,String> properties = new HashMap<>();
Map<String,Collection<String>> properties = new HashMap<>();
if(!includeObsoleteEntities)
properties.put("isObsolete", "false");
properties.put("isObsolete", List.of("false"));
properties.putAll(searchProperties);

return new ResponseEntity<>(
Expand All @@ -68,12 +71,12 @@ public HttpEntity<V2PagedAndFacetedResponse<V2Entity>> getTerms(
@RequestParam(value = "facetFields", required = false) String facetFields,
@RequestParam(value = "exactMatch", required = false, defaultValue = "false") boolean exactMatch,
@RequestParam(value = "includeObsoleteEntities", required = false, defaultValue = "false") boolean includeObsoleteEntities,
@RequestParam Map<String,String> searchProperties
@RequestParam MultiValueMap<String,String> searchProperties
) throws ResourceNotFoundException, IOException {

Map<String,String> properties = new HashMap<>();
Map<String,Collection<String>> properties = new HashMap<>();
if(!includeObsoleteEntities)
properties.put("isObsolete", "false");
properties.put("isObsolete", List.of("false"));
properties.putAll(searchProperties);

return new ResponseEntity<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -23,7 +24,9 @@

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
Expand All @@ -42,12 +45,12 @@ public HttpEntity<V2PagedAndFacetedResponse<V2Entity>> getIndividuals(
@RequestParam(value = "boostFields", required = false) String boostFields,
@RequestParam(value = "exactMatch", required = false, defaultValue = "false") boolean exactMatch,
@RequestParam(value = "includeObsoleteEntities", required = false, defaultValue = "false") boolean includeObsoleteEntities,
@RequestParam Map<String,String> searchProperties
@RequestParam MultiValueMap<String,String> searchProperties
) throws ResourceNotFoundException, IOException {

Map<String,String> properties = new HashMap<>();
Map<String, Collection<String>> properties = new HashMap<>();
if(!includeObsoleteEntities)
properties.put("isObsolete", "false");
properties.put("isObsolete", List.of("false"));
properties.putAll(searchProperties);

return new ResponseEntity<>(
Expand All @@ -67,12 +70,12 @@ public HttpEntity<V2PagedAndFacetedResponse<V2Entity>> getIndividuals(
@RequestParam(value = "boostFields", required = false) String boostFields,
@RequestParam(value = "exactMatch", required = false, defaultValue = "false") boolean exactMatch,
@RequestParam(value = "includeObsoleteEntities", required = false, defaultValue = "false") boolean includeObsoleteEntities,
@RequestParam Map<String,String> searchProperties
@RequestParam MultiValueMap<String,String> searchProperties
) throws ResourceNotFoundException, IOException {

Map<String,String> properties = new HashMap<>();
Map<String, Collection<String>> properties = new HashMap<>();
if(!includeObsoleteEntities)
properties.put("isObsolete", "false");
properties.put("isObsolete", List.of("false"));
properties.putAll(searchProperties);

return new ResponseEntity<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import uk.ac.ebi.spot.ols.repository.v2.V2OntologyRepository;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
Expand All @@ -47,12 +49,12 @@ public HttpEntity<V2PagedAndFacetedResponse<V2Entity>> getOntologies(
@RequestParam(value = "boostFields", required = false) String boostFields,
@RequestParam(value = "exactMatch", required = false, defaultValue = "false") boolean exactMatch,
@RequestParam(value = "includeObsoleteEntities", required = false, defaultValue = "false") boolean includeObsoleteEntities,
@RequestParam Map<String,String> searchProperties
@RequestParam Map<String, Collection<String>> searchProperties
) throws ResourceNotFoundException, IOException {

Map<String,String> properties = new HashMap<>();
Map<String,Collection<String>> properties = new HashMap<>();
if(!includeObsoleteEntities)
properties.put("isObsolete", "false");
properties.put("isObsolete", List.of("false"));
properties.putAll(searchProperties);

return new ResponseEntity<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriUtils;
import uk.ac.ebi.spot.ols.controller.api.v2.helpers.DynamicQueryHelper;
Expand All @@ -19,7 +20,9 @@

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
Expand All @@ -38,12 +41,12 @@ public HttpEntity<V2PagedAndFacetedResponse<V2Entity>> getProperties(
@RequestParam(value = "boostFields", required = false) String boostFields,
@RequestParam(value = "exactMatch", required = false, defaultValue = "false") boolean exactMatch,
@RequestParam(value = "includeObsoleteEntities", required = false, defaultValue = "false") boolean includeObsoleteEntities,
@RequestParam Map<String,String> searchProperties
@RequestParam Map<String, Collection<String>> searchProperties
) throws ResourceNotFoundException, IOException {

Map<String,String> properties = new HashMap<>();
Map<String,Collection<String>> properties = new HashMap<>();
if(!includeObsoleteEntities)
properties.put("isObsolete", "false");
properties.put("isObsolete", List.of("false"));
properties.putAll(searchProperties);

return new ResponseEntity<>(
Expand All @@ -63,12 +66,12 @@ public HttpEntity<V2PagedAndFacetedResponse<V2Entity>> getProperties(
@RequestParam(value = "boostFields", required = false) String boostFields,
@RequestParam(value = "exactMatch", required = false, defaultValue = "false") boolean exactMatch,
@RequestParam(value = "includeObsoleteEntities", required = false, defaultValue = "false") boolean includeObsoleteEntities,
@RequestParam Map<String,String> searchProperties
@RequestParam MultiValueMap<String,String> searchProperties
) throws ResourceNotFoundException, IOException {

Map<String,String> properties = new HashMap<>();
Map<String,Collection<String>> properties = new HashMap<>();
if(!includeObsoleteEntities)
properties.put("isObsolete", "false");
properties.put("isObsolete", List.of("false"));
properties.putAll(searchProperties);

return new ResponseEntity<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Collection;

public class DynamicQueryHelper {

public static Map<String,String> filterProperties(Map<String,String> properties) {
public static Map<String,Collection<String>> filterProperties(Map<String,Collection<String>> properties) {

Map<String,String> newProps = new HashMap<>();
Map<String,Collection<String>> newProps = new HashMap<>();

for(String k : properties.keySet()) {

String value = properties.get(k);

k = UriUtils.decode(k, "UTF-8");

if(k.equals("lang") || k.equals("search") || k.equals("searchFields")
|| k.equals("boostFields") || k.equals("page") || k.equals("size") || k.equals("exactMatch")
|| k.equals("includeObsoleteEntities"))
continue;

newProps.put(k, value);
newProps.put(k, properties.get(k));
}

return newProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -115,6 +116,7 @@ public QueryResponse runSolrQuery(SolrQuery query, Pageable pageable) {
}

System.out.println("solr query: " + query.toQueryString());
System.out.println("solr query urldecoded: " + URLDecoder.decode(query.toQueryString()));
System.out.println("solr host: " + host);

org.apache.solr.client.solrj.SolrClient mySolrClient = new HttpSolrClient.Builder(host + "/solr/ols4_entities").build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.ac.ebi.spot.ols.repository.solr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
Expand Down Expand Up @@ -43,8 +44,8 @@ public void addFacetField(String propertyName) {
this.facetFields.add(propertyName);
}

public void addFilter(String propertyName, String propertyValue, SearchType searchType) {
this.filters.add(new Filter(propertyName, propertyValue, searchType));
public void addFilter(String propertyName, Collection<String> propertyValues, SearchType searchType) {
this.filters.add(new Filter(propertyName, propertyValues, searchType));
}

public SolrQuery constructQuery() {
Expand Down Expand Up @@ -98,13 +99,35 @@ public SolrQuery constructQuery() {
}

for(Filter f : filters) {
query.addFilterQuery(
ClientUtils.escapeQueryChars(getSolrPropertyName(f.propertyName, f.searchType))
+ ":\"" + ClientUtils.escapeQueryChars(getSolrPropertyValue(f.propertyValue, exactMatch ? SearchType.WHOLE_FIELD : f.searchType)) + "\"");

StringBuilder fq = new StringBuilder();

if(facetFields.contains(f.propertyName)) {
fq.append("{!tag=olsfacet}");
}

fq.append( ClientUtils.escapeQueryChars(getSolrPropertyName(f.propertyName, f.searchType)) );
fq.append(":(");

int n = 0;

for(String value : f.propertyValues) {
if(n ++ > 0) {
fq.append(" OR ");
}
fq.append("\"");
fq.append(ClientUtils.escapeQueryChars(getSolrPropertyValue(value, exactMatch ? SearchType.WHOLE_FIELD : f.searchType)));
fq.append("\"");
}
fq.append(")");

query.addFilterQuery(fq.toString());
}

if(facetFields.size() > 0) {
query.addFacetField(facetFields.toArray(new String[0]));
for(String facetField : facetFields) {
query.addFacetField("{!ex=olsfacet}" + facetField);
}
}

return query;
Expand All @@ -113,12 +136,12 @@ public SolrQuery constructQuery() {
private class Filter {

String propertyName;
String propertyValue;
Collection<String> propertyValues; // all values to search for ("OR")
SearchType searchType;

public Filter(String propertyName, String propertyValue, SearchType searchType) {
public Filter(String propertyName, Collection<String> propertyValues, SearchType searchType) {
this.propertyName = propertyName;
this.propertyValue = propertyValue;
this.propertyValues = propertyValues;
this.searchType = searchType;
}
}
Expand Down
Loading

0 comments on commit e614a96

Please sign in to comment.