Class QueryStringBuilder

java.lang.Object
org.codelibs.fess.util.QueryStringBuilder

public class QueryStringBuilder extends Object
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 Details

    • QueryStringBuilder

      public QueryStringBuilder()
      Default constructor for QueryStringBuilder. Initializes a new instance with default settings for escape and sortField.
  • Method Details

    • quote

      protected String quote(String value)
      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

      protected String escapeQuery(String value)
      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

      public String 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

      protected void appendQuery(StringBuilder queryBuf, String query)
      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 to
      query - the query string to append
    • buildBaseQuery

      protected String 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

      protected void appendConditions(StringBuilder queryBuf, Map<String,String[]> conditions)
      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 to
      conditions - a map of condition types to their values
    • isOccurrence

      protected boolean isOccurrence(String value)
      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

      protected String escape(String q, String... values)
      Escapes specific characters in a query string. Replaces each specified character with its escaped version (prefixed with backslash).
      Parameters:
      q - the query string to escape
      values - the characters to escape
      Returns:
      the escaped query string
    • params

      public QueryStringBuilder params(SearchRequestParams 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

      public QueryStringBuilder sortField(String 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

      public QueryStringBuilder escape(boolean 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