diff --git a/sdk/search/search-documents/review/search-documents.api.md b/sdk/search/search-documents/review/search-documents.api.md index 6cccd9663f89..e31fb8f73fd9 100644 --- a/sdk/search/search-documents/review/search-documents.api.md +++ b/sdk/search/search-documents/review/search-documents.api.md @@ -984,7 +984,7 @@ export interface ScoringProfile { export class SearchClient { constructor(endpoint: string, indexName: string, credential: KeyCredential, options?: SearchClientOptions); readonly apiVersion: string; - autocomplete(searchText: string, suggesterName: string, options: AutocompleteOptions): Promise; + autocomplete(searchText: string, suggesterName: string, options?: AutocompleteOptions): Promise; deleteDocuments(documents: T[], options?: DeleteDocumentsOptions): Promise; deleteDocuments(keyName: keyof T, keyValues: string[], options?: DeleteDocumentsOptions): Promise; readonly endpoint: string; @@ -995,7 +995,7 @@ export class SearchClient { mergeDocuments(documents: T[], options?: MergeDocumentsOptions): Promise; mergeOrUploadDocuments(documents: T[], options?: MergeOrUploadDocumentsOptions): Promise; search(searchText?: string, options?: SearchOptions): Promise>>; - suggest(searchText: string, suggesterName: string, options: SuggestOptions): Promise>>; + suggest(searchText: string, suggesterName: string, options?: SuggestOptions): Promise>>; uploadDocuments(documents: T[], options?: UploadDocumentsOptions): Promise; } @@ -1255,7 +1255,8 @@ export type SearchResult = { readonly highlights?: { [propertyName: string]: string[]; }; -} & T; + document: T; +}; // @public export interface SearchServiceStatistics { @@ -1443,7 +1444,8 @@ export interface SuggestRequest { // @public export type SuggestResult = { readonly text: string; -} & T; + document: T; +}; // @public export interface SynonymMap { diff --git a/sdk/search/search-documents/src/generated/data/models/index.ts b/sdk/search/search-documents/src/generated/data/models/index.ts index eb109406f4d9..e07596e5f457 100644 --- a/sdk/search/search-documents/src/generated/data/models/index.ts +++ b/sdk/search/search-documents/src/generated/data/models/index.ts @@ -17,7 +17,7 @@ export interface SuggestResult { * The text of the suggestion result. * **NOTE: This property will not be serialized. It can only be populated by the server.** */ - readonly text: string; + readonly _text: string; /** * Describes unknown properties. The value of an unknown property can be of "any" type. */ @@ -166,13 +166,13 @@ export interface SearchResult { * The relevance score of the document compared to other documents returned by the query. * **NOTE: This property will not be serialized. It can only be populated by the server.** */ - readonly score: number; + readonly _score: number; /** * Text fragments from the document that indicate the matching search terms, organized by each * applicable field; null if hit highlighting was not enabled for the query. * **NOTE: This property will not be serialized. It can only be populated by the server.** */ - readonly highlights?: { [propertyName: string]: string[] }; + readonly _highlights?: { [propertyName: string]: string[] }; /** * Describes unknown properties. The value of an unknown property can be of "any" type. */ diff --git a/sdk/search/search-documents/src/generated/data/models/mappers.ts b/sdk/search/search-documents/src/generated/data/models/mappers.ts index 0666273d53f4..c1cb8a0ec7d8 100644 --- a/sdk/search/search-documents/src/generated/data/models/mappers.ts +++ b/sdk/search/search-documents/src/generated/data/models/mappers.ts @@ -15,7 +15,7 @@ export const SuggestResult: coreHttp.CompositeMapper = { name: "Composite", className: "SuggestResult", modelProperties: { - text: { + _text: { required: true, readOnly: true, serializedName: "@search\\.text", @@ -226,7 +226,7 @@ export const SearchResult: coreHttp.CompositeMapper = { name: "Composite", className: "SearchResult", modelProperties: { - score: { + _score: { required: true, nullable: false, readOnly: true, @@ -235,7 +235,7 @@ export const SearchResult: coreHttp.CompositeMapper = { name: "Number" } }, - highlights: { + _highlights: { readOnly: true, serializedName: "@search\\.highlights", type: { diff --git a/sdk/search/search-documents/src/indexModels.ts b/sdk/search/search-documents/src/indexModels.ts index 233997627adb..4e7a0bc006e7 100644 --- a/sdk/search/search-documents/src/indexModels.ts +++ b/sdk/search/search-documents/src/indexModels.ts @@ -7,8 +7,7 @@ import { SearchMode, FacetResult, AutocompleteMode, - IndexActionType, - SearchDocumentsResult as GeneratedSearchResult + IndexActionType } from "./generated/data/models"; import { PagedAsyncIterableIterator } from "@azure/core-paging"; @@ -93,20 +92,6 @@ export type SearchIterator = PagedAsyncIterableIterator< ListSearchResultsPageSettings >; -/** - * An intermediate type for encoding the continuation token. - */ -export type ContinuableSearchResult = Omit< - GeneratedSearchResult, - "nextPageParameters" | "nextLink" -> & { - /** - * A token used for retrieving the next page of results when the server - * enforces pagination. - */ - continuationToken?: string; -}; - // BEGIN manually modified generated interfaces // // This section is for places where we have to manually fix issues @@ -326,7 +311,9 @@ export type SearchResult = { * **NOTE: This property will not be serialized. It can only be populated by the server.** */ readonly highlights?: { [propertyName: string]: string[] }; -} & T; + + document: T; +}; /** * Response containing search results from an index. @@ -449,7 +436,8 @@ export type SuggestResult = { * **NOTE: This property will not be serialized. It can only be populated by the server.** */ readonly text: string; -} & T; + document: T; +}; /** * Response containing suggestion query results from an index. diff --git a/sdk/search/search-documents/src/searchClient.ts b/sdk/search/search-documents/src/searchClient.ts index 974b80e0a9ab..5e7001c892b9 100644 --- a/sdk/search/search-documents/src/searchClient.ts +++ b/sdk/search/search-documents/src/searchClient.ts @@ -42,12 +42,12 @@ import { DeleteDocumentsOptions, SearchDocumentsPageResult, MergeOrUploadDocumentsOptions, - ContinuableSearchResult, SearchRequest } from "./indexModels"; import { odataMetadataPolicy } from "./odataMetadataPolicy"; import { IndexDocumentsBatch } from "./indexDocumentsBatch"; import { encode, decode } from "./base64"; +import * as utils from "./serviceUtils"; /** * Client options used to configure Cognitive Search API requests. @@ -199,7 +199,7 @@ export class SearchClient { public async autocomplete( searchText: string, suggesterName: string, - options: AutocompleteOptions + options: AutocompleteOptions = {} ): Promise { const { operationOptions, restOptions } = this.extractOperationOptions({ ...options }); const { searchFields, ...nonFieldOptions } = restOptions; @@ -265,8 +265,11 @@ export class SearchClient { ); const { results, count, coverage, facets, nextLink, nextPageParameters } = result; - const converted: ContinuableSearchResult = { - results, + + let modifiedResults = utils.generatedSearchResultToPublicSearchResult(results); + + const converted: SearchDocumentsPageResult = { + results: modifiedResults, count, coverage, facets, @@ -363,6 +366,7 @@ export class SearchClient { const pageResult = await this.searchDocuments(searchText, options); const { count, coverage, facets } = pageResult; + return { count, coverage, @@ -390,7 +394,7 @@ export class SearchClient { public async suggest( searchText: string, suggesterName: string, - options: SuggestOptions + options: SuggestOptions = {} ): Promise>> { const { operationOptions, restOptions } = this.extractOperationOptions({ ...options }); const { select, searchFields, orderBy, ...nonFieldOptions } = restOptions; @@ -418,7 +422,12 @@ export class SearchClient { fullOptions, operationOptionsToRequestOptionsBase(updatedOptions) ); - return deserialize>>(result); + + const modifiedResult = utils.generatedSuggestDocumentsResultToPublicSuggestDocumentsResult( + result + ); + + return deserialize>>(modifiedResult); } catch (e) { span.setStatus({ code: CanonicalCode.UNKNOWN, diff --git a/sdk/search/search-documents/src/serviceUtils.ts b/sdk/search/search-documents/src/serviceUtils.ts index d5f59db5fbbc..e1a611fb5c01 100644 --- a/sdk/search/search-documents/src/serviceUtils.ts +++ b/sdk/search/search-documents/src/serviceUtils.ts @@ -48,6 +48,11 @@ import { SimilarityAlgorithm, SearchResourceEncryptionKey } from "./serviceModels"; +import { SuggestDocumentsResult, SuggestResult, SearchResult } from "./indexModels"; +import { + SuggestDocumentsResult as GeneratedSuggestDocumentsResult, + SearchResult as GeneratedSearchResult +} from "./generated/data/models"; export function convertSkillsToPublic(skills: SearchIndexerSkillUnion[]): SearchIndexerSkill[] { if (!skills) { @@ -365,6 +370,48 @@ export function generatedIndexToPublicIndex(generatedIndex: GeneratedSearchIndex }; } +export function generatedSearchResultToPublicSearchResult(results: GeneratedSearchResult[]) { + const returnValues: SearchResult[] = results.map>((result) => { + const { _score, _highlights, ...restProps } = result; + const doc: { [key: string]: any } = { + ...restProps + }; + const obj = { + score: _score, + highlights: _highlights, + document: doc + }; + return obj as SearchResult; + }); + return returnValues; +} + +export function generatedSuggestDocumentsResultToPublicSuggestDocumentsResult( + searchDocumentsResult: GeneratedSuggestDocumentsResult +): SuggestDocumentsResult { + const results = searchDocumentsResult.results.map>((element) => { + const { _text, ...restProps } = element; + + const doc: { [key: string]: any } = { + ...restProps + }; + + const obj = { + text: _text, + document: doc + }; + + return obj as SuggestResult; + }); + + const result: SuggestDocumentsResult = { + results: results, + coverage: searchDocumentsResult.coverage + }; + + return result; +} + export function publicIndexToGeneratedIndex(index: SearchIndex): GeneratedSearchIndex { return { name: index.name, diff --git a/sdk/search/search-documents/swagger/Data.md b/sdk/search/search-documents/swagger/Data.md index 3c13d0430cb2..95c2b5ff477b 100644 --- a/sdk/search/search-documents/swagger/Data.md +++ b/sdk/search/search-documents/swagger/Data.md @@ -58,3 +58,25 @@ directive: $.properties['@search.action']['x-ms-client-name'] = '__actionType'; $.required = ['@search.action']; ``` + + +### Change text to _text in SuggestResult + +```yaml +directive: + - from: swagger-document + where: $.definitions.SuggestResult.properties['@search.text'] + transform: > + $['x-ms-client-name'] = '_text' +``` + +### Change score to _score & highlights to _highlights in SuggestResult + +```yaml +directive: + - from: swagger-document + where: $.definitions.SearchResult + transform: > + $.properties['@search.score']['x-ms-client-name'] = '_score'; + $.properties['@search.highlights']['x-ms-client-name'] = '_highlights'; +```