Example usage for org.apache.solr.client.solrj.response SpellCheckResponse getCollatedResults

List of usage examples for org.apache.solr.client.solrj.response SpellCheckResponse getCollatedResults

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.response SpellCheckResponse getCollatedResults.

Prototype

public List<Collation> getCollatedResults() 

Source Link

Document

Return all collations.

Usage

From source file:fr.paris.lutece.plugins.search.solr.web.SolrSearchApp.java

License:Open Source License

/**
 * Performs a search and fills the model (useful when a page needs to remind
 * search parameters/results)/* w ww .  ja v a2  s .co  m*/
 *
 * @param request the request
 * @param conf the configuration
 * @return the model
 * @throws SiteMessageException if an error occurs
 */
public static Map<String, Object> getSearchResultModel(HttpServletRequest request, SolrSearchAppConf conf)
        throws SiteMessageException {
    String strQuery = request.getParameter(PARAMETER_QUERY);
    String[] facetQuery = request.getParameterValues(PARAMETER_FACET_QUERY);
    String sort = request.getParameter(PARAMETER_SORT_NAME);
    String order = request.getParameter(PARAMETER_SORT_ORDER);
    String strCurrentPageIndex = request.getParameter(PARAMETER_PAGE_INDEX);

    String fname = StringUtils.isBlank(request.getParameter(PARAMETER_FACET_NAME)) ? null
            : request.getParameter(PARAMETER_FACET_LABEL).trim();
    String flabel = StringUtils.isBlank(request.getParameter(PARAMETER_FACET_LABEL)) ? null
            : request.getParameter(PARAMETER_FACET_LABEL).trim();
    String strConfCode = request.getParameter(PARAMETER_CONF);

    Locale locale = request.getLocale();

    if (conf == null) {
        //Use default conf if not provided
        conf = SolrSearchAppConfService.loadConfiguration(null);
    }

    StringBuilder sbFacetQueryUrl = new StringBuilder();
    SolrFieldManager sfm = new SolrFieldManager();

    List<String> lstSingleFacetQueries = new ArrayList<String>();
    Hashtable<String, Boolean> switchType = getSwitched();
    ArrayList<String> facetQueryTmp = new ArrayList<String>();
    if (facetQuery != null) {
        for (String fq : facetQuery) {
            if (sbFacetQueryUrl.indexOf(fq) == -1) {
                String strFqNameIHM = getFacetNameFromIHM(fq);
                String strFqValueIHM = getFacetValueFromIHM(fq);
                if (fname == null || !switchType.containsKey(fname)
                        || (strFqNameIHM != null && strFqValueIHM != null
                                && strFqValueIHM.equalsIgnoreCase(flabel)
                                && strFqNameIHM.equalsIgnoreCase(fname))) {
                    sbFacetQueryUrl.append("&fq=" + fq);
                    sfm.addFacet(fq);
                    facetQueryTmp.add(fq);
                    lstSingleFacetQueries.add(fq);
                }
            }
        }
        //             for (String fq : facetQuery)
        //            {
        //                if (sbFacetQueryUrl.indexOf(fq) == -1)
        //                {   
        //                   //   sbFacetQueryUrl.append("&fq=" + fq);
        //                       sfm.addFacet(fq);
        //                       lstSingleFacetQueries.add(fq);
        //                }
        //            }
    }
    facetQuery = new String[facetQueryTmp.size()];
    facetQuery = facetQueryTmp.toArray(facetQuery);

    if (StringUtils.isNotBlank(conf.getFilterQuery())) {
        int nNewLength = (facetQuery == null) ? 1 : (facetQuery.length + 1);
        String[] newFacetQuery = new String[nNewLength];

        for (int i = 0; i < (nNewLength - 1); i++) {
            newFacetQuery[i] = facetQuery[i];
        }

        newFacetQuery[newFacetQuery.length - 1] = conf.getFilterQuery();
        facetQuery = newFacetQuery;
    }

    boolean bEncodeUri = Boolean.parseBoolean(
            AppPropertiesService.getProperty(PROPERTY_ENCODE_URI, Boolean.toString(DEFAULT_ENCODE_URI)));

    String strSearchPageUrl = AppPropertiesService.getProperty(PROPERTY_SEARCH_PAGE_URL);
    String strError = SolrConstants.CONSTANT_EMPTY_STRING;

    int nLimit = SOLR_RESPONSE_MAX;

    // Check XSS characters
    if ((strQuery != null) && (StringUtil.containsXssCharacters(strQuery))) {
        strError = I18nService.getLocalizedString(MESSAGE_INVALID_SEARCH_TERMS, locale);
    }

    if (StringUtils.isNotBlank(strError) || StringUtils.isBlank(strQuery)) {
        strQuery = ALL_SEARCH_QUERY;

        String strOnlyFacets = AppPropertiesService.getProperty(PROPERTY_ONLY_FACTES);

        if (StringUtils.isNotBlank(strError)
                || (((facetQuery == null) || (facetQuery.length <= 0)) && StringUtils.isNotBlank(strOnlyFacets)
                        && SolrConstants.CONSTANT_TRUE.equals(strOnlyFacets))) {
            //no request and no facet selected : we show the facets but no result
            nLimit = 0;
        }
    }

    // paginator & session related elements
    int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt(PROPERTY_RESULTS_PER_PAGE,
            DEFAULT_RESULTS_PER_PAGE);
    String strCurrentItemsPerPage = request.getParameter(PARAMETER_NB_ITEMS_PER_PAGE);
    int nCurrentItemsPerPage = strCurrentItemsPerPage != null ? Integer.parseInt(strCurrentItemsPerPage) : 0;
    int nItemsPerPage = Paginator.getItemsPerPage(request, Paginator.PARAMETER_ITEMS_PER_PAGE,
            nCurrentItemsPerPage, nDefaultItemsPerPage);

    strCurrentPageIndex = (strCurrentPageIndex != null) ? strCurrentPageIndex : DEFAULT_PAGE_INDEX;

    SolrSearchEngine engine = SolrSearchEngine.getInstance();

    SolrFacetedResult facetedResult = engine.getFacetedSearchResults(strQuery, facetQuery, sort, order, nLimit,
            Integer.parseInt(strCurrentPageIndex), nItemsPerPage, SOLR_SPELLCHECK);
    List<SolrSearchResult> listResults = facetedResult.getSolrSearchResults();

    List<HashMap<String, Object>> points = null;
    if (conf.getExtraMappingQuery()) {
        List<SolrSearchResult> listResultsGeoloc = engine.getGeolocSearchResults(strQuery, facetQuery, nLimit);
        points = getGeolocModel(listResultsGeoloc);
    }

    // The page should not be added to the cache
    // Notify results infos to QueryEventListeners 
    notifyQueryListeners(strQuery, listResults.size(), request);

    UrlItem url = new UrlItem(strSearchPageUrl);
    String strQueryForPaginator = strQuery;

    if (bEncodeUri) {
        strQueryForPaginator = SolrUtil.encodeUrl(request, strQuery);
    }

    url.addParameter(PARAMETER_QUERY, strQueryForPaginator);
    url.addParameter(PARAMETER_NB_ITEMS_PER_PAGE, nItemsPerPage);

    if (strConfCode != null) {
        url.addParameter(PARAMETER_CONF, strConfCode);
    }

    for (String strFacetName : lstSingleFacetQueries) {
        url.addParameter(PARAMETER_FACET_QUERY, SolrUtil.encodeUrl(strFacetName));
    }

    // nb items per page
    IPaginator<SolrSearchResult> paginator = new DelegatePaginator<SolrSearchResult>(listResults, nItemsPerPage,
            url.getUrl(), PARAMETER_PAGE_INDEX, strCurrentPageIndex, facetedResult.getCount());

    Map<String, Object> model = new HashMap<String, Object>();
    model.put(MARK_RESULTS_LIST, paginator.getPageItems());
    // put the query only if it's not *.*
    model.put(MARK_QUERY, ALL_SEARCH_QUERY.equals(strQuery) ? SolrConstants.CONSTANT_EMPTY_STRING : strQuery);
    model.put(MARK_FACET_QUERY, sbFacetQueryUrl.toString());
    model.put(MARK_PAGINATOR, paginator);
    model.put(MARK_NB_ITEMS_PER_PAGE, nItemsPerPage);
    model.put(MARK_ERROR, strError);
    model.put(MARK_FACETS, facetedResult.getFacetFields());
    model.put(MARK_SOLR_FIELDS, SolrFieldManager.getFacetList());
    model.put(MARK_FACETS_DATE, facetedResult.getFacetDateList());
    model.put(MARK_HISTORIQUE, sfm.getCurrentFacet());
    model.put(MARK_FACETS_LIST, lstSingleFacetQueries);
    model.put(MARK_CONF_QUERY, strConfCode);
    model.put(MARK_CONF, conf);
    model.put(MARK_POINTS, points);

    if (SOLR_SPELLCHECK && (strQuery != null) && (strQuery.compareToIgnoreCase(ALL_SEARCH_QUERY) != 0)) {
        SpellCheckResponse checkResponse = engine.getSpellChecker(strQuery);

        if (checkResponse != null) {
            model.put(MARK_SUGGESTION, checkResponse.getCollatedResults());
            //model.put(MARK_SUGGESTION, facetedResult.getSolrSpellCheckResponse().getCollatedResults());
        }
    }

    model.put(MARK_SORT_NAME, sort);
    model.put(MARK_SORT_ORDER, order);
    model.put(MARK_SORT_LIST, SolrFieldManager.getSortList());
    model.put(MARK_FACET_TREE, facetedResult.getFacetIntersection());
    model.put(MARK_ENCODING, SolrUtil.getEncoding());

    String strRequestUrl = request.getRequestURL().toString();
    model.put(FULL_URL, strRequestUrl);
    model.put(SOLR_FACET_DATE_GAP, SolrSearchEngine.SOLR_FACET_DATE_GAP);

    return model;
}