⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmssearch.java

📁 一个cms内容管理平台
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        int todo = 0;
        // TODO: handle the multiple search roots (could be easy, just use multiple parameters?)
        // TODO: handle the categories
        // TODO: handle the search fields
        // params.append("&searchRoot=");
        // params.append(CmsEncoder.encode(m_searchRoots[0]));

        return params.toString();
        // cw: cannot store searchParameters any longer since resultRestrictions changes query
        // m_searchParameters = params.toString();
        // return m_searchParameters;
        /* } else {
         return m_searchParameters;
         } */
    }

    /**
     * Returns the search result for the current query, as a list of <code>{@link CmsSearchResult}</code> objects.<p>
     * 
     * @return the search result (may be empty) or null if no index or query was set before
     */
    public List getSearchResult() {

        if (m_cms != null
            && m_result == null
            && m_parameters.getIndex() != null
            && CmsStringUtil.isNotEmpty(m_parameters.getQuery())) {

            if ((getQueryLength() > 0) && (m_parameters.getQuery().trim().length() < getQueryLength())) {

                m_lastException = new CmsSearchException(Messages.get().container(
                    Messages.ERR_QUERY_TOO_SHORT_1,
                    new Integer(getQueryLength())));

                return m_result;
            }

            try {

                CmsSearchResultList result = m_parameters.getSearchIndex().search(
                    m_cms,
                    getParameters(),
                    m_matchesPerPage);

                if (result.size() > 0) {

                    m_result = result;
                    m_searchResultCount = result.getHitCount();
                    m_categoriesFound = result.getCategories();

                    // re-caluclate the number of pages for this search result
                    m_pageCount = m_searchResultCount / m_matchesPerPage;
                    if ((m_searchResultCount % m_matchesPerPage) != 0) {
                        m_pageCount++;
                    }

                    // re-calculate the URLs to browse forward and backward in the search result
                    String url = m_cms.getRequestContext().getUri() + getSearchParameters() + "&searchPage=";
                    if (m_parameters.getSearchPage() > 1) {
                        m_prevUrl = url + (m_parameters.getSearchPage() - 1);
                    }
                    if (m_parameters.getSearchPage() < m_pageCount) {
                        m_nextUrl = url + (m_parameters.getSearchPage() + 1);
                    }
                } else {
                    m_result = Collections.EMPTY_LIST;
                    m_searchResultCount = 0;
                    m_categoriesFound = null;
                    m_pageCount = 0;
                    m_prevUrl = null;
                    m_nextUrl = null;
                }
            } catch (Exception exc) {

                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_SEARCHING_FAILED_0), exc);
                }

                m_result = null;
                m_searchResultCount = 0;
                m_pageCount = 0;

                m_lastException = exc;
            }
        }

        return m_result;
    }

    /**
     * Returns a map of categories (Strings) for the last search result, mapped to the hit count (Integer) of 
     * the documents in this category, or <code>null</code> if the categories have not been calculated.<p> 
     *
     * @return a map of categories for the last search result
     * 
     * @see CmsSearch#getCalculateCategories()
     * @see CmsSearch#setCalculateCategories(boolean)
     */
    public Map getSearchResultCategories() {

        return m_categoriesFound;
    }

    /**
     * Returns the total number of search results matching the query.<p>
     * 
     * @return the total number of search results matching the query
     */
    public int getSearchResultCount() {

        return m_searchResultCount;
    }

    /**
     * Returns the search roots.<p>
     * 
     * Only resources that are sub-resources of one of the search roots
     * are included in the search result.<p>
     * 
     * The search roots are used <i>in addition to</i> the current site root
     * of the user performing the search.<p>
     * 
     * By default, the search roots contain only one entry with an empty string.<p>
     * 
     * @return the search roots
     */
    public String[] getSearchRoots() {

        List l = m_parameters.getRoots();
        return (String[])l.toArray(new String[l.size()]);
    }

    /**
     * Returns the sort order used for sorting the results of s search.<p>
     *
     * @return the sort order used for sorting the results of s search
     */
    public Sort getSortOrder() {

        return m_parameters.getSort();
    }

    /**
     * Initializes the bean with the cms object.<p>
     * 
     * @param cms the cms object
     */
    public void init(CmsObject cms) {

        m_cms = cms;
        m_result = null;
        m_lastException = null;
        m_pageCount = 0;
        m_nextUrl = null;
        m_prevUrl = null;
    }

    /**
     * Sets the flag that controls calculation of result categories for the next search, 
     * use this only if it's really required since the search can become very slow using this option.<p>
     *
     * <b>Please note:</b> The calculation of the category count slows down the search time by an order
     * of magnitude. Make sure that you only use this feature if it's really required! 
     * Be especially careful if your search result list can become large (> 1000 documents), since in this case
     * overall system performance will certainly be impacted considerably when calculating the categories.<p> 
     *
     * @param calculateCategories if <code>true</code>, the category count will be calculated for the next search
     */
    public void setCalculateCategories(boolean calculateCategories) {

        m_parameters.setCalculateCategories(calculateCategories);
    }

    /**
     * Sets the search categories, all search results must be in one of the categories,
     * the category set must match the indexed category exactly.<p>
     *
     * All categories will automatically be trimmed and lowercased, since search categories
     * are also stored this way in the index.<p>
     *
     * @param categories the categories to set
     */
    public void setCategories(String[] categories) {

        List setCategories = new LinkedList();
        if (categories != null) {
            if (categories.length != 0) {
                // ensure all categories are not null, trimmed, not-empty and lowercased
                String cat;
                for (int i = 0; i < categories.length; i++) {
                    cat = categories[i];
                    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(cat)) {
                        // all categories must internally be lower case, 
                        // since the index keywords are lowercased as well
                        cat = cat.trim().toLowerCase();
                        setCategories.add(cat);
                    }
                }
            }
        }
        m_parameters.setCategories(setCategories);
        resetLastResult();
    }

    /**
     * Sets the maximum number of pages which should be shown.<p>
     * 
     * Enter an odd value to achieve a nice, "symmetric" output.<p> 
     * 
     * @param value the maximum number of pages which should be shown
     */
    public void setDisplayPages(int value) {

        m_displayPages = value;
    }

    /**
     * Sets the fields to search.<p>
     * 
     * If the fields are set to <code>null</code>, 
     * or not set at all, the default fields "content" and "meta" are used.<p>
     * 
     * For a list of valid field names, see the Interface constants of
     * <code>{@link org.opencms.search.documents.I_CmsDocumentFactory}</code>. 
     * 
     * @param fields the fields to search
     */
    public void setField(String[] fields) {

        List l = new LinkedList(Arrays.asList(fields));
        m_parameters.setFields(l);
        resetLastResult();
    }

    /**
     * Set the name of the index to search.<p>
     * 
     * A former search result will be deleted.<p>
     * 
     * @param indexName the name of the index
     */
    public void setIndex(String indexName) {

        resetLastResult();
        CmsSearchIndex index;
        if (CmsStringUtil.isNotEmpty(indexName)) {
            try {
                index = OpenCms.getSearchManager().getIndex(indexName);
                if (index == null) {
                    throw new CmsException(Messages.get().container(Messages.ERR_INDEX_NOT_FOUND_1, indexName));
                }
                m_parameters.setSearchIndex(index);
            } catch (Exception exc) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_INDEX_ACCESS_FAILED_1, indexName), exc);
                }
                m_lastException = exc;
            }
        }
    }

    /**
     * Sets the number of matches per page.<p>
     * 
     * @param matches the number of matches per page
     */
    public void setMatchesPerPage(int matches) {

        m_matchesPerPage = matches;
        resetLastResult();
    }

    /**
     * Set the parameters to use if a non null instance is provided. <p>
     * 
     * @param parameters the parameters to use for the search if a non null instance is provided 
     * 
     */
    public void setParameters(CmsSearchParameters parameters) {

        if (parameters != null) {
            m_parameters = parameters;
        }
    }

    /**
     * Sets the search query.<p>
     * 
     * The syntax of the query depends on the search engine used. 
     * A former search result will be deleted.<p>
     * 
     * @param query the search query (escaped format)
     */
    public void setQuery(String query) {

        try {
            m_parameters.setQuery(CmsEncoder.decodeParameter(query));
        } catch (CmsIllegalArgumentException iae) {
            m_lastException = iae;
        }
        resetLastResult();
    }

    /**
     * Sets the minimum length of the search query.<p>
     * 
     * @param length the minimum search query length
     */
    public void setQueryLength(int length) {

        m_parameters.setQueryLength(length);
    }

    /**
     * Restrict the result of the next search to the results of the last search, 
     * restricted with the provided parameters.<p>
     * 
     * Use this for "seach in search result" functions.<p> 
     * 
     * @param restriction the restriction to use
     * 
     * @see CmsSearchParameters#restrict(CmsSearchParameters)
     */
    public void setResultRestriction(CmsSearchParameters restriction) {

        resetLastResult();
        m_parameterRestriction = restriction;
    }

    /**
     * Sets the current result page.<p>
     * 
     * Works with jsp bean mechanism for request parameter "searchPage" 
     * that is generated here for page links.<p>
     * 
     * @param page the current result page
     */
    public void setSearchPage(int page) {

        m_parameters.setSearchPage(page);
        resetLastResult();
    }

    /**
     * Convenience method to set exactly one search root.<p>
     * 
     * @param searchRoot the search root to set
     *
     * @see #setSearchRoots(String[])
     */
    public void setSearchRoot(String searchRoot) {

        setSearchRoots(CmsStringUtil.splitAsArray(searchRoot, ","));
    }

    /**
     * Sets the search root list.<p>
     * 
     * Only resources that are sub-resources of one of the search roots
     * are included in the search result.<p>
     * 
     * The search roots set here are used <i>in addition to</i> the current site root
     * of the user performing the search.<p>
     * 
     * By default, the search roots contain only one entry with an empty string.<p>
     *
     * @param searchRoots the search roots to set
     */
    public void setSearchRoots(String[] searchRoots) {

        List l = new LinkedList(Arrays.asList(searchRoots));
        m_parameters.setRoots(l);
        resetLastResult();
    }

    /**
     * Sets the sort order used for sorting the results of s search.<p>
     *
     * @param sortOrder the sort order to set
     */
    public void setSortOrder(Sort sortOrder) {

        m_parameters.setSort(sortOrder);
        resetLastResult();
    }

    /**
     * Resets the last seach result.<p>
     */
    private void resetLastResult() {

        m_result = null;
        m_lastException = null;
        m_categoriesFound = null;
        m_parameterRestriction = null;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -