Search results

This page shows how pagination works when performing search requests.

Prerequisites

Examples

The SDKs provide two distinct methods, each suited to different use cases and requirements. Selecting the most efficient method is important for optimizing the performance of your application:

  • Direct pagination: Choose this method mainly when the total number of items is manageable, or you must fetch a single page of results.
  • Iterative pagination: Choose this method mainly when your application must retrieve a large number of items.

Direct pagination

To retrieve the first page of results, invoke the query method of the search object. The response will contain the following properties:

  • data: An array containing the first page of results
  • page_info: An object containing information about pagination:
    • limit_per_page: The maximum number of results per page.
    • total_results: The total number of results.
    • page_expires_at: The time when the current page expires.
    • next_page_token: A token you can use to retrieve the results on the next page. The platform doesn’t return this field when you’ve reached the end of the dataset
    • prev_page_token: A token you can use to retrieve the results on the previous page. The platform doesn’t return this field when you’ve retrieved the first page.
  • pool: An object that contains details about the index you queried.

To retrieve a specific page of results, invoke the by_page_token method of the search object, passing the next_page_token property of the page_info object as the value of the page_token parameter. The response will be similar to the one returned when retrieving the first page of results. When the platform does not return the next_page_token field, you’ve reached the end of the dataset.

Retrieve the first page of results

To retrieve the first page of results, invoke the query method of the search object:

1from twelvelabs import TwelveLabs
2
3client = TwelveLabs(api_key="<YOUR_API_KEY>")
4
5search_results = client.search.query(
6 index_id="<YOUR_INDEX_ID>",
7 query_text="<YOUR_QUERY>",
8 options=["<YOUR_SEARCH_OPTIONS>"]
9)
10
11print(f"Page info: {search_results.page_info}")
12print(f"Pool: {search_results.pool}")
13
14# Utility function to print a specific page
15def print_page(page):
16 for clip in page:
17 print(
18 f"video_id={clip.video_id} score={clip.score} start={clip.start} end={clip.end} confidence={clip.confidence}"
19 )
20
21print_page(search_results.data)

The example output below was truncated for brevity:

Page info: limit_per_page=10 total_results=93 page_expired_at='2024-03-19T12:43:36Z' next_page_token='74d1190c-2014-4c69-a662-4bfbbbe55bb5-1' prev_page_token=None
Pool: total_count=2 total_duration=1305.0 index_id='65d46e4879384c4778ab30f9'
video_id=65d4700a48db9fa780cb4136 score=84.8 start=164.671875 end=200.71875 confidence=high
video_id=65d46ef848db9fa780cb4135 score=84.77 start=446.46875 end=470.25 confidence=high
video_id=65d46ef848db9fa780cb4135 score=84.76 start=167.078125 end=207.1875 confidence=high

Retrieve a specific page of results

To retrieve a specific page of results, invoke the by_page_token method of the search object, passing the next_page_token property of the page_info object as the value of the page_token parameter:

1from twelvelabs import TwelveLabs
2
3client = TwelveLabs(api_key="<YOUR_API_KEY>")
4
5search_results = client.search.by_page_token(
6 page_token="<YOUR_PAGE_TOKEN>"
7)
8
9print(f"Page info: {search_results.page_info}")
10print(f"Pool: {search_results.pool}")
11
12# Utility function to print a specific page
13def print_page(page):
14 for clip in page:
15 print(
16 f"video_id={clip.video_id} score={clip.score} start={clip.start} end={clip.end} confidence={clip.confidence}"
17 )
18
19print_page(search_results.data)

Specify limits

The following example code iterates over results and and uses the page_limit parameter to specify a limit of 5 items on each page:

1search_results = client.search.query(
2 index_id="<YOUR_INDEX_ID",
3 query_text="<YOUR_QUERY>",
4 options=["<YOUR_SEARCH_OPTIONS>"],
5 page_limit=5
6)
7
8print(f"Page info: {search_results.page_info}")
9print(f"Pool: {search_results.pool}")
10
11# Utility function to print a specific page
12def print_page(page):
13 for clip in page:
14 print(
15 f"video_id={clip.video_id} score={clip.score} start={clip.start} end={clip.end} confidence={clip.confidence}"
16 )
17
18print_page(search_results.data)

