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.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Searcher(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 theSearchResults
object.SearcherFactory.Searcher
addContextElement(String key, Object value)
Add a value to the search context.SearcherFactory.Searcher
addFacet(SearchField facet)
Add a faceted field.SearcherFactory.Searcher
addFilterQuery(Query query)
Add a filter query to the existing ones (as aQuery
object).SearcherFactory.Searcher
addFilterQueryString(String query)
Add a filter query to the existing ones (as a String).SearcherFactory.Searcher
addSort(Sort sortClause)
Add a sort clause to the existing ones.SearcherFactory.Searcher
checkRightsComparingTo(AllowedUsers compareTo)
Check rights when searching, not according to the current user, but according to the givenvisibilty
to 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 String
getQuery()
Get the query string from the parameters.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)
Get the solr query object.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<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.Searcher
setCheckRights(boolean checkRights)
Whether to check rights when searching, false otherwise.SearcherFactory.Searcher
setDebugOn()
Sets the debug on the Solr queryprotected 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.protected void
setSort(org.apache.solr.client.solrj.SolrQuery solrQuery, List<Sort> sortCriteria)
Set the sort definition in the solr query object.SearcherFactory.Searcher
withContext(Map<String,Object> searchContext)
Set the search context.SearcherFactory.Searcher
withFacets(Collection<SearchField> facets)
Set the faceted fields.SearcherFactory.Searcher
withFacets(SearchField... facets)
Set the faceted fields.SearcherFactory.Searcher
withFacetValues(Map<String,List<String>> facetValues)
Set the facet values.SearcherFactory.Searcher
withFilterQueries(Collection<Query> queries)
Set the filter queries (asQuery
objects).SearcherFactory.Searcher
withFilterQueries(Query... queries)
Set the filter queries (asQuery
objects).SearcherFactory.Searcher
withFilterQueryStrings(String... queries)
Set the filter queries (as Strings).SearcherFactory.Searcher
withFilterQueryStrings(Collection<String> queries)
Set the filter queries (as Strings).SearcherFactory.Searcher
withLimits(int start, int maxResults)
Set the search offset and limit.SearcherFactory.Searcher
withQuery(Query query)
Set the query (as aQuery
object).SearcherFactory.Searcher
withQueryString(String query)
Set the query (as a String).SearcherFactory.Searcher
withSort(List<Sort> sortClauses)
Set the sort clauses.SearcherFactory.Searcher
withSort(Sort... sortClauses)
Set the sort clauses.
-
-
-
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 aQuery
object).- Parameters:
query
- the query (as aQuery
object).- 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 (asQuery
objects).- Parameters:
queries
- the filter queries (asQuery
objects).- Returns:
- The Searcher object itself. The Searcher object itself.
-
withFilterQueries
public SearcherFactory.Searcher withFilterQueries(Collection<Query> queries)
Set the filter queries (asQuery
objects).- Parameters:
queries
- the filter queries (asQuery
objects).- 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 aQuery
object).- Parameters:
query
- the filter query to add (as aQuery
object).- 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
-true
to check rights,false
otherwise.- 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 givenvisibilty
to compare each result with.- Parameters:
compareTo
- thevisibilty
to 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 theSearchResults
object.- 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.
-
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
- TheAllowedUsers
object to compare with for checking rights- Returns:
- The solr query object.
- Throws:
Exception
- If an error occurs.
-
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.
-
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
-
-