Search in sources :

Example 1 with SearchException

use of edu.harvard.iq.dataverse.search.SearchException in project dataverse by IQSS.

the class DataRetrieverAPI method retrieveMyDataAsJsonString.

/**
 * @todo This should support the "X-Dataverse-key" header like the other
 * APIs.
 */
@Path(retrieveDataPartialAPIPath)
@GET
@Produces({ "application/json" })
public String retrieveMyDataAsJsonString(@QueryParam("dvobject_types") List<String> dvobject_types, @QueryParam("published_states") List<String> published_states, @QueryParam("selected_page") Integer selectedPage, @QueryParam("mydata_search_term") String searchTerm, @QueryParam("role_ids") List<Long> roleIds, @QueryParam("userIdentifier") String userIdentifier, @QueryParam("key") String apiToken) {
    // String myDataParams) {
    // System.out.println("_YE_OLDE_QUERY_COUNTER_");
    // msgt("_YE_OLDE_QUERY_COUNTER_");  // for debug purposes
    boolean DEBUG_MODE = false;
    boolean OTHER_USER = false;
    // For, superusers, the searchUser may differ from the authUser
    // 
    AuthenticatedUser searchUser = null;
    if (DEBUG_MODE == true) {
        // DEBUG: use userIdentifier
        authUser = getUserFromIdentifier(userIdentifier);
        if (authUser == null) {
            return this.getJSONErrorString("Requires authentication", "retrieveMyDataAsJsonString. User not found!  Shouldn't be using this anyway");
        }
    } else if ((session.getUser() != null) && (session.getUser().isAuthenticated())) {
        authUser = (AuthenticatedUser) session.getUser();
        // and use that instead
        if ((authUser.isSuperuser()) && (userIdentifier != null) && (!userIdentifier.isEmpty())) {
            searchUser = getUserFromIdentifier(userIdentifier);
            if (searchUser != null) {
                authUser = searchUser;
                OTHER_USER = true;
            } else {
                return this.getJSONErrorString("No user found for: \"" + userIdentifier + "\"", null);
            }
        }
    } else if (apiToken != null) {
        // Is this being accessed by an API Token?
        authUser = findUserByApiToken(apiToken);
        if (authUser == null) {
            return this.getJSONErrorString("Requires authentication.  Please login.", "retrieveMyDataAsJsonString. User not found!  Shouldn't be using this anyway");
        } else {
            // and use that instead
            if ((authUser.isSuperuser()) && (userIdentifier != null) && (!userIdentifier.isEmpty())) {
                searchUser = getUserFromIdentifier(userIdentifier);
                if (searchUser != null) {
                    authUser = searchUser;
                    OTHER_USER = true;
                } else {
                    return this.getJSONErrorString("No user found for: \"" + userIdentifier + "\"", null);
                }
            }
        }
    } else {
        return this.getJSONErrorString("Requires authentication.  Please login.", "retrieveMyDataAsJsonString. User not found!  Shouldn't be using this anyway");
    }
    roleList = dataverseRoleService.findAll();
    rolePermissionHelper = new DataverseRolePermissionHelper(roleList);
    List<String> dtypes;
    if (dvobject_types != null) {
        dtypes = dvobject_types;
    } else {
        dtypes = MyDataFilterParams.defaultDvObjectTypes;
    }
    List<String> pub_states = null;
    if (published_states != null) {
        pub_states = published_states;
    }
    // ---------------------------------
    // (1) Initialize filterParams and check for Errors
    // ---------------------------------
    DataverseRequest dataverseRequest = createDataverseRequest(authUser);
    MyDataFilterParams filterParams = new MyDataFilterParams(dataverseRequest, dtypes, pub_states, roleIds, searchTerm);
    if (filterParams.hasError()) {
        return this.getJSONErrorString(filterParams.getErrorMessage(), filterParams.getErrorMessage());
    }
    // ---------------------------------
    // (2) Initialize MyDataFinder and check for Errors
    // ---------------------------------
    myDataFinder = new MyDataFinder(rolePermissionHelper, roleAssigneeService, dvObjectServiceBean, groupService);
    this.myDataFinder.runFindDataSteps(filterParams);
    if (myDataFinder.hasError()) {
        return this.getJSONErrorString(myDataFinder.getErrorMessage(), myDataFinder.getErrorMessage());
    }
    // ---------------------------------
    // (3) Make Solr Query
    // ---------------------------------
    int paginationStart = 1;
    if (selectedPage != null) {
        paginationStart = selectedPage;
    }
    int solrCardStart = (paginationStart - 1) * SearchConstants.NUM_SOLR_DOCS_TO_RETRIEVE;
    // 
    if (searchUser == null) {
        searchUser = authUser;
    }
    // msg("search with user: " + searchUser.getIdentifier());
    List<String> filterQueries = this.myDataFinder.getSolrFilterQueries();
    if (filterQueries == null) {
        logger.fine("No ids found for this search");
        return this.getJSONErrorString(DataRetrieverAPI.MSG_NO_RESULTS_FOUND, null);
    }
    try {
        solrQueryResponse = searchService.search(dataverseRequest, // subtree, default it to Dataverse for now
        null, // "*", //
        filterParams.getSearchTerm(), // filterQueries,
        filterQueries, // SearchFields.NAME_SORT, SortBy.ASCENDING,
        SearchFields.RELEASE_OR_CREATE_DATE, SortBy.DESCENDING, // paginationStart,
        solrCardStart, // dataRelatedToMe
        true, // 10 // SearchFields.NUM_SOLR_DOCS_TO_RETRIEVE
        SearchConstants.NUM_SOLR_DOCS_TO_RETRIEVE);
        // msgt("getSolrSearchResults: " + this.solrQueryResponse.getSolrSearchResults().toString());
        if (this.solrQueryResponse.getNumResultsFound() == 0) {
            return this.getJSONErrorString(DataRetrieverAPI.MSG_NO_RESULTS_FOUND, null);
        }
    } catch (SearchException ex) {
        solrQueryResponse = null;
        this.logger.severe("Solr SearchException: " + ex.getMessage());
    }
    if (solrQueryResponse == null) {
        return this.getJSONErrorString("Sorry!  There was an error with the search service.", "Sorry!  There was a SOLR Error");
    }
    // ---------------------------------
    // (4) Build JSON document including:
    // - Pager
    // - Formatted solr docs
    // - Num results found
    // - Search term
    // - DvObject counts
    // ---------------------------------
    // Initialize JSON response
    JsonObjectBuilder jsonData = Json.createObjectBuilder();
    Pager pager = new Pager(solrQueryResponse.getNumResultsFound().intValue(), SearchConstants.NUM_SOLR_DOCS_TO_RETRIEVE, paginationStart);
    RoleTagRetriever roleTagRetriever = new RoleTagRetriever(this.rolePermissionHelper, this.roleAssigneeSvc, this.dvObjectServiceBean);
    roleTagRetriever.loadRoles(dataverseRequest, solrQueryResponse);
    jsonData.add(DataRetrieverAPI.JSON_SUCCESS_FIELD_NAME, true).add(DataRetrieverAPI.JSON_DATA_FIELD_NAME, Json.createObjectBuilder().add("pagination", pager.asJsonObjectBuilderUsingCardTerms()).add(SearchConstants.SEARCH_API_ITEMS, this.formatSolrDocs(solrQueryResponse, roleTagRetriever)).add(SearchConstants.SEARCH_API_TOTAL_COUNT, solrQueryResponse.getNumResultsFound()).add(SearchConstants.SEARCH_API_START, solrQueryResponse.getResultsStart()).add("search_term", filterParams.getSearchTerm()).add("dvobject_counts", this.getDvObjectTypeCounts(solrQueryResponse)).add("pubstatus_counts", this.getPublicationStatusCounts(solrQueryResponse)).add("selected_filters", this.myDataFinder.getSelectedFilterParamsAsJSON()));
    if (OTHER_USER == true) {
        jsonData.add("other_user", searchUser.getIdentifier());
    }
    return jsonData.build().toString();
}
Also used : SearchException(edu.harvard.iq.dataverse.search.SearchException) DataverseRolePermissionHelper(edu.harvard.iq.dataverse.authorization.DataverseRolePermissionHelper) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) JsonObjectBuilder(javax.json.JsonObjectBuilder) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with SearchException