Iterative pagination

To retrieve the first page of results, invoke the query method of the search object. To retrieve subsequent pages of results, use the iterator protocol in Python or the async iterator protocol in JavaScript.

Retrieve the first page of results

The following example code retrieves the first page of results by calling the query method of the search object:

1from twelvelabs import TwelveLabs
2
3client = TwelveLabs(api_key="<YOUR_API_KEY>")
4
5search_results = client.search.query(
6 index_id="<YOUR_INDEX_ID",
7 query_text="<YOUR_QUERY>",
8 options=["visual"]
9)
10
11# Utility function to print a specific page
12def print_page(page):
13 for clip in page:
14 print(
15 f" video_id={clip.video_id} score={clip.score} start={clip.start} end={clip.end} confidence={clip.confidence}"
16 )
17
18print_page(search_results.data)

The example output below was truncated for brevity:

video_id= 65d4700a48db9fa780cb4136 score=84.4 start=377.71875 end=390.46875 confidence=high
video_id= 65d46ef848db9fa780cb4135 score=84.25 start=290.4375 end=297.96875 confidence=high
video_id= 65d4700a48db9fa780cb4136 score=84.24 start=554.90625 end=559.6875 confidence=high

Iterate over the results

The following example code iterates over results by invoking the next function in Python or the next method of the object containing the search results in JavaScript:

1from twelvelabs import TwelveLabs
2
3client = TwelveLabs(api_key="<YOUR_API_KEY>")
4
5search_results = client.search.query(
6 index_id="<YOUR_INDEX_ID>",
7 query_text="<YOUR_QUERY>",
8 options=["visual"]
9)
10
11# Utility function to print a specific page
12def print_page(page):
13 for clip in page:
14 print(
15 f"video_id={clip.video_id} score={clip.score} start={clip.start} end={clip.end} confidence={clip.confidence}"
16 )
17
18print_page(search_results.data)
19
20while True:
21 try:
22 print_page(next(search_results))
23 except StopIteration:
24 break

Specify limits

The following example code iterates over results and and uses the page_limit parameter to specify a limit of 5 items on each page:

1from twelvelabs import TwelveLabs
2
3client = TwelveLabs(api_key="<YOUR_API_KEY>")
4
5search_results = client.search.query(
6 index_id="<YOUR_INDEX_ID>,
7 query_text="<YOUR_QUERY>",
8 options=["visual"],
9 page_limit=5
10)
11
12# Utility function to print a specific page
13def print_page(page):
14 for clip in page:
15 print(
16 f"video_id={clip.video_id} score={clip.score} start={clip.start} end={clip.end} confidence={clip.confidence}"
17 )
18print_page(search_results.data)
19
20while True:
21 try:
22 print_page(next(search_results))
23 except StopIteration:
24 break

Use pagination in conjunction with grouping

When the response is grouped by video, you can use the page_limit parameter to specify the number of videos the platform will return on each page. The following example code sets the page limit to three and specifies that the result must be grouped by video:

1from twelvelabs import TwelveLabs
2
3client = TwelveLabs(api_key="<YOUR_API_KEY>")
4
5search_results = client.search.query(
6 index_id="<YOUR_INDEX_ID>",
7 query_text="<YOUR_QUERY>",
8 options=["visual"],
9 page_limit=3,
10 group_by="video"
11)
12
13# Utility function to print a specific page
14def print_page(page):
15 for video in page:
16 print(f"Video id: {video.id}")
17 for clip in video.clips:
18 print(clip)
19 print(
20 f"\tscore={clip.score} start={clip.start} end={clip.end} confidence={clip.confidence}"
21 )
22
23print_page(search_results.data)
24
25while True:
26 try:
27 print_page(next(search_results))
28 except StopIteration:
29 break
Built with