Skip to content

client.suggest return type seems to be incorrect / behaves different from client.search #33161

Open
@marc-wilson

Description

@marc-wilson
  • Package Name: @azure/search-documents
  • Package Version: 12
  • Operating system: Mac
  • nodejs
    • 22:
  • browser
    • chrome/latest:
  • typescript
    • 5.6.3:
  • Is the bug related to documentation in

Describe the bug
When doing client.suggest(...), i want to define a type to model what the response is. Looking at the package, both SuggestOptions and client.suggest are generics. However, no matter what I do, the resulting array is always {} | Pick<MyModel, "field name"> | .... I don't think I should need to cast the result into MyModel (ie: result.document as MyModel). Should just work.

Here's an example of what is happening. I discovered this while converting my search over to a suggest. once i converted, i got some typescript errors. i believe it's an issue with the type returned by the suggest method.

export interface ISearchResult {
  description?: string;
  name?: string;
}


private async _search(term: string): Promise<ISearchResult[]> {
    if (!term) return [];
    const items: ISearchResult[] = [];
    const options: SuggestOptions<ISearchResult> = {
      top: 10,
      useFuzzyMatching: true,
      searchFields: ['name'],
      select: ['name', 'description'],
    };
    const results = await this._searchClient.suggest(term, 'suggestor-name', options);
    for await (const result of results.results) {
      const document = result.document; // TS says this is {} | Pick<ISearchResult, "name"> | ...
      console.log(document); // actually ISearchResult
      if (document.name) { ... } // TS2339: Property name does not exist on type Pick<ISearchResult, "name" | ....
      items.push(document);
    }
    return items;
  }

If i give a type to suggest, i get

TS2344: Type ISearchResult does not satisfy the constraint 'description' | 'name' | undefined

this._searchClient.suggest<ISearchResult>(...)

However, if you use searchClient.search, it acts as it should.

private async _search(term: string): Promise<ISearchResult[]> {
    if (!term) return [];
    const items: ISearchResult[] = [];
    const options: SearchOptions<ISearchResult> = {
      top: 10,
    };
    const results = await this._searchClient.search(term, options);
    for await (const result of results.results) {
      const document = result.document; // intellisense has it as ISearchResult
      console.log(document); // actually ISearchResult
      if (document.name) { ... } // no typescript error 
      items.push(result.document);
    }
    return items;
  }

Is this just a bug on the suggest method or is client.suggest supposed to act differently?

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.Searchneeds-team-attentionWorkflow: This issue needs attention from Azure service team or SDK team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions