Package version:

Class SearchClient<TModel>

Class used to perform operations against a search index, including querying documents in the index as well as adding, updating, and removing them.

Type Parameters

  • TModel extends object

Implements

Constructors

  • Creates an instance of SearchClient.

    Example usage:

    const { SearchClient, AzureKeyCredential } = require("@azure/search-documents");

    const client = new SearchClient(
    "<endpoint>",
    "<indexName>",
    new AzureKeyCredential("<Admin Key>")
    );

    Optionally, the type of the model can be used to enable strong typing and type hints:

    type TModel = {
    keyName: string;
    field1?: string | null;
    field2?: { anotherField?: string | null } | null;
    };

    const client = new SearchClient<TModel>(
    ...
    );

    Type Parameters

    • TModel extends object

      An optional type that represents the documents stored in the search index. For the best typing experience, all non-key fields should be marked optional and nullable, and the key property should have the non-nullable type string.

    Parameters

    • endpoint: string

      The endpoint of the search service

    • indexName: string

      The name of the index

    • credential: any

      Used to authenticate requests to the service.

    • options: SearchClientOptions = {}

      Used to configure the Search client.

    Returns SearchClient<TModel>

Properties

apiVersion: string = utils.defaultServiceVersion

The API version to use when communicating with the service.

use {@Link serviceVersion} instead

endpoint: string

The endpoint of the search service

indexName: string

The name of the index

pipeline: Pipeline

A reference to the internal HTTP pipeline for use with raw requests

serviceVersion: string = utils.defaultServiceVersion

The service version to use when communicating with the service.

Methods

  • Based on a partial searchText from the user, return a list of potential completion strings based on a specified suggester.

    Parameters

    • searchText: string

      The search text on which to base autocomplete results.

    • suggesterName: string

      The name of the suggester as specified in the suggesters collection that's part of the index definition.

    • options: any = {}

      Options to the autocomplete operation.

    Returns Promise<AutocompleteResult>

    import {
    AzureKeyCredential,
    SearchClient,
    SearchFieldArray,
    } from "@azure/search-documents";

    type TModel = {
    key: string;
    azure?: { sdk: string | null } | null;
    };

    const client = new SearchClient<TModel>(
    "endpoint.azure",
    "indexName",
    new AzureKeyCredential("key")
    );

    const searchFields: SearchFieldArray<TModel> = ["azure/sdk"];

    const autocompleteResult = await client.autocomplete(
    "searchText",
    "suggesterName",
    { searchFields }
    );
  • Retrieves the number of documents in the index.

    Parameters

    • options: OperationOptions = {}

      Options to the count operation.

    Returns Promise<number>

  • Performs a search on the current index given the specified arguments.

    Type Parameters

    • TFields extends any

    Parameters

    • OptionalsearchText: string

      Text to search

    • Optionaloptions: any

      Options for the search operation.

    Returns Promise<SearchDocumentsResult<TModel, TFields>>

    import {
    AzureKeyCredential,
    SearchClient,
    SearchFieldArray,
    } from "@azure/search-documents";

    type TModel = {
    key: string;
    azure?: { sdk: string | null } | null;
    };

    const client = new SearchClient<TModel>(
    "endpoint.azure",
    "indexName",
    new AzureKeyCredential("key")
    );

    const select = ["azure/sdk"] as const;
    const searchFields: SearchFieldArray<TModel> = ["azure/sdk"];

    const searchResult = await client.search("searchText", {
    select,
    searchFields,
    });
  • Returns a short list of suggestions based on the searchText and specified suggester.

    Type Parameters

    • TFields extends any = never

    Parameters

    • searchText: string

      The search text to use to suggest documents. Must be at least 1 character, and no more than 100 characters.

    • suggesterName: string

      The name of the suggester as specified in the suggesters collection that's part of the index definition.

    • options: any = {}

      Options for the suggest operation

    Returns Promise<SuggestDocumentsResult<TModel, TFields>>

    import {
    AzureKeyCredential,
    SearchClient,
    SearchFieldArray,
    } from "@azure/search-documents";

    type TModel = {
    key: string;
    azure?: { sdk: string | null } | null;
    };

    const client = new SearchClient<TModel>(
    "endpoint.azure",
    "indexName",
    new AzureKeyCredential("key")
    );

    const select = ["azure/sdk"] as const;
    const searchFields: SearchFieldArray<TModel> = ["azure/sdk"];

    const suggestResult = await client.suggest("searchText", "suggesterName", {
    select,
    searchFields,
    });