Package org.codelibs.fess.helper
Class QueryHelper
java.lang.Object
org.codelibs.fess.helper.QueryHelper
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 Summary
FieldsModifier and TypeFieldDescriptionprotected StringAdditional query string to be appended to all search queriesprotected List<org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder> List of boost functions to be applied to search queries for custom scoringprotected FacetInfoDefault facet information configuration for search resultsprotected GeoInfoDefault geographic information configuration for location-based searchesprotected org.opensearch.search.sort.SortBuilder<?>[]Default sort builders to be applied when no explicit sorting is specifiedMap containing field-specific boost values for search scoringprotected StringPrefix used for highlight field names in search resultsprotected static final StringConstant used to indicate that query-based preference should be used for search routingprotected List<QueryRescorer> List of query rescorers for post-processing search resultsprotected StringPrefix used to identify sort parameters in search queries -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBoostFunction(org.opensearch.index.query.functionscore.ScoreFunctionBuilder<?> scoreFunction) Adds a boost function to modify document scoring during search.voidaddBoostFunction(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.voidaddDefaultSort(String fieldName, String order) Adds a default sort configuration to be applied when no explicit sorting is specified.voidaddQueryRescorer(QueryRescorer rescorer) Adds a query rescorer for post-processing search results.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.voidbuildBaseQuery(QueryContext queryContext, Consumer<QueryContext> context) Builds the base query from the user's search string using the configured query parser.protected voidbuildBoostQuery(QueryContext queryContext) Builds boost query functions to modify document scoring based on various factors.voidbuildRoleQuery(Set<String> roleSet, org.opensearch.index.query.BoolQueryBuilder boolQuery) Builds role-based query filters using the provided role set.protected voidbuildRoleQuery(QueryContext queryContext, SearchRequestParams.SearchRequestType searchRequestType) Builds role-based access control query filters to restrict search results based on user roles.protected voidbuildVirtualHostQuery(QueryContext queryContext, SearchRequestParams.SearchRequestType searchRequestType) Builds virtual host query filters to restrict search results to the current virtual host.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.Generates a unique identifier string by creating a UUID and removing hyphens.Gets the additional query string that is appended to all search queries.Gets the default facet information configuration.Gets the default geographic information configuration.Gets the current highlight prefix used for highlight field names.protected QueryParserGets the query parser instance for parsing search query strings.org.opensearch.search.rescore.RescorerBuilder<?>[]getRescorers(Map<String, Object> params) Gets an array of rescorer builders for post-processing search results.Gets the sort prefix used for identifying sort parameters in search queries.protected StringprocessGsaSearchPreference(jakarta.servlet.http.HttpServletRequest req, String query) Processes search preferences specifically for GSA (Google Search Appliance) compatible requests.protected StringprocessJsonSearchPreference(jakarta.servlet.http.HttpServletRequest req, String query) Processes search preferences specifically for JSON API requests.voidprocessSearchPreference(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.voidsetAdditionalQuery(String additionalQuery) Sets the additional query string that is appended to all search queries.voidsetDefaultFacetInfo(FacetInfo defaultFacetInfo) Sets the default facet information configuration for search results.voidsetDefaultGeoInfo(GeoInfo defaultGeoInfo) Sets the default geographic information configuration for location-based searches.voidsetHighlightPrefix(String highlightPrefix) Sets the prefix used for highlight field names in search results.voidsetSortPrefix(String sortPrefix) Sets the sort prefix used for identifying sort parameters in search queries.
-
Field Details
-
PREFERENCE_QUERY
Constant used to indicate that query-based preference should be used for search routing- See Also:
-
sortPrefix
Prefix used to identify sort parameters in search queries -
additionalQuery
Additional query string to be appended to all search queries -
defaultSortBuilders
protected org.opensearch.search.sort.SortBuilder<?>[] defaultSortBuildersDefault sort builders to be applied when no explicit sorting is specified -
highlightPrefix
Prefix used for highlight field names in search results -
defaultFacetInfo
Default facet information configuration for search results -
defaultGeoInfo
Default geographic information configuration for location-based searches -
fieldBoostMap
Map containing field-specific boost values for search scoring -
boostFunctionList
protected List<org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder> boostFunctionListList of boost functions to be applied to search queries for custom scoring -
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 stringcontext- 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 modifysearchRequestType- 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 modifysearchRequestType- 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 filteringboolQuery- the boolean query builder to add role filters to
-
buildBoostQuery
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
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 stringcontext- a consumer for additional query context customization- Throws:
InvalidQueryException- if the query string cannot be parsed
-
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 configureuserBean- the optional user bean containing user informationquery- 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 requestquery- 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 requestquery- the search query string- Returns:
- the preference value for GSA search requests, or null if not applicable
-
getSortPrefix
Gets the sort prefix used for identifying sort parameters in search queries.- Returns:
- the sortPrefix
-
setSortPrefix
Sets the sort prefix used for identifying sort parameters in search queries.- Parameters:
sortPrefix- the sortPrefix to set
-
getAdditionalQuery
Gets the additional query string that is appended to all search queries.- Returns:
- the additionalQuery
-
setAdditionalQuery
Sets the additional query string that is appended to all search queries.- Parameters:
additionalQuery- the additionalQuery to set
-
addDefaultSort
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 byorder- 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 byorder- the sort order (ASC or DESC)- Returns:
- a configured sort builder
-
setHighlightPrefix
Sets the prefix used for highlight field names in search results.- Parameters:
highlightPrefix- the prefix string to use for highlight fields
-
getHighlightPrefix
Gets the current highlight prefix used for highlight field names.- Returns:
- the current highlight prefix
-
getDefaultFacetInfo
Gets the default facet information configuration.- Returns:
- the default facet information, or null if not configured
-
setDefaultFacetInfo
Sets the default facet information configuration for search results.- Parameters:
defaultFacetInfo- the facet information to use as default
-
getDefaultGeoInfo
Gets the default geographic information configuration.- Returns:
- the default geographic information, or null if not configured
-
setDefaultGeoInfo
Sets the default geographic information configuration for location-based searches.- Parameters:
defaultGeoInfo- the geographic information to use as default
-
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 toscoreFunction- the score function to add for boosting
-
getRescorers
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
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
-