Class SearchBuilder


  • public class SearchBuilder
    extends java.lang.Object
    Helper class for importing Search objects in an unambiguous way. The pattern for using this is to 1) Import all available search definitions, using the importXXX() methods, 2) provide the available rank types and rank expressions, using the setRankXXX() methods, 3) invoke the build() method, and 4) retrieve the built search objects using the getSearch(String) method.
    • Constructor Detail

      • SearchBuilder

        public SearchBuilder()
        For testing only
      • SearchBuilder

        public SearchBuilder​(boolean documentsOnly)
        Used for generating documents for typed access to document fields in Java
      • SearchBuilder

        public SearchBuilder​(com.yahoo.config.application.api.ApplicationPackage app)
        For testing only
      • SearchBuilder

        public SearchBuilder​(RankProfileRegistry rankProfileRegistry)
        For testing only
      • SearchBuilder

        public SearchBuilder​(RankProfileRegistry rankProfileRegistry,
                             com.yahoo.search.query.profile.QueryProfileRegistry queryProfileRegistry)
        For testing only
      • SearchBuilder

        public SearchBuilder​(com.yahoo.config.application.api.ApplicationPackage app,
                             RankProfileRegistry rankProfileRegistry,
                             com.yahoo.search.query.profile.QueryProfileRegistry queryProfileRegistry)
      • SearchBuilder

        public SearchBuilder​(com.yahoo.config.application.api.ApplicationPackage app,
                             RankProfileRegistry rankProfileRegistry,
                             com.yahoo.search.query.profile.QueryProfileRegistry queryProfileRegistry,
                             boolean documentsOnly)
    • Method Detail

      • importFile

        public java.lang.String importFile​(java.lang.String fileName,
                                           com.yahoo.config.application.api.DeployLogger deployLogger)
                                    throws java.io.IOException,
                                           ParseException
        Import search definition.
        Parameters:
        fileName - The name of the file to import.
        deployLogger - Logger for deploy messages.
        Returns:
        The name of the imported object.
        Throws:
        java.io.IOException - Thrown if the file can not be read for some reason.
        ParseException - Thrown if the file does not contain a valid search definition. ```
      • importFile

        public java.lang.String importFile​(java.lang.String fileName)
                                    throws java.io.IOException,
                                           ParseException
        Import search definition.
        Parameters:
        fileName - The name of the file to import.
        Returns:
        The name of the imported object.
        Throws:
        java.io.IOException - Thrown if the file can not be read for some reason.
        ParseException - Thrown if the file does not contain a valid search definition.
      • importFile

        public java.lang.String importFile​(java.nio.file.Path file)
                                    throws java.io.IOException,
                                           ParseException
        Throws:
        java.io.IOException
        ParseException
      • importReader

        public java.lang.String importReader​(com.yahoo.io.reader.NamedReader reader,
                                             java.lang.String searchDefDir,
                                             com.yahoo.config.application.api.DeployLogger deployLogger)
                                      throws java.io.IOException,
                                             ParseException
        Reads and parses the search definition string provided by the given reader. Once all search definitions have been imported, call build().
        Parameters:
        reader - The reader whose content to import.
        searchDefDir - The path to use when resolving file references.
        Returns:
        The name of the imported object.
        Throws:
        ParseException - Thrown if the file does not contain a valid search definition.
        java.io.IOException
      • importString

        public java.lang.String importString​(java.lang.String str)
                                      throws ParseException
        Import search definition.
        Parameters:
        str - the string to parse.
        Returns:
        the name of the imported object.
        Throws:
        ParseException - thrown if the file does not contain a valid search definition.
      • importString

        public java.lang.String importString​(java.lang.String str,
                                             com.yahoo.config.application.api.DeployLogger logger)
                                      throws ParseException
        Import search definition.
        Parameters:
        str - the string to parse.
        Returns:
        the name of the imported object.
        Throws:
        ParseException - thrown if the file does not contain a valid search definition.
      • importRawSearch

        public java.lang.String importRawSearch​(Search rawSearch)
        Registers the given search object to the internal list of objects to be processed during build(). A Search object is considered to be "raw" if it has not already been processed. This is the case for most programmatically constructed search objects used in unit tests.
        Parameters:
        rawSearch - the object to import.
        Returns:
        the name of the imported object.
        Throws:
        java.lang.IllegalArgumentException - if the given search object has already been processed.
      • build

        public void build()
        Only for testing. Processes and finalizes the imported search definitions so that they become available through the getSearch(String) method.
        Throws:
        java.lang.IllegalStateException - Thrown if this method has already been called.
      • build

        public void build​(boolean validate,
                          com.yahoo.config.application.api.DeployLogger deployLogger)
        Processes and finalizes the imported search definitions so that they become available through the getSearch(String) method.
        Parameters:
        deployLogger - The logger to use during build
        Throws:
        java.lang.IllegalStateException - Thrown if this method has already been called.
      • process

        protected void process​(Search search,
                               com.yahoo.config.application.api.DeployLogger deployLogger,
                               QueryProfiles queryProfiles,
                               boolean validate)
        Processes and returns the given Search object. This method has been factored out of the build() method so that subclasses can choose not to build anything.
      • getSearch

        public Search getSearch()
        Convenience method to call getSearch(String) when there is only a single Search object built. This method will never return null.
        Returns:
        the built object
        Throws:
        java.lang.IllegalStateException - if there is not exactly one search.
      • getSearch

        public Search getSearch​(java.lang.String name)
        Returns the built Search object that has the given name. If the name is unknown, this method will simply return null.
        Parameters:
        name - the name of the search definition to return, or null to return the only one or throw an exception if there are multiple to choose from
        Returns:
        the built object, or null if none with this name
        Throws:
        java.lang.IllegalStateException - if build() has not been called.
      • getSearchList

        public java.util.List<Search> getSearchList()
        Convenience method to return a list of all built Search objects.
        Returns:
        The list of built searches.
      • createFromString

        public static SearchBuilder createFromString​(java.lang.String sd)
                                              throws ParseException
        Convenience factory method to import and build a Search object from a string.
        Parameters:
        sd - The string to build from.
        Returns:
        The built SearchBuilder object.
        Throws:
        ParseException - Thrown if there was a problem parsing the string.
      • createFromFile

        public static SearchBuilder createFromFile​(java.lang.String fileName)
                                            throws java.io.IOException,
                                                   ParseException
        Convenience factory method to import and build a Search object from a file. Only for testing.
        Parameters:
        fileName - the file to build from
        Returns:
        the built SearchBuilder object
        Throws:
        java.io.IOException - if there was a problem reading the file.
        ParseException - if there was a problem parsing the file content.
      • createFromFiles

        public static SearchBuilder createFromFiles​(java.util.Collection<java.lang.String> fileNames)
                                             throws java.io.IOException,
                                                    ParseException
        Convenience factory methdd to create a SearchBuilder from multiple SD files. Only for testing.
        Throws:
        java.io.IOException
        ParseException
      • createFromFile

        public static SearchBuilder createFromFile​(java.lang.String fileName,
                                                   com.yahoo.config.application.api.DeployLogger logger)
                                            throws java.io.IOException,
                                                   ParseException
        Throws:
        java.io.IOException
        ParseException
      • createFromFiles

        public static SearchBuilder createFromFiles​(java.util.Collection<java.lang.String> fileNames,
                                                    com.yahoo.config.application.api.DeployLogger logger)
                                             throws java.io.IOException,
                                                    ParseException
        Throws:
        java.io.IOException
        ParseException
      • createFromFile

        public static SearchBuilder createFromFile​(java.lang.String fileName,
                                                   com.yahoo.config.application.api.DeployLogger deployLogger,
                                                   RankProfileRegistry rankProfileRegistry,
                                                   com.yahoo.search.query.profile.QueryProfileRegistry queryprofileRegistry)
                                            throws java.io.IOException,
                                                   ParseException
        Convenience factory method to import and build a Search object from a file.
        Parameters:
        fileName - the file to build from.
        deployLogger - logger for deploy messages.
        rankProfileRegistry - registry for rank profiles.
        Returns:
        the built SearchBuilder object.
        Throws:
        java.io.IOException - if there was a problem reading the file.
        ParseException - if there was a problem parsing the file content.
      • createFromFiles

        public static SearchBuilder createFromFiles​(java.util.Collection<java.lang.String> fileNames,
                                                    com.yahoo.config.application.api.DeployLogger deployLogger,
                                                    RankProfileRegistry rankProfileRegistry,
                                                    com.yahoo.search.query.profile.QueryProfileRegistry queryprofileRegistry)
                                             throws java.io.IOException,
                                                    ParseException
        Convenience factory methdd to create a SearchBuilder from multiple SD files..
        Throws:
        java.io.IOException
        ParseException
      • createFromDirectory

        public static SearchBuilder createFromDirectory​(java.lang.String dir,
                                                        RankProfileRegistry rankProfileRegistry,
                                                        com.yahoo.search.query.profile.QueryProfileRegistry queryProfileRegistry)
                                                 throws java.io.IOException,
                                                        ParseException
        Throws:
        java.io.IOException
        ParseException
      • buildFromFile

        public static Search buildFromFile​(java.lang.String fileName)
                                    throws java.io.IOException,
                                           ParseException
        Convenience factory method to import and build a Search object from a file. Only for testing.
        Parameters:
        fileName - The file to build from.
        Returns:
        The built Search object.
        Throws:
        java.io.IOException - Thrown if there was a problem reading the file.
        ParseException - Thrown if there was a problem parsing the file content.
      • buildFromFile

        public static Search buildFromFile​(java.lang.String fileName,
                                           RankProfileRegistry rankProfileRegistry,
                                           com.yahoo.search.query.profile.QueryProfileRegistry queryProfileRegistry)
                                    throws java.io.IOException,
                                           ParseException
        Convenience factory method to import and build a Search object from a file.
        Parameters:
        fileName - The file to build from.
        rankProfileRegistry - Registry for rank profiles.
        Returns:
        The built Search object.
        Throws:
        java.io.IOException - Thrown if there was a problem reading the file.
        ParseException - Thrown if there was a problem parsing the file content.
      • buildFromFile

        public static Search buildFromFile​(java.lang.String fileName,
                                           com.yahoo.config.application.api.DeployLogger deployLogger,
                                           RankProfileRegistry rankProfileRegistry,
                                           com.yahoo.search.query.profile.QueryProfileRegistry queryProfileRegistry)
                                    throws java.io.IOException,
                                           ParseException
        Convenience factory method to import and build a Search object from a file.
        Parameters:
        fileName - The file to build from.
        deployLogger - Logger for deploy messages.
        rankProfileRegistry - Registry for rank profiles.
        Returns:
        The built Search object.
        Throws:
        java.io.IOException - Thrown if there was a problem reading the file.
        ParseException - Thrown if there was a problem parsing the file content.
      • createFromRawSearch

        public static SearchBuilder createFromRawSearch​(Search rawSearch,
                                                        RankProfileRegistry rankProfileRegistry,
                                                        com.yahoo.search.query.profile.QueryProfileRegistry queryProfileRegistry)
        Convenience factory method to import and build a Search object from a raw object.
        Parameters:
        rawSearch - the raw object to build from.
        Returns:
        the built SearchBuilder object.
        See Also:
        importRawSearch(Search)
      • buildFromRawSearch

        public static Search buildFromRawSearch​(Search rawSearch,
                                                RankProfileRegistry rankProfileRegistry,
                                                com.yahoo.search.query.profile.QueryProfileRegistry queryProfileRegistry)
        Convenience factory method to import and build a Search object from a raw object.
        Parameters:
        rawSearch - The raw object to build from.
        Returns:
        The built Search object.
        See Also:
        importRawSearch(Search)
      • getQueryProfileRegistry

        public com.yahoo.search.query.profile.QueryProfileRegistry getQueryProfileRegistry()