Package org.ametys.cms.search.solr
Class SearcherFactory.Searcher
- java.lang.Object
-
- org.ametys.cms.search.solr.SearcherFactory.Searcher
-
- Direct Known Subclasses:
FrontOfficeSearcherFactory.FrontOfficeSearcher
- Enclosing class:
- SearcherFactory
public class SearcherFactory.Searcher extends Object
Class searching objects corresponding to a query, with optional sort, facets, and so on.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean_checkRightsprivate AllowedUsers_checkRightsComparingToprivate boolean_debugprivate List<SearchField>_facetsprivate Map<String,List<String>>_facetValuesprivate List<Query>_filterQueriesprivate List<String>_filterQueryStringsprivate org.slf4j.Logger_loggerprivate int_maxResultsprivate Query_queryprivate String_queryStringprivate Map<String,Object>_searchContextprivate List<Sort>_sortClausesprivate int_start
-
Constructor Summary
Constructors Modifier Constructor Description protectedSearcher(org.slf4j.Logger logger)Build a Searcher with default values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <A extends AmetysObject>
SearchResults<A>_buildResults(org.apache.solr.client.solrj.response.QueryResponse response, List<SearchField> facets)From the Solr server response, builds theSearchResultsobject.private void_checkAllowedUsers(org.apache.solr.client.solrj.SolrQuery solrQuery, AllowedUsers allowedUsersToCompare)private void_checkRightsQuery(org.apache.solr.client.solrj.SolrQuery solrQuery)private void_handleDebug(org.apache.solr.client.solrj.response.QueryResponse response)private void_logSearchQueries()private org.apache.solr.client.solrj.response.QueryResponse_querySolrServer()private void_setJoinedFacet(org.apache.solr.client.solrj.SolrQuery solrQuery, String fieldName, List<String> joinedPaths, String solrFieldName, String facetFunction, Map<String,List<String>> facetValues)private void_setNonJoinedFacet(org.apache.solr.client.solrj.SolrQuery solrQuery, String fieldName, String solrFieldName, Map<String,List<String>> facetValues)SearcherFactory.SearcheraddContextElement(String key, Object value)Add a value to the search context.SearcherFactory.SearcheraddFacet(SearchField facet)Add a faceted field.SearcherFactory.SearcheraddFilterQuery(Query query)Add a filter query to the existing ones (as aQueryobject).SearcherFactory.SearcheraddFilterQueryString(String query)Add a filter query to the existing ones (as a String).SearcherFactory.SearcheraddSort(Sort sortClause)Add a sort clause to the existing ones.SearcherFactory.SearchercheckRightsComparingTo(AllowedUsers compareTo)Check rights when searching, not according to the current user, but according to the givenvisibiltyto compare each result with.protected Map<String,Map<String,Integer>>getFacetResults(org.apache.solr.client.solrj.response.QueryResponse response, Collection<SearchField> facets)Retrieve the facet results from the solr response.protected List<String>getFilterQueries()Get the filter queries from the parameters.protected StringgetQuery()Get the query string from the parameters.protected org.apache.solr.client.solrj.SolrQuerygetSolrQuery(String query, Collection<String> filterQueries, int start, int maxResults, Map<String,Object> searchContext, boolean checkRights, AllowedUsers allowedUsersToCompare)Get the solr query object.protected voidmodifySolrQuery(org.apache.solr.client.solrj.SolrQuery query)Template method to do additional operations on the Solr query before passing it to the Solr client<A extends AmetysObject>
AmetysObjectIterable<A>search()Execute the search with the current parameters.<A extends AmetysObject>
SearchResults<A>searchWithFacets()Execute the search with the current parameters.SearcherFactory.SearchersetCheckRights(boolean checkRights)Whether to check rights when searching, false otherwise.SearcherFactory.SearchersetDebugOn()Sets the debug on the Solr queryprotected voidsetFacets(org.apache.solr.client.solrj.SolrQuery solrQuery, Collection<SearchField> facets, Map<String,List<String>> facetValues)Set the facet definition in the solr query object and return a mapping from solr field name to criterion ID.protected voidsetSort(org.apache.solr.client.solrj.SolrQuery solrQuery, List<Sort> sortCriteria)Set the sort definition in the solr query object.SearcherFactory.SearcherwithContext(Map<String,Object> searchContext)Set the search context.SearcherFactory.SearcherwithFacets(Collection<SearchField> facets)Set the faceted fields.SearcherFactory.SearcherwithFacets(SearchField... facets)Set the faceted fields.SearcherFactory.SearcherwithFacetValues(Map<String,List<String>> facetValues)Set the facet values.SearcherFactory.SearcherwithFilterQueries(Collection<Query> queries)Set the filter queries (asQueryobjects).SearcherFactory.SearcherwithFilterQueries(Query... queries)Set the filter queries (asQueryobjects).SearcherFactory.SearcherwithFilterQueryStrings(String... queries)Set the filter queries (as Strings).SearcherFactory.SearcherwithFilterQueryStrings(Collection<String> queries)Set the filter queries (as Strings).SearcherFactory.SearcherwithLimits(int start, int maxResults)Set the search offset and limit.SearcherFactory.SearcherwithQuery(Query query)Set the query (as aQueryobject).SearcherFactory.SearcherwithQueryString(String query)Set the query (as a String).SearcherFactory.SearcherwithSort(List<Sort> sortClauses)Set the sort clauses.SearcherFactory.SearcherwithSort(Sort... sortClauses)Set the sort clauses.
-
-
-
Field Detail
-
_logger
private org.slf4j.Logger _logger
-
_queryString
private String _queryString
-
_filterQueryStrings
private List<String> _filterQueryStrings
-
_filterQueries
private List<Query> _filterQueries
-
_sortClauses
private List<Sort> _sortClauses
-
_facets
private List<SearchField> _facets
-
_facetValues
private Map<String,List<String>> _facetValues
-
_start
private int _start
-
_maxResults
private int _maxResults
-
_searchContext
private Map<String,Object> _searchContext
-
_checkRights
private boolean _checkRights
-
_checkRightsComparingTo
private AllowedUsers _checkRightsComparingTo
-
_debug
private boolean _debug
-
-
Constructor Detail
-
Searcher
protected Searcher(org.slf4j.Logger logger)
Build a Searcher with default values.- Parameters:
logger- The logger.
-
-
Method Detail
-
withQueryString
public SearcherFactory.Searcher withQueryString(String query)
Set the query (as a String).- Parameters:
query- the query (as a String).- Returns:
- The Searcher object itself.
-
withQuery
public SearcherFactory.Searcher withQuery(Query query)
Set the query (as aQueryobject).- Parameters:
query- the query (as aQueryobject).- Returns:
- The Searcher object itself.
-
withFilterQueryStrings
public SearcherFactory.Searcher withFilterQueryStrings(String... queries)
Set the filter queries (as Strings).- Parameters:
queries- the filter queries (as Strings).- Returns:
- The Searcher object itself. The Searcher object itself.
-
withFilterQueryStrings
public SearcherFactory.Searcher withFilterQueryStrings(Collection<String> queries)
Set the filter queries (as Strings).- Parameters:
queries- the filter queries (as Strings).- Returns:
- The Searcher object itself. The Searcher object itself.
-
addFilterQueryString
public SearcherFactory.Searcher addFilterQueryString(String query)
Add a filter query to the existing ones (as a String).- Parameters:
query- the filter query to add (as a String).- Returns:
- The Searcher object itself. The Searcher object itself.
-
withFilterQueries
public SearcherFactory.Searcher withFilterQueries(Query... queries)
Set the filter queries (asQueryobjects).- Parameters:
queries- the filter queries (asQueryobjects).- Returns:
- The Searcher object itself. The Searcher object itself.
-
withFilterQueries
public SearcherFactory.Searcher withFilterQueries(Collection<Query> queries)
Set the filter queries (asQueryobjects).- Parameters:
queries- the filter queries (asQueryobjects).- Returns:
- The Searcher object itself. The Searcher object itself.
-
addFilterQuery
public SearcherFactory.Searcher addFilterQuery(Query query)
Add a filter query to the existing ones (as aQueryobject).- Parameters:
query- the filter query to add (as aQueryobject).- Returns:
- The Searcher object itself. The Searcher object itself.
-
withSort
public SearcherFactory.Searcher withSort(Sort... sortClauses)
Set the sort clauses.- Parameters:
sortClauses- the sort clauses.- Returns:
- The Searcher object itself.
-
withSort
public SearcherFactory.Searcher withSort(List<Sort> sortClauses)
Set the sort clauses.- Parameters:
sortClauses- the sort clauses.- Returns:
- The Searcher object itself.
-
addSort
public SearcherFactory.Searcher addSort(Sort sortClause)
Add a sort clause to the existing ones.- Parameters:
sortClause- The sort clause to add.- Returns:
- The Searcher object itself.
-
withFacets
public SearcherFactory.Searcher withFacets(SearchField... facets)
Set the faceted fields.- Parameters:
facets- the faceted fields.- Returns:
- The Searcher object itself.
-
withFacets
public SearcherFactory.Searcher withFacets(Collection<SearchField> facets)
Set the faceted fields.- Parameters:
facets- the faceted fields.- Returns:
- The Searcher object itself.
-
addFacet
public SearcherFactory.Searcher addFacet(SearchField facet)
Add a faceted field.- Parameters:
facet- The faceted field to add.- Returns:
- The Searcher object itself.
-
withFacetValues
public SearcherFactory.Searcher withFacetValues(Map<String,List<String>> facetValues)
Set the facet values.- Parameters:
facetValues- The facet values.- Returns:
- The Searcher object itself.
-
withLimits
public SearcherFactory.Searcher withLimits(int start, int maxResults)
Set the search offset and limit.- Parameters:
start- The start index (offset).maxResults- The maximum number of results.- Returns:
- The Searcher object itself.
-
withContext
public SearcherFactory.Searcher withContext(Map<String,Object> searchContext)
Set the search context.- Parameters:
searchContext- The search context.- Returns:
- The Searcher object itself.
-
addContextElement
public SearcherFactory.Searcher addContextElement(String key, Object value)
Add a value to the search context.- Parameters:
key- The context key.value- The value.- Returns:
- The Searcher object itself.
-
setCheckRights
public SearcherFactory.Searcher setCheckRights(boolean checkRights)
Whether to check rights when searching, false otherwise.- Parameters:
checkRights-trueto check rights,falseotherwise.- Returns:
- The Searcher object itself.
-
checkRightsComparingTo
public SearcherFactory.Searcher checkRightsComparingTo(AllowedUsers compareTo)
Check rights when searching, not according to the current user, but according to the givenvisibiltyto compare each result with.- Parameters:
compareTo- thevisibiltyto compare each result with.- Returns:
- The Searcher object itself.
-
setDebugOn
public SearcherFactory.Searcher setDebugOn()
Sets the debug on the Solr query- Returns:
- The Searcher object itself.
-
search
public <A extends AmetysObject> AmetysObjectIterable<A> search() throws Exception
Execute the search with the current parameters.- Type Parameters:
A- The type of search results- Returns:
- An iterable on the result ametys objects.
- Throws:
Exception- If an error occurs.
-
searchWithFacets
public <A extends AmetysObject> SearchResults<A> searchWithFacets() throws Exception
Execute the search with the current parameters.- Type Parameters:
A- The type of search results- Returns:
- An iterable on the search result objects.
- Throws:
Exception- If an error occurs.
-
_buildResults
protected <A extends AmetysObject> SearchResults<A> _buildResults(org.apache.solr.client.solrj.response.QueryResponse response, List<SearchField> facets) throws Exception
From the Solr server response, builds theSearchResultsobject.- Type Parameters:
A- The type of search results- Parameters:
response- The response from the Solr serverfacets- The facet fields to return- Returns:
- An iterable on the search result objects.
- Throws:
Exception- If an error occurs.
-
_handleDebug
private void _handleDebug(org.apache.solr.client.solrj.response.QueryResponse response)
-
_querySolrServer
private org.apache.solr.client.solrj.response.QueryResponse _querySolrServer() throws Exception
- Throws:
Exception
-
_logSearchQueries
private void _logSearchQueries()
-
getQuery
protected String getQuery() throws QuerySyntaxException
Get the query string from the parameters.- Returns:
- The query string.
- Throws:
QuerySyntaxException- If the query is invalid.
-
getFilterQueries
protected List<String> getFilterQueries() throws QuerySyntaxException
Get the filter queries from the parameters.- Returns:
- The list of filter queries.
- Throws:
QuerySyntaxException- If one of the queries is invalid.
-
getSolrQuery
protected org.apache.solr.client.solrj.SolrQuery getSolrQuery(String query, Collection<String> filterQueries, int start, int maxResults, Map<String,Object> searchContext, boolean checkRights, AllowedUsers allowedUsersToCompare) throws Exception
Get the solr query object.- Parameters:
query- The solr query string.filterQueries- The filter queries (as Strings).start- The start index.maxResults- The maximum number of results.searchContext- The search context.checkRights- Whether to check rights when searching or not.allowedUsersToCompare- TheAllowedUsersobject to compare with for checking rights- Returns:
- The solr query object.
- Throws:
Exception- If an error occurs.
-
_checkRightsQuery
private void _checkRightsQuery(org.apache.solr.client.solrj.SolrQuery solrQuery)
-
_checkAllowedUsers
private void _checkAllowedUsers(org.apache.solr.client.solrj.SolrQuery solrQuery, AllowedUsers allowedUsersToCompare)
-
setSort
protected void setSort(org.apache.solr.client.solrj.SolrQuery solrQuery, List<Sort> sortCriteria)
Set the sort definition in the solr query object.- Parameters:
solrQuery- The solr query object.sortCriteria- The sort criteria.
-
setFacets
protected void setFacets(org.apache.solr.client.solrj.SolrQuery solrQuery, Collection<SearchField> facets, Map<String,List<String>> facetValues)
Set the facet definition in the solr query object and return a mapping from solr field name to criterion ID.- Parameters:
solrQuery- the solr query object to fill.facets- The facet definitions to use.facetValues- the facet values.
-
_setJoinedFacet
private void _setJoinedFacet(org.apache.solr.client.solrj.SolrQuery solrQuery, String fieldName, List<String> joinedPaths, String solrFieldName, String facetFunction, Map<String,List<String>> facetValues)
-
_setNonJoinedFacet
private void _setNonJoinedFacet(org.apache.solr.client.solrj.SolrQuery solrQuery, String fieldName, String solrFieldName, Map<String,List<String>> facetValues)
-
getFacetResults
protected Map<String,Map<String,Integer>> getFacetResults(org.apache.solr.client.solrj.response.QueryResponse response, Collection<SearchField> facets)
Retrieve the facet results from the solr response.- Parameters:
response- the solr response.facets- The facet fields to return.- Returns:
- the facet results.
-
modifySolrQuery
protected void modifySolrQuery(org.apache.solr.client.solrj.SolrQuery query)
Template method to do additional operations on the Solr query before passing it to the Solr client- Parameters:
query- the Solr query
-
-