|
| 1 | +""" |
| 2 | +This module provides functionality for performing multi-search operations in the Typesense API. |
| 3 | +
|
| 4 | +It contains the MultiSearch class, which allows for executing multiple search queries |
| 5 | +in a single API call. |
| 6 | +
|
| 7 | +Classes: |
| 8 | + MultiSearch: Manages multi-search operations in the Typesense API. |
| 9 | +
|
| 10 | +Dependencies: |
| 11 | + - typesense.api_call: Provides the ApiCall class for making API requests. |
| 12 | + - typesense.preprocess: |
| 13 | + Provides the stringify_search_params function for parameter processing. |
| 14 | + - typesense.types.document: |
| 15 | + Provides the MultiSearchCommonParameters type. |
| 16 | + - typesense.types.multi_search: |
| 17 | + Provides MultiSearchRequestSchema and MultiSearchResponse types. |
| 18 | +
|
| 19 | +Note: This module uses conditional imports to support both Python 3.11+ and earlier versions. |
| 20 | +""" |
| 21 | + |
1 | 22 | import sys |
2 | 23 |
|
3 | 24 | from typesense.api_call import ApiCall |
|
11 | 32 | import typing_extensions as typing |
12 | 33 |
|
13 | 34 |
|
14 | | -class MultiSearch(object): |
15 | | - RESOURCE_PATH = "/multi_search" |
| 35 | +class MultiSearch: |
| 36 | + """ |
| 37 | + Manages multi-search operations in the Typesense API. |
| 38 | +
|
| 39 | + This class provides methods to perform multiple search queries in a single API call. |
| 40 | +
|
| 41 | + Attributes: |
| 42 | + RESOURCE_PATH (str): The API endpoint path for multi-search operations. |
| 43 | + api_call (ApiCall): The ApiCall instance for making API requests. |
| 44 | + """ |
| 45 | + |
| 46 | + resource_path: typing.Final[str] = "/multi_search" |
16 | 47 |
|
17 | 48 | def __init__(self, api_call: ApiCall) -> None: |
| 49 | + """ |
| 50 | + Initialize the MultiSearch instance. |
| 51 | +
|
| 52 | + Args: |
| 53 | + api_call (ApiCall): The ApiCall instance for making API requests. |
| 54 | + """ |
18 | 55 | self.api_call = api_call |
19 | 56 |
|
20 | 57 | def perform( |
21 | 58 | self, |
22 | 59 | search_queries: MultiSearchRequestSchema, |
23 | 60 | common_params: typing.Union[MultiSearchCommonParameters, None] = None, |
24 | 61 | ) -> MultiSearchResponse: |
| 62 | + """ |
| 63 | + Perform a multi-search operation. |
| 64 | +
|
| 65 | + This method allows executing multiple search queries in a single API call. |
| 66 | + It processes the search parameters, sends the request to the Typesense API, |
| 67 | + and returns the multi-search response. |
| 68 | +
|
| 69 | + Args: |
| 70 | + search_queries (MultiSearchRequestSchema): |
| 71 | + A dictionary containing the list of search queries to perform. |
| 72 | + The dictionary should have a 'searches' key with a list of search |
| 73 | + parameter dictionaries. |
| 74 | + common_params (Union[MultiSearchCommonParameters, None], optional): |
| 75 | + Common parameters to apply to all search queries. Defaults to None. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + MultiSearchResponse: |
| 79 | + The response from the multi-search operation, containing |
| 80 | + the results of all search queries. |
| 81 | + """ |
25 | 82 | stringified_search_params = [ |
26 | 83 | stringify_search_params(search_params) |
27 | 84 | for search_params in search_queries.get("searches") |
28 | 85 | ] |
29 | 86 | search_body = {"searches": stringified_search_params} |
30 | 87 | response: MultiSearchResponse = self.api_call.post( |
31 | | - MultiSearch.RESOURCE_PATH, |
| 88 | + MultiSearch.resource_path, |
32 | 89 | body=search_body, |
33 | 90 | params=common_params, |
34 | 91 | as_json=True, |
|
0 commit comments