Class Search

  • All Implemented Interfaces:
    org.alfresco.processor.ProcessorExtension, Scopeable, org.springframework.beans.factory.InitializingBean

    public class Search
    extends BaseScopableProcessorExtension
    implements org.springframework.beans.factory.InitializingBean
    Search component for use by the ScriptService.

    Provides access to Lucene search facilities including saved search objects. The results from a search are returned as an array (collection) of scriptable Node wrapper objects.

    The object is added to the root of the model to provide syntax such as: var results = search.luceneSearch(statement); and var results = search.savedSearch(node);

    Author:
    Kevin Roast
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  Search.SortColumn
      Search sort column
    • Constructor Summary

      Constructors 
      Constructor Description
      Search()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addFacetQuery​(org.alfresco.service.cmr.search.SearchParameters sp, java.lang.String field, java.util.List<java.lang.String> facetQueries, java.lang.String query)
      Adds facet queries to the SearchParameters
      void afterPropertiesSet()  
      ScriptNode findNode​(java.lang.String ref)
      Find a single Node by the Node reference
      ScriptNode findNode​(java.lang.String referenceType, java.lang.String[] reference)
      Helper to convert a Web Script Request URL to a Node Ref 1) Node - {store_type}/{store_id}/{node_id} Resolve to node via its Node Reference.
      ScriptNode findNode​(org.alfresco.service.cmr.repository.NodeRef ref)
      Find a single Node by the Node reference
      boolean getBooleanValue​(java.lang.String attribute, boolean defaultValue, java.util.Map<java.io.Serializable,​java.io.Serializable> sourceObject)
      Attempts to retrieve and parse an attribute in the supplied object to an integer.
      int getIntegerValue​(java.lang.String attribute, int defaultValue, java.util.Map<java.io.Serializable,​java.io.Serializable> sourceObject)
      Attempts to retrieve and parse an attribute in the supplied object to an integer.
      java.lang.String getSearchSubsystem()  
      java.lang.String ISO9075Decode​(java.lang.String s)
      Decode a string from ISO9075
      java.lang.String ISO9075Encode​(java.lang.String s)
      Encode a string to ISO9075 - used to build valid paths for Lucene queries etc.
      boolean isValidXpathQuery​(java.lang.String query)
      Validation Xpath query
      org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String search)
      Execute a Lucene search
      org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String store, java.lang.String search)
      Execute a Lucene search
      org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String search, java.lang.String sortColumn, boolean asc)
      Execute a Lucene search (sorted)
      org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String search, java.lang.String sortColumn, boolean asc, int max)  
      org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String store, java.lang.String search, java.lang.String sortColumn, boolean asc)  
      org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String store, java.lang.String search, java.lang.String sortColumn, boolean asc, int max)
      Execute a Lucene search (sorted)
      org.mozilla.javascript.Scriptable query​(java.lang.Object search)
      Execute a query based on the supplied search definition object.
      protected java.lang.Object[] query​(java.lang.String store, java.lang.String search, Search.SortColumn[] sort, java.lang.String language)
      Execute the query Removes any duplicates that may be present (ID search can cause duplicates - it is better to remove them here)
      protected java.lang.Object[] query​(java.lang.String store, java.lang.String search, Search.SortColumn[] sort, java.lang.String language, int maxResults, int skipResults)
      Execute the query Removes any duplicates that may be present (ID search can cause duplicates - it is better to remove them here)
      protected java.lang.Object[] query​(org.alfresco.service.cmr.search.SearchParameters sp, boolean exceptionOnError)
      Execute the query Removes any duplicates that may be present (ID search can cause duplicates - it is better to remove them here)
      protected org.alfresco.util.Pair<java.lang.Object[],​java.util.Map<java.lang.String,​java.lang.Object>> queryResultMeta​(org.alfresco.service.cmr.search.SearchParameters sp, boolean exceptionOnError)
      Execute the query Removes any duplicates that may be present (ID search can cause duplicates - it is better to remove them here)
      org.mozilla.javascript.Scriptable queryResultSet​(java.lang.Object search)  
      org.mozilla.javascript.Scriptable savedSearch​(java.lang.String searchRef)
      Execute a saved Lucene search
      org.mozilla.javascript.Scriptable savedSearch​(ScriptNode savedSearch)
      Execute a saved Lucene search
      org.mozilla.javascript.Scriptable selectNodes​(java.lang.String search)
      Execute a SelectNodes XPath search
      org.mozilla.javascript.Scriptable selectNodes​(java.lang.String store, java.lang.String search)
      Execute a SelectNodes XPath search
      void setRepositoryHelper​(Repository repository)
      Set the repository helper
      void setSearchSubsystemSwitchableApplicationContextFactory​(SwitchableApplicationContextFactory searchSubsystem)  
      void setServiceRegistry​(ServiceRegistry services)
      Set the service registry
      void setStoreUrl​(java.lang.String storeRef)
      Set the default store reference
      ScriptNode[] tagSearch​(java.lang.String store, java.lang.String tag)
      Searchs the store for all nodes with the given tag applied.
      org.mozilla.javascript.Scriptable xpathSearch​(java.lang.String search)
      Execute a XPath search
      org.mozilla.javascript.Scriptable xpathSearch​(java.lang.String store, java.lang.String search)
      Execute a XPath search
      • Methods inherited from class java.lang.Object

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

      • storeRef

        protected org.alfresco.service.cmr.repository.StoreRef storeRef
        Default store reference
      • repository

        protected Repository repository
        Repository helper
    • Constructor Detail

      • Search

        public Search()
    • Method Detail

      • afterPropertiesSet

        public void afterPropertiesSet()
                                throws java.lang.Exception
        Specified by:
        afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
        Throws:
        java.lang.Exception
      • setStoreUrl

        public void setStoreUrl​(java.lang.String storeRef)
        Set the default store reference
        Parameters:
        storeRef - the default store reference
      • setServiceRegistry

        public void setServiceRegistry​(ServiceRegistry services)
        Set the service registry
        Parameters:
        services - the service registry
      • setRepositoryHelper

        public void setRepositoryHelper​(Repository repository)
        Set the repository helper
        Parameters:
        repository - the repository helper
      • setSearchSubsystemSwitchableApplicationContextFactory

        public void setSearchSubsystemSwitchableApplicationContextFactory​(SwitchableApplicationContextFactory searchSubsystem)
      • getSearchSubsystem

        public java.lang.String getSearchSubsystem()
      • findNode

        public ScriptNode findNode​(org.alfresco.service.cmr.repository.NodeRef ref)
        Find a single Node by the Node reference
        Parameters:
        ref - The NodeRef of the Node to find
        Returns:
        the Node if found or null if failed to find
      • findNode

        public ScriptNode findNode​(java.lang.String ref)
        Find a single Node by the Node reference
        Parameters:
        ref - The fully qualified NodeRef in String format
        Returns:
        the Node if found or null if failed to find
      • findNode

        public ScriptNode findNode​(java.lang.String referenceType,
                                   java.lang.String[] reference)
        Helper to convert a Web Script Request URL to a Node Ref 1) Node - {store_type}/{store_id}/{node_id} Resolve to node via its Node Reference. 2) Path - {store_type}/{store_id}/{path} Resolve to node via its display path.
        Parameters:
        referenceType - one of "node", "path"
        reference - array of reference segments (as described above for each reference type)
        Returns:
        ScriptNode the script node
      • xpathSearch

        public org.mozilla.javascript.Scriptable xpathSearch​(java.lang.String search)
        Execute a XPath search
        Parameters:
        search - XPath search string to execute
        Returns:
        JavaScript array of Node results from the search - can be empty but not null
      • xpathSearch

        public org.mozilla.javascript.Scriptable xpathSearch​(java.lang.String store,
                                                             java.lang.String search)
        Execute a XPath search
        Parameters:
        store - Store reference to search against i.e. workspace://SpacesStore
        search - XPath search string to execute
        Returns:
        JavaScript array of Node results from the search - can be empty but not null
      • selectNodes

        public org.mozilla.javascript.Scriptable selectNodes​(java.lang.String search)
        Execute a SelectNodes XPath search
        Parameters:
        search - SelectNodes XPath search string to execute
        Returns:
        JavaScript array of Node results from the search - can be empty but not null
      • selectNodes

        public org.mozilla.javascript.Scriptable selectNodes​(java.lang.String store,
                                                             java.lang.String search)
        Execute a SelectNodes XPath search
        Parameters:
        store - Store reference to search against i.e. workspace://SpacesStore
        search - SelectNodes XPath search string to execute
        Returns:
        JavaScript array of Node results from the search - can be empty but not null
      • isValidXpathQuery

        public boolean isValidXpathQuery​(java.lang.String query)
        Validation Xpath query
        Parameters:
        query - xpath query
        Returns:
        true if xpath query valid
      • luceneSearch

        public org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String search)
        Execute a Lucene search
        Parameters:
        search - Lucene search string to execute
        Returns:
        JavaScript array of Node results from the search - can be empty but not null
      • luceneSearch

        public org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String store,
                                                              java.lang.String search)
        Execute a Lucene search
        Parameters:
        store - Store reference to search against i.e. workspace://SpacesStore
        search - Lucene search string to execute
        Returns:
        JavaScript array of Node results from the search - can be empty but not null
      • luceneSearch

        public org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String search,
                                                              java.lang.String sortColumn,
                                                              boolean asc)
        Execute a Lucene search (sorted)
        Parameters:
        search - Lucene search string to execute
        sortColumn - column to sort on
        asc - true => ascending sort
        Returns:
        JavaScript array of Node results from the search - can be empty but not null
      • luceneSearch

        public org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String search,
                                                              java.lang.String sortColumn,
                                                              boolean asc,
                                                              int max)
      • luceneSearch

        public org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String store,
                                                              java.lang.String search,
                                                              java.lang.String sortColumn,
                                                              boolean asc)
      • luceneSearch

        public org.mozilla.javascript.Scriptable luceneSearch​(java.lang.String store,
                                                              java.lang.String search,
                                                              java.lang.String sortColumn,
                                                              boolean asc,
                                                              int max)
        Execute a Lucene search (sorted)
        Parameters:
        store - Store reference to search against i.e. workspace://SpacesStore
        search - Lucene search string to execute
        sortColumn - column to sort on
        asc - true => ascending sort
        Returns:
        JavaScript array of Node results from the search - can be empty but not null
      • savedSearch

        public org.mozilla.javascript.Scriptable savedSearch​(ScriptNode savedSearch)
        Execute a saved Lucene search
        Parameters:
        savedSearch - Node that contains the saved search XML content
        Returns:
        JavaScript array of Node results from the search - can be empty but not null
      • savedSearch

        public org.mozilla.javascript.Scriptable savedSearch​(java.lang.String searchRef)
        Execute a saved Lucene search
        Parameters:
        searchRef - NodeRef string that points to the node containing saved search XML content
        Returns:
        JavaScript array of Node results from the search - can be empty but not null
      • tagSearch

        public ScriptNode[] tagSearch​(java.lang.String store,
                                      java.lang.String tag)
        Searchs the store for all nodes with the given tag applied.
        Parameters:
        store - store ref string, default used if null provided
        tag - tag name
        Returns:
        ScriptNode[] nodes with tag applied
      • query

        public org.mozilla.javascript.Scriptable query​(java.lang.Object search)
        Execute a query based on the supplied search definition object. Search object is defined in JavaScript thus:
         search
         {
            query: string,          mandatory, in appropriate format and encoded for the given language
            store: string,          optional, defaults to 'workspace://SpacesStore'
            language: string,       optional, one of: lucene, xpath, fts-alfresco - defaults to 'lucene'
            templates: [],          optional, Array of query language template objects (see below) - if supported by the language 
            sort: [],               optional, Array of sort column objects (see below) - if supported by the language
            page: object,           optional, paging information object (see below) - if supported by the language
            namespace: string,      optional, the default namespace for properties
            defaultField: string,   optional, the default field for query elements when not explicit in the query
            defaultOperator: string,optional, the default operator for query elements when they are not explicit in the query AND or OR
            fieldFacets: [],        optional, Array of fields (as full QName strings) to facet against
            onerror: string         optional, result on error - one of: exception, no-results - defaults to 'exception'
         }
         
         sort
         {
            column: string,         mandatory, sort column in appropriate format for the language
            ascending: boolean      optional, defaults to false
         }
         
         page
         {
            maxItems: int,          optional, max number of items to return in result set
            skipCount: int          optional, number of items to skip over before returning results
         }
         
         template
         {
            field: string,          mandatory, custom field name for the template
            template: string        mandatory, query template replacement for the template
         }
         
         Note that only some query languages support custom query templates, such as 'fts-alfresco'. 
         See the following documentation for more details:
         Templates
         
        Parameters:
        search - Search definition object as above
        Returns:
        Array of ScriptNode results
      • queryResultSet

        public org.mozilla.javascript.Scriptable queryResultSet​(java.lang.Object search)
      • getIntegerValue

        public int getIntegerValue​(java.lang.String attribute,
                                   int defaultValue,
                                   java.util.Map<java.io.Serializable,​java.io.Serializable> sourceObject)
        Attempts to retrieve and parse an attribute in the supplied object to an integer. If the attribute cannot be found or cannot be parsed then the supplied default is returned.
        Parameters:
        attribute -
        defaultValue -
        sourceObject -
        Returns:
      • getBooleanValue

        public boolean getBooleanValue​(java.lang.String attribute,
                                       boolean defaultValue,
                                       java.util.Map<java.io.Serializable,​java.io.Serializable> sourceObject)
        Attempts to retrieve and parse an attribute in the supplied object to an integer. If the attribute cannot be found or cannot be parsed then the supplied default is returned.
        Parameters:
        attribute -
        defaultValue -
        sourceObject -
        Returns:
      • ISO9075Encode

        public java.lang.String ISO9075Encode​(java.lang.String s)
        Encode a string to ISO9075 - used to build valid paths for Lucene queries etc.
        Parameters:
        s - Value to encode
        Returns:
        encoded value
      • ISO9075Decode

        public java.lang.String ISO9075Decode​(java.lang.String s)
        Decode a string from ISO9075
        Parameters:
        s - Value to decode
        Returns:
        decoded value
      • query

        protected java.lang.Object[] query​(java.lang.String store,
                                           java.lang.String search,
                                           Search.SortColumn[] sort,
                                           java.lang.String language)
        Execute the query Removes any duplicates that may be present (ID search can cause duplicates - it is better to remove them here)
        Parameters:
        store - StoreRef to search against - null for default configured store
        search - Lucene search to execute
        sort - Columns to sort by
        language - Search language to use e.g. SearchService.LANGUAGE_LUCENE
        Returns:
        Array of Node objects
      • query

        protected java.lang.Object[] query​(java.lang.String store,
                                           java.lang.String search,
                                           Search.SortColumn[] sort,
                                           java.lang.String language,
                                           int maxResults,
                                           int skipResults)
        Execute the query Removes any duplicates that may be present (ID search can cause duplicates - it is better to remove them here)
        Parameters:
        store - StoreRef to search against - null for default configured store
        search - Lucene search to execute
        sort - Columns to sort by
        language - Search language to use e.g. SearchService.LANGUAGE_LUCENE
        maxResults - Maximum results to return if > 0
        skipResults - Results to skip in the result set
        Returns:
        Array of Node objects
      • query

        protected java.lang.Object[] query​(org.alfresco.service.cmr.search.SearchParameters sp,
                                           boolean exceptionOnError)
        Execute the query Removes any duplicates that may be present (ID search can cause duplicates - it is better to remove them here)
        Parameters:
        sp - SearchParameters describing the search to execute.
        exceptionOnError - True to throw a runtime exception on error, false to return empty resultset
        Returns:
        Array of Node objects
      • queryResultMeta

        protected org.alfresco.util.Pair<java.lang.Object[],​java.util.Map<java.lang.String,​java.lang.Object>> queryResultMeta​(org.alfresco.service.cmr.search.SearchParameters sp,
                                                                                                                                          boolean exceptionOnError)
        Execute the query Removes any duplicates that may be present (ID search can cause duplicates - it is better to remove them here)
        Parameters:
        sp - SearchParameters describing the search to execute.
        exceptionOnError - True to throw a runtime exception on error, false to return empty resultset
        Returns:
        Pair containing Object[] of Node objects, and the ResultSet metadata hash.
      • addFacetQuery

        protected void addFacetQuery​(org.alfresco.service.cmr.search.SearchParameters sp,
                                     java.lang.String field,
                                     java.util.List<java.lang.String> facetQueries,
                                     java.lang.String query)
        Adds facet queries to the SearchParameters
        Parameters:
        sp - the SearchParameters
        field - the requested field facet
        facetQueries - list of generated facet queries
        query - the requested search query