Class CmsSearch


  • public class CmsSearch
    extends java.lang.Object
    Helper class to access the search facility within a jsp.

    Typically, the following fields are available for searching:

    • title - the title of a resource
    • keywords - the keywords of a resource
    • description - the description of a resource
    • content - the aggregated content of a resource
    • created - the creation date of a resource
    • lastmodified - the date of the last modification of a resource
    • path - the path to display the resource
    • channel - the channel of a resource
    • contentdefinition - the name of the content definition class of a resource
    Since:
    6.0.0
    • Constructor Summary

      Constructors 
      Constructor Description
      CmsSearch()
      Default constructor, used to instantiate the search facility as a bean.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFieldQuery​(java.lang.String fieldName, java.lang.String searchQuery, org.apache.lucene.search.BooleanClause.Occur occur)
      Adds an individual query for a search field.
      void addFieldQuery​(CmsSearchParameters.CmsSearchFieldQuery fieldQuery)
      Adds an individual query for a search field.
      void addFieldQueryMust​(java.lang.String fieldName, java.lang.String searchQuery)
      Adds an individual query for a search field that MUST occur.
      void addFieldQueryMustNot​(java.lang.String fieldName, java.lang.String searchQuery)
      Adds an individual query for a search field that MUST NOT occur.
      void addFieldQueryShould​(java.lang.String fieldName, java.lang.String searchQuery)
      Adds an individual query for a search field that SHOULD occur.
      boolean getCalculateCategories()
      Returns true if a category overview should be shown as part of the result.
      java.lang.String[] getCategories()
      Returns the search categories.
      int getDisplayPages()
      Returns the maximum number of pages which should be shown.
      java.lang.String getFields()
      Gets the current fields list.
      java.lang.String getIndex()
      Gets the name of the current search index.
      java.lang.Exception getLastException()
      Gets the last exception after a search operation.
      int getMatchesPerPage()
      Gets the number of matches displayed on each page.
      long getMaxDateCreated()
      Returns the maximum creation date a resource must have to be included in the search result.
      long getMaxDateLastModified()
      Returns the maximum last modification date a resource must have to be included in the search result.
      long getMinDateCreated()
      Returns the minimum creation date a resource must have to be included in the search result.
      long getMinDateLastModified()
      Returns the minimum last modification date a resource must have to be included in the search result.
      java.lang.String getNextUrl()
      Gets the URL for the link to the next result page.
      java.util.Map<java.lang.Integer,​java.lang.String> getPageLinks()
      Creates a sorted map of URLs to link to other search result pages.
      CmsSearchParameters getParameters()
      Returns the search parameters used for searching, build out of the given individual parameter values.
      java.lang.String getParsedQuery()
      Returns the parsed query.
      java.lang.String getPreviousUrl()
      Gets the URL for the link to the previous result page.
      java.lang.String getQuery()
      Gets the current search query.
      int getQueryLength()
      Gets the minimum search query length.
      int getSearchPage()
      Gets the current result page.
      java.util.List<CmsSearchResult> getSearchResult()
      Returns the search result for the current query, as a list of CmsSearchResult objects.
      java.util.Map<java.lang.String,​java.lang.Integer> getSearchResultCategories()
      Returns a map of categories (Strings) for the last search result, mapped to the hit count (Integer) of the documents in this category, or null if the categories have not been calculated.
      int getSearchResultCount()
      Returns the total number of search results matching the query.
      java.lang.String[] getSearchRoots()
      Returns the search roots.
      org.apache.lucene.search.Sort getSortOrder()
      Returns the sort order used for sorting the results of s search.
      void init​(CmsObject cms)
      Initializes the bean with the cms object.
      void setCalculateCategories​(boolean calculateCategories)
      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.
      void setCategories​(java.lang.String[] categories)
      Sets the search categories, all search results must be in one of the categories, the category set must match the indexed category exactly.
      void setDisplayPages​(int value)
      Sets the maximum number of pages which should be shown.
      void setExcerptOnlySearchedFields​(boolean value)
      Controls if the excerpt from a field is generated only for searched fields, or for all fields (the default).
      void setField​(java.lang.String[] fields)
      Sets the fields to search.
      void setIndex​(java.lang.String indexName)
      Set the name of the index to search.
      void setMatchesPerPage​(int matches)
      Sets the number of matches per page.
      void setMaxDateCreated​(java.lang.String maxDateCreated)
      Sets the maximum creation date a resource must have to be included in the search result.
      void setMaxDateLastModified​(java.lang.String maxDateLastModified)
      Sets the maximum last modification date a resource must have to be included in the search result.
      void setMinDateCreated​(java.lang.String minDateCreated)
      Sets the minimum creation date a resource must have to be included in the search result.
      void setMinDateLastModified​(java.lang.String minDateLastModified)
      Sets the minimum last modification date a resource must have to be included in the search result.
      void setParameters​(CmsSearchParameters parameters)
      Set the parameters to use if a non null instance is provided.
      void setParsedQuery​(java.lang.String parsedQuery)
      Sets the parsed query, which will be parameter decoded first.
      void setQuery​(java.lang.String query)
      Sets the search query.
      void setQueryLength​(int length)
      Sets the minimum length of the search query.
      void setResourceType​(java.lang.String resourceType)
      Limits the search to a given of resource type only.
      void setResourceTypes​(java.lang.String[] resourceTypes)
      Limits the search to a given list of resource types only.
      void setResultRestriction​(CmsSearchParameters restriction)
      Restrict the result of the next search to the results of the last search, restricted with the provided parameters.
      void setSearchPage​(int page)
      Sets the current result page.
      void setSearchRoot​(java.lang.String searchRoot)
      Convenience method to set exactly one search root.
      void setSearchRoots​(java.lang.String[] searchRoots)
      Sets the search root list.
      void setSortOrder​(org.apache.lucene.search.Sort sortOrder)
      Sets the sort order used for sorting the results of s search.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • m_categoriesFound

        protected java.util.Map<java.lang.String,​java.lang.Integer> m_categoriesFound
        The result categories of a search.
      • m_lastException

        protected java.lang.Exception m_lastException
        The latest exception.
      • m_nextUrl

        protected java.lang.String m_nextUrl
        The URL which leads to the next result page.
      • m_pageCount

        protected int m_pageCount
        The number of pages for the result list.
      • m_prevUrl

        protected java.lang.String m_prevUrl
        The URL which leads to the previous result page.
      • m_searchParameters

        protected java.lang.String m_searchParameters
        The search parameter String.
      • m_searchResultCount

        protected int m_searchResultCount
        The total number of search results matching the query.
    • Constructor Detail

      • CmsSearch

        public CmsSearch()
        Default constructor, used to instantiate the search facility as a bean.

    • Method Detail

      • addFieldQuery

        public void addFieldQuery​(CmsSearchParameters.CmsSearchFieldQuery fieldQuery)
        Adds an individual query for a search field.

        If this is used, any setting made with setQuery(String) and setField(String[]) will be ignored and only the individual field search settings will be used.

        When combining occurrences of SHOULD, MUST and MUST_NOT, keep the following in mind: All SHOULD clauses will be grouped and wrapped in one query, all MUST and MUST_NOT clauses will be grouped in another query. This means that at least one of the terms which are given as a SHOULD query must occur in the search result.

        Parameters:
        fieldQuery - the field query to use
        Since:
        8.0.2
      • addFieldQuery

        public void addFieldQuery​(java.lang.String fieldName,
                                  java.lang.String searchQuery,
                                  org.apache.lucene.search.BooleanClause.Occur occur)
        Adds an individual query for a search field.

        If this is used, any setting made with setQuery(String) and setField(String[]) will be ignored and only the individual field search settings will be used.

        When combining occurrences of SHOULD, MUST and MUST_NOT, keep the following in mind: All SHOULD clauses will be grouped and wrapped in one query, all MUST and MUST_NOT clauses will be grouped in another query. This means that at least one of the terms which are given as a SHOULD query must occur in the search result.

        Parameters:
        fieldName - the field name
        searchQuery - the search query
        occur - the occur parameter for the query in the field
        Since:
        7.5.1
      • addFieldQueryMust

        public void addFieldQueryMust​(java.lang.String fieldName,
                                      java.lang.String searchQuery)
        Adds an individual query for a search field that MUST occur.

        If this is used, any setting made with setQuery(String) and setField(String[]) will be ignored and only the individual field search settings will be used.

        When combining occurrences of SHOULD, MUST and MUST_NOT, keep the following in mind: All SHOULD clauses will be grouped and wrapped in one query, all MUST and MUST_NOT clauses will be grouped in another query. This means that at least one of the terms which are given as a SHOULD query must occur in the search result.

        Parameters:
        fieldName - the field name
        searchQuery - the search query
        Since:
        7.5.1
      • addFieldQueryMustNot

        public void addFieldQueryMustNot​(java.lang.String fieldName,
                                         java.lang.String searchQuery)
        Adds an individual query for a search field that MUST NOT occur.

        If this is used, any setting made with setQuery(String) and setField(String[]) will be ignored and only the individual field search settings will be used.

        When combining occurrences of SHOULD, MUST and MUST_NOT, keep the following in mind: All SHOULD clauses will be grouped and wrapped in one query, all MUST and MUST_NOT clauses will be grouped in another query. This means that at least one of the terms which are given as a SHOULD query must occur in the search result.

        Parameters:
        fieldName - the field name
        searchQuery - the search query
        Since:
        7.5.1
      • addFieldQueryShould

        public void addFieldQueryShould​(java.lang.String fieldName,
                                        java.lang.String searchQuery)
        Adds an individual query for a search field that SHOULD occur.

        If this is used, any setting made with setQuery(String) and setField(String[]) will be ignored and only the individual field search settings will be used.

        When combining occurrences of SHOULD, MUST and MUST_NOT, keep the following in mind: All SHOULD clauses will be grouped and wrapped in one query, all MUST and MUST_NOT clauses will be grouped in another query. This means that at least one of the terms which are given as a SHOULD query must occur in the search result.

        Parameters:
        fieldName - the field name
        searchQuery - the search query
        Since:
        7.5.1
      • getCalculateCategories

        public boolean getCalculateCategories()
        Returns true if a category overview should be shown as part of the result.

        Please note: 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.

        Returns:
        true if a category overview should be shown as part of the result
      • getCategories

        public java.lang.String[] getCategories()
        Returns the search categories.

        Returns:
        the search categories
      • getDisplayPages

        public int getDisplayPages()
        Returns the maximum number of pages which should be shown.

        Returns:
        the maximum number of pages which should be shown
      • getFields

        public java.lang.String getFields()
        Gets the current fields list.

        Returns:
        the fields to search
      • getIndex

        public java.lang.String getIndex()
        Gets the name of the current search index.

        Returns:
        the name of the index
      • getLastException

        public java.lang.Exception getLastException()
        Gets the last exception after a search operation.

        Returns:
        the exception occurred in a search operation or null
      • getMatchesPerPage

        public int getMatchesPerPage()
        Gets the number of matches displayed on each page.

        Returns:
        matches per result page
      • getMaxDateCreated

        public long getMaxDateCreated()
        Returns the maximum creation date a resource must have to be included in the search result.

        Returns:
        the maximum creation date a resource must have to be included in the search result
      • getMaxDateLastModified

        public long getMaxDateLastModified()
        Returns the maximum last modification date a resource must have to be included in the search result.

        Returns:
        the maximum last modification date a resource must have to be included in the search result
      • getMinDateCreated

        public long getMinDateCreated()
        Returns the minimum creation date a resource must have to be included in the search result.

        Returns:
        the minimum creation date a resource must have to be included in the search result
      • getMinDateLastModified

        public long getMinDateLastModified()
        Returns the minimum last modification date a resource must have to be included in the search result.

        Returns:
        the minimum last modification date a resource must have to be included in the search result
      • getNextUrl

        public java.lang.String getNextUrl()
        Gets the URL for the link to the next result page.

        Returns:
        the URL to the next result page
      • getPageLinks

        public java.util.Map<java.lang.Integer,​java.lang.String> getPageLinks()
        Creates a sorted map of URLs to link to other search result pages.

        The key values are Integers representing the page number, the entry holds the corresponding link.

        Returns:
        a map with String URLs
      • getParameters

        public CmsSearchParameters getParameters()
        Returns the search parameters used for searching, build out of the given individual parameter values.

        Returns:
        the search parameters used for searching, build out of the given individual parameter values
      • getParsedQuery

        public java.lang.String getParsedQuery()
        Returns the parsed query.

        The parsed query is automatically set by the OpenCms search index when a query is created with either setQuery(String) or addFieldQuery(CmsSearchFieldQuery). The Lucene query build from the parameters is stored here and can be later used for paging through the results.

        Please note that this returns only to the query part, not the filter part of the search.

        Returns:
        the parsed query
      • getPreviousUrl

        public java.lang.String getPreviousUrl()
        Gets the URL for the link to the previous result page.

        Returns:
        the URL to the previous result page
      • getQuery

        public java.lang.String getQuery()
        Gets the current search query.

        Returns:
        the current query string or null if no query was set before
      • getQueryLength

        public int getQueryLength()
        Gets the minimum search query length.

        Returns:
        the minimum search query length
      • getSearchPage

        public int getSearchPage()
        Gets the current result page.

        Returns:
        the current result page
      • getSearchResult

        public java.util.List<CmsSearchResultgetSearchResult()
        Returns the search result for the current query, as a list of CmsSearchResult objects.

        Returns:
        the search result (may be empty) or null if no index or query was set before
      • getSearchResultCategories

        public java.util.Map<java.lang.String,​java.lang.Integer> getSearchResultCategories()
        Returns a map of categories (Strings) for the last search result, mapped to the hit count (Integer) of the documents in this category, or null if the categories have not been calculated.

        Returns:
        a map of categories for the last search result
        See Also:
        getCalculateCategories(), setCalculateCategories(boolean)
      • getSearchResultCount

        public int getSearchResultCount()
        Returns the total number of search results matching the query.

        Returns:
        the total number of search results matching the query
      • getSearchRoots

        public java.lang.String[] getSearchRoots()
        Returns the search roots.

        Only resources that are sub-resources of one of the search roots are included in the search result.

        The search roots are used in addition to the current site root of the user performing the search.

        By default, the search roots contain only one entry with an empty string.

        Returns:
        the search roots
      • getSortOrder

        public org.apache.lucene.search.Sort getSortOrder()
        Returns the sort order used for sorting the results of s search.

        Returns:
        the sort order used for sorting the results of s search
      • init

        public void init​(CmsObject cms)
        Initializes the bean with the cms object.

        Parameters:
        cms - the cms object
      • setCalculateCategories

        public void setCalculateCategories​(boolean calculateCategories)
        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.

        Please note: 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.

        Parameters:
        calculateCategories - if true, the category count will be calculated for the next search
      • setCategories

        public void setCategories​(java.lang.String[] categories)
        Sets the search categories, all search results must be in one of the categories, the category set must match the indexed category exactly.

        All categories will automatically be trimmed and lower cased, since search categories are also stored this way in the index.

        Parameters:
        categories - the categories to set
      • setDisplayPages

        public void setDisplayPages​(int value)
        Sets the maximum number of pages which should be shown.

        Enter an odd value to achieve a nice, "symmetric" output.

        Parameters:
        value - the maximum number of pages which should be shown
      • setExcerptOnlySearchedFields

        public void setExcerptOnlySearchedFields​(boolean value)
        Controls if the excerpt from a field is generated only for searched fields, or for all fields (the default).

        The default setting is false, which means all text fields configured for the excerpt will be used to generate the excerpt, regardless if they have been searched in or not.

        Please note: A field will only be included in the excerpt if it has been configured as excerpt="true" in opencms-search.xml. This method controls if so configured fields are used depending on the fields searched, see setField(String[]).

        Parameters:
        value - if true, the excerpt is generated only from the fields actually searched in
      • setField

        public void setField​(java.lang.String[] fields)
        Sets the fields to search.

        If the fields are set to null, or not set at all, the default fields "content" and "meta" are used.

        For a list of valid field names, see the Interface constants of I_CmsDocumentFactory.

        Parameters:
        fields - the fields to search
      • setIndex

        public void setIndex​(java.lang.String indexName)
        Set the name of the index to search.

        A former search result will be deleted.

        Parameters:
        indexName - the name of the index
      • setMatchesPerPage

        public void setMatchesPerPage​(int matches)
        Sets the number of matches per page.

        Parameters:
        matches - the number of matches per page
      • setMaxDateCreated

        public void setMaxDateCreated​(java.lang.String maxDateCreated)
        Sets the maximum creation date a resource must have to be included in the search result.

        Parameters:
        maxDateCreated - the maximum creation date to set
      • setMaxDateLastModified

        public void setMaxDateLastModified​(java.lang.String maxDateLastModified)
        Sets the maximum last modification date a resource must have to be included in the search result.

        Parameters:
        maxDateLastModified - the maximum last modification date to set
      • setMinDateCreated

        public void setMinDateCreated​(java.lang.String minDateCreated)
        Sets the minimum creation date a resource must have to be included in the search result.

        Parameters:
        minDateCreated - the minimum creation date to set
      • setMinDateLastModified

        public void setMinDateLastModified​(java.lang.String minDateLastModified)
        Sets the minimum last modification date a resource must have to be included in the search result.

        Parameters:
        minDateLastModified - he minimum last modification date to set
      • setParameters

        public void setParameters​(CmsSearchParameters parameters)
        Set the parameters to use if a non null instance is provided.

        Parameters:
        parameters - the parameters to use for the search if a non null instance is provided
      • setParsedQuery

        public void setParsedQuery​(java.lang.String parsedQuery)
        Sets the parsed query, which will be parameter decoded first.

        The parsed query is automatically set by the OpenCms search index when a query is created with either setQuery(String) or addFieldQuery(CmsSearchFieldQuery). The Lucene query build from the parameters is stored here and can be later used for paging through the results.

        Please note that this applies only to the query part, not the filter part of the search.

        Parameters:
        parsedQuery - the parsed query to set
      • setQuery

        public void setQuery​(java.lang.String query)
        Sets the search query.

        The syntax of the query depends on the search engine used. A former search result will be deleted.

        Parameters:
        query - the search query (escaped format)
      • setQueryLength

        public void setQueryLength​(int length)
        Sets the minimum length of the search query.

        Parameters:
        length - the minimum search query length
      • setResourceType

        public void setResourceType​(java.lang.String resourceType)
        Limits the search to a given of resource type only.

        Parameters:
        resourceType - the resource type to limit the search result to
        Since:
        7.5.1
      • setResourceTypes

        public void setResourceTypes​(java.lang.String[] resourceTypes)
        Limits the search to a given list of resource types only.

        Parameters:
        resourceTypes - the resource types to limit the search result to
      • setSearchPage

        public void setSearchPage​(int page)
        Sets the current result page.

        Works with jsp bean mechanism for request parameter "searchPage" that is generated here for page links.

        Parameters:
        page - the current result page
      • setSearchRoot

        public void setSearchRoot​(java.lang.String searchRoot)
        Convenience method to set exactly one search root.

        Parameters:
        searchRoot - the search root to set
        See Also:
        setSearchRoots(String[])
      • setSearchRoots

        public void setSearchRoots​(java.lang.String[] searchRoots)
        Sets the search root list.

        Only resources that are sub-resources of one of the search roots are included in the search result.

        The search roots set here are used in addition to the current site root of the user performing the search.

        By default, the search roots contain only one entry with an empty string.

        Parameters:
        searchRoots - the search roots to set
      • setSortOrder

        public void setSortOrder​(org.apache.lucene.search.Sort sortOrder)
        Sets the sort order used for sorting the results of s search.

        Parameters:
        sortOrder - the sort order to set