This page explains how to order the results of a search query made on structured data stores and on unstructured data stores with metadata.
Supported data types for ordering the results
These are the field types that you can have your search results ordered by:
string
number
datetime
geolocation
Before you begin
Make sure that you have an app with a data store that contains structured data or unstructured data with metadata.
Order your search results
To order the search results for a structured data store or for an unstructured data store with metadata, follow these steps:
REST
To use the API to order your search results for an app with structured data or
unstructured data with metadata,
use the engines.servingConfigs.search
method:
Find your app ID. If you already have your app ID, skip to the next step.
In the Google Cloud console, go to the Agentspace page.
On the Apps page, find the name of your app and get the app's ID from the ID column.
Make a search query and include the
orderBy
field.curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://discoveryengine.googleapis.com/v1alpha/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:search" \ -d '{ "servingConfig": "projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search", "query": "QUERY", "orderBy": "ORDER_BY" }'
Replace the following:
- PROJECT_ID: the ID of your project..
- APP_ID: the ID of the app..
- QUERY: the query text to search.
- ORDER_BY: the order in which the results are arranged. The
default sort order is ascending. For example, specifying
date
returns results from the oldest to the newest. To get descending sort order, appenddesc
to thedate
value—for example,date desc
. For more examples, seeorderBy
examples.
orderBy
examples
To order on a string field called title
:
"orderBy": "title"
to return the documents in ascending alphabetic order according to title."orderBy": "title desc"
to return the documents sorted in reverse alphabetic order (from Z to A) according to the title.
To order on a number field called rating
:
"orderBy": "rating"
to order from the lowest to the highest rated documents."orderBy": "rating desc"
to order from the highest to the lowest rated documents.
To order on a datetime field called available_date
:
"orderBy": "available_date"
to order from the soonest available document to the farthest out."orderBy": "available_date desc"
to order from the document with the farthest out available date to the one soonest available.
To order according to geolocation on a field called location
:
"orderBy": "GEO_DISTANCE(location, \"Mountain View, CA\")"
sorts the documents in order from those nearest to Mountain View to those farthest from Mountain View."orderBy": "GEO_DISTANCE(location, 37.38, -122.08) desc"
sorts the documents in order from farthest to nearest to the location 37.38°N and 122.08°W.