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
  • Field Details

    • services

      protected ServiceRegistry services
      Service registry
    • storeRef

      protected StoreRef storeRef
      Default store reference
    • repository

      protected Repository repository
      Repository helper
  • Constructor Details

    • Search

      public Search()
  • Method Details

    • afterPropertiesSet

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

      public void setStoreUrl(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 String getSearchSubsystem()
    • findNode

      public ScriptNode findNode(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(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(String referenceType, 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(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(String store, 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(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(String store, 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(String query)
      Validation Xpath query
      Parameters:
      query - xpath query
      Returns:
      true if xpath query valid
    • luceneSearch

      public org.mozilla.javascript.Scriptable luceneSearch(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(String store, 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(String search, 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(String search, String sortColumn, boolean asc, int max)
    • luceneSearch

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

      public org.mozilla.javascript.Scriptable luceneSearch(String store, String search, 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(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(String store, 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(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(Object search)
    • getIntegerValue

      public int getIntegerValue(String attribute, int defaultValue, Map<Serializable,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(String attribute, boolean defaultValue, Map<Serializable,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 String ISO9075Encode(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 String ISO9075Decode(String s)
      Decode a string from ISO9075
      Parameters:
      s - Value to decode
      Returns:
      decoded value
    • query

      protected Object[] query(String store, String search, Search.SortColumn[] sort, 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 Object[] query(String store, String search, Search.SortColumn[] sort, 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 Object[] query(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 Pair<Object[],Map<String,Object>> queryResultMeta(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(SearchParameters sp, String field, List<String> facetQueries, 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