Class SearchResult<T extends Identifiable>

java.lang.Object
ubic.gemma.core.search.SearchResult<T>
All Implemented Interfaces:
Comparable<SearchResult<?>>

public class SearchResult<T extends Identifiable> extends Object implements Comparable<SearchResult<?>>
Represents an individual search result.

Search result minimally have a type and ID and may have their result object populated at a later time via setResultObject(Identifiable).

Results have a score and possibly number of highlights. Two results are considered equal if they have the same type and ID. You may use a SearchResultSet to combine results in a sensible way, retaining result objects and highlights when a better result is added.

Author:
paul, poirigui
See Also:
  • Constructor Details

    • SearchResult

      public SearchResult(Class<? extends Identifiable> resultType, long resultId, double score, @Nullable Map<String,String> highlights, Object source)
      Creates a new SearchResult instance.
      Parameters:
      resultType - Type of search result, immutable.
      resultId - ID of the result, immutable.
      score - Score for ranking this result among other results.
      highlights - Highlights for this result.

      Keys are fields of SearchResult and values are substrings that were matched.

      source - Object representing the source of this result object.

      This can simply be a String.

  • Method Details

    • from

      public static <T extends Identifiable> SearchResult<T> from(Class<? extends Identifiable> resultType, T entity, double score, @Nullable Map<String,String> highlights, Object source)
      Create a search result from a given identifiable entity.

      The result can be cleared later with clearResultObject().

    • from

      public static <T extends Identifiable> SearchResult<T> from(Class<? extends Identifiable> resultType, long entityId, double score, @Nullable Map<String,String> highlights, Object source)
      Create a new provisional search result with a result type and ID.

      The result can be set later with setResultObject(Identifiable).

    • compareTo

      public int compareTo(SearchResult<?> o)
      Specified by:
      compareTo in interface Comparable<T extends Identifiable>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getResultId

      @Nonnull public Long getResultId()
      Obtain the result ID.

      For consistency with Identifiable.getId(), thus returns a Long. It is however backed internally by a native long and cannot ever be null.

    • setResultObject

      public void setResultObject(T resultObject)
      Set the result object.
      Throws:
      IllegalArgumentException - if the provided result object is null or if its ID differs from getResultId().
    • clearResultObject

      public void clearResultObject()
      Clear the result object.
    • withResultObject

      public <S extends Identifiable> SearchResult<S> withResultObject(@Nullable S resultObject)
      Create a search result from an existing one, replacing the result object with the target one.

      The new result object does not have to be of the same type as the original result object. This is useful if you need to convert the result object (i.e. to a VO) while preserving the metadata (score, highlighted text, etc.).

    • withHighlights

      public SearchResult<T> withHighlights(Map<String,String> highlights)
      Copy this search result with the given highlights.
    • getResultType

      public Class<? extends Identifiable> getResultType()
      Type of search result, immutable.
    • getResultObject

      @Nullable public T getResultObject()
      Result object this search result is referring to.

      This can be null, at least initially if the resultType and resultId are provided.

      It may also be replaced at a later time via setResultObject(Identifiable).

    • getScore

      public double getScore()
      Score for ranking this result among other results.
    • getHighlights

      @Nullable public Map<String,String> getHighlights()
      Highlights for this result.

      Keys are fields of SearchResult and values are substrings that were matched.

    • getSource

      public Object getSource()
      Object representing the source of this result object.

      This can simply be a String.

    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object