Search in sources :

Example 1 with SearchResult

use of org.apereo.portal.search.SearchResult in project uPortal by Jasig.

the class DirectoryPortletController method search2.

@EventMapping(SearchConstants.SEARCH_REQUEST_QNAME_STRING)
public void search2(EventRequest request, EventResponse response) {
    // get the search query object from the event
    Event event = request.getEvent();
    SearchRequest query = (SearchRequest) event.getValue();
    // search the portal's directory service for people matching the request
    final List<IPersonAttributes> people = searchDirectory(query.getSearchTerms(), request);
    if (people.size() > 0) {
        // transform the list of directory results into our generic search
        // response object
        final SearchResults results = new SearchResults();
        results.setQueryId(query.getQueryId());
        results.setWindowId(request.getWindowID());
        for (IPersonAttributes person : people) {
            final SearchResult result = new SearchResult();
            result.setTitle((String) person.getAttributeValue("displayName"));
            result.getType().add(directorySearchResultType);
            PortletUrl url = new PortletUrl();
            url.setType(PortletUrlType.RENDER);
            url.setPortletMode("VIEW");
            url.setWindowState("maximized");
            PortletUrlParameter actionParam = new PortletUrlParameter();
            actionParam.setName("action");
            actionParam.getValue().add("findByUsername");
            url.getParam().add(actionParam);
            PortletUrlParameter usernameParam = new PortletUrlParameter();
            usernameParam.setName("username");
            usernameParam.getValue().add(person.getName());
            url.getParam().add(usernameParam);
            result.setPortletUrl(url);
            results.getSearchResult().add(result);
        }
        // fire a search response event
        response.setEvent(SearchConstants.SEARCH_RESULTS_QNAME, results);
    }
}
Also used : SearchRequest(org.apereo.portal.search.SearchRequest) IPersonAttributes(org.apereo.services.persondir.IPersonAttributes) Event(javax.portlet.Event) PortletUrlParameter(org.apereo.portal.search.PortletUrlParameter) SearchResult(org.apereo.portal.search.SearchResult) SearchResults(org.apereo.portal.search.SearchResults) PortletUrl(org.apereo.portal.search.PortletUrl) EventMapping(org.springframework.web.portlet.bind.annotation.EventMapping)

Example 2 with SearchResult

use of org.apereo.portal.search.SearchResult in project uPortal by Jasig.

the class SearchPortletController method handleSearchRequest.

/**
 * Performs a search of the explicitly configured {@link IPortalSearchService}s. This is done as
 * an event handler so that it can run concurrently with the other portlets handling the search
 * request
 */
@SuppressWarnings("unchecked")
@EventMapping(SearchConstants.SEARCH_REQUEST_QNAME_STRING)
public void handleSearchRequest(EventRequest request, EventResponse response) {
    // UP-3887 Design flaw.  Both the searchLauncher portlet instance and the search portlet
    // instance receive
    // searchRequest and searchResult events because they are in the same portlet code base (to
    // share
    // autosuggest_handler.jsp and because we have to calculate the search portlet url for the
    // ajax call)
    // and share the portlet.xml which defines the event handling behavior.
    // If this instance is the searchLauncher, ignore the searchResult. The search was submitted
    // to the search
    // portlet instance.
    final String searchLaunchFname = request.getPreferences().getValue(SEARCH_LAUNCH_FNAME, null);
    if (searchLaunchFname != null) {
        // discarding message");
        return;
    }
    final Event event = request.getEvent();
    final SearchRequest searchQuery = (SearchRequest) event.getValue();
    // Map used to track searches that have been handled, used so that one search doesn't get
    // duplicate results
    ConcurrentMap<String, Boolean> searchHandledCache;
    final PortletSession session = request.getPortletSession();
    synchronized (org.springframework.web.portlet.util.PortletUtils.getSessionMutex(session)) {
        searchHandledCache = (ConcurrentMap<String, Boolean>) session.getAttribute(SEARCH_HANDLED_CACHE_NAME, PortletSession.APPLICATION_SCOPE);
        if (searchHandledCache == null) {
            searchHandledCache = CacheBuilder.newBuilder().maximumSize(20).expireAfterAccess(5, TimeUnit.MINUTES).<String, Boolean>build().asMap();
            session.setAttribute(SEARCH_HANDLED_CACHE_NAME, searchHandledCache, PortletSession.APPLICATION_SCOPE);
        }
    }
    final String queryId = searchQuery.getQueryId();
    if (searchHandledCache.putIfAbsent(queryId, Boolean.TRUE) != null) {
        // Already handled this search request
        return;
    }
    // Create the results
    final SearchResults results = new SearchResults();
    results.setQueryId(queryId);
    results.setWindowId(request.getWindowID());
    final List<SearchResult> searchResultList = results.getSearchResult();
    // Run the search for each service appending the results
    for (IPortalSearchService searchService : searchServices) {
        try {
            logger.debug("For queryId {}, query '{}', searching search service {}", queryId, searchQuery.getSearchTerms(), searchService.getClass().toString());
            final SearchResults serviceResults = searchService.getSearchResults(request, searchQuery);
            logger.debug("For queryId {}, obtained {} results from search service {}", queryId, serviceResults.getSearchResult().size(), searchService.getClass().toString());
            searchResultList.addAll(serviceResults.getSearchResult());
        } catch (Exception e) {
            logger.warn(searchService.getClass() + " threw an exception when searching, it will be ignored. " + searchQuery, e);
        }
    }
    // Respond with a results event if results were found
    if (!searchResultList.isEmpty()) {
        response.setEvent(SearchConstants.SEARCH_RESULTS_QNAME, results);
    }
}
Also used : SearchRequest(org.apereo.portal.search.SearchRequest) PortletSession(javax.portlet.PortletSession) Event(javax.portlet.Event) SearchResult(org.apereo.portal.search.SearchResult) SearchResults(org.apereo.portal.search.SearchResults) SpelParseException(org.springframework.expression.spel.SpelParseException) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) IOException(java.io.IOException) EventMapping(org.springframework.web.portlet.bind.annotation.EventMapping)

