Class QueryHelper

java.lang.Object
org.codelibs.fess.helper.QueryHelper

public class QueryHelper extends Object
QueryHelper is responsible for building and managing OpenSearch queries for Fess search functionality. It handles query construction, role-based access control, boost functions, sorting, and search preferences. This class serves as the central component for translating user search requests into properly formatted OpenSearch queries with appropriate filters and scoring mechanisms.
  • Field Details

    • PREFERENCE_QUERY

      protected static final String PREFERENCE_QUERY
      Constant used to indicate that query-based preference should be used for search routing
      See Also:
    • sortPrefix

      protected String sortPrefix
      Prefix used to identify sort parameters in search queries
    • additionalQuery

      protected String additionalQuery
      Additional query string to be appended to all search queries
    • defaultSortBuilders

      protected org.opensearch.search.sort.SortBuilder<?>[] defaultSortBuilders
      Default sort builders to be applied when no explicit sorting is specified
    • highlightPrefix

      protected String highlightPrefix
      Prefix used for highlight field names in search results
    • defaultFacetInfo

      protected FacetInfo defaultFacetInfo
      Default facet information configuration for search results
    • defaultGeoInfo

      protected GeoInfo defaultGeoInfo
      Default geographic information configuration for location-based searches
    • fieldBoostMap

      protected Map<String,String> fieldBoostMap
      Map containing field-specific boost values for search scoring
    • boostFunctionList

      protected List<org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder> boostFunctionList
      List of boost functions to be applied to search queries for custom scoring
    • queryRescorerList

      protected List<QueryRescorer> queryRescorerList
      List of query rescorers for post-processing search results
  • Constructor Details

    • QueryHelper

      public QueryHelper()
      Default constructor.
  • Method Details

    • build

      public QueryContext build(SearchRequestParams.SearchRequestType searchRequestType, String query, Consumer<QueryContext> context)
      Builds a complete QueryContext for search operations, applying all necessary filters, boosts, and role-based access controls.
      Parameters:
      searchRequestType - the type of search request (e.g., regular search, admin search)
      query - the user's search query string
      context - a consumer that allows additional customization of the query context
      Returns:
      a fully constructed QueryContext ready for OpenSearch execution
    • buildVirtualHostQuery

      protected void buildVirtualHostQuery(QueryContext queryContext, SearchRequestParams.SearchRequestType searchRequestType)
      Builds virtual host query filters to restrict search results to the current virtual host. This method adds filters based on the virtual host key, except for admin searches.
      Parameters:
      queryContext - the query context to modify
      searchRequestType - the type of search request to determine if virtual host filtering should be applied
    • buildRoleQuery

      protected void buildRoleQuery(QueryContext queryContext, SearchRequestParams.SearchRequestType searchRequestType)
      Builds role-based access control query filters to restrict search results based on user roles. This method applies role-based filtering to ensure users only see documents they have access to.
      Parameters:
      queryContext - the query context to modify
      searchRequestType - the type of search request to determine role filtering requirements
    • buildRoleQuery

      public void buildRoleQuery(Set<String> roleSet, org.opensearch.index.query.BoolQueryBuilder boolQuery)
      Builds role-based query filters using the provided role set. This method adds should clauses for allowed roles and must-not clauses for denied roles.
      Parameters:
      roleSet - the set of roles to use for filtering
      boolQuery - the boolean query builder to add role filters to
    • buildBoostQuery

      protected void buildBoostQuery(QueryContext queryContext)
      Builds boost query functions to modify document scoring based on various factors. This method adds field value factors, key matching boosts, and custom boost functions.
      Parameters:
      queryContext - the query context to add boost functions to
    • buildBaseQuery

      public void buildBaseQuery(QueryContext queryContext, Consumer<QueryContext> context)
      Builds the base query from the user's search string using the configured query parser. This method parses the query string, processes it, and applies any additional customizations.
      Parameters:
      queryContext - the query context containing the query string
      context - a consumer for additional query context customization
      Throws:
      InvalidQueryException - if the query string cannot be parsed
    • getQueryParser

      protected QueryParser getQueryParser()
      Gets the query parser instance for parsing search query strings.
      Returns:
      the configured query parser
    • processSearchPreference

      public void processSearchPreference(org.opensearch.action.search.SearchRequestBuilder searchRequestBuilder, org.dbflute.optional.OptionalThing<org.codelibs.fess.mylasta.action.FessUserBean> userBean, String query)
      Processes and sets search preferences for routing search requests to appropriate OpenSearch shards. This method determines the preference value based on user roles, session information, or request parameters.
      Parameters:
      searchRequestBuilder - the search request builder to configure
      userBean - the optional user bean containing user information
      query - the search query string
    • processJsonSearchPreference

      protected String processJsonSearchPreference(jakarta.servlet.http.HttpServletRequest req, String query)
      Processes search preferences specifically for JSON API requests. This method determines the preference value based on configuration and query content.
      Parameters:
      req - the HTTP servlet request
      query - the search query string
      Returns:
      the preference value for JSON search requests, or null if not applicable
    • processGsaSearchPreference

      protected String processGsaSearchPreference(jakarta.servlet.http.HttpServletRequest req, String query)
      Processes search preferences specifically for GSA (Google Search Appliance) compatible requests. This method determines the preference value based on configuration and query content.
      Parameters:
      req - the HTTP servlet request
      query - the search query string
      Returns:
      the preference value for GSA search requests, or null if not applicable
    • getSortPrefix

      public String getSortPrefix()
      Gets the sort prefix used for identifying sort parameters in search queries.
      Returns:
      the sortPrefix
    • setSortPrefix

      public void setSortPrefix(String sortPrefix)
      Sets the sort prefix used for identifying sort parameters in search queries.
      Parameters:
      sortPrefix - the sortPrefix to set
    • getAdditionalQuery

      public String getAdditionalQuery()
      Gets the additional query string that is appended to all search queries.
      Returns:
      the additionalQuery
    • setAdditionalQuery

      public void setAdditionalQuery(String additionalQuery)
      Sets the additional query string that is appended to all search queries.
      Parameters:
      additionalQuery - the additionalQuery to set
    • addDefaultSort

      public void addDefaultSort(String fieldName, String order)
      Adds a default sort configuration to be applied when no explicit sorting is specified. This method appends the new sort to existing default sorts.
      Parameters:
      fieldName - the field name to sort by
      order - the sort order ("ASC" or "DESC")
    • createFieldSortBuilder

      protected org.opensearch.search.sort.SortBuilder<?> createFieldSortBuilder(String field, org.opensearch.search.sort.SortOrder order)
      Creates a sort builder for the specified field and order. This method handles special cases for score fields and regular field sorting.
      Parameters:
      field - the field name to sort by
      order - the sort order (ASC or DESC)
      Returns:
      a configured sort builder
    • setHighlightPrefix

      public void setHighlightPrefix(String highlightPrefix)
      Sets the prefix used for highlight field names in search results.
      Parameters:
      highlightPrefix - the prefix string to use for highlight fields
    • getHighlightPrefix

      public String getHighlightPrefix()
      Gets the current highlight prefix used for highlight field names.
      Returns:
      the current highlight prefix
    • getDefaultFacetInfo

      public FacetInfo getDefaultFacetInfo()
      Gets the default facet information configuration.
      Returns:
      the default facet information, or null if not configured
    • setDefaultFacetInfo

      public void setDefaultFacetInfo(FacetInfo defaultFacetInfo)
      Sets the default facet information configuration for search results.
      Parameters:
      defaultFacetInfo - the facet information to use as default
    • getDefaultGeoInfo

      public GeoInfo getDefaultGeoInfo()
      Gets the default geographic information configuration.
      Returns:
      the default geographic information, or null if not configured
    • setDefaultGeoInfo

      public void setDefaultGeoInfo(GeoInfo defaultGeoInfo)
      Sets the default geographic information configuration for location-based searches.
      Parameters:
      defaultGeoInfo - the geographic information to use as default
    • generateId

      public String generateId()
      Generates a unique identifier string by creating a UUID and removing hyphens.
      Returns:
      a unique identifier string
    • addBoostFunction

      public void addBoostFunction(org.opensearch.index.query.functionscore.ScoreFunctionBuilder<?> scoreFunction)
      Adds a boost function to modify document scoring during search. This method adds a boost function that applies to all documents.
      Parameters:
      scoreFunction - the score function to add for boosting
    • addBoostFunction

      public void addBoostFunction(org.opensearch.index.query.QueryBuilder filter, org.opensearch.index.query.functionscore.ScoreFunctionBuilder<?> scoreFunction)
      Adds a boost function with a filter to modify document scoring during search. This method adds a boost function that applies only to documents matching the filter.
      Parameters:
      filter - the query filter to determine which documents the boost applies to
      scoreFunction - the score function to add for boosting
    • getRescorers

      public org.opensearch.search.rescore.RescorerBuilder<?>[] getRescorers(Map<String,Object> params)
      Gets an array of rescorer builders for post-processing search results. This method evaluates all configured query rescorers with the provided parameters.
      Parameters:
      params - parameters to pass to the rescorers during evaluation
      Returns:
      an array of rescorer builders, filtered to exclude null values
    • addQueryRescorer

      public void addQueryRescorer(QueryRescorer rescorer)
      Adds a query rescorer for post-processing search results. Query rescorers allow modification of search scores after the initial query execution.
      Parameters:
      rescorer - the query rescorer to add