Skip to content

Commit

Permalink
Merge pull request #675 from TIBHannover/17-facet-data-is-missing-fro…
Browse files Browse the repository at this point in the history
…m-the-search-call-response-v1

17 facet data is missing from the search call response v1
  • Loading branch information
henrietteharmse authored Jun 13, 2024
2 parents 7965403 + 68bf966 commit 3e5dfc0
Showing 1 changed file with 77 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package uk.ac.ebi.spot.ols.controller.api.v1;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.servlet.http.HttpServletResponse;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,6 +26,11 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import uk.ac.ebi.spot.ols.repository.Validation;
import uk.ac.ebi.spot.ols.repository.solr.OlsSolrClient;
import uk.ac.ebi.spot.ols.repository.transforms.LocalizationTransform;
Expand All @@ -22,14 +39,6 @@
import uk.ac.ebi.spot.ols.repository.v1.V1OntologyRepository;
import uk.ac.ebi.spot.ols.repository.v1.mappers.AnnotationExtractor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author Simon Jupp
* @date 02/07/2015
Expand Down Expand Up @@ -181,6 +190,18 @@ public void search(
// solrQuery.addHighlightField("https://github.com/EBISPOT/owl2neo#definition");

// solrQuery.addFacetField("ontology_name", "ontology_prefix", "type", "subset", "is_defining_ontology", "is_obsolete");

/*
* Fix: Start issue -
* https://github.com/EBISPOT/ols4/issues/613
* Added new OLS4 faceFields
*
*/
// TODO: Need to check and add additional faceted fields if required
solrQuery.addFacetField("ontologyId", "ontologyIri", "ontologyPreferredPrefix", "type", "isDefiningOntology", "isObsolete");
/*
* Fix: End
*/
solrQuery.add("wt", format);


Expand Down Expand Up @@ -264,16 +285,60 @@ public void search(
responseBody.put("numFound", qr.getResults().getNumFound());
responseBody.put("start", start);
responseBody.put("docs", docs);

/*
* Fix: Start issue -
* https://github.com/EBISPOT/ols4/issues/613
* Created facetFieldsMap: Start Gson not able to parse FacetField format -
* [ontologyId:[efo (17140)] Converting FacetFied to Map format
*/
Map<String, List<String>> facetFieldsMap = parseFacetFields(qr.getFacetFields());
Map<String, Object> facetCounts = new HashMap<>();
facetCounts.put("facet_fields", facetFieldsMap);
/*
* Fix: End
*/

Map<String, Object> responseObj = new HashMap<>();
responseObj.put("responseHeader", responseHeader);
responseObj.put("response", responseBody);

/*
* Fix: Start issue -
* https://github.com/EBISPOT/ols4/issues/613
* Added facet_counts to responseObj
*/
responseObj.put("facet_counts", facetCounts);
/*
* Fix: End
*/

response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
response.getOutputStream().write(gson.toJson(responseObj).getBytes(StandardCharsets.UTF_8));
response.flushBuffer();
}

private Map<String, List<String>> parseFacetFields(List<FacetField> facetFields) {
Map<String, List<String>> facetFieldsMap = new HashMap<>();
List<String> newFacetFields;
if (facetFields != null && facetFields.size() > 0) {
for (FacetField ff : facetFields) {
List<Count> facetFieldCount = ff.getValues();
if (facetFieldsMap.containsKey(ff.getName()))
newFacetFields = facetFieldsMap.get(ff.getName());
else
newFacetFields = new ArrayList<>();

for (Count ffCount : facetFieldCount) {
newFacetFields.add(ffCount.getName());
newFacetFields.add("" + ffCount.getCount());
}
facetFieldsMap.put(ff.getName(), newFacetFields);
}
}
return facetFieldsMap;
}

Function<String, String> addQuotes = new Function<String, String>() {
@Override
Expand Down

0 comments on commit 3e5dfc0

Please sign in to comment.