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

Changes related to the next Meilisearch release (v1.10.0) #1682

Merged
merged 15 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,29 @@ distinct_attribute_guide_filterable_1: |-
client.index('products').updateFilterableAttributes(['product_id', 'sku', 'url'])
distinct_attribute_guide_distinct_parameter_1: |-
client.index('products').search('white shirt', { distinct: 'sku' })
multi_search_federated_1: |-
client.multiSearch({
federation: {},
queries: [
{
indexUid: 'movies',
q: 'batman',
limit: 5,
},
{
indexUid: 'comics',
q: 'batman',
limit: 5,
},
]
})
search_parameter_reference_locales_1: |-
client.index('INDEX_NAME').search('進撃の巨人', { locales: ['jpn'] })
get_localized_attribute_settings_1: |-
client.index('INDEX_NAME').getLocalizedAttributes()
update_localized_attribute_settings_1: |-
client.index('INDEX_NAME').updateLocalizedAttributes([
{ attributePatterns: ['jpn'], locales: ['*_ja'] },
];)
reset_localized_attribute_settings_1: |-
client.index('INDEX_NAME').resetLocalizedAttributes()
18 changes: 13 additions & 5 deletions src/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Copyright: 2019, MeiliSearch
*/

'use strict';

import { Index } from '../indexes';
import {
KeyCreation,
Expand Down Expand Up @@ -34,6 +32,8 @@ import {
DeleteTasksQuery,
MultiSearchParams,
MultiSearchResponse,
SearchResponse,
FederatedMultiSearchParams,
} from '../types';
import { HttpRequests } from '../http-requests';
import { TaskClient, Task } from '../task';
Expand Down Expand Up @@ -216,10 +216,18 @@ class Client {
* @param config - Additional request configuration options
* @returns Promise containing the search responses
*/
async multiSearch<T extends Record<string, any> = Record<string, any>>(
queries?: MultiSearchParams,
multiSearch<T extends Record<string, unknown> = Record<string, any>>(
queries: MultiSearchParams,
config?: Partial<Request>,
): Promise<MultiSearchResponse<T>>;
multiSearch<T extends Record<string, unknown> = Record<string, any>>(
queries: FederatedMultiSearchParams,
config?: Partial<Request>,
): Promise<SearchResponse<T>>;
async multiSearch<T extends Record<string, unknown> = Record<string, any>>(
queries: MultiSearchParams | FederatedMultiSearchParams,
config?: Partial<Request>,
): Promise<MultiSearchResponse<T>> {
): Promise<MultiSearchResponse<T> | SearchResponse<T>> {
const url = `multi-search`;

return await this.httpRequest.post(url, queries, undefined, config);
Expand Down
64 changes: 64 additions & 0 deletions src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import {
Embedders,
SearchCutoffMs,
SearchSimilarDocumentsParams,
LocalizedAttributes,
UpdateDocumentsByFunctionOptions,
} from './types';
import { removeUndefinedFromObject } from './utils';
import { HttpRequests } from './http-requests';
Expand Down Expand Up @@ -629,6 +631,27 @@ class Index<T extends Record<string, any> = Record<string, any>> {
return task;
}

/**
* This is an EXPERIMENTAL feature, which may break without a major version.
* It's available after Meilisearch v1.10.
*
* More info about the feature:
* https://github.com/orgs/meilisearch/discussions/762 More info about
* experimental features in general:
* https://www.meilisearch.com/docs/reference/api/experimental-features
*
* @param options - Object containing the function string and related options
* @returns Promise containing an EnqueuedTask
*/
async updateDocumentsByFunction(
options: UpdateDocumentsByFunctionOptions,
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/documents/edit`;
const task = await this.httpRequest.post(url, options);

return new EnqueuedTask(task);
}

///
/// SETTINGS
///
Expand Down Expand Up @@ -1393,6 +1416,47 @@ class Index<T extends Record<string, any> = Record<string, any>> {

return new EnqueuedTask(task);
}

///
/// LOCALIZED ATTRIBUTES SETTINGS
///

/**
* Get the localized attributes settings.
*
* @returns Promise containing object of localized attributes settings
*/
async getLocalizedAttributes(): Promise<LocalizedAttributes> {
const url = `indexes/${this.uid}/settings/localized-attributes`;
return await this.httpRequest.get<LocalizedAttributes>(url);
}

/**
* Update the localized attributes settings.
*
* @param localizedAttributes - Localized attributes object
* @returns Promise containing an EnqueuedTask
*/
async updateLocalizedAttributes(
localizedAttributes: LocalizedAttributes,
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/localized-attributes`;
const task = await this.httpRequest.put(url, localizedAttributes);

return new EnqueuedTask(task);
}

/**
* Reset the localized attributes settings.
*
* @returns Promise containing an EnqueuedTask
*/
async resetLocalizedAttributes(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/localized-attributes`;
const task = await this.httpRequest.delete(url);

return new EnqueuedTask(task);
}
}

export { Index };
68 changes: 54 additions & 14 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export type HybridSearch = {
semanticRatio?: number;
};

// https://www.meilisearch.com/docs/reference/api/settings#localized-attributes
export type Locale = string;

export type SearchParams = Query &
Pagination &
Highlight &
Expand All @@ -130,6 +133,7 @@ export type SearchParams = Query &
hybrid?: HybridSearch;
distinct?: string;
retrieveVectors?: boolean;
locales?: Locale[];
};

// Search parameters for searches made with the GET method
Expand All @@ -152,13 +156,24 @@ export type SearchRequestGET = Pagination &
rankingScoreThreshold?: number;
distinct?: string;
retrieveVectors?: boolean;
locales?: Locale[];
};

export type FederationOptions = { weight: number };
export type MultiSearchFederation = { limit?: number; offset?: number };

export type MultiSearchQuery = SearchParams & { indexUid: string };
export type MultiSearchQueryWithFederation = MultiSearchQuery & {
federationOptions?: FederationOptions;
};

export type MultiSearchParams = {
queries: MultiSearchQuery[];
};
export type FederatedMultiSearchParams = {
federation: MultiSearchFederation;
queries: MultiSearchQueryWithFederation[];
};

export type CategoriesDistribution = {
[category: string]: number;
Expand All @@ -170,13 +185,6 @@ export type MatchesPosition<T> = Partial<
Record<keyof T, Array<{ start: number; length: number }>>
>;

export type Hit<T = Record<string, any>> = T & {
_formatted?: Partial<T>;
_matchesPosition?: MatchesPosition<T>;
_rankingScore?: number;
_rankingScoreDetails?: RankingScoreDetails;
};

export type RankingScoreDetails = {
words?: {
order: number;
Expand Down Expand Up @@ -208,6 +216,20 @@ export type RankingScoreDetails = {
[key: string]: Record<string, any> | undefined;
};

export type FederationDetails = {
indexUid: string;
queriesPosition: number;
weightedRankingScore: number;
};

export type Hit<T = Record<string, any>> = T & {
_formatted?: Partial<T>;
_matchesPosition?: MatchesPosition<T>;
_rankingScore?: number;
_rankingScoreDetails?: RankingScoreDetails;
_federation?: FederationDetails;
};

export type Hits<T = Record<string, any>> = Array<Hit<T>>;

export type FacetStat = { min: number; max: number };
Expand Down Expand Up @@ -326,6 +348,12 @@ export type DocumentsDeletionQuery = {

export type DocumentsIds = string[] | number[];

export type UpdateDocumentsByFunctionOptions = {
function: string;
filter?: string | string[];
context?: Record<string, any>;
};

/*
** Settings
*/
Expand Down Expand Up @@ -366,6 +394,7 @@ export type OpenAiEmbedder = {
documentTemplate?: string;
dimensions?: number;
distribution?: Distribution;
url?: string;
};

export type HuggingFaceEmbedder = {
Expand All @@ -388,12 +417,10 @@ export type RestEmbedder = {
apiKey?: string;
dimensions?: number;
documentTemplate?: string;
inputField?: string[] | null;
inputType?: 'text' | 'textArray';
query?: Record<string, any> | null;
pathToEmbeddings?: string[] | null;
embeddingObject?: string[] | null;
distribution?: Distribution;
request: Record<string, any>;
response: Record<string, any>;
headers?: Record<string, string>;
};

export type OllamaEmbedder = {
Expand All @@ -403,6 +430,7 @@ export type OllamaEmbedder = {
model?: string;
documentTemplate?: string;
distribution?: Distribution;
dimensions?: number;
};

export type Embedder =
Expand All @@ -428,6 +456,13 @@ export type PaginationSettings = {

export type SearchCutoffMs = number | null;

export type LocalizedAttribute = {
attributePatterns: string[];
locales: Locale[];
};

export type LocalizedAttributes = LocalizedAttribute[] | null;

export type Settings = {
filterableAttributes?: FilterableAttributes;
distinctAttribute?: DistinctAttribute;
Expand All @@ -446,6 +481,7 @@ export type Settings = {
proximityPrecision?: ProximityPrecision;
embedders?: Embedders;
searchCutoffMs?: SearchCutoffMs;
localizedAttributes?: LocalizedAttributes;
};

/*
Expand Down Expand Up @@ -677,9 +713,9 @@ export interface FetchError extends Error {

export type MeiliSearchErrorResponse = {
message: string;
// @TODO: Could be typed, but will it be kept updated? https://www.meilisearch.com/docs/reference/errors/error_codes
// https://www.meilisearch.com/docs/reference/errors/error_codes
code: string;
// @TODO: Could be typed https://www.meilisearch.com/docs/reference/errors/overview#errors
// https://www.meilisearch.com/docs/reference/errors/overview#errors
type: string;
link: string;
};
Expand Down Expand Up @@ -992,6 +1028,10 @@ export const ErrorStatusCode = {
/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_settings_search_cutoff_ms */
INVALID_SETTINGS_SEARCH_CUTOFF_MS: 'invalid_settings_search_cutoff_ms',

/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_settings_search_cutoff_ms */
INVALID_SETTINGS_LOCALIZED_ATTRIBUTES:
'invalid_settings_localized_attributes',

/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_task_before_enqueued_at */
INVALID_TASK_BEFORE_ENQUEUED_AT: 'invalid_task_before_enqueued_at',

Expand Down
Loading
Loading