Search in sources :

Example 1 with FacetLabel

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"));
    }
}
Also used : SolrSearchResult(edu.harvard.iq.dataverse.search.SolrSearchResult) SolrQueryResponse(edu.harvard.iq.dataverse.search.SolrQueryResponse) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) GuestUser(edu.harvard.iq.dataverse.authorization.users.GuestUser) SortBy(edu.harvard.iq.dataverse.search.SortBy) FacetLabel(edu.harvard.iq.dataverse.search.FacetLabel) SearchException(edu.harvard.iq.dataverse.search.SearchException) Dataverse(edu.harvard.iq.dataverse.Dataverse) SearchException(edu.harvard.iq.dataverse.search.SearchException) FacetCategory(edu.harvard.iq.dataverse.search.FacetCategory) ArrayList(java.util.ArrayList) List(java.util.List) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map) GET(javax.ws.rs.GET)

Aggregations

Dataverse (edu.harvard.iq.dataverse.Dataverse)1 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 GuestUser (edu.harvard.iq.dataverse.authorization.users.GuestUser)1 User (edu.harvard.iq.dataverse.authorization.users.User)1 FacetCategory (edu.harvard.iq.dataverse.search.FacetCategory)1 FacetLabel (edu.harvard.iq.dataverse.search.FacetLabel)1 SearchException (edu.harvard.iq.dataverse.search.SearchException)1 SolrQueryResponse (edu.harvard.iq.dataverse.search.SolrQueryResponse)1 SolrSearchResult (edu.harvard.iq.dataverse.search.SolrSearchResult)1 SortBy (edu.harvard.iq.dataverse.search.SortBy)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 GET (javax.ws.rs.GET)1