Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
6 tasks
marc-wilson opened this issue Feb 21, 2025 · 0 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. Search

Comments

@marc-wilson
Copy link

  • 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?

@marc-wilson marc-wilson changed the title client.suggest type is not incorrect client.suggest return type seems to be incorrect / behaves different from client.search Feb 21, 2025
@jeremymeng jeremymeng added Client This issue points to a problem in the data-plane of the library. Search labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. Search
Projects
None yet
Development

No branches or pull requests

3 participants