use of edu.harvard.iq.dataverse.search.SearchException in project dataverse by IQSS.

the class Index method searchDebug.

/**
 * This method is for integration tests of search.
 */
@GET
@Path("test")
public Response searchDebug(@QueryParam("key") String apiToken, @QueryParam("q") String query, @QueryParam("fq") final List<String> filterQueries) {
    User user = findUserByApiToken(apiToken);
    if (user == null) {
        return error(Response.Status.UNAUTHORIZED, "Invalid apikey '" + apiToken + "'");
    }
    Dataverse subtreeScope = dataverseService.findRootDataverse();
    String sortField = SearchFields.ID;
    String sortOrder = SortBy.ASCENDING;
    int paginationStart = 0;
    boolean dataRelatedToMe = false;
    int numResultsPerPage = Integer.MAX_VALUE;
    SolrQueryResponse solrQueryResponse;
    try {
        solrQueryResponse = searchService.search(createDataverseRequest(user), subtreeScope, query, filterQueries, sortField, sortOrder, paginationStart, dataRelatedToMe, numResultsPerPage);
    } catch (SearchException ex) {
        return error(Response.Status.INTERNAL_SERVER_ERROR, ex.getLocalizedMessage() + ": " + ex.getCause().getLocalizedMessage());
    }
    JsonArrayBuilder itemsArrayBuilder = Json.createArrayBuilder();
    List<SolrSearchResult> solrSearchResults = solrQueryResponse.getSolrSearchResults();
    for (SolrSearchResult solrSearchResult : solrSearchResults) {
        itemsArrayBuilder.add(solrSearchResult.getType() + ":" + solrSearchResult.getNameSort());
    }
    return ok(itemsArrayBuilder);
}
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) SearchException(edu.harvard.iq.dataverse.search.SearchException) JsonArrayBuilder(javax.json.JsonArrayBuilder) Dataverse(edu.harvard.iq.dataverse.Dataverse) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with SearchException

