Discoveryengine SearchResponse Results Order issue

Hi!

We are trying to use Vertex AI for our internal system and build a search app with DiscoveryEngine_v1 (0.13.11) using the Python SDK.

While reviewing the Python SDK reference, we noticed that sending a search request to the SearchService.Search method with an unset order_by field in the SearchRequest object will return a SearchResponse object ordered by relevance.

However, the current script doesn’t work as the reference describes.

client_options = (
    ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
    if location != "global"
    else None
)

client = SearchServiceClient(client_options=client_options)

snippet_spec = SearchRequest.ContentSearchSpec.SnippetSpec(
    return_snippet=True
)

preamble_text = (
    "PREAMBLE_TEXT"
)

model_prompt_spec = (
    SearchRequest.ContentSearchSpec.SummarySpec.ModelPromptSpec(
        preamble=preamble_text
    )
)

summary_spec = SearchRequest.ContentSearchSpec.SummarySpec(
    summary_result_count=10,
    include_citations=False,
    ignore_adversarial_query=False,
    ignore_non_summary_seeking_query=False,
    ignore_low_relevant_content=False,
    ignore_jail_breaking_query=False,
    model_prompt_spec=model_prompt_spec,
    model_spec=SearchRequest.ContentSearchSpec.SummarySpec.ModelSpec(
        version="stable"
    ),
    use_semantic_chunks=True,
)

extractive_content_spec = (
    SearchRequest.ContentSearchSpec.ExtractiveContentSpec(
        max_extractive_answer_count=5,
        return_extractive_segment_score=False,
        num_previous_segments=2,
        num_next_segments=2,
    )
)

search_result_mode = SearchRequest.ContentSearchSpec.SearchResultMode(
    SearchRequest.ContentSearchSpec.SearchResultMode.DOCUMENTS
)

content_search_spec = SearchRequest.ContentSearchSpec(
    snippet_spec=snippet_spec,
    summary_spec=summary_spec,
    search_result_mode=search_result_mode,
)

relevance_score_spec = SearchRequest.RelevanceScoreSpec(
    return_relevance_score=True
)

request_params = {
    "serving_config": SERVING_CONFIG,
    "query": query,
    "content_search_spec": content_search_spec,
    "relevance_threshold": SearchRequest.RelevanceThreshold.LOW,
    "relevance_score_spec": relevance_score_spec,
}

request = SearchRequest(**request_params)

response = search_client.search(request)

We’d appreciate any guidance and assistance.

Thanks in advance.

Hi @BearPark,

I recommend submitting an issue report about the discrepancy to confirm whether it’s a bug or a documentation error. Before filing, be sure to review the guidelines so you know what to expect. Your feedback helps Google make their product and documentation better.

While the default order_by doesn’t work as expected, one possible approach is to post-process the results by filtering based on the relevance score.

Hi @caryna

Thanks for your reply. It was a big help for me.

I tried to follow your approach but the relevance_value of SearchResponse is not sorted. That is main issue for now and relevance_threthold is not working too.

Do you have any solution to resolve issue?