Class SearchHit

java.lang.Object
org.elasticsearch.search.SearchHit
All Implemented Interfaces:
Writeable, RefCounted, ToXContent, ToXContentObject

public final class SearchHit extends Object implements Writeable, ToXContentObject, RefCounted
A single search hit.
See Also:
  • Constructor Details

  • Method Details

    • readFrom

      public static SearchHit readFrom(StreamInput in, boolean pooled) throws IOException
      Throws:
      IOException
    • unpooled

      public static SearchHit unpooled(int docId)
    • unpooled

      public static SearchHit unpooled(int docId, String id)
    • unpooled

      public static SearchHit unpooled(int nestedTopDocId, String id, SearchHit.NestedIdentity nestedIdentity)
    • writeTo

      public void writeTo(StreamOutput out) throws IOException
      Description copied from interface: Writeable
      Write this into the StreamOutput.
      Specified by:
      writeTo in interface Writeable
      Throws:
      IOException
    • docId

      public int docId()
    • score

      public void score(float score)
    • getScore

      public float getScore()
      The score.
    • setRank

      public void setRank(int rank)
    • getRank

      public int getRank()
    • version

      public void version(long version)
    • getVersion

      public long getVersion()
      The version of the hit.
    • setSeqNo

      public void setSeqNo(long seqNo)
    • setPrimaryTerm

      public void setPrimaryTerm(long primaryTerm)
    • getSeqNo

      public long getSeqNo()
      returns the sequence number of the last modification to the document, or SequenceNumbers.UNASSIGNED_SEQ_NO if not requested.
    • getPrimaryTerm

      public long getPrimaryTerm()
      returns the primary term of the last modification to the document, or SequenceNumbers.UNASSIGNED_PRIMARY_TERM if not requested.
    • getIndex

      public String getIndex()
      The index of the hit.
    • getId

      public String getId()
      The id of the document.
    • getNestedIdentity

      public SearchHit.NestedIdentity getNestedIdentity()
      If this is a nested hit then nested reference information is returned otherwise null is returned.
    • getSourceRef

      public BytesReference getSourceRef()
      Returns bytes reference, also uncompress the source if needed.
    • sourceRef

      public SearchHit sourceRef(BytesReference source)
      Sets representation, might be compressed....
    • hasSource

      public boolean hasSource()
      Is the source available or not. A source with no fields will return true. This will return false if fields doesn't contain _source or if source is disabled in the mapping.
    • getSourceAsString

      public String getSourceAsString()
      The source of the document as string (can be null).
    • getSourceAsMap

      public Map<String,Object> getSourceAsMap()
      The source of the document as a map (can be null). This method is expected to be called at most once during the lifetime of the object as the generated map is expensive to generate and it does not get cache.
    • field

      public DocumentField field(String fieldName)
      The hit field matching the given field name.
    • setDocumentField

      public void setDocumentField(DocumentField field)
    • addDocumentFields

      public void addDocumentFields(Map<String,DocumentField> docFields, Map<String,DocumentField> metaFields)
    • removeDocumentField

      public DocumentField removeDocumentField(String field)
    • getMetadataFields

      public Map<String,DocumentField> getMetadataFields()
      Returns:
      a map of metadata fields for this hit
    • getDocumentFields

      public Map<String,DocumentField> getDocumentFields()
      Returns:
      a map of non-metadata fields requested for this hit
    • getFields

      public Map<String,DocumentField> getFields()
      A map of hit fields (from field name to hit fields) if additional fields were required to be loaded. Includes both document and metadata fields.
    • hasLookupFields

      public boolean hasLookupFields()
      Whether this search hit has any lookup fields
    • resolveLookupFields

      public void resolveLookupFields(Map<LookupField,List<Object>> lookupResults)
      Resolve the lookup fields with the given results and merge them as regular fetch fields.
    • getHighlightFields

      public Map<String,HighlightField> getHighlightFields()
      A map of highlighted fields.
    • highlightFields

      public void highlightFields(Map<String,HighlightField> highlightFields)
    • sortValues

      public void sortValues(Object[] sortValues, DocValueFormat[] sortValueFormats)
    • sortValues

      public void sortValues(SearchSortValues sortValues)
    • getSortValues

      public Object[] getSortValues()
      An array of the (formatted) sort values used.
    • getRawSortValues

      public Object[] getRawSortValues()
      An array of the (raw) sort values used.
    • getExplanation

      public org.apache.lucene.search.Explanation getExplanation()
      If enabled, the explanation of the search hit.
    • explanation

      public void explanation(org.apache.lucene.search.Explanation explanation)
    • getShard

      public SearchShardTarget getShard()
      The shard of the search hit.
    • shard

      public void shard(SearchShardTarget target)
    • getClusterAlias

      public String getClusterAlias()
      Returns the cluster alias this hit comes from or null if it comes from a local cluster
    • matchedQueries

      public void matchedQueries(Map<String,Float> matchedQueries)
    • getMatchedQueries

      public String[] getMatchedQueries()
      The set of query and filter names the query matched with. Mainly makes sense for compound filters and queries.
    • getMatchedQueryScore

      public Float getMatchedQueryScore(String name)
      Returns:
      The score of the provided named query if it matches, null otherwise.
    • getMatchedQueriesAndScores

      public Map<String,Float> getMatchedQueriesAndScores()
      Returns:
      The map of the named queries that matched and their associated score.
    • getInnerHits

      public Map<String,SearchHits> getInnerHits()
      Returns:
      Inner hits or null if there are none
    • setInnerHits

      public void setInnerHits(Map<String,SearchHits> innerHits)
    • incRef

      public void incRef()
      Specified by:
      incRef in interface RefCounted
    • tryIncRef

      public boolean tryIncRef()
      Specified by:
      tryIncRef in interface RefCounted
    • decRef

      public boolean decRef()
      Specified by:
      decRef in interface RefCounted
    • hasReferences

      public boolean hasReferences()
      Specified by:
      hasReferences in interface RefCounted
    • asUnpooled

      public SearchHit asUnpooled()
    • isPooled

      public boolean isPooled()
    • toXContent

      public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException
      Specified by:
      toXContent in interface ToXContent
      Throws:
      IOException
    • toInnerXContent

      public XContentBuilder toInnerXContent(XContentBuilder builder, ToXContent.Params params) throws IOException
      Throws:
      IOException
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object