use of edu.harvard.iq.dataverse.search.SearchException 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)

Example 4 with SearchException

use of edu.harvard.iq.dataverse.search.SearchException in project dataverse by IQSS.

the class DataversePage method saveLinkedDataverse.

public String saveLinkedDataverse() {
    if (linkingDataverseId == null) {
        JsfHelper.addSuccessMessage("You must select a linking dataverse.");
        return "";
    }
    AuthenticatedUser savedSearchCreator = getAuthenticatedUser();
    if (savedSearchCreator == null) {
        String msg = "Only authenticated users can link a dataverse.";
        logger.severe(msg);
        JsfHelper.addErrorMessage(msg);
        return returnRedirect();
    }
    linkingDataverse = dataverseService.find(linkingDataverseId);
    LinkDataverseCommand cmd = new LinkDataverseCommand(dvRequestService.getDataverseRequest(), linkingDataverse, dataverse);
    // LinkDvObjectCommand cmd = new LinkDvObjectCommand (session.getUser(), linkingDataverse, dataverse);
    try {
        commandEngine.submit(cmd);
    } catch (CommandException ex) {
        String msg = "Unable to link " + dataverse.getDisplayName() + " to " + linkingDataverse.getDisplayName() + ". An internal error occurred.";
        logger.log(Level.SEVERE, "{0} {1}", new Object[] { msg, ex });
        JsfHelper.addErrorMessage(msg);
        return returnRedirect();
    }
    SavedSearch savedSearchOfChildren = createSavedSearchForChildren(savedSearchCreator);
    boolean createLinksAndIndexRightNow = false;
    if (createLinksAndIndexRightNow) {
        try {
            // create links (does indexing) right now (might be expensive)
            boolean debug = false;
            DataverseRequest dataverseRequest = new DataverseRequest(savedSearchCreator, SavedSearchServiceBean.getHttpServletRequest());
            savedSearchService.makeLinksForSingleSavedSearch(dataverseRequest, savedSearchOfChildren, debug);
            JsfHelper.addSuccessMessage(BundleUtil.getStringFromBundle("dataverse.linked.success", getSuccessMessageArguments()));
            return returnRedirect();
        } catch (SearchException | CommandException ex) {
            // error: solr is down, etc. can't link children right now
            JsfHelper.addErrorMessage(BundleUtil.getStringFromBundle("dataverse.linked.internalerror", getSuccessMessageArguments()));
            String msg = dataverse.getDisplayName() + " has been successfully linked to " + linkingDataverse.getDisplayName() + " but contents will not appear until an internal error has been fixed.";
            logger.log(Level.SEVERE, "{0} {1}", new Object[] { msg, ex });
            // JsfHelper.addErrorMessage(msg);
            return returnRedirect();
        }
    } else {
        // defer: please wait for the next timer/cron job
        // JsfHelper.addSuccessMessage(dataverse.getDisplayName() + " has been successfully linked to " + linkingDataverse.getDisplayName() + ". Please wait for its contents to appear.");
        JsfHelper.addSuccessMessage(BundleUtil.getStringFromBundle("dataverse.linked.success.wait", getSuccessMessageArguments()));
        return returnRedirect();
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) SavedSearch(edu.harvard.iq.dataverse.search.savedsearch.SavedSearch) SearchException(edu.harvard.iq.dataverse.search.SearchException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) LinkDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.LinkDataverseCommand)

