mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-08-30 09:10:42 +02:00
Merge pull request #497 from flycran/master
Better typescript type inference
This commit is contained in:
379
index.d.ts
vendored
379
index.d.ts
vendored
@@ -77,26 +77,26 @@ declare module "flexsearch" {
|
|||||||
* **Document:**
|
* **Document:**
|
||||||
* * Search options: https://github.com/nextapps-de/flexsearch#search-options
|
* * Search options: https://github.com/nextapps-de/flexsearch#search-options
|
||||||
*/
|
*/
|
||||||
type SearchOptions = {
|
export type SearchOptions<R extends boolean = false> = {
|
||||||
query?: string;
|
query?: string;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
offset?: number;
|
offset?: number;
|
||||||
suggest?: boolean;
|
suggest?: boolean;
|
||||||
resolution?: number;
|
resolution?: number;
|
||||||
context?: boolean;
|
context?: boolean;
|
||||||
resolve?: boolean;
|
resolve?: R;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* **Document:**
|
* **Document:**
|
||||||
* * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor
|
* * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor
|
||||||
*/
|
*/
|
||||||
type DocumentDescriptor = {
|
type DocumentDescriptor<D extends DocumentData = DocumentData> = {
|
||||||
id?: string | "id";
|
id?: string | "id";
|
||||||
field?: FieldName | FieldName[] | FieldOptions | Array<FieldOptions>;
|
field?: FieldName<D> | FieldName<D>[] | FieldOptions<D> | Array<FieldOptions<D>>;
|
||||||
index?: FieldName | FieldName[] | FieldOptions | Array<FieldOptions>;
|
index?: FieldName<D> | FieldName<D>[] | FieldOptions<D> | Array<FieldOptions<D>>;
|
||||||
tag?: FieldName | FieldName[] | TagOptions | Array<TagOptions>;
|
tag?: FieldName<D> | FieldName<D>[] | TagOptions<D> | Array<TagOptions<D>>;
|
||||||
store?: FieldName | FieldName[] | StoreOptions | Array<StoreOptions> | boolean;
|
store?: FieldName<D> | FieldName<D>[] | StoreOptions | Array<StoreOptions> | boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type WorkerURL = string;
|
type WorkerURL = string;
|
||||||
@@ -104,7 +104,15 @@ declare module "flexsearch" {
|
|||||||
type WorkerConfigURL = string;
|
type WorkerConfigURL = string;
|
||||||
type WorkerConfigPath = string;
|
type WorkerConfigPath = string;
|
||||||
type SerializedFunctionString = string;
|
type SerializedFunctionString = string;
|
||||||
type FieldName = string;
|
type FieldName<D = DocumentData> = D extends object
|
||||||
|
? {
|
||||||
|
[K in keyof D]: K extends string
|
||||||
|
? D[K] extends Array<infer U>
|
||||||
|
? `${ K }` | `${ K }[]:${ FieldName<U> & string }`
|
||||||
|
: K | `${ K }:${ FieldName<D[K]> & string }`
|
||||||
|
: never
|
||||||
|
}[keyof D]
|
||||||
|
: never
|
||||||
/**
|
/**
|
||||||
* The template to be applied on matches (e.g. <code>"\<b>$1\</b>"</code>), where <code>\$1</code> is a placeholder for the matched partial
|
* The template to be applied on matches (e.g. <code>"\<b>$1\</b>"</code>), where <code>\$1</code> is a placeholder for the matched partial
|
||||||
*/
|
*/
|
||||||
@@ -116,26 +124,25 @@ declare module "flexsearch" {
|
|||||||
* * Language: https://github.com/nextapps-de/flexsearch#languages
|
* * Language: https://github.com/nextapps-de/flexsearch#languages
|
||||||
*/
|
*/
|
||||||
|
|
||||||
global {
|
|
||||||
type EncoderOptions = {
|
type EncoderOptions = {
|
||||||
rtl?: boolean;
|
rtl?: boolean;
|
||||||
dedupe?: boolean;
|
dedupe?: boolean;
|
||||||
include?: EncoderSplitOptions;
|
include?: EncoderSplitOptions;
|
||||||
exclude?: EncoderSplitOptions;
|
exclude?: EncoderSplitOptions;
|
||||||
split?: string|RegExp|""|false;
|
split?: string | RegExp | "" | false;
|
||||||
numeric?: boolean;
|
numeric?: boolean;
|
||||||
normalize?: boolean|((str: string) => string);
|
normalize?: boolean | ((str: string) => string);
|
||||||
prepare?: (str: string) => string;
|
prepare?: (str: string) => string;
|
||||||
finalize?: (terms: string[]) => string[];
|
finalize?: (terms: string[]) => string[];
|
||||||
filter?: Set<string>|((term: string) => boolean);
|
filter?: Set<string> | ((term: string) => boolean);
|
||||||
matcher?: Map<string, string>;
|
matcher?: Map<string, string>;
|
||||||
mapper?: Map<string, string>;
|
mapper?: Map<string, string>;
|
||||||
stemmer?: Map<string, string>;
|
stemmer?: Map<string, string>;
|
||||||
replacer?: [string|RegExp, string|""];
|
replacer?: [ string | RegExp, string | "" ];
|
||||||
minlength?: number;
|
minlength?: number;
|
||||||
maxlength?: number;
|
maxlength?: number;
|
||||||
cache?: boolean|number;
|
cache?: boolean | number;
|
||||||
}}
|
};
|
||||||
|
|
||||||
type EncoderSplitOptions = {
|
type EncoderSplitOptions = {
|
||||||
letter?: boolean;
|
letter?: boolean;
|
||||||
@@ -143,7 +150,7 @@ declare module "flexsearch" {
|
|||||||
symbol?: boolean;
|
symbol?: boolean;
|
||||||
punctuation?: boolean;
|
punctuation?: boolean;
|
||||||
control?: boolean;
|
control?: boolean;
|
||||||
char?: string|string[];
|
char?: string | string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Charset: {
|
export const Charset: {
|
||||||
@@ -171,7 +178,7 @@ declare module "flexsearch" {
|
|||||||
* * Right-To-Left: https://github.com/nextapps-de/flexsearch/doc/encoder.md#right-to-left-support
|
* * Right-To-Left: https://github.com/nextapps-de/flexsearch/doc/encoder.md#right-to-left-support
|
||||||
* * Language: https://github.com/nextapps-de/flexsearch/doc/encoder.md#built-in-language-packs
|
* * Language: https://github.com/nextapps-de/flexsearch/doc/encoder.md#built-in-language-packs
|
||||||
*/
|
*/
|
||||||
type IndexOptions = {
|
type IndexOptions<D extends StorageInterface = undefined> = {
|
||||||
preset?: Preset;
|
preset?: Preset;
|
||||||
tokenize?: Tokenizer;
|
tokenize?: Tokenizer;
|
||||||
cache?: boolean | number;
|
cache?: boolean | number;
|
||||||
@@ -185,11 +192,11 @@ declare module "flexsearch" {
|
|||||||
term: string,
|
term: string,
|
||||||
term_index: number,
|
term_index: number,
|
||||||
partial: string,
|
partial: string,
|
||||||
partial_index: number
|
partial_index: number,
|
||||||
) => number;
|
) => number;
|
||||||
|
|
||||||
// Persistent-specific options
|
// Persistent-specific options
|
||||||
db?: StorageInterface;
|
db?: D;
|
||||||
commit?: boolean;
|
commit?: boolean;
|
||||||
|
|
||||||
// Language-specific Options and Encoding
|
// Language-specific Options and Encoding
|
||||||
@@ -213,7 +220,7 @@ declare module "flexsearch" {
|
|||||||
|
|
||||||
type DefaultSearchResults = Id[];
|
type DefaultSearchResults = Id[];
|
||||||
type IntermediateSearchResults = Array<Id[]>;
|
type IntermediateSearchResults = Array<Id[]>;
|
||||||
type SearchResults = DefaultSearchResults | Resolver;
|
type SearchResults<R extends boolean = false> = R extends true ? Resolver : DefaultSearchResults;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* **Document:**
|
* **Document:**
|
||||||
@@ -222,73 +229,100 @@ declare module "flexsearch" {
|
|||||||
* * Usage: https://github.com/nextapps-de/flexsearch#usage
|
* * Usage: https://github.com/nextapps-de/flexsearch#usage
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export class Index {
|
type IndexSearchResultsWrapper<W extends boolean = false, D extends StorageInterface = undefined, R extends boolean = false> =
|
||||||
constructor(options?: Preset | IndexOptions);
|
W extends false ? D extends undefined ? SearchResults<R> : Promise<SearchResults<R>> : Promise<SearchResults<R>>
|
||||||
|
|
||||||
|
export class Index<W extends boolean = false, D extends StorageInterface = undefined> {
|
||||||
|
constructor(options?: Preset | IndexOptions<D>);
|
||||||
|
|
||||||
add(id: Id, content: string): this | Promise<this>;
|
add(id: Id, content: string): this | Promise<this>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated The method "append" will be removed in an upcoming release, just use "add" instead
|
* @deprecated The method "append" will be removed in an upcoming release, just use "add" instead
|
||||||
*/
|
*/
|
||||||
append(id: Id, content: string): this | Promise<this>;
|
append(id: Id, content: string): this | Promise<this>;
|
||||||
|
|
||||||
update(id: Id, content: string): this | Promise<this>;
|
update(id: Id, content: string): this | Promise<this>;
|
||||||
|
|
||||||
remove(id: Id): this | Promise<this>;
|
remove(id: Id): this | Promise<this>;
|
||||||
search(query: string, options?: Limit | SearchOptions): SearchResults | Promise<SearchResults>;
|
|
||||||
search(query: string, limit: number, options: SearchOptions): SearchResults | Promise<SearchResults>;
|
search(query: string, limit?: Limit): IndexSearchResultsWrapper<W, D>;
|
||||||
search(options: SearchOptions): SearchResults | Promise<SearchResults>;
|
search<R extends boolean = false>(query: string, options?: SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
searchCache(query: string, options?: Limit | SearchOptions): SearchResults | Promise<SearchResults>;
|
search<R extends boolean = false>(query: string, limit: Limit, options: SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
searchCache(query: string, limit: number, options: SearchOptions): SearchResults | Promise<SearchResults>;
|
search<R extends boolean = false>(options: SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
searchCache(options: SearchOptions): SearchResults | Promise<SearchResults>;
|
|
||||||
|
searchCache(query: string, limit?: Limit): W extends false ? SearchResults : Promise<SearchResults>;
|
||||||
|
searchCache<R extends boolean = false>(query: string, options?: Limit | SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
|
searchCache<R extends boolean = false>(query: string, limit: Limit, options: SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
|
searchCache<R extends boolean = false>(options: SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
|
|
||||||
// https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids
|
// https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids
|
||||||
contain(id: Id): boolean | Promise<boolean>;
|
contain(id: Id): boolean | Promise<boolean>;
|
||||||
|
|
||||||
clear(): void | Promise<void>;
|
clear(): void | Promise<void>;
|
||||||
|
|
||||||
cleanup(): void | Promise<void>;
|
cleanup(): void | Promise<void>;
|
||||||
|
|
||||||
// Export and Import
|
// Export and Import
|
||||||
export(handler: ExportHandler): void;
|
export(handler: ExportHandler): void;
|
||||||
export(handler: ExportHandlerAsync): Promise<void>;
|
export(handler: ExportHandlerAsync): Promise<void>;
|
||||||
|
|
||||||
import(key: string, data: string): void;
|
import(key: string, data: string): void;
|
||||||
|
|
||||||
serialize(with_function_wrapper?: boolean): SerializedFunctionString;
|
serialize(with_function_wrapper?: boolean): SerializedFunctionString;
|
||||||
|
|
||||||
// Persistent Index
|
// Persistent Index
|
||||||
mount(db: StorageInterface): Promise<void>;
|
mount(db: StorageInterface): Promise<void>;
|
||||||
|
|
||||||
commit(replace_all_contents?: boolean): Promise<void>;
|
commit(replace_all_contents?: boolean): Promise<void>;
|
||||||
|
|
||||||
destroy(): Promise<void>;
|
destroy(): Promise<void>;
|
||||||
|
|
||||||
// Async Methods
|
// Async Methods
|
||||||
addAsync(
|
addAsync(
|
||||||
id: Id,
|
id: Id,
|
||||||
content: string,
|
content: string,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
|
|
||||||
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
|
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
|
||||||
appendAsync(
|
appendAsync(
|
||||||
id: Id,
|
id: Id,
|
||||||
content: string,
|
content: string,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
|
|
||||||
updateAsync(
|
updateAsync(
|
||||||
id: Id,
|
id: Id,
|
||||||
content: string,
|
content: string,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
|
|
||||||
removeAsync(
|
removeAsync(
|
||||||
id: Id,
|
id: Id,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
|
|
||||||
searchAsync(
|
searchAsync(
|
||||||
query: string,
|
query: string,
|
||||||
options?: Limit | SearchOptions,
|
limit?: Limit,
|
||||||
callback?: AsyncCallback<SearchResults>
|
callback?: AsyncCallback<SearchResults>,
|
||||||
): Promise<SearchResults>
|
): Promise<SearchResults>
|
||||||
searchAsync(
|
searchAsync<R extends boolean = false>(
|
||||||
|
query: string,
|
||||||
|
options?: SearchOptions<R>,
|
||||||
|
callback?: AsyncCallback<SearchResults<R>>,
|
||||||
|
): Promise<SearchResults<R>>
|
||||||
|
searchAsync<R extends boolean = false>(
|
||||||
query: string,
|
query: string,
|
||||||
limit: Limit,
|
limit: Limit,
|
||||||
options?: SearchOptions,
|
options?: SearchOptions<R>,
|
||||||
callback?: AsyncCallback<SearchResults>
|
callback?: AsyncCallback<SearchResults<R>>,
|
||||||
): Promise<SearchResults>;
|
): Promise<SearchResults<R>>;
|
||||||
searchAsync(
|
searchAsync<R extends boolean = false>(
|
||||||
options: SearchOptions,
|
options: SearchOptions<R>,
|
||||||
callback?: AsyncCallback<SearchResults>
|
callback?: AsyncCallback<SearchResults<R>>,
|
||||||
): Promise<SearchResults>;
|
): Promise<SearchResults<R>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -298,9 +332,11 @@ declare module "flexsearch" {
|
|||||||
* * Worker index: https://github.com/nextapps-de/flexsearch#worker-index
|
* * Worker index: https://github.com/nextapps-de/flexsearch#worker-index
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export class Worker extends Index {
|
export class Worker extends Index<true> {
|
||||||
constructor(options?: Preset | WorkerIndexOptions);
|
constructor(options?: Preset | WorkerIndexOptions);
|
||||||
|
|
||||||
export(): Promise<void>;
|
export(): Promise<void>;
|
||||||
|
|
||||||
import(): Promise<void>;
|
import(): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,15 +344,15 @@ declare module "flexsearch" {
|
|||||||
/* Document Search */
|
/* Document Search */
|
||||||
/************************************/
|
/************************************/
|
||||||
|
|
||||||
type FieldOptions = IndexOptions & {
|
type FieldOptions<D extends DocumentData = DocumentData> = IndexOptions & {
|
||||||
field: FieldName,
|
field: FieldName<D>,
|
||||||
filter?: (content: string) => boolean;
|
filter?: (content: string) => boolean;
|
||||||
custom?: (content: string) => string;
|
custom?: (content: string) => string;
|
||||||
config?: WorkerConfigURL | WorkerConfigPath;
|
config?: WorkerConfigURL | WorkerConfigPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
type TagOptions = {
|
type TagOptions<D extends DocumentData> = {
|
||||||
field: FieldName;
|
field: FieldName<D>;
|
||||||
filter?: (content: string) => boolean;
|
filter?: (content: string) => boolean;
|
||||||
custom?: (content: string) => string;
|
custom?: (content: string) => string;
|
||||||
db?: StorageInterface;
|
db?: StorageInterface;
|
||||||
@@ -334,40 +370,47 @@ declare module "flexsearch" {
|
|||||||
* * Document options: https://github.com/nextapps-de/flexsearch#document-options
|
* * Document options: https://github.com/nextapps-de/flexsearch#document-options
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type DocumentOptions = IndexOptions & {
|
type WorkerType = boolean | WorkerURL | WorkerPath
|
||||||
worker?: boolean | WorkerURL | WorkerPath;
|
|
||||||
doc?: DocumentDescriptor;
|
type DocumentOptions<D extends DocumentData = DocumentData, W extends WorkerType = false, B extends StorageInterface = undefined> =
|
||||||
document?: DocumentDescriptor;
|
IndexOptions<B>
|
||||||
|
& {
|
||||||
|
worker?: W;
|
||||||
|
doc?: DocumentDescriptor<D>;
|
||||||
|
document?: DocumentDescriptor<D>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type DefaultDocumentSearchResults = Array<{
|
export type DefaultDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
|
||||||
field?: FieldName;
|
field?: FieldName<D>;
|
||||||
tag?: FieldName;
|
tag?: FieldName<D>;
|
||||||
result: DefaultSearchResults;
|
result: DefaultSearchResults;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
type EnrichedDocumentSearchResults = Array<{
|
export type EnrichedDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
|
||||||
field?: FieldName;
|
field?: FieldName<D>;
|
||||||
tag?: FieldName;
|
tag?: FieldName<D>;
|
||||||
result: Array<{
|
result: Array<{
|
||||||
id: Id;
|
id: Id;
|
||||||
doc: DocumentData | null;
|
doc: D | null;
|
||||||
highlight?: string;
|
highlight?: string;
|
||||||
}>;
|
}>;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
type MergedDocumentSearchResults = Array<{
|
export type MergedDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
|
||||||
id: Id;
|
id: Id;
|
||||||
doc: DocumentData | null;
|
doc: D | null;
|
||||||
field?: FieldName[];
|
field?: FieldName[];
|
||||||
tag?: FieldName[];
|
tag?: FieldName[];
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
type DocumentSearchResults =
|
type DocumentSearchResults<D extends DocumentData = DocumentData, R extends boolean = false, E extends boolean = false, M extends boolean = false> =
|
||||||
DefaultDocumentSearchResults |
|
R extends true ? Resolver :
|
||||||
EnrichedDocumentSearchResults |
|
M extends true ?
|
||||||
MergedDocumentSearchResults |
|
MergedDocumentSearchResults<D> :
|
||||||
Resolver;
|
E extends true ?
|
||||||
|
EnrichedDocumentSearchResults<D> :
|
||||||
|
DefaultDocumentSearchResults
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* # Document Search Result
|
* # Document Search Result
|
||||||
@@ -384,14 +427,16 @@ declare module "flexsearch" {
|
|||||||
* * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options
|
* * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type DocumentSearchOptions = SearchOptions & {
|
type DocumentSearchOptions<D extends DocumentData = DocumentData, R extends boolean = false, E extends boolean = false, M extends boolean = false> =
|
||||||
|
SearchOptions<R>
|
||||||
|
& {
|
||||||
tag?: Object | Array<Object>;
|
tag?: Object | Array<Object>;
|
||||||
field?: Array<DocumentSearchOptions> | DocumentSearchOptions | string[] | string;
|
field?: Array<DocumentSearchOptions<D, R, E, M>> | DocumentSearchOptions<D, R, E, M> | string[] | string;
|
||||||
index?: Array<DocumentSearchOptions> | DocumentSearchOptions | string[] | string;
|
index?: Array<DocumentSearchOptions<D, R, E, M>> | DocumentSearchOptions<D, R, E, M> | string[] | string;
|
||||||
pluck?: FieldName | DocumentSearchOptions;
|
pluck?: FieldName | DocumentSearchOptions<D, R, E, M>;
|
||||||
highlight?: HighlightOptions | TemplateResultHighlighting;
|
highlight?: HighlightOptions | TemplateResultHighlighting;
|
||||||
enrich?: boolean;
|
enrich?: E;
|
||||||
merge?: boolean;
|
merge?: M;
|
||||||
};
|
};
|
||||||
|
|
||||||
type DocumentValue =
|
type DocumentValue =
|
||||||
@@ -405,103 +450,153 @@ declare module "flexsearch" {
|
|||||||
[key: string]: DocumentValue | DocumentValue[];
|
[key: string]: DocumentValue | DocumentValue[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type DocumentSearchResultsWrapper<
|
||||||
|
D extends DocumentData = DocumentData,
|
||||||
|
W extends WorkerType = false,
|
||||||
|
B extends StorageInterface = undefined,
|
||||||
|
R extends boolean = false,
|
||||||
|
E extends boolean = false,
|
||||||
|
M extends boolean = false,
|
||||||
|
> = W extends false
|
||||||
|
? B extends undefined
|
||||||
|
? DocumentSearchResults<D, R, E, M>
|
||||||
|
: Promise<DocumentSearchResults<D, R, E, M>>
|
||||||
|
: Promise<DocumentSearchResults<D, R, E, M>>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* **Document:**
|
* **Document:**
|
||||||
* * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants
|
* * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants
|
||||||
* * API overview: https://github.com/nextapps-de/flexsearch#api-overview
|
* * API overview: https://github.com/nextapps-de/flexsearch#api-overview
|
||||||
* * Document store: https://github.com/nextapps-de/flexsearch#document-store
|
* * Document store: https://github.com/nextapps-de/flexsearch#document-store
|
||||||
*/
|
*/
|
||||||
export class Document {
|
export class Document<D extends DocumentData = DocumentData, W extends WorkerType = false, B extends StorageInterface = undefined> {
|
||||||
constructor(options: DocumentOptions);
|
constructor(options: DocumentOptions<D, W, B>);
|
||||||
|
|
||||||
|
add(id: Id, document: D): this | Promise<this>;
|
||||||
|
add(document: D): this | Promise<this>;
|
||||||
|
|
||||||
add(id: Id, document: DocumentData): this | Promise<this>;
|
|
||||||
add(document: DocumentData): this | Promise<this>;
|
|
||||||
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
|
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
|
||||||
append(id: Id, document: DocumentData): this | Promise<this>;
|
append(id: Id, document: D): this | Promise<this>;
|
||||||
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
|
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
|
||||||
append(document: DocumentData): this | Promise<this>;
|
append(document: D): this | Promise<this>;
|
||||||
update(id: Id, document: DocumentData): this | Promise<this>;
|
|
||||||
update(document: DocumentData): this | Promise<this>;
|
update(id: Id, document: D): this | Promise<this>;
|
||||||
|
update(document: D): this | Promise<this>;
|
||||||
|
|
||||||
remove(id: Id): this | Promise<this>;
|
remove(id: Id): this | Promise<this>;
|
||||||
remove(document: DocumentData): this | Promise<this>;
|
remove(document: D): this | Promise<this>;
|
||||||
|
|
||||||
// https://github.com/nextapps-de/flexsearch#field-search
|
// https://github.com/nextapps-de/flexsearch#field-search
|
||||||
search(query: string, options?: Limit | DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>;
|
search(query: string, limit: Limit): DocumentSearchResultsWrapper<D, W, B>;
|
||||||
search(query: string, limit: number, options: DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>;
|
search<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
search(options: DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>;
|
query: string,
|
||||||
searchCache(query: string, options?: Limit | DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>;
|
options?: DocumentSearchOptions<D, R, E, M>,
|
||||||
searchCache(query: string, limit: number, options: DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>;
|
): DocumentSearchResultsWrapper<D, W, B, R, E, M>;
|
||||||
searchCache(options: DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>;
|
search<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
|
query: string,
|
||||||
|
limit: Limit,
|
||||||
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
|
): DocumentSearchResultsWrapper<D, W, B, R, E, M>;
|
||||||
|
search<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
|
): DocumentSearchResultsWrapper<D, W, B, R, E, M>;
|
||||||
|
|
||||||
|
searchCache(query: string, limit: Limit): W extends false ? DocumentSearchResults<D> : Promise<DocumentSearchResults<D>>;
|
||||||
|
searchCache<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
|
query: string,
|
||||||
|
options?: DocumentSearchOptions<D, R, E, M>,
|
||||||
|
): DocumentSearchResultsWrapper<D, W, B, R, E, M>;
|
||||||
|
searchCache<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
|
query: string,
|
||||||
|
limit: Limit, options: DocumentSearchOptions<D, R, E, M>,
|
||||||
|
): DocumentSearchResultsWrapper<D, W, B, R, E, M>;
|
||||||
|
searchCache<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
|
): DocumentSearchResultsWrapper<D, W, B, R, E, M>;
|
||||||
|
|
||||||
// https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids
|
// https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids
|
||||||
contain(id: Id): boolean | Promise<boolean>;
|
contain(id: Id): boolean | Promise<boolean>;
|
||||||
|
|
||||||
clear(): void | Promise<void>;
|
clear(): void | Promise<void>;
|
||||||
|
|
||||||
cleanup(): void | Promise<void>;
|
cleanup(): void | Promise<void>;
|
||||||
get(id: Id): Promise<DocumentData> | DocumentData | null;
|
|
||||||
set(id: Id, document: DocumentData): this;
|
get(id: Id): Promise<D> | D | null;
|
||||||
set(document: DocumentData): this;
|
|
||||||
|
set(id: Id, document: D): this;
|
||||||
|
set(document: D): this;
|
||||||
|
|
||||||
// Export and Import
|
// Export and Import
|
||||||
export(handler: ExportHandler): void;
|
export(handler: ExportHandler): void;
|
||||||
export(handler: ExportHandlerAsync): Promise<void>;
|
export(handler: ExportHandlerAsync): Promise<void>;
|
||||||
|
|
||||||
import(key: string, data: string): void;
|
import(key: string, data: string): void;
|
||||||
|
|
||||||
// Persistent Index
|
// Persistent Index
|
||||||
mount(db: StorageInterface): Promise<void>;
|
mount(db: StorageInterface): Promise<void>;
|
||||||
|
|
||||||
commit(replace_all_contents?: boolean): Promise<void>;
|
commit(replace_all_contents?: boolean): Promise<void>;
|
||||||
|
|
||||||
destroy(): Promise<void>;
|
destroy(): Promise<void>;
|
||||||
|
|
||||||
// Async Methods
|
// Async Methods
|
||||||
addAsync(
|
addAsync(
|
||||||
id: Id,
|
id: Id,
|
||||||
document: DocumentData,
|
document: D,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
addAsync(
|
addAsync(
|
||||||
document: DocumentData,
|
document: D,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
|
|
||||||
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
|
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
|
||||||
appendAsync(
|
appendAsync(
|
||||||
id: Id,
|
id: Id,
|
||||||
document: DocumentData,
|
document: D,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
|
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
|
||||||
appendAsync(
|
appendAsync(
|
||||||
document: DocumentData,
|
document: D,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
|
|
||||||
updateAsync(
|
updateAsync(
|
||||||
id: Id,
|
id: Id,
|
||||||
document: DocumentData,
|
document: D,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
updateAsync(
|
updateAsync(
|
||||||
document: DocumentData,
|
document: D,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
|
|
||||||
removeAsync(
|
removeAsync(
|
||||||
id: Id,
|
id: Id,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
removeAsync(
|
removeAsync(
|
||||||
document: DocumentData,
|
document: D,
|
||||||
callback?: AsyncCallback<void>
|
callback?: AsyncCallback<void>,
|
||||||
): Promise<this>;
|
): Promise<this>;
|
||||||
searchAsync(
|
|
||||||
|
searchAsync(query: string, limit?: Limit): Promise<DocumentSearchResults<D>>
|
||||||
|
searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
query: string,
|
query: string,
|
||||||
options?: Limit | DocumentSearchOptions,
|
options?: DocumentSearchOptions<D, R, E, M>,
|
||||||
callback?: AsyncCallback<DocumentSearchResults>
|
callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
|
||||||
): Promise<DocumentSearchResults>
|
): Promise<DocumentSearchResults<D, R, E, M>>
|
||||||
searchAsync(
|
searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
query: string,
|
query: string,
|
||||||
limit: number,
|
limit: Limit,
|
||||||
options?: DocumentSearchOptions,
|
options?: DocumentSearchOptions<D, R, E, M>,
|
||||||
callback?: AsyncCallback<DocumentSearchResults>
|
callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
|
||||||
): Promise<DocumentSearchResults>;
|
): Promise<DocumentSearchResults<D, R, E, M>>;
|
||||||
searchAsync(
|
searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
options: DocumentSearchOptions,
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
callback?: AsyncCallback<DocumentSearchResults>
|
callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
|
||||||
): Promise<DocumentSearchResults>;
|
): Promise<DocumentSearchResults<D, R, E, M>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
type IdType =
|
type IdType =
|
||||||
@@ -577,42 +672,64 @@ declare module "flexsearch" {
|
|||||||
|
|
||||||
export class Encoder {
|
export class Encoder {
|
||||||
constructor(options?: EncoderOptions);
|
constructor(options?: EncoderOptions);
|
||||||
|
|
||||||
assign(options: EncoderOptions): this;
|
assign(options: EncoderOptions): this;
|
||||||
|
|
||||||
encode(content: string): string[];
|
encode(content: string): string[];
|
||||||
|
|
||||||
addMapper(char_match: string, char_replace: string): this;
|
addMapper(char_match: string, char_replace: string): this;
|
||||||
|
|
||||||
addMatcher(match: string, replace: string): this;
|
addMatcher(match: string, replace: string): this;
|
||||||
|
|
||||||
addStemmer(match: string, replace: string): this;
|
addStemmer(match: string, replace: string): this;
|
||||||
|
|
||||||
addFilter(term: string): this;
|
addFilter(term: string): this;
|
||||||
|
|
||||||
addReplacer(match: string | RegExp, replace: string): this;
|
addReplacer(match: string | RegExp, replace: string): this;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Resolver {
|
export class Resolver {
|
||||||
constructor(options?: ResolverOptions | IntermediateSearchResults);
|
|
||||||
result: IntermediateSearchResults;
|
result: IntermediateSearchResults;
|
||||||
|
|
||||||
|
constructor(options?: ResolverOptions | IntermediateSearchResults);
|
||||||
|
|
||||||
and(options: ResolverOptions): this;
|
and(options: ResolverOptions): this;
|
||||||
|
|
||||||
or(options: ResolverOptions): this;
|
or(options: ResolverOptions): this;
|
||||||
|
|
||||||
xor(options: ResolverOptions): this;
|
xor(options: ResolverOptions): this;
|
||||||
|
|
||||||
not(options: ResolverOptions): this;
|
not(options: ResolverOptions): this;
|
||||||
|
|
||||||
limit(limit: number): this;
|
limit(limit: number): this;
|
||||||
|
|
||||||
offset(offset: number): this;
|
offset(offset: number): this;
|
||||||
|
|
||||||
boost(boost: number): this;
|
boost(boost: number): this;
|
||||||
|
|
||||||
resolve(options?: DefaultResolve): SearchResults;
|
resolve(options?: DefaultResolve): SearchResults;
|
||||||
}
|
}
|
||||||
|
|
||||||
global{
|
export class StorageInterface {
|
||||||
class StorageInterface {
|
|
||||||
constructor(name: string, config: PersistentOptions);
|
|
||||||
constructor(config: string | PersistentOptions);
|
|
||||||
mount(index: Index | Document) : Promise<void>;
|
|
||||||
open() : Promise<void>;
|
|
||||||
close() : Promise<void>;
|
|
||||||
destroy() : Promise<void>;
|
|
||||||
clear() : Promise<void>;
|
|
||||||
db: any;
|
db: any;
|
||||||
}}
|
|
||||||
|
|
||||||
export class IndexedDB extends StorageInterface{
|
constructor(name: string, config: PersistentOptions);
|
||||||
db: IDBDatabase
|
|
||||||
|
constructor(config: string | PersistentOptions);
|
||||||
|
|
||||||
|
mount(index: Index | Document): Promise<void>;
|
||||||
|
|
||||||
|
open(): Promise<void>;
|
||||||
|
|
||||||
|
close(): Promise<void>;
|
||||||
|
|
||||||
|
destroy(): Promise<void>;
|
||||||
|
|
||||||
|
clear(): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class IndexedDB extends StorageInterface {
|
||||||
|
db: IDBDatabase;
|
||||||
}
|
}
|
||||||
|
|
||||||
const FlexSearch: {
|
const FlexSearch: {
|
||||||
@@ -623,7 +740,7 @@ declare module "flexsearch" {
|
|||||||
Charset: typeof Charset,
|
Charset: typeof Charset,
|
||||||
Resolver: typeof Resolver,
|
Resolver: typeof Resolver,
|
||||||
IndexedDB: typeof IndexedDB
|
IndexedDB: typeof IndexedDB
|
||||||
}
|
};
|
||||||
|
|
||||||
export default FlexSearch;
|
export default FlexSearch;
|
||||||
}
|
}
|
||||||
|
70
test/types.ts
Normal file
70
test/types.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import {
|
||||||
|
DefaultDocumentSearchResults,
|
||||||
|
Document,
|
||||||
|
Index,
|
||||||
|
Worker,
|
||||||
|
Resolver,
|
||||||
|
StorageInterface,
|
||||||
|
} from "flexsearch";
|
||||||
|
import "../index";
|
||||||
|
|
||||||
|
async function test() {
|
||||||
|
const document = new Document<{
|
||||||
|
title: string
|
||||||
|
description: string
|
||||||
|
tags: {
|
||||||
|
name: string
|
||||||
|
id: number
|
||||||
|
}[]
|
||||||
|
}>({
|
||||||
|
document: {
|
||||||
|
index: [ "tags" ],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// The correct type
|
||||||
|
const doc1 = await document.searchAsync({});
|
||||||
|
const doc2: Resolver = await document.searchAsync({
|
||||||
|
resolve: true,
|
||||||
|
});
|
||||||
|
const doc3 = await document.searchAsync({
|
||||||
|
enrich: true,
|
||||||
|
});
|
||||||
|
const doc4 = await document.searchAsync({
|
||||||
|
enrich: true,
|
||||||
|
merge: true,
|
||||||
|
});
|
||||||
|
doc4[0].doc.title;
|
||||||
|
// The wrong type
|
||||||
|
// @ts-expect-error
|
||||||
|
const docw1: Resolver = await document.searchAsync({});
|
||||||
|
// @ts-expect-error
|
||||||
|
const docw2: DefaultDocumentSearchResults = await document.searchAsync({
|
||||||
|
enrich: true,
|
||||||
|
});
|
||||||
|
// Promise?
|
||||||
|
const documentNoWorker = new Document({});
|
||||||
|
const doc5 = documentNoWorker.search({}); // No Promise
|
||||||
|
|
||||||
|
const documentWorker = new Document({
|
||||||
|
worker: true,
|
||||||
|
});
|
||||||
|
const doc6 = await documentWorker.search({}) // Promise
|
||||||
|
|
||||||
|
const documentWorker2 = new Document({
|
||||||
|
worker: '...',
|
||||||
|
});
|
||||||
|
const doc7 = await documentWorker2.search({}) // Promise
|
||||||
|
|
||||||
|
const index = new Index({})
|
||||||
|
const idx = index.search({}) // No Promise
|
||||||
|
|
||||||
|
const worker = new Worker()
|
||||||
|
const wkr = await worker.search({}) // Promise
|
||||||
|
|
||||||
|
const documentDb = new Document({
|
||||||
|
db: {} as unknown as StorageInterface
|
||||||
|
})
|
||||||
|
|
||||||
|
const doc8 = documentDb.search({}) // Promise
|
||||||
|
}
|
||||||
|
|
5
tsconfig.json
Normal file
5
tsconfig.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ESNext"
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user