class SearchHit
Represents a single search result item within Microsoft Graph search results, encapsulating content source, summary, resource entity, and template information.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/search/hit.py
4 - 19
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 resultresource: office365.entity.Entity object representing the underlying Graph resourceresult_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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class SearchHitsContainer 74.2% similar
-
class SearchResponse 65.0% similar
-
class SearchBucket 59.3% similar
-
class CustomResult 58.7% similar
-
class SearchEntity 57.3% similar