Package org.codelibs.fess.util
Class QueryStringBuilder
java.lang.Object
org.codelibs.fess.util.QueryStringBuilder
A utility class for building query strings with proper escaping and parameters.
This class provides methods to construct search queries from various parameters,
handle special characters, and format the final query string for search operations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidappendConditions(StringBuilder queryBuf, Map<String, String[]> conditions) Appends various search conditions to the query buffer.protected voidappendQuery(StringBuilder queryBuf, String query) Appends a query string to the query buffer with proper formatting.build()Builds the complete query string from the configured parameters.protected StringBuilds the base query string from search parameters.escape(boolean escape) Sets whether to escape special characters in queries.protected StringEscapes specific characters in a query string.protected StringescapeQuery(String value) Escapes special characters in a query string if escaping is enabled.protected booleanisOccurrence(String value) Checks if a value represents an occurrence-based search modifier.params(SearchRequestParams params) Sets the search request parameters for this builder.protected StringQuotes a string value if it contains spaces.Sets the sort field for the query.
-
Constructor Details
-
QueryStringBuilder
public QueryStringBuilder()Default constructor for QueryStringBuilder. Initializes a new instance with default settings for escape and sortField.
-
-
Method Details
-
quote
Quotes a string value if it contains spaces. Multi-word values are wrapped in double quotes with internal quotes replaced by spaces.- Parameters:
value- the string value to quote- Returns:
- the quoted string if it contains spaces, otherwise the original value
-
escapeQuery
Escapes special characters in a query string if escaping is enabled. Replaces reserved characters with their escaped equivalents based on the Constants.RESERVED array.- Parameters:
value- the query string to escape- Returns:
- the escaped query string, or the original value if escaping is disabled
-
build
Builds the complete query string from the configured parameters. Combines base query, extra queries, field filters, and sort field into a single query string.- Returns:
- the complete formatted query string
-
appendQuery
Appends a query string to the query buffer with proper formatting. Handles OR operators and wraps complex queries in parentheses when necessary.- Parameters:
queryBuf- the StringBuilder to append toquery- the query string to append
-
buildBaseQuery
Builds the base query string from search parameters. Handles both condition-based queries and simple text queries, including related query expansion.- Returns:
- the base query string
-
appendConditions
Appends various search conditions to the query buffer. Processes advanced search parameters like occurrence, phrases, OR queries, NOT queries, file types, site searches, and timestamp filters.- Parameters:
queryBuf- the StringBuilder to append conditions toconditions- a map of condition types to their values
-
isOccurrence
Checks if a value represents an occurrence-based search modifier. Currently supports "allintitle" and "allinurl" modifiers.- Parameters:
value- the value to check- Returns:
- true if the value is an occurrence modifier, false otherwise
-
escape
Escapes specific characters in a query string. Replaces each specified character with its escaped version (prefixed with backslash).- Parameters:
q- the query string to escapevalues- the characters to escape- Returns:
- the escaped query string
-
params
Sets the search request parameters for this builder. This method follows the builder pattern for method chaining.- Parameters:
params- the search request parameters to use- Returns:
- this QueryStringBuilder instance for method chaining
-
sortField
Sets the sort field for the query. This method follows the builder pattern for method chaining.- Parameters:
sortField- the field name to sort by- Returns:
- this QueryStringBuilder instance for method chaining
-
escape
Sets whether to escape special characters in queries. This method follows the builder pattern for method chaining.- Parameters:
escape- true to enable escaping, false to disable- Returns:
- this QueryStringBuilder instance for method chaining
-