SearchQueryHandlerFactory extends ListLinkHandlerFactory to provide specialized handling for search queries. It treats the ID field as the search string and creates SearchQueryHandler instances.
Class Overview
- The “ID” represents the search query string
- Content filters represent search categories (e.g., “videos”, “channels”, “playlists”)
- Sort filters represent result ordering (e.g., “relevance”, “upload_date”, “view_count”)
Abstract Methods (Must Override)
Construct a search URL from a query string with filters applied.Parameters:Important: Always URL-encode the query string to handle special characters properly.
query(String) - The search query stringcontentFilter(List<String>) - List of content type filters (e.g., search only videos)sortFilter(String) - Sort order for results (e.g., “relevance”, “date”)
ParsingException- If the URL cannot be constructedUnsupportedOperationException- If URL construction is not supported
Optional Methods
Extract the search query string from a search URL. Override this to enable URL-to-query conversion.Parameters:Note: This method is used by
url(String) - The search URL to parse
getId() to extract the search string from URLs. If you want to support creating handlers from search URLs, you must override this method.Overridden Methods
Extract the search query string from a URL. Delegates to Note: This overrides the parent class to treat the “ID” as the search string.
getSearchString(url).Parameters:url(String) - The search URL
ParsingException- If the query cannot be extractedUnsupportedOperationException- If not supported
Build a SearchQueryHandler from a search query with filters. This is the primary method for creating search handlers.Parameters:
query(String) - The search query stringcontentFilter(List<String>) - Content type filters for the searchsortFilter(String) - Sort order for results
ParsingException- If the search URL cannot be constructed
- Calls parent
ListLinkHandlerFactory.fromQuery() - Wraps the result in a new SearchQueryHandler
Build a SearchQueryHandler from a search query without filters.Parameters:
query(String) - The search query string
ParsingException- If the search URL cannot be constructed
- Calls
fromQuery(query, Collections.emptyList(), "") - Creates handler with no content or sort filters
Determine if this factory can handle the given URL. Default implementation returns false.Parameters:Rationale: Search URL handling is optional for NewPipe. Services may choose not to support extracting search queries from URLs.Override this if you want to support creating handlers from search URLs:
url(String) - The URL to validate
false by defaultDefault behavior:Inherited Methods
SearchQueryHandlerFactory inherits all methods fromListLinkHandlerFactory:
fromUrl(String url)- Parse a search URL (requiresonAcceptUrl()andgetSearchString()overrides)fromUrl(String url, String baseUrl)- Parse with base URLfromId(String id)- Create handler from search string (same as query)fromId(String id, String baseUrl)- Create with base URLgetAvailableContentFilter()- Return available search category filtersgetAvailableSortFilter()- Return available sort options
SearchQueryHandler Class
TheSearchQueryHandler class extends ListLinkHandler with search-specific semantics:
Immutable data class for search queries. The ID field represents the search string.Constructors:Inherited Fields:
originalUrl(String) - The original URLurl(String) - The normalized search URLid(String) - The search query stringcontentFilters(List<String>) - Search category filterssortFilter(String) - Result sort order
- All methods from ListLinkHandler:
getUrl(),getId(),getContentFilters(),getSortFilter() String getSearchString()- Returns the search query (equivalent togetId())
id field holds the search string, and getSearchString() is an alias for getId() that makes the intent clearer.Implementation Example
Usage Example
Search Filter Design
Content Filters for Search
Content filters in search context typically represent:- Content types: “videos”, “channels”, “playlists”
- Categories: “music”, “gaming”, “news”
- Attributes: “hd”, “4k”, “creative_commons”
- Duration: “short”, “medium”, “long”
- Features: “live”, “subtitles”, “3d”
Sort Filters for Search
Sort filters for search typically include:- relevance - Default, most relevant results
- upload_date / date - Newest first
- view_count / views - Most viewed
- rating - Highest rated
Special Considerations
URL Encoding
Always URL-encode search queries to handle:- Spaces and special characters
- Non-ASCII characters (Unicode)
- Reserved URL characters (&, =, ?, etc.)
Empty Query Handling
Decide how to handle empty search strings:- Throw
ParsingExceptionfor invalid input - Return a “trending” or “home” page
- Return an empty results URL
URL-to-Query Extraction
getSearchString() is optional but recommended:
- Enables creating handlers from search URLs
- Useful for handling shared search links
- Required if you want
acceptUrl()to return true for search URLs
Best Practices
- Always URL-encode queries: Use
URLEncoder.encode()ingetUrl() - URL-decode in extraction: Use
URLDecoder.decode()ingetSearchString() - Handle encoding errors: Wrap encoding operations in try-catch
- Document filter behavior: Clearly explain what each filter does
- Test special characters: Ensure queries with quotes, symbols work correctly
- Consider empty queries: Define behavior for empty search strings
- Match service behavior: Filters should match what the service actually supports
Common Pitfalls
- Forgetting URL encoding: Will fail with special characters
- Not handling multiple filters: Services may support multiple simultaneous filters
- Incorrect parameter format: Each service has unique URL parameter schemes
- Assuming URL support: Not all services need to support
fromUrl()for searches - Hard-coding base URL: Consider using the baseUrl parameter for flexibility
See Also
- Link Handler System Overview
- LinkHandlerFactory - Base factory
- ListLinkHandlerFactory - Parent class