Copyright© 2008-2022 Sitevision AB, all rights reserved.
public interface SearchResult extends Iterable<SearchHit>, VelocityAccess.SearchResultConstants
A SearchResult
is a container for the hits returned by a search query. The object is lazy loaded
and can potentially change its state with each extracted hit (i.e. next()
is called on the Iterator<SearchHit>
).
Note! This object is short lived in that sense that it is invalid after a specified timespan. Make sure it's handled correspondingly (i.e. do not cache it).
Modifier and Type | Field and Description |
---|---|
static int |
STATUS_INDEX_NOT_AVAILABLE
Search is unsuccessful due to a missing index.
|
static int |
STATUS_OK
Search is successful.
|
static int |
STATUS_PARSE_ERROR
Search is unsuccessful due to an invalid search query.
|
static int |
STATUS_UNEXPECTED_ERROR
Search is unsuccessful due to an unexpected error.
|
Modifier and Type | Method and Description |
---|---|
int |
getApproximateCount()
Predicts the number of hits by disregarding expensive operations like node authorization etc.
|
List<CustomSort> |
getCustomSorts()
Returns the custom sorts for this search result.
|
SimpleDateFormat |
getDateFormatter(Locale aLocale)
Deprecated.
since Sitevision 3.0
|
String |
getDisplayQuery()
The "human friendly" variant of the query to display in search results.
|
int |
getEffectiveCount(int aHitCountThreshold)
Returns the exact hit count or the approximate hit count, given a specified count threshold.
|
int |
getExactCount()
Accessor to the exact number of hits.
|
List<SearchFacetField> |
getFacetFields()
Returns the facet fields for this search result.
|
Iterator<SearchHit> |
getHits()
Accessor to the lazy loaded
Iterator containing the search hits. |
String |
getQuery()
The search query that was used to create this search result.
|
SearchHighlighter |
getSearchHighlighter()
Deprecated.
since Sitevision 3.0
|
int |
getStatus()
The current status of the search.
|
String |
getStatusMessage()
Accessor to a string representation of the status code.
|
List<SearchSuggestion> |
getSuggestions()
Returns suggestions for this search result.
|
boolean |
hasHits()
Whether the search result contains hits or not.
|
Iterator<SearchHit> |
iterator()
Convenience method for iterating search hits using the Java foreach loop construct.
|
forEach, spliterator
getSTATUS_INDEX_NOT_AVAILABLE, getSTATUS_OK, getSTATUS_PARSE_ERROR, getSTATUS_UNEXPECTED_ERROR
static final int STATUS_OK
getStatus()
,
Constant Field Valuesstatic final int STATUS_INDEX_NOT_AVAILABLE
getStatus()
,
Constant Field Valuesstatic final int STATUS_PARSE_ERROR
getStatus()
,
Constant Field Valuesstatic final int STATUS_UNEXPECTED_ERROR
getStatus()
,
Constant Field ValuesIterator<SearchHit> getHits()
Accessor to the lazy loaded Iterator
containing the search hits. Iterating the
Iterator
potentially changes the status of the SearchResult
object.
IMPORTANT NOTE! You should only call this method once! Subsequent calls
will throw an IllegalStateException
.
Iterator
containing the SearchHit
objectsIllegalStateException
- if this method is called more than oncegetStatus()
,
getStatusMessage()
,
iterator()
Iterator<SearchHit> iterator()
Convenience method for iterating search hits using the Java foreach loop construct.
NOTE! This convenience method is just an ALIAS for the getHits()
method!
Restrictions and such that applies to getHits()
also applies to this method!
This is a convenience method for enabling iteration of search hits using the foreach loop:
SearchResult searchResult = ...
for (SearchHit searchHit : searchResult) {
...
}
...so you don't have to use the for loop and iterate via the getHits()
method:
SearchResult searchResult = ...
for (Iterator<SearchHit> searchHits = searchResult.getHits(); searchHits.hasNext();) {
SearchHit searchHit = searchHits.next();
...
}
...or use the while loop:
SearchResult searchResult = ...
Iterator<SearchHit> searchHits = searchResult.getHits();
while (searchHits.hasNext()) {
SearchHit searchHit = searchHits.next();
...
}
int getExactCount()
Accessor to the exact number of hits. This operation renders the lazy loaded
optimization obsolete having to resolve each node to do authorization etc. The
value can be less than the value returned by getApproximateCount()
for
authenticated users.
Potentially a very expensive operation! To be used only when an
exact count is really, REALLY needed. Normally getApproximateCount()
will
suffise (paging etc.).
Tip! If you require exact accuracy for "small" search results and accept
potential inaccurate hit count for "large" results - use getEffectiveCount(int)
instead!
Note: If your only interest in this method is to check if the exact count is more
than zero or not, you SHOULD use hasHits()
instead.
In other words, this:
if (!mySearchResult.hasHits())
{
// Handle no hits
...
}
is always much, Much, MUCH preferred to this:
if (mySearchResult.getExactCount() == 0)
{
// Handle no hits
...
}
getApproximateCount()
,
getEffectiveCount(int)
,
hasHits()
boolean hasHits()
Whether the search result contains hits or not.
This is a convenience method that enables you to check if there are any hits, without obtaining a (possibly unneeded)
iterator via getHits()
.
In other words, this:
if (mySearchResult.hasHits())
{
// Handle hits
Iterator<SearchHit> hitIterator = mySearchResult.getHits();
...
}
is preferred to this:
Iterator<SearchHit> hitIterator = mySearchResult.getHits();
if (hitIterator.hasNext())
{
// Handle hits
...
}
Note: This method must be called before iterating the hits. If you already have started iterating the hits, the result of this method is indeterminate.
Note: If your only interest is to check if exact count is more than zero or not, this method
is much, much less expensive than calling getExactCount()
and comparing the result to zero.
int getApproximateCount()
Predicts the number of hits by disregarding expensive operations like node authorization etc.
This value can be higher than the value returned by getExactCount()
for authenticated users.
Tip! If you require exact accuracy for "small" search results and accept approximate hit count for
"large" results, you would typically use the getEffectiveCount(int)
method!
getEffectiveCount(int)
,
getExactCount()
int getEffectiveCount(int aHitCountThreshold)
Returns the exact hit count or the approximate hit count, given a specified count threshold.
This method enables you to get accurate hit count for "small" search results and estimated hit count for "large" search results.
A "silver bullet" method when deciding between using the potentially less performant (but exact) getExactCount()
and the
well performant (but potentially inexact) getApproximateCount()
An example: getEffectiveCount(100)
exactCount
if the estimated hit count is lower than 100approximateCount
if the estimated hit count is equal or higher than 100aHitCountThreshold
- threshold for when "exact" count is less important, i.e. threshold for when to use
approximate count instead of exact count.getApproximateCount()
,
getExactCount()
String getQuery()
The search query that was used to create this search result.
SearchResult
objectgetDisplayQuery()
String getDisplayQuery()
The "human friendly" variant of the query to display in search results.
Note! if no specific display query was specified when assembling/executing the search
(see Searcher
), this method will
return same value as getQuery()
.
SearchResult
objectgetQuery()
int getStatus()
The current status of the search. This value might change when iterating the
Iterator
containing the SearchHit
objects.
STATUS
fields
STATUS_OK
, STATUS_INDEX_NOT_AVAILABLE
, STATUS_PARSE_ERROR
,
STATUS_UNEXPECTED_ERROR
String getStatusMessage()
Accessor to a string representation of the status code.
null
if status is STATUS_OK
getStatus()
List<SearchFacetField> getFacetFields()
null
if there are no facet fieldsList<SearchSuggestion> getSuggestions()
null
if there are no suggestionsList<CustomSort> getCustomSorts()
null
if there are no custom sorts@Deprecated SearchHighlighter getSearchHighlighter()
null
if the search result doesn't have any hits (i.e !aSearchResult.hasHits()
)@Deprecated SimpleDateFormat getDateFormatter(Locale aLocale)
Use ScriptUtil.getFormatPattern(java.util.Locale)
and ScriptUtil.getDateAsString(String, java.util.Date)
instead to format dates for search hits.
aLocale
- the locale that should be used when creating the date formatter, if null
, locale.ENGLISH
will be usedSitevision - Portal and Content Management Made Easy
Sitevision is an advanced Java enterprise portal product and a portlet container (JSR 286) that implements Java Content Repository (JSR 283).
Copyright© 2008-2022 Sitevision AB, all rights reserved.