Example 5 with SearchException

use of edu.harvard.iq.dataverse.search.SearchException in project dataverse by IQSS.

the class CreateSavedSearchCommand method execute.

@Override
public SavedSearch execute(CommandContext ctxt) throws CommandException {
    String query = savedSearchToCreate.getQuery();
    if (query == null) {
        /**
         * @todo This probably shouldn't be the default. We are disallowing
         * wildcard searches. Go fix the DataversePage and make sure the
         * query is set.
         */
        String wildcardSearch = "*";
        savedSearchToCreate.setQuery(wildcardSearch);
    }
    SavedSearch persistedSavedSearch = ctxt.savedSearches().save(savedSearchToCreate);
    if (persistedSavedSearch != null) {
        try {
            DataverseRequest dataverseRequest = new DataverseRequest(savedSearchToCreate.getCreator(), SavedSearchServiceBean.getHttpServletRequest());
            JsonObjectBuilder result = ctxt.savedSearches().makeLinksForSingleSavedSearch(dataverseRequest, persistedSavedSearch, true);
            logger.log(Level.INFO, "result from attempt to make links from saved search: {0}", result.build().toString());
        } catch (SearchException ex) {
            logger.info(ex.getLocalizedMessage());
        }
        return persistedSavedSearch;
    } else {
        return null;
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) SavedSearch(edu.harvard.iq.dataverse.search.savedsearch.SavedSearch) SearchException(edu.harvard.iq.dataverse.search.SearchException) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Aggregations

SearchException (edu.harvard.iq.dataverse.search.SearchException)8 JsonObjectBuilder (javax.json.JsonObjectBuilder)5 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)4 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 SolrQueryResponse (edu.harvard.iq.dataverse.search.SolrQueryResponse)3 SavedSearch (edu.harvard.iq.dataverse.search.savedsearch.SavedSearch)3 Dataverse (edu.harvard.iq.dataverse.Dataverse)2 GuestUser (edu.harvard.iq.dataverse.authorization.users.GuestUser)2 User (edu.harvard.iq.dataverse.authorization.users.User)2 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)2 SolrSearchResult (edu.harvard.iq.dataverse.search.SolrSearchResult)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 DataverseRolePermissionHelper (edu.harvard.iq.dataverse.authorization.DataverseRolePermissionHelper)1 LinkDataverseCommand (edu.harvard.iq.dataverse.engine.command.impl.LinkDataverseCommand)1 FacetCategory (edu.harvard.iq.dataverse.search.FacetCategory)1 FacetLabel (edu.harvard.iq.dataverse.search.FacetLabel)1 SortBy (edu.harvard.iq.dataverse.search.SortBy)1 ArrayList (java.util.ArrayList)1