use of edu.harvard.iq.dataverse.search.FacetLabel in project dataverse by IQSS.
the class Search method search.
@GET
public Response search(@QueryParam("q") String query, @QueryParam("type") final List<String> types, @QueryParam("subtree") String subtreeRequested, @QueryParam("sort") String sortField, @QueryParam("order") String sortOrder, @QueryParam("per_page") final int numResultsPerPageRequested, @QueryParam("start") final int paginationStart, @QueryParam("show_relevance") boolean showRelevance, @QueryParam("show_facets") boolean showFacets, @QueryParam("fq") final List<String> filterQueries, @QueryParam("show_entity_ids") boolean showEntityIds, @QueryParam("show_api_urls") boolean showApiUrls, @QueryParam("show_my_data") boolean showMyData, @Context HttpServletResponse response) {
User user;
try {
user = getUser();
} catch (WrappedResponse ex) {
return ex.getResponse();
}
if (query != null) {
// sanity checking on user-supplied arguments
SortBy sortBy;
int numResultsPerPage;
Dataverse subtree;
try {
if (!types.isEmpty()) {
filterQueries.add(getFilterQueryFromTypes(types));
}
sortBy = SearchUtil.getSortBy(sortField, sortOrder);
numResultsPerPage = getNumberOfResultsPerPage(numResultsPerPageRequested);
subtree = getSubtree(subtreeRequested);
if (!subtree.equals(dataverseService.findRootDataverse())) {
String dataversePath = dataverseService.determineDataversePath(subtree);
String filterDownToSubtree = SearchFields.SUBTREE + ":\"" + dataversePath + "\"";
/**
* @todo Should filterDownToSubtree logic be centralized in
* SearchServiceBean?
*/
filterQueries.add(filterDownToSubtree);
}
} catch (Exception ex) {
return error(Response.Status.BAD_REQUEST, ex.getLocalizedMessage());
}
// users can't change these (yet anyway)
// getDataRelatedToMe();
boolean dataRelatedToMe = showMyData;
SolrQueryResponse solrQueryResponse;
try {
solrQueryResponse = searchService.search(createDataverseRequest(user), subtree, query, filterQueries, sortBy.getField(), sortBy.getOrder(), paginationStart, dataRelatedToMe, numResultsPerPage);
} catch (SearchException ex) {
Throwable cause = ex;
StringBuilder sb = new StringBuilder();
sb.append(cause + " ");
while (cause.getCause() != null) {
cause = cause.getCause();
sb.append(cause.getClass().getCanonicalName() + " ");
sb.append(cause + " ");
// if you search for a colon you see RemoteSolrException: org.apache.solr.search.SyntaxError: Cannot parse ':'
}
String message = "Exception running search for [" + query + "] with filterQueries " + filterQueries + " and paginationStart [" + paginationStart + "]: " + sb.toString();
logger.info(message);
return error(Response.Status.INTERNAL_SERVER_ERROR, message);
}
JsonArrayBuilder itemsArrayBuilder = Json.createArrayBuilder();
List<SolrSearchResult> solrSearchResults = solrQueryResponse.getSolrSearchResults();
for (SolrSearchResult solrSearchResult : solrSearchResults) {
itemsArrayBuilder.add(solrSearchResult.toJsonObject(showRelevance, showEntityIds, showApiUrls));
}
JsonObjectBuilder spelling_alternatives = Json.createObjectBuilder();
for (Map.Entry<String, List<String>> entry : solrQueryResponse.getSpellingSuggestionsByToken().entrySet()) {
spelling_alternatives.add(entry.getKey(), entry.getValue().toString());
}
JsonArrayBuilder facets = Json.createArrayBuilder();
JsonObjectBuilder facetCategoryBuilder = Json.createObjectBuilder();
for (FacetCategory facetCategory : solrQueryResponse.getFacetCategoryList()) {
JsonObjectBuilder facetCategoryBuilderFriendlyPlusData = Json.createObjectBuilder();
JsonArrayBuilder facetLabelBuilderData = Json.createArrayBuilder();
for (FacetLabel facetLabel : facetCategory.getFacetLabel()) {
JsonObjectBuilder countBuilder = Json.createObjectBuilder();
countBuilder.add(facetLabel.getName(), facetLabel.getCount());
facetLabelBuilderData.add(countBuilder);
}
facetCategoryBuilderFriendlyPlusData.add("friendly", facetCategory.getFriendlyName());
facetCategoryBuilderFriendlyPlusData.add("labels", facetLabelBuilderData);
facetCategoryBuilder.add(facetCategory.getName(), facetCategoryBuilderFriendlyPlusData);
}
facets.add(facetCategoryBuilder);
JsonObjectBuilder value = Json.createObjectBuilder().add("q", query).add("total_count", solrQueryResponse.getNumResultsFound()).add("start", solrQueryResponse.getResultsStart()).add("spelling_alternatives", spelling_alternatives).add("items", itemsArrayBuilder.build());
if (showFacets) {
value.add("facets", facets);
}
value.add("count_in_response", solrSearchResults.size());
// value.add("fq_provided", filterQueries.toString());
if (solrQueryResponse.getError() != null) {
/**
* @todo You get here if you pass only ":" as a query, for
* example. Should we return more or better information?
*/
return error(Response.Status.BAD_REQUEST, solrQueryResponse.getError());
}
response.setHeader("Access-Control-Allow-Origin", "*");
return allowCors(ok(value));
} else {
return allowCors(error(Response.Status.BAD_REQUEST, "q parameter is missing"));
}
}
Aggregations