Example 3 with SearchResult

use of org.apereo.portal.search.SearchResult in project uPortal by Jasig.

the class SearchPortletController method getCleanedAndSortedMapResults.

/**
 * Return the search results in a sorted map based on priority of the search result type
 *
 * @param resultsMap Search results map
 * @return Sorted map of search results ordered on search result type priority
 */
private SortedMap<Integer, List<AutocompleteResultsModel>> getCleanedAndSortedMapResults(ConcurrentMap<String, List<Tuple<SearchResult, String>>> resultsMap, int maxTextLength) {
    SortedMap<Integer, List<AutocompleteResultsModel>> prioritizedResultsMap = createAutocompletePriorityMap();
    // Put the results into the map of <priority,list>
    for (Map.Entry<String, List<Tuple<SearchResult, String>>> entry : resultsMap.entrySet()) {
        for (Tuple<SearchResult, String> tupleSearchResult : entry.getValue()) {
            SearchResult searchResult = tupleSearchResult.getFirst();
            List<String> resultTypes = searchResult.getType();
            // If the search result doesn't have a type defined, use the undefined result type.
            if (resultTypes.size() == 0) {
                resultTypes = UNDEFINED_SEARCH_RESULT_TYPE;
            }
            for (String category : resultTypes) {
                // Exclude the result if it is a result type that's in the ignore list.
                if (!autocompleteIgnoreResultTypes.contains(category)) {
                    int priority = calculatePriorityFromCategory(category);
                    AutocompleteResultsModel result = new AutocompleteResultsModel(cleanAndTrimString(searchResult.getTitle(), maxTextLength), cleanAndTrimString(searchResult.getSummary(), maxTextLength), tupleSearchResult.getSecond(), category);
                    prioritizedResultsMap.get(priority).add(result);
                }
            }
        }
    }
    return prioritizedResultsMap;
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) SearchResult(org.apereo.portal.search.SearchResult) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 4 with SearchResult

use of org.apereo.portal.search.SearchResult in project uPortal by Jasig.

the class GoogleCustomSearchService method getSearchResults.

@Override
public SearchResults getSearchResults(PortletRequest request, SearchRequest query) {
    final Map<String, Object> parameters = new LinkedHashMap<>();
    parameters.put(VERSION_PARAM, VERSION);
    parameters.put(RESULT_SIZE_PARAM, resultSize);
    parameters.put(CUSTOM_SEARCH_PARAM, customSearchId);
    parameters.put(QUERY_PARAM, query.getSearchTerms());
    parameters.put(USER_IP_PARAM, request.getProperty("REMOTE_ADDR"));
    parameters.put(START_PARAM, query.getStartIndex());
    final JsonNode googleResponse = this.restOperations.getForObject(BASE_SEARCH_URL, JsonNode.class, parameters);
    final SearchResults searchResults = new SearchResults();
    searchResults.setQueryId(query.getQueryId());
    final List<SearchResult> searchResultList = searchResults.getSearchResult();
    final JsonNode results = googleResponse.get("responseData").get("results");
    for (final Iterator<JsonNode> resultItr = results.elements(); resultItr.hasNext(); ) {
        final JsonNode googleResult = resultItr.next();
        final SearchResult searchResult = new SearchResult();
        searchResult.setTitle(googleResult.get("title").asText());
        searchResult.setSummary(googleResult.get("content").asText());
        searchResult.setExternalUrl(googleResult.get("clicktrackUrl").asText());
        searchResult.getType().add(resultType);
        searchResultList.add(searchResult);
    }
    return searchResults;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) SearchResult(org.apereo.portal.search.SearchResult) SearchResults(org.apereo.portal.search.SearchResults) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with SearchResult

use of org.apereo.portal.search.SearchResult in project uPortal by Jasig.

the class GsaSearchService method search.

protected SearchResults search(String query) {
    Map<String, String> vars = new HashMap<>();
    vars.put("query", query);
    vars.put("baseUrl", gsaBaseUrl);
    vars.put("site", gsaSite);
    GsaResults gsaResults = restTemplate.getForObject(urlTemplate, GsaResults.class, vars);
    SearchResults results = new SearchResults();
    for (GsaSearchResult gsaResult : gsaResults.getSearchResults()) {
        SearchResult result = new SearchResult();
        result.setTitle(gsaResult.getTitle());
        result.setExternalUrl(gsaResult.getLink());
        result.setSummary(gsaResult.getSnippet());
        result.getType().add(this.resultType);
        results.getSearchResult().add(result);
    }
    return results;
}
Also used : HashMap(java.util.HashMap) SearchResult(org.apereo.portal.search.SearchResult) SearchResults(org.apereo.portal.search.SearchResults)

Aggregations

SearchResult (org.apereo.portal.search.SearchResult)9 SearchResults (org.apereo.portal.search.SearchResults)6 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Event (javax.portlet.Event)2 PortletSession (javax.portlet.PortletSession)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)2 PortletUrl (org.apereo.portal.search.PortletUrl)2 PortletUrlParameter (org.apereo.portal.search.PortletUrlParameter)2 SearchRequest (org.apereo.portal.search.SearchRequest)2 EventMapping (org.springframework.web.portlet.bind.annotation.EventMapping)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IOException (java.io.IOException)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1