Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TIB Facet count fix #678

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading