🔍 Code Extractor

class SearchHit

Maturity: 38

Represents a single search result item within Microsoft Graph search results, encapsulating content source, summary, resource entity, and template information.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/search/hit.py
Lines:
4 - 19
Complexity:
simple

Purpose

SearchHit is a data transfer object (DTO) that represents an individual search result returned from Microsoft Graph search operations. It inherits from ClientValue, making it serializable for API communication. The class stores metadata about a search result including its content source, summary text, the underlying Graph entity resource, and a template ID for rendering. This class is typically used as part of a larger search response collection when querying Microsoft 365 services through the Graph API.

Source Code

class SearchHit(ClientValue):
    def __init__(
        self, content_source=None, summary=None, resource=None, result_template_id=None
    ):
        """
        Represents a single result within the list of search results.
        :param str content_source:
        :param str summary: A summary of the result, if a summary is available.
        :param office365.entity.Entity resource: The underlying Microsoft Graph representation of the search result.
        :param str result_template_id: ID of the result template used to render the search result. This ID must map to
            a display layout in the resultTemplates dictionary that is also included in the searchResponse.
        """
        self.contentSource = content_source
        self.summary = summary
        self.resource = resource
        self.resultTemplateId = result_template_id

Parameters

Name Type Default Kind
bases ClientValue -

Parameter Details

content_source: A string identifier indicating the source of the search result content (e.g., SharePoint, OneDrive, Exchange). Can be None if not specified.

summary: A string containing a brief summary or snippet of the search result content. This is typically an excerpt showing why the result matched the search query. Can be None if no summary is available.

resource: An instance of office365.entity.Entity representing the underlying Microsoft Graph object that was found (e.g., a DriveItem, ListItem, or Message). This provides access to the full properties of the found item. Can be None if not provided.

result_template_id: A string identifier that maps to a display layout template in the resultTemplates dictionary of the search response. This ID determines how the search result should be rendered in the UI. Can be None if no specific template is assigned.

Return Value

Instantiation returns a SearchHit object that encapsulates all the provided search result metadata. The object itself doesn't have methods that return values, but its attributes can be accessed to retrieve the stored search result information.

Class Interface

Methods

__init__(self, content_source=None, summary=None, resource=None, result_template_id=None)

Purpose: Initializes a SearchHit instance with search result metadata

Parameters:

  • content_source: String identifier for the content source (e.g., 'SharePoint', 'OneDrive')
  • summary: String summary or excerpt of the search result
  • resource: office365.entity.Entity object representing the underlying Graph resource
  • result_template_id: String ID mapping to a display template for rendering

Returns: None (constructor)

Attributes

Name Type Description Scope
contentSource str or None Stores the identifier of the content source where the search result originated instance
summary str or None Stores a summary or snippet of the search result content instance
resource office365.entity.Entity or None Stores the underlying Microsoft Graph entity object representing the full search result instance
resultTemplateId str or None Stores the template ID used to determine how the search result should be rendered instance

Dependencies

  • office365

Required Imports

from office365.runtime.client_value import ClientValue

Usage Example

from office365.runtime.client_value import ClientValue
from office365.entity import Entity

# Create a search hit with basic information
search_hit = SearchHit(
    content_source='SharePoint',
    summary='This document contains information about project planning...',
    resource=None,  # Would typically be an Entity object from Graph API
    result_template_id='template_001'
)

# Access the search hit properties
print(f'Source: {search_hit.contentSource}')
print(f'Summary: {search_hit.summary}')
print(f'Template ID: {search_hit.resultTemplateId}')

# Typically used as part of a search response
search_results = [search_hit]  # Would contain multiple SearchHit objects
for hit in search_results:
    if hit.summary:
        print(hit.summary)

Best Practices

  • SearchHit is primarily a data container and should be instantiated with data received from Microsoft Graph API search responses
  • All constructor parameters are optional and can be None, so always check for None values before accessing attributes
  • The resource attribute should be an instance of office365.entity.Entity when provided, representing the actual Graph object found
  • This class inherits from ClientValue, which provides serialization capabilities for API communication - do not modify this inheritance
  • SearchHit objects are typically created by the Microsoft Graph SDK internally when processing search responses, rather than manually by developers
  • The resultTemplateId should correspond to a template defined in the search response's resultTemplates dictionary for proper rendering
  • When working with collections of SearchHit objects, iterate through them to process multiple search results
  • The class is immutable by design after instantiation - attributes are set once in __init__ and not modified thereafter

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class SearchHitsContainer 74.2% similar

    A container class that represents search results from a query, including the hits, total count, pagination information, and aggregations.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/search/hits_container.py
  • class SearchResponse 65.0% similar

    A data class that encapsulates search query results and the search terms used to generate those results, inheriting from ClientValue for Office 365 API integration.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/search/response.py
  • class SearchBucket 59.3% similar

    A data container class representing aggregated search results that share the same value for a specific entity field, used in Microsoft Graph API search operations.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/search/bucket.py
  • class CustomResult 58.7% similar

    A data class representing custom search query results with layout templates and properties for Microsoft Office Server Search REST API.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/search/custom_result.py
  • class SearchEntity 57.3% similar

    A wrapper class for the Microsoft Graph Search API endpoint that provides methods to query various Microsoft 365 resources including messages, events, drive items, and other entity types.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/search/entity.py
← Back to Browse