From 16b4e5faede80e597778a2069a2e305513d916da Mon Sep 17 00:00:00 2001 From: Javad Date: Wed, 6 Nov 2024 11:02:03 +0330 Subject: [PATCH 1/4] fix: interface pollution for service and index --- index.go | 685 ++------------------------------------- index_interface.go | 611 ++++++++++++++++++++++++++++++++++ index_task.go | 75 +++++ index_task_test.go | 264 +++++++++++++++ index_test.go | 257 --------------- meilisearch.go | 175 ++-------- meilisearch_interface.go | 185 +++++++++++ 7 files changed, 1180 insertions(+), 1072 deletions(-) create mode 100644 index_interface.go create mode 100644 index_task.go create mode 100644 index_task_test.go create mode 100644 meilisearch_interface.go diff --git a/index.go b/index.go index 31a5c3b9..21accfe2 100644 --- a/index.go +++ b/index.go @@ -2,12 +2,7 @@ package meilisearch import ( "context" - "encoding/json" - "io" "net/http" - "strconv" - "strings" - "time" ) // index is the type that represent an index in meilisearch @@ -17,603 +12,39 @@ type index struct { client *client } -type IndexManager interface { - // FetchInfo retrieves information about the index. - FetchInfo() (*IndexResult, error) - - // FetchInfoWithContext retrieves information about the index using the provided context for cancellation. - FetchInfoWithContext(ctx context.Context) (*IndexResult, error) - - // FetchPrimaryKey retrieves the primary key of the index. - FetchPrimaryKey() (*string, error) - - // FetchPrimaryKeyWithContext retrieves the primary key of the index using the provided context for cancellation. - FetchPrimaryKeyWithContext(ctx context.Context) (*string, error) - - // UpdateIndex updates the primary key of the index. - UpdateIndex(primaryKey string) (*TaskInfo, error) - - // UpdateIndexWithContext updates the primary key of the index using the provided context for cancellation. - UpdateIndexWithContext(ctx context.Context, primaryKey string) (*TaskInfo, error) - - // Delete removes the index identified by the given UID. - Delete(uid string) (bool, error) - - // DeleteWithContext removes the index identified by the given UID using the provided context for cancellation. - DeleteWithContext(ctx context.Context, uid string) (bool, error) - - // GetStats retrieves statistical information about the index. - GetStats() (*StatsIndex, error) - - // GetStatsWithContext retrieves statistical information about the index using the provided context for cancellation. - GetStatsWithContext(ctx context.Context) (*StatsIndex, error) - - // AddDocuments adds multiple documents to the index. - AddDocuments(documentsPtr interface{}, primaryKey ...string) (*TaskInfo, error) - - // AddDocumentsWithContext adds multiple documents to the index using the provided context for cancellation. - AddDocumentsWithContext(ctx context.Context, documentsPtr interface{}, primaryKey ...string) (*TaskInfo, error) - - // AddDocumentsInBatches adds documents to the index in batches of specified size. - AddDocumentsInBatches(documentsPtr interface{}, batchSize int, primaryKey ...string) ([]TaskInfo, error) - - // AddDocumentsInBatchesWithContext adds documents to the index in batches of specified size using the provided context for cancellation. - AddDocumentsInBatchesWithContext(ctx context.Context, documentsPtr interface{}, batchSize int, primaryKey ...string) ([]TaskInfo, error) - - // AddDocumentsCsv adds documents from a CSV byte array to the index. - AddDocumentsCsv(documents []byte, options *CsvDocumentsQuery) (*TaskInfo, error) - - // AddDocumentsCsvWithContext adds documents from a CSV byte array to the index using the provided context for cancellation. - AddDocumentsCsvWithContext(ctx context.Context, documents []byte, options *CsvDocumentsQuery) (*TaskInfo, error) - - // AddDocumentsCsvInBatches adds documents from a CSV byte array to the index in batches of specified size. - AddDocumentsCsvInBatches(documents []byte, batchSize int, options *CsvDocumentsQuery) ([]TaskInfo, error) - - // AddDocumentsCsvInBatchesWithContext adds documents from a CSV byte array to the index in batches of specified size using the provided context for cancellation. - AddDocumentsCsvInBatchesWithContext(ctx context.Context, documents []byte, batchSize int, options *CsvDocumentsQuery) ([]TaskInfo, error) - - // AddDocumentsCsvFromReaderInBatches adds documents from a CSV reader to the index in batches of specified size. - AddDocumentsCsvFromReaderInBatches(documents io.Reader, batchSize int, options *CsvDocumentsQuery) ([]TaskInfo, error) - - // AddDocumentsCsvFromReaderInBatchesWithContext adds documents from a CSV reader to the index in batches of specified size using the provided context for cancellation. - AddDocumentsCsvFromReaderInBatchesWithContext(ctx context.Context, documents io.Reader, batchSize int, options *CsvDocumentsQuery) ([]TaskInfo, error) - - // AddDocumentsCsvFromReader adds documents from a CSV reader to the index. - AddDocumentsCsvFromReader(documents io.Reader, options *CsvDocumentsQuery) (*TaskInfo, error) - - // AddDocumentsCsvFromReaderWithContext adds documents from a CSV reader to the index using the provided context for cancellation. - AddDocumentsCsvFromReaderWithContext(ctx context.Context, documents io.Reader, options *CsvDocumentsQuery) (*TaskInfo, error) - - // AddDocumentsNdjson adds documents from a NDJSON byte array to the index. - AddDocumentsNdjson(documents []byte, primaryKey ...string) (*TaskInfo, error) - - // AddDocumentsNdjsonWithContext adds documents from a NDJSON byte array to the index using the provided context for cancellation. - AddDocumentsNdjsonWithContext(ctx context.Context, documents []byte, primaryKey ...string) (*TaskInfo, error) - - // AddDocumentsNdjsonInBatches adds documents from a NDJSON byte array to the index in batches of specified size. - AddDocumentsNdjsonInBatches(documents []byte, batchSize int, primaryKey ...string) ([]TaskInfo, error) - - // AddDocumentsNdjsonInBatchesWithContext adds documents from a NDJSON byte array to the index in batches of specified size using the provided context for cancellation. - AddDocumentsNdjsonInBatchesWithContext(ctx context.Context, documents []byte, batchSize int, primaryKey ...string) ([]TaskInfo, error) - - // AddDocumentsNdjsonFromReader adds documents from a NDJSON reader to the index. - AddDocumentsNdjsonFromReader(documents io.Reader, primaryKey ...string) (*TaskInfo, error) - - // AddDocumentsNdjsonFromReaderWithContext adds documents from a NDJSON reader to the index using the provided context for cancellation. - AddDocumentsNdjsonFromReaderWithContext(ctx context.Context, documents io.Reader, primaryKey ...string) (*TaskInfo, error) - - // AddDocumentsNdjsonFromReaderInBatches adds documents from a NDJSON reader to the index in batches of specified size. - AddDocumentsNdjsonFromReaderInBatches(documents io.Reader, batchSize int, primaryKey ...string) ([]TaskInfo, error) - - // AddDocumentsNdjsonFromReaderInBatchesWithContext adds documents from a NDJSON reader to the index in batches of specified size using the provided context for cancellation. - AddDocumentsNdjsonFromReaderInBatchesWithContext(ctx context.Context, documents io.Reader, batchSize int, primaryKey ...string) ([]TaskInfo, error) - - // UpdateDocuments updates multiple documents in the index. - UpdateDocuments(documentsPtr interface{}, primaryKey ...string) (*TaskInfo, error) - - // UpdateDocumentsWithContext updates multiple documents in the index using the provided context for cancellation. - UpdateDocumentsWithContext(ctx context.Context, documentsPtr interface{}, primaryKey ...string) (*TaskInfo, error) - - // UpdateDocumentsInBatches updates documents in the index in batches of specified size. - UpdateDocumentsInBatches(documentsPtr interface{}, batchSize int, primaryKey ...string) ([]TaskInfo, error) - - // UpdateDocumentsInBatchesWithContext updates documents in the index in batches of specified size using the provided context for cancellation. - UpdateDocumentsInBatchesWithContext(ctx context.Context, documentsPtr interface{}, batchSize int, primaryKey ...string) ([]TaskInfo, error) - - // UpdateDocumentsCsv updates documents in the index from a CSV byte array. - UpdateDocumentsCsv(documents []byte, options *CsvDocumentsQuery) (*TaskInfo, error) - - // UpdateDocumentsCsvWithContext updates documents in the index from a CSV byte array using the provided context for cancellation. - UpdateDocumentsCsvWithContext(ctx context.Context, documents []byte, options *CsvDocumentsQuery) (*TaskInfo, error) - - // UpdateDocumentsCsvInBatches updates documents in the index from a CSV byte array in batches of specified size. - UpdateDocumentsCsvInBatches(documents []byte, batchsize int, options *CsvDocumentsQuery) ([]TaskInfo, error) - - // UpdateDocumentsCsvInBatchesWithContext updates documents in the index from a CSV byte array in batches of specified size using the provided context for cancellation. - UpdateDocumentsCsvInBatchesWithContext(ctx context.Context, documents []byte, batchsize int, options *CsvDocumentsQuery) ([]TaskInfo, error) - - // UpdateDocumentsNdjson updates documents in the index from a NDJSON byte array. - UpdateDocumentsNdjson(documents []byte, primaryKey ...string) (*TaskInfo, error) - - // UpdateDocumentsNdjsonWithContext updates documents in the index from a NDJSON byte array using the provided context for cancellation. - UpdateDocumentsNdjsonWithContext(ctx context.Context, documents []byte, primaryKey ...string) (*TaskInfo, error) - - // UpdateDocumentsNdjsonInBatches updates documents in the index from a NDJSON byte array in batches of specified size. - UpdateDocumentsNdjsonInBatches(documents []byte, batchsize int, primaryKey ...string) ([]TaskInfo, error) - - // UpdateDocumentsNdjsonInBatchesWithContext updates documents in the index from a NDJSON byte array in batches of specified size using the provided context for cancellation. - UpdateDocumentsNdjsonInBatchesWithContext(ctx context.Context, documents []byte, batchsize int, primaryKey ...string) ([]TaskInfo, error) - - // GetDocument retrieves a single document from the index by identifier. - GetDocument(identifier string, request *DocumentQuery, documentPtr interface{}) error - - // GetDocumentWithContext retrieves a single document from the index by identifier using the provided context for cancellation. - GetDocumentWithContext(ctx context.Context, identifier string, request *DocumentQuery, documentPtr interface{}) error - - // GetDocuments retrieves multiple documents from the index. - GetDocuments(param *DocumentsQuery, resp *DocumentsResult) error - - // GetDocumentsWithContext retrieves multiple documents from the index using the provided context for cancellation. - GetDocumentsWithContext(ctx context.Context, param *DocumentsQuery, resp *DocumentsResult) error - - // DeleteDocument deletes a single document from the index by identifier. - DeleteDocument(identifier string) (*TaskInfo, error) - - // DeleteDocumentWithContext deletes a single document from the index by identifier using the provided context for cancellation. - DeleteDocumentWithContext(ctx context.Context, identifier string) (*TaskInfo, error) - - // DeleteDocuments deletes multiple documents from the index by identifiers. - DeleteDocuments(identifiers []string) (*TaskInfo, error) - - // DeleteDocumentsWithContext deletes multiple documents from the index by identifiers using the provided context for cancellation. - DeleteDocumentsWithContext(ctx context.Context, identifiers []string) (*TaskInfo, error) - - // DeleteDocumentsByFilter deletes documents from the index by filter. - DeleteDocumentsByFilter(filter interface{}) (*TaskInfo, error) - - // DeleteDocumentsByFilterWithContext deletes documents from the index by filter using the provided context for cancellation. - DeleteDocumentsByFilterWithContext(ctx context.Context, filter interface{}) (*TaskInfo, error) - - // DeleteAllDocuments deletes all documents from the index. - DeleteAllDocuments() (*TaskInfo, error) - - // DeleteAllDocumentsWithContext deletes all documents from the index using the provided context for cancellation. - DeleteAllDocumentsWithContext(ctx context.Context) (*TaskInfo, error) - - // Search performs a search query on the index. - Search(query string, request *SearchRequest) (*SearchResponse, error) - - // SearchWithContext performs a search query on the index using the provided context for cancellation. - SearchWithContext(ctx context.Context, query string, request *SearchRequest) (*SearchResponse, error) - - // SearchRaw performs a raw search query on the index, returning a JSON response. - SearchRaw(query string, request *SearchRequest) (*json.RawMessage, error) - - // SearchRawWithContext performs a raw search query on the index using the provided context for cancellation, returning a JSON response. - SearchRawWithContext(ctx context.Context, query string, request *SearchRequest) (*json.RawMessage, error) - - // FacetSearch performs a facet search query on the index. - FacetSearch(request *FacetSearchRequest) (*json.RawMessage, error) - - // FacetSearchWithContext performs a facet search query on the index using the provided context for cancellation. - FacetSearchWithContext(ctx context.Context, request *FacetSearchRequest) (*json.RawMessage, error) - - // SearchSimilarDocuments performs a search for similar documents. - SearchSimilarDocuments(param *SimilarDocumentQuery, resp *SimilarDocumentResult) error - - // SearchSimilarDocumentsWithContext performs a search for similar documents using the provided context for cancellation. - SearchSimilarDocumentsWithContext(ctx context.Context, param *SimilarDocumentQuery, resp *SimilarDocumentResult) error - - // GetTask retrieves a task by its UID. - GetTask(taskUID int64) (*Task, error) - - // GetTaskWithContext retrieves a task by its UID using the provided context for cancellation. - GetTaskWithContext(ctx context.Context, taskUID int64) (*Task, error) - - // GetTasks retrieves multiple tasks based on query parameters. - GetTasks(param *TasksQuery) (*TaskResult, error) - - // GetTasksWithContext retrieves multiple tasks based on query parameters using the provided context for cancellation. - GetTasksWithContext(ctx context.Context, param *TasksQuery) (*TaskResult, error) - - // GetSettings retrieves the settings of the index. - GetSettings() (*Settings, error) - - // GetSettingsWithContext retrieves the settings of the index using the provided context for cancellation. - GetSettingsWithContext(ctx context.Context) (*Settings, error) - - // UpdateSettings updates the settings of the index. - UpdateSettings(request *Settings) (*TaskInfo, error) - - // UpdateSettingsWithContext updates the settings of the index using the provided context for cancellation. - UpdateSettingsWithContext(ctx context.Context, request *Settings) (*TaskInfo, error) - - // ResetSettings resets the settings of the index to default values. - ResetSettings() (*TaskInfo, error) - - // ResetSettingsWithContext resets the settings of the index to default values using the provided context for cancellation. - ResetSettingsWithContext(ctx context.Context) (*TaskInfo, error) - - // GetRankingRules retrieves the ranking rules of the index. - GetRankingRules() (*[]string, error) - - // GetRankingRulesWithContext retrieves the ranking rules of the index using the provided context for cancellation. - GetRankingRulesWithContext(ctx context.Context) (*[]string, error) - - // UpdateRankingRules updates the ranking rules of the index. - UpdateRankingRules(request *[]string) (*TaskInfo, error) - - // UpdateRankingRulesWithContext updates the ranking rules of the index using the provided context for cancellation. - UpdateRankingRulesWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) - - // ResetRankingRules resets the ranking rules of the index to default values. - ResetRankingRules() (*TaskInfo, error) - - // ResetRankingRulesWithContext resets the ranking rules of the index to default values using the provided context for cancellation. - ResetRankingRulesWithContext(ctx context.Context) (*TaskInfo, error) - - // GetDistinctAttribute retrieves the distinct attribute of the index. - GetDistinctAttribute() (*string, error) - - // GetDistinctAttributeWithContext retrieves the distinct attribute of the index using the provided context for cancellation. - GetDistinctAttributeWithContext(ctx context.Context) (*string, error) - - // UpdateDistinctAttribute updates the distinct attribute of the index. - UpdateDistinctAttribute(request string) (*TaskInfo, error) - - // UpdateDistinctAttributeWithContext updates the distinct attribute of the index using the provided context for cancellation. - UpdateDistinctAttributeWithContext(ctx context.Context, request string) (*TaskInfo, error) - - // ResetDistinctAttribute resets the distinct attribute of the index to default value. - ResetDistinctAttribute() (*TaskInfo, error) - - // ResetDistinctAttributeWithContext resets the distinct attribute of the index to default value using the provided context for cancellation. - ResetDistinctAttributeWithContext(ctx context.Context) (*TaskInfo, error) - - // GetSearchableAttributes retrieves the searchable attributes of the index. - GetSearchableAttributes() (*[]string, error) - - // GetSearchableAttributesWithContext retrieves the searchable attributes of the index using the provided context for cancellation. - GetSearchableAttributesWithContext(ctx context.Context) (*[]string, error) - - // UpdateSearchableAttributes updates the searchable attributes of the index. - UpdateSearchableAttributes(request *[]string) (*TaskInfo, error) - - // UpdateSearchableAttributesWithContext updates the searchable attributes of the index using the provided context for cancellation. - UpdateSearchableAttributesWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) - - // ResetSearchableAttributes resets the searchable attributes of the index to default values. - ResetSearchableAttributes() (*TaskInfo, error) - - // ResetSearchableAttributesWithContext resets the searchable attributes of the index to default values using the provided context for cancellation. - ResetSearchableAttributesWithContext(ctx context.Context) (*TaskInfo, error) - - // GetDisplayedAttributes retrieves the displayed attributes of the index. - GetDisplayedAttributes() (*[]string, error) - - // GetDisplayedAttributesWithContext retrieves the displayed attributes of the index using the provided context for cancellation. - GetDisplayedAttributesWithContext(ctx context.Context) (*[]string, error) - - // UpdateDisplayedAttributes updates the displayed attributes of the index. - UpdateDisplayedAttributes(request *[]string) (*TaskInfo, error) - - // UpdateDisplayedAttributesWithContext updates the displayed attributes of the index using the provided context for cancellation. - UpdateDisplayedAttributesWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) - - // ResetDisplayedAttributes resets the displayed attributes of the index to default values. - ResetDisplayedAttributes() (*TaskInfo, error) - - // ResetDisplayedAttributesWithContext resets the displayed attributes of the index to default values using the provided context for cancellation. - ResetDisplayedAttributesWithContext(ctx context.Context) (*TaskInfo, error) - - // GetStopWords retrieves the stop words of the index. - GetStopWords() (*[]string, error) - - // GetStopWordsWithContext retrieves the stop words of the index using the provided context for cancellation. - GetStopWordsWithContext(ctx context.Context) (*[]string, error) - - // UpdateStopWords updates the stop words of the index. - UpdateStopWords(request *[]string) (*TaskInfo, error) - - // UpdateStopWordsWithContext updates the stop words of the index using the provided context for cancellation. - UpdateStopWordsWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) - - // ResetStopWords resets the stop words of the index to default values. - ResetStopWords() (*TaskInfo, error) - - // ResetStopWordsWithContext resets the stop words of the index to default values using the provided context for cancellation. - ResetStopWordsWithContext(ctx context.Context) (*TaskInfo, error) - - // GetSynonyms retrieves the synonyms of the index. - GetSynonyms() (*map[string][]string, error) - - // GetSynonymsWithContext retrieves the synonyms of the index using the provided context for cancellation. - GetSynonymsWithContext(ctx context.Context) (*map[string][]string, error) - - // UpdateSynonyms updates the synonyms of the index. - UpdateSynonyms(request *map[string][]string) (*TaskInfo, error) - - // UpdateSynonymsWithContext updates the synonyms of the index using the provided context for cancellation. - UpdateSynonymsWithContext(ctx context.Context, request *map[string][]string) (*TaskInfo, error) - - // ResetSynonyms resets the synonyms of the index to default values. - ResetSynonyms() (*TaskInfo, error) - - // ResetSynonymsWithContext resets the synonyms of the index to default values using the provided context for cancellation. - ResetSynonymsWithContext(ctx context.Context) (*TaskInfo, error) - - // GetFilterableAttributes retrieves the filterable attributes of the index. - GetFilterableAttributes() (*[]string, error) - - // GetFilterableAttributesWithContext retrieves the filterable attributes of the index using the provided context for cancellation. - GetFilterableAttributesWithContext(ctx context.Context) (*[]string, error) - - // UpdateFilterableAttributes updates the filterable attributes of the index. - UpdateFilterableAttributes(request *[]string) (*TaskInfo, error) - - // UpdateFilterableAttributesWithContext updates the filterable attributes of the index using the provided context for cancellation. - UpdateFilterableAttributesWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) - - // ResetFilterableAttributes resets the filterable attributes of the index to default values. - ResetFilterableAttributes() (*TaskInfo, error) - - // ResetFilterableAttributesWithContext resets the filterable attributes of the index to default values using the provided context for cancellation. - ResetFilterableAttributesWithContext(ctx context.Context) (*TaskInfo, error) - - // GetSortableAttributes retrieves the sortable attributes of the index. - GetSortableAttributes() (*[]string, error) - - // GetSortableAttributesWithContext retrieves the sortable attributes of the index using the provided context for cancellation. - GetSortableAttributesWithContext(ctx context.Context) (*[]string, error) - - // UpdateSortableAttributes updates the sortable attributes of the index. - UpdateSortableAttributes(request *[]string) (*TaskInfo, error) - - // UpdateSortableAttributesWithContext updates the sortable attributes of the index using the provided context for cancellation. - UpdateSortableAttributesWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) - - // ResetSortableAttributes resets the sortable attributes of the index to default values. - ResetSortableAttributes() (*TaskInfo, error) - - // ResetSortableAttributesWithContext resets the sortable attributes of the index to default values using the provided context for cancellation. - ResetSortableAttributesWithContext(ctx context.Context) (*TaskInfo, error) - - // GetTypoTolerance retrieves the typo tolerance settings of the index. - GetTypoTolerance() (*TypoTolerance, error) - - // GetTypoToleranceWithContext retrieves the typo tolerance settings of the index using the provided context for cancellation. - GetTypoToleranceWithContext(ctx context.Context) (*TypoTolerance, error) - - // UpdateTypoTolerance updates the typo tolerance settings of the index. - UpdateTypoTolerance(request *TypoTolerance) (*TaskInfo, error) - - // UpdateTypoToleranceWithContext updates the typo tolerance settings of the index using the provided context for cancellation. - UpdateTypoToleranceWithContext(ctx context.Context, request *TypoTolerance) (*TaskInfo, error) - - // ResetTypoTolerance resets the typo tolerance settings of the index to default values. - ResetTypoTolerance() (*TaskInfo, error) - - // ResetTypoToleranceWithContext resets the typo tolerance settings of the index to default values using the provided context for cancellation. - ResetTypoToleranceWithContext(ctx context.Context) (*TaskInfo, error) - - // GetPagination retrieves the pagination settings of the index. - GetPagination() (*Pagination, error) - - // GetPaginationWithContext retrieves the pagination settings of the index using the provided context for cancellation. - GetPaginationWithContext(ctx context.Context) (*Pagination, error) - - // UpdatePagination updates the pagination settings of the index. - UpdatePagination(request *Pagination) (*TaskInfo, error) - - // UpdatePaginationWithContext updates the pagination settings of the index using the provided context for cancellation. - UpdatePaginationWithContext(ctx context.Context, request *Pagination) (*TaskInfo, error) - - // ResetPagination resets the pagination settings of the index to default values. - ResetPagination() (*TaskInfo, error) - - // ResetPaginationWithContext resets the pagination settings of the index to default values using the provided context for cancellation. - ResetPaginationWithContext(ctx context.Context) (*TaskInfo, error) - - // GetFaceting retrieves the faceting settings of the index. - GetFaceting() (*Faceting, error) - - // GetFacetingWithContext retrieves the faceting settings of the index using the provided context for cancellation. - GetFacetingWithContext(ctx context.Context) (*Faceting, error) - - // UpdateFaceting updates the faceting settings of the index. - UpdateFaceting(request *Faceting) (*TaskInfo, error) - - // UpdateFacetingWithContext updates the faceting settings of the index using the provided context for cancellation. - UpdateFacetingWithContext(ctx context.Context, request *Faceting) (*TaskInfo, error) - - // ResetFaceting resets the faceting settings of the index to default values. - ResetFaceting() (*TaskInfo, error) - - // ResetFacetingWithContext resets the faceting settings of the index to default values using the provided context for cancellation. - ResetFacetingWithContext(ctx context.Context) (*TaskInfo, error) - - // GetEmbedders retrieves the embedders of the index. - GetEmbedders() (map[string]Embedder, error) - - // GetEmbeddersWithContext retrieves the embedders of the index using the provided context for cancellation. - GetEmbeddersWithContext(ctx context.Context) (map[string]Embedder, error) - - // UpdateEmbedders updates the embedders of the index. - UpdateEmbedders(request map[string]Embedder) (*TaskInfo, error) - - // UpdateEmbeddersWithContext updates the embedders of the index using the provided context for cancellation. - UpdateEmbeddersWithContext(ctx context.Context, request map[string]Embedder) (*TaskInfo, error) - - // ResetEmbedders resets the embedders of the index to default values. - ResetEmbedders() (*TaskInfo, error) - - // ResetEmbeddersWithContext resets the embedders of the index to default values using the provided context for cancellation. - ResetEmbeddersWithContext(ctx context.Context) (*TaskInfo, error) - - // GetSearchCutoffMs retrieves the search cutoff time in milliseconds. - GetSearchCutoffMs() (int64, error) - - // GetSearchCutoffMsWithContext retrieves the search cutoff time in milliseconds using the provided context for cancellation. - GetSearchCutoffMsWithContext(ctx context.Context) (int64, error) - - // UpdateSearchCutoffMs updates the search cutoff time in milliseconds. - UpdateSearchCutoffMs(request int64) (*TaskInfo, error) - - // UpdateSearchCutoffMsWithContext updates the search cutoff time in milliseconds using the provided context for cancellation. - UpdateSearchCutoffMsWithContext(ctx context.Context, request int64) (*TaskInfo, error) - - // ResetSearchCutoffMs resets the search cutoff time in milliseconds to default value. - ResetSearchCutoffMs() (*TaskInfo, error) - - // ResetSearchCutoffMsWithContext resets the search cutoff time in milliseconds to default value using the provided context for cancellation. - ResetSearchCutoffMsWithContext(ctx context.Context) (*TaskInfo, error) - - // GetSeparatorTokens returns separators tokens - // https://www.meilisearch.com/docs/reference/api/settings#get-separator-tokens - GetSeparatorTokens() ([]string, error) - - // GetSeparatorTokensWithContext returns separator tokens and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#get-separator-tokens - GetSeparatorTokensWithContext(ctx context.Context) ([]string, error) - - // UpdateSeparatorTokens update separator tokens - // https://www.meilisearch.com/docs/reference/api/settings#update-separator-tokens - UpdateSeparatorTokens(tokens []string) (*TaskInfo, error) - - // UpdateSeparatorTokensWithContext update separator tokens and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#update-separator-tokens - UpdateSeparatorTokensWithContext(ctx context.Context, tokens []string) (*TaskInfo, error) - - // ResetSeparatorTokens reset separator tokens - // https://www.meilisearch.com/docs/reference/api/settings#reset-separator-tokens - ResetSeparatorTokens() (*TaskInfo, error) - - // ResetSeparatorTokensWithContext reset separator tokens and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#reset-separator-tokens - ResetSeparatorTokensWithContext(ctx context.Context) (*TaskInfo, error) - - // GetNonSeparatorTokens returns non-separator tokens - // https://www.meilisearch.com/docs/reference/api/settings#get-non-separator-tokens - GetNonSeparatorTokens() ([]string, error) - - // GetNonSeparatorTokensWithContext returns non-separator tokens and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#get-non-separator-tokens - GetNonSeparatorTokensWithContext(ctx context.Context) ([]string, error) - - // UpdateNonSeparatorTokens update non-separator tokens - // https://www.meilisearch.com/docs/reference/api/settings#update-non-separator-tokens - UpdateNonSeparatorTokens(tokens []string) (*TaskInfo, error) - - // UpdateNonSeparatorTokensWithContext update non-separator tokens and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#update-non-separator-tokens - UpdateNonSeparatorTokensWithContext(ctx context.Context, tokens []string) (*TaskInfo, error) - - // ResetNonSeparatorTokens reset non-separator tokens - // https://www.meilisearch.com/docs/reference/api/settings#reset-non-separator-tokens - ResetNonSeparatorTokens() (*TaskInfo, error) - - // ResetNonSeparatorTokensWithContext reset non-separator tokens and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#reset-non-separator-tokens - ResetNonSeparatorTokensWithContext(ctx context.Context) (*TaskInfo, error) - - // GetDictionary returns user dictionary - // - //Allows users to instruct Meilisearch to consider groups of strings as a - //single term by adding a supplementary dictionary of user-defined terms. - //This is particularly useful when working with datasets containing many domain-specific - //words, and in languages where words are not separated by whitespace such as Japanese. - //Custom dictionaries are also useful in a few use-cases for space-separated languages, - //such as datasets with names such as "J. R. R. Tolkien" and "W. E. B. Du Bois". - // - // https://www.meilisearch.com/docs/reference/api/settings#get-dictionary - GetDictionary() ([]string, error) - - // GetDictionaryWithContext returns user dictionary and support parent context - // - //Allows users to instruct Meilisearch to consider groups of strings as a - //single term by adding a supplementary dictionary of user-defined terms. - //This is particularly useful when working with datasets containing many domain-specific - //words, and in languages where words are not separated by whitespace such as Japanese. - //Custom dictionaries are also useful in a few use-cases for space-separated languages, - //such as datasets with names such as "J. R. R. Tolkien" and "W. E. B. Du Bois". - // - // https://www.meilisearch.com/docs/reference/api/settings#get-dictionary - GetDictionaryWithContext(ctx context.Context) ([]string, error) - - // UpdateDictionary update user dictionary - // https://www.meilisearch.com/docs/reference/api/settings#update-dictionary - UpdateDictionary(words []string) (*TaskInfo, error) - - // UpdateDictionaryWithContext update user dictionary and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#update-dictionary - UpdateDictionaryWithContext(ctx context.Context, words []string) (*TaskInfo, error) - - // ResetDictionary reset user dictionary - // https://www.meilisearch.com/docs/reference/api/settings#reset-dictionary - ResetDictionary() (*TaskInfo, error) - - // ResetDictionaryWithContext reset user dictionary and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#reset-dictionary - ResetDictionaryWithContext(ctx context.Context) (*TaskInfo, error) - - // GetProximityPrecision returns ProximityPrecision configuration value - // https://www.meilisearch.com/docs/reference/api/settings#get-proximity-precision-settings - GetProximityPrecision() (ProximityPrecisionType, error) - - // GetProximityPrecisionWithContext returns ProximityPrecision configuration value and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#get-proximity-precision-settings - GetProximityPrecisionWithContext(ctx context.Context) (ProximityPrecisionType, error) - - // UpdateProximityPrecision set ProximityPrecision value ByWord or ByAttribute - // https://www.meilisearch.com/docs/reference/api/settings#update-proximity-precision-settings - UpdateProximityPrecision(proximityType ProximityPrecisionType) (*TaskInfo, error) - - // UpdateProximityPrecisionWithContext set ProximityPrecision value ByWord or ByAttribute and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#update-proximity-precision-settings - UpdateProximityPrecisionWithContext(ctx context.Context, proximityType ProximityPrecisionType) (*TaskInfo, error) - - // ResetProximityPrecision reset ProximityPrecision to default ByWord - // https://www.meilisearch.com/docs/reference/api/settings#reset-proximity-precision-settings - ResetProximityPrecision() (*TaskInfo, error) - - // ResetProximityPrecisionWithContext reset ProximityPrecision to default ByWord and support parent context - // https://www.meilisearch.com/docs/reference/api/settings#reset-proximity-precision-settings - ResetProximityPrecisionWithContext(ctx context.Context) (*TaskInfo, error) - - // GetLocalizedAttributes get the localized attributes settings of an index - // https://www.meilisearch.com/docs/reference/api/settings#get-localized-attributes-settings - GetLocalizedAttributes() ([]*LocalizedAttributes, error) - - // GetLocalizedAttributesWithContext get the localized attributes settings of an index using the provided context for cancellation - // https://www.meilisearch.com/docs/reference/api/settings#get-localized-attributes-settings - GetLocalizedAttributesWithContext(ctx context.Context) ([]*LocalizedAttributes, error) +func newIndex(cli *client, uid string) IndexManager { + return &index{ + client: cli, + uid: uid, + } +} - // UpdateLocalizedAttributes update the localized attributes settings of an index - // https://www.meilisearch.com/docs/reference/api/settings#update-localized-attribute-settings - UpdateLocalizedAttributes(request []*LocalizedAttributes) (*TaskInfo, error) +func (i *index) GetTaskReader() TaskReader { + return i +} - // UpdateLocalizedAttributesWithContext update the localized attributes settings of an index using the provided context for cancellation - // https://www.meilisearch.com/docs/reference/api/settings#update-localized-attribute-settings - UpdateLocalizedAttributesWithContext(ctx context.Context, request []*LocalizedAttributes) (*TaskInfo, error) +func (i *index) GetDocumentManager() DocumentManager { + return i +} - // ResetLocalizedAttributes reset the localized attributes settings - ResetLocalizedAttributes() (*TaskInfo, error) +func (i *index) GetDocumentReader() DocumentReader { + return i +} - // ResetLocalizedAttributesWithContext reset the localized attributes settings using the provided context for cancellation - ResetLocalizedAttributesWithContext(ctx context.Context) (*TaskInfo, error) +func (i *index) GetSettingsManager() SettingsManager { + return i +} - // WaitForTask waits for a task to complete by its UID with the given interval. - WaitForTask(taskUID int64, interval time.Duration) (*Task, error) +func (i *index) GetSettingsReader() SettingsReader { + return i +} - // WaitForTaskWithContext waits for a task to complete by its UID with the given interval using the provided context for cancellation. - WaitForTaskWithContext(ctx context.Context, taskUID int64, interval time.Duration) (*Task, error) +func (i *index) GetSearch() SearchReader { + return i } -func newIndex(cli *client, uid string) IndexManager { - return &index{ - client: cli, - uid: uid, - } +func (i *index) GetIndexReader() IndexReader { + return i } func (i *index) FetchInfo() (*IndexResult, error) { @@ -721,69 +152,3 @@ func (i *index) GetStatsWithContext(ctx context.Context) (*StatsIndex, error) { } return resp, nil } - -func (i *index) GetTask(taskUID int64) (*Task, error) { - return i.GetTaskWithContext(context.Background(), taskUID) -} - -func (i *index) GetTaskWithContext(ctx context.Context, taskUID int64) (*Task, error) { - return getTask(ctx, i.client, taskUID) -} - -func (i *index) GetTasks(param *TasksQuery) (*TaskResult, error) { - return i.GetTasksWithContext(context.Background(), param) -} - -func (i *index) GetTasksWithContext(ctx context.Context, param *TasksQuery) (*TaskResult, error) { - resp := new(TaskResult) - req := &internalRequest{ - endpoint: "/tasks", - method: http.MethodGet, - withRequest: nil, - withResponse: resp, - withQueryParams: map[string]string{}, - acceptedStatusCodes: []int{http.StatusOK}, - functionName: "GetTasks", - } - if param != nil { - if param.Limit != 0 { - req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10) - } - if param.From != 0 { - req.withQueryParams["from"] = strconv.FormatInt(param.From, 10) - } - if len(param.Statuses) != 0 { - statuses := make([]string, len(param.Statuses)) - for i, status := range param.Statuses { - statuses[i] = string(status) - } - req.withQueryParams["statuses"] = strings.Join(statuses, ",") - } - - if len(param.Types) != 0 { - types := make([]string, len(param.Types)) - for i, t := range param.Types { - types[i] = string(t) - } - req.withQueryParams["types"] = strings.Join(types, ",") - } - if len(param.IndexUIDS) != 0 { - param.IndexUIDS = append(param.IndexUIDS, i.uid) - req.withQueryParams["indexUids"] = strings.Join(param.IndexUIDS, ",") - } else { - req.withQueryParams["indexUids"] = i.uid - } - } - if err := i.client.executeRequest(ctx, req); err != nil { - return nil, err - } - return resp, nil -} - -func (i *index) WaitForTask(taskUID int64, interval time.Duration) (*Task, error) { - return waitForTask(context.Background(), i.client, taskUID, interval) -} - -func (i *index) WaitForTaskWithContext(ctx context.Context, taskUID int64, interval time.Duration) (*Task, error) { - return waitForTask(ctx, i.client, taskUID, interval) -} diff --git a/index_interface.go b/index_interface.go new file mode 100644 index 00000000..06151e94 --- /dev/null +++ b/index_interface.go @@ -0,0 +1,611 @@ +package meilisearch + +import ( + "context" + "encoding/json" + "io" +) + +type IndexManager interface { + IndexReader + TaskReader + DocumentManager + SettingsManager + SearchReader + + GetIndexReader() IndexReader + GetTaskReader() TaskReader + GetDocumentManager() DocumentManager + GetDocumentReader() DocumentReader + GetSettingsManager() SettingsManager + GetSettingsReader() SettingsReader + GetSearch() SearchReader + + // UpdateIndex updates the primary key of the index. + UpdateIndex(primaryKey string) (*TaskInfo, error) + + // UpdateIndexWithContext updates the primary key of the index using the provided context for cancellation. + UpdateIndexWithContext(ctx context.Context, primaryKey string) (*TaskInfo, error) + + // Delete removes the index identified by the given UID. + Delete(uid string) (bool, error) + + // DeleteWithContext removes the index identified by the given UID using the provided context for cancellation. + DeleteWithContext(ctx context.Context, uid string) (bool, error) +} + +type IndexReader interface { + // FetchInfo retrieves information about the index. + FetchInfo() (*IndexResult, error) + + // FetchInfoWithContext retrieves information about the index using the provided context for cancellation. + FetchInfoWithContext(ctx context.Context) (*IndexResult, error) + + // FetchPrimaryKey retrieves the primary key of the index. + FetchPrimaryKey() (*string, error) + + // FetchPrimaryKeyWithContext retrieves the primary key of the index using the provided context for cancellation. + FetchPrimaryKeyWithContext(ctx context.Context) (*string, error) + + // GetStats retrieves statistical information about the index. + GetStats() (*StatsIndex, error) + + // GetStatsWithContext retrieves statistical information about the index using the provided context for cancellation. + GetStatsWithContext(ctx context.Context) (*StatsIndex, error) +} + +type DocumentManager interface { + DocumentReader + + // AddDocuments adds multiple documents to the index. + AddDocuments(documentsPtr interface{}, primaryKey ...string) (*TaskInfo, error) + + // AddDocumentsWithContext adds multiple documents to the index using the provided context for cancellation. + AddDocumentsWithContext(ctx context.Context, documentsPtr interface{}, primaryKey ...string) (*TaskInfo, error) + + // AddDocumentsInBatches adds documents to the index in batches of specified size. + AddDocumentsInBatches(documentsPtr interface{}, batchSize int, primaryKey ...string) ([]TaskInfo, error) + + // AddDocumentsInBatchesWithContext adds documents to the index in batches of specified size using the provided context for cancellation. + AddDocumentsInBatchesWithContext(ctx context.Context, documentsPtr interface{}, batchSize int, primaryKey ...string) ([]TaskInfo, error) + + // AddDocumentsCsv adds documents from a CSV byte array to the index. + AddDocumentsCsv(documents []byte, options *CsvDocumentsQuery) (*TaskInfo, error) + + // AddDocumentsCsvWithContext adds documents from a CSV byte array to the index using the provided context for cancellation. + AddDocumentsCsvWithContext(ctx context.Context, documents []byte, options *CsvDocumentsQuery) (*TaskInfo, error) + + // AddDocumentsCsvInBatches adds documents from a CSV byte array to the index in batches of specified size. + AddDocumentsCsvInBatches(documents []byte, batchSize int, options *CsvDocumentsQuery) ([]TaskInfo, error) + + // AddDocumentsCsvInBatchesWithContext adds documents from a CSV byte array to the index in batches of specified size using the provided context for cancellation. + AddDocumentsCsvInBatchesWithContext(ctx context.Context, documents []byte, batchSize int, options *CsvDocumentsQuery) ([]TaskInfo, error) + + // AddDocumentsCsvFromReaderInBatches adds documents from a CSV reader to the index in batches of specified size. + AddDocumentsCsvFromReaderInBatches(documents io.Reader, batchSize int, options *CsvDocumentsQuery) ([]TaskInfo, error) + + // AddDocumentsCsvFromReaderInBatchesWithContext adds documents from a CSV reader to the index in batches of specified size using the provided context for cancellation. + AddDocumentsCsvFromReaderInBatchesWithContext(ctx context.Context, documents io.Reader, batchSize int, options *CsvDocumentsQuery) ([]TaskInfo, error) + + // AddDocumentsCsvFromReader adds documents from a CSV reader to the index. + AddDocumentsCsvFromReader(documents io.Reader, options *CsvDocumentsQuery) (*TaskInfo, error) + + // AddDocumentsCsvFromReaderWithContext adds documents from a CSV reader to the index using the provided context for cancellation. + AddDocumentsCsvFromReaderWithContext(ctx context.Context, documents io.Reader, options *CsvDocumentsQuery) (*TaskInfo, error) + + // AddDocumentsNdjson adds documents from a NDJSON byte array to the index. + AddDocumentsNdjson(documents []byte, primaryKey ...string) (*TaskInfo, error) + + // AddDocumentsNdjsonWithContext adds documents from a NDJSON byte array to the index using the provided context for cancellation. + AddDocumentsNdjsonWithContext(ctx context.Context, documents []byte, primaryKey ...string) (*TaskInfo, error) + + // AddDocumentsNdjsonInBatches adds documents from a NDJSON byte array to the index in batches of specified size. + AddDocumentsNdjsonInBatches(documents []byte, batchSize int, primaryKey ...string) ([]TaskInfo, error) + + // AddDocumentsNdjsonInBatchesWithContext adds documents from a NDJSON byte array to the index in batches of specified size using the provided context for cancellation. + AddDocumentsNdjsonInBatchesWithContext(ctx context.Context, documents []byte, batchSize int, primaryKey ...string) ([]TaskInfo, error) + + // AddDocumentsNdjsonFromReader adds documents from a NDJSON reader to the index. + AddDocumentsNdjsonFromReader(documents io.Reader, primaryKey ...string) (*TaskInfo, error) + + // AddDocumentsNdjsonFromReaderWithContext adds documents from a NDJSON reader to the index using the provided context for cancellation. + AddDocumentsNdjsonFromReaderWithContext(ctx context.Context, documents io.Reader, primaryKey ...string) (*TaskInfo, error) + + // AddDocumentsNdjsonFromReaderInBatches adds documents from a NDJSON reader to the index in batches of specified size. + AddDocumentsNdjsonFromReaderInBatches(documents io.Reader, batchSize int, primaryKey ...string) ([]TaskInfo, error) + + // AddDocumentsNdjsonFromReaderInBatchesWithContext adds documents from a NDJSON reader to the index in batches of specified size using the provided context for cancellation. + AddDocumentsNdjsonFromReaderInBatchesWithContext(ctx context.Context, documents io.Reader, batchSize int, primaryKey ...string) ([]TaskInfo, error) + + // UpdateDocuments updates multiple documents in the index. + UpdateDocuments(documentsPtr interface{}, primaryKey ...string) (*TaskInfo, error) + + // UpdateDocumentsWithContext updates multiple documents in the index using the provided context for cancellation. + UpdateDocumentsWithContext(ctx context.Context, documentsPtr interface{}, primaryKey ...string) (*TaskInfo, error) + + // UpdateDocumentsInBatches updates documents in the index in batches of specified size. + UpdateDocumentsInBatches(documentsPtr interface{}, batchSize int, primaryKey ...string) ([]TaskInfo, error) + + // UpdateDocumentsInBatchesWithContext updates documents in the index in batches of specified size using the provided context for cancellation. + UpdateDocumentsInBatchesWithContext(ctx context.Context, documentsPtr interface{}, batchSize int, primaryKey ...string) ([]TaskInfo, error) + + // UpdateDocumentsCsv updates documents in the index from a CSV byte array. + UpdateDocumentsCsv(documents []byte, options *CsvDocumentsQuery) (*TaskInfo, error) + + // UpdateDocumentsCsvWithContext updates documents in the index from a CSV byte array using the provided context for cancellation. + UpdateDocumentsCsvWithContext(ctx context.Context, documents []byte, options *CsvDocumentsQuery) (*TaskInfo, error) + + // UpdateDocumentsCsvInBatches updates documents in the index from a CSV byte array in batches of specified size. + UpdateDocumentsCsvInBatches(documents []byte, batchsize int, options *CsvDocumentsQuery) ([]TaskInfo, error) + + // UpdateDocumentsCsvInBatchesWithContext updates documents in the index from a CSV byte array in batches of specified size using the provided context for cancellation. + UpdateDocumentsCsvInBatchesWithContext(ctx context.Context, documents []byte, batchsize int, options *CsvDocumentsQuery) ([]TaskInfo, error) + + // UpdateDocumentsNdjson updates documents in the index from a NDJSON byte array. + UpdateDocumentsNdjson(documents []byte, primaryKey ...string) (*TaskInfo, error) + + // UpdateDocumentsNdjsonWithContext updates documents in the index from a NDJSON byte array using the provided context for cancellation. + UpdateDocumentsNdjsonWithContext(ctx context.Context, documents []byte, primaryKey ...string) (*TaskInfo, error) + + // UpdateDocumentsNdjsonInBatches updates documents in the index from a NDJSON byte array in batches of specified size. + UpdateDocumentsNdjsonInBatches(documents []byte, batchsize int, primaryKey ...string) ([]TaskInfo, error) + + // UpdateDocumentsNdjsonInBatchesWithContext updates documents in the index from a NDJSON byte array in batches of specified size using the provided context for cancellation. + UpdateDocumentsNdjsonInBatchesWithContext(ctx context.Context, documents []byte, batchsize int, primaryKey ...string) ([]TaskInfo, error) + + // DeleteDocument deletes a single document from the index by identifier. + DeleteDocument(identifier string) (*TaskInfo, error) + + // DeleteDocumentWithContext deletes a single document from the index by identifier using the provided context for cancellation. + DeleteDocumentWithContext(ctx context.Context, identifier string) (*TaskInfo, error) + + // DeleteDocuments deletes multiple documents from the index by identifiers. + DeleteDocuments(identifiers []string) (*TaskInfo, error) + + // DeleteDocumentsWithContext deletes multiple documents from the index by identifiers using the provided context for cancellation. + DeleteDocumentsWithContext(ctx context.Context, identifiers []string) (*TaskInfo, error) + + // DeleteDocumentsByFilter deletes documents from the index by filter. + DeleteDocumentsByFilter(filter interface{}) (*TaskInfo, error) + + // DeleteDocumentsByFilterWithContext deletes documents from the index by filter using the provided context for cancellation. + DeleteDocumentsByFilterWithContext(ctx context.Context, filter interface{}) (*TaskInfo, error) + + // DeleteAllDocuments deletes all documents from the index. + DeleteAllDocuments() (*TaskInfo, error) + + // DeleteAllDocumentsWithContext deletes all documents from the index using the provided context for cancellation. + DeleteAllDocumentsWithContext(ctx context.Context) (*TaskInfo, error) +} + +type DocumentReader interface { + // GetDocument retrieves a single document from the index by identifier. + GetDocument(identifier string, request *DocumentQuery, documentPtr interface{}) error + + // GetDocumentWithContext retrieves a single document from the index by identifier using the provided context for cancellation. + GetDocumentWithContext(ctx context.Context, identifier string, request *DocumentQuery, documentPtr interface{}) error + + // GetDocuments retrieves multiple documents from the index. + GetDocuments(param *DocumentsQuery, resp *DocumentsResult) error + + // GetDocumentsWithContext retrieves multiple documents from the index using the provided context for cancellation. + GetDocumentsWithContext(ctx context.Context, param *DocumentsQuery, resp *DocumentsResult) error +} + +type SearchReader interface { + // Search performs a search query on the index. + Search(query string, request *SearchRequest) (*SearchResponse, error) + + // SearchWithContext performs a search query on the index using the provided context for cancellation. + SearchWithContext(ctx context.Context, query string, request *SearchRequest) (*SearchResponse, error) + + // SearchRaw performs a raw search query on the index, returning a JSON response. + SearchRaw(query string, request *SearchRequest) (*json.RawMessage, error) + + // SearchRawWithContext performs a raw search query on the index using the provided context for cancellation, returning a JSON response. + SearchRawWithContext(ctx context.Context, query string, request *SearchRequest) (*json.RawMessage, error) + + // FacetSearch performs a facet search query on the index. + FacetSearch(request *FacetSearchRequest) (*json.RawMessage, error) + + // FacetSearchWithContext performs a facet search query on the index using the provided context for cancellation. + FacetSearchWithContext(ctx context.Context, request *FacetSearchRequest) (*json.RawMessage, error) + + // SearchSimilarDocuments performs a search for similar documents. + SearchSimilarDocuments(param *SimilarDocumentQuery, resp *SimilarDocumentResult) error + + // SearchSimilarDocumentsWithContext performs a search for similar documents using the provided context for cancellation. + SearchSimilarDocumentsWithContext(ctx context.Context, param *SimilarDocumentQuery, resp *SimilarDocumentResult) error +} + +type SettingsManager interface { + SettingsReader + + // UpdateSettings updates the settings of the index. + UpdateSettings(request *Settings) (*TaskInfo, error) + + // UpdateSettingsWithContext updates the settings of the index using the provided context for cancellation. + UpdateSettingsWithContext(ctx context.Context, request *Settings) (*TaskInfo, error) + + // ResetSettings resets the settings of the index to default values. + ResetSettings() (*TaskInfo, error) + + // ResetSettingsWithContext resets the settings of the index to default values using the provided context for cancellation. + ResetSettingsWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateRankingRules updates the ranking rules of the index. + UpdateRankingRules(request *[]string) (*TaskInfo, error) + + // UpdateRankingRulesWithContext updates the ranking rules of the index using the provided context for cancellation. + UpdateRankingRulesWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) + + // ResetRankingRules resets the ranking rules of the index to default values. + ResetRankingRules() (*TaskInfo, error) + + // ResetRankingRulesWithContext resets the ranking rules of the index to default values using the provided context for cancellation. + ResetRankingRulesWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateDistinctAttribute updates the distinct attribute of the index. + UpdateDistinctAttribute(request string) (*TaskInfo, error) + + // UpdateDistinctAttributeWithContext updates the distinct attribute of the index using the provided context for cancellation. + UpdateDistinctAttributeWithContext(ctx context.Context, request string) (*TaskInfo, error) + + // ResetDistinctAttribute resets the distinct attribute of the index to default value. + ResetDistinctAttribute() (*TaskInfo, error) + + // ResetDistinctAttributeWithContext resets the distinct attribute of the index to default value using the provided context for cancellation. + ResetDistinctAttributeWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateSearchableAttributes updates the searchable attributes of the index. + UpdateSearchableAttributes(request *[]string) (*TaskInfo, error) + + // UpdateSearchableAttributesWithContext updates the searchable attributes of the index using the provided context for cancellation. + UpdateSearchableAttributesWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) + + // ResetSearchableAttributes resets the searchable attributes of the index to default values. + ResetSearchableAttributes() (*TaskInfo, error) + + // ResetSearchableAttributesWithContext resets the searchable attributes of the index to default values using the provided context for cancellation. + ResetSearchableAttributesWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateDisplayedAttributes updates the displayed attributes of the index. + UpdateDisplayedAttributes(request *[]string) (*TaskInfo, error) + + // UpdateDisplayedAttributesWithContext updates the displayed attributes of the index using the provided context for cancellation. + UpdateDisplayedAttributesWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) + + // ResetDisplayedAttributes resets the displayed attributes of the index to default values. + ResetDisplayedAttributes() (*TaskInfo, error) + + // ResetDisplayedAttributesWithContext resets the displayed attributes of the index to default values using the provided context for cancellation. + ResetDisplayedAttributesWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateStopWords updates the stop words of the index. + UpdateStopWords(request *[]string) (*TaskInfo, error) + + // UpdateStopWordsWithContext updates the stop words of the index using the provided context for cancellation. + UpdateStopWordsWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) + + // ResetStopWords resets the stop words of the index to default values. + ResetStopWords() (*TaskInfo, error) + + // ResetStopWordsWithContext resets the stop words of the index to default values using the provided context for cancellation. + ResetStopWordsWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateSynonyms updates the synonyms of the index. + UpdateSynonyms(request *map[string][]string) (*TaskInfo, error) + + // UpdateSynonymsWithContext updates the synonyms of the index using the provided context for cancellation. + UpdateSynonymsWithContext(ctx context.Context, request *map[string][]string) (*TaskInfo, error) + + // ResetSynonyms resets the synonyms of the index to default values. + ResetSynonyms() (*TaskInfo, error) + + // ResetSynonymsWithContext resets the synonyms of the index to default values using the provided context for cancellation. + ResetSynonymsWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateFilterableAttributes updates the filterable attributes of the index. + UpdateFilterableAttributes(request *[]string) (*TaskInfo, error) + + // UpdateFilterableAttributesWithContext updates the filterable attributes of the index using the provided context for cancellation. + UpdateFilterableAttributesWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) + + // ResetFilterableAttributes resets the filterable attributes of the index to default values. + ResetFilterableAttributes() (*TaskInfo, error) + + // ResetFilterableAttributesWithContext resets the filterable attributes of the index to default values using the provided context for cancellation. + ResetFilterableAttributesWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateSortableAttributes updates the sortable attributes of the index. + UpdateSortableAttributes(request *[]string) (*TaskInfo, error) + + // UpdateSortableAttributesWithContext updates the sortable attributes of the index using the provided context for cancellation. + UpdateSortableAttributesWithContext(ctx context.Context, request *[]string) (*TaskInfo, error) + + // ResetSortableAttributes resets the sortable attributes of the index to default values. + ResetSortableAttributes() (*TaskInfo, error) + + // ResetSortableAttributesWithContext resets the sortable attributes of the index to default values using the provided context for cancellation. + ResetSortableAttributesWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateTypoTolerance updates the typo tolerance settings of the index. + UpdateTypoTolerance(request *TypoTolerance) (*TaskInfo, error) + + // UpdateTypoToleranceWithContext updates the typo tolerance settings of the index using the provided context for cancellation. + UpdateTypoToleranceWithContext(ctx context.Context, request *TypoTolerance) (*TaskInfo, error) + + // ResetTypoTolerance resets the typo tolerance settings of the index to default values. + ResetTypoTolerance() (*TaskInfo, error) + + // ResetTypoToleranceWithContext resets the typo tolerance settings of the index to default values using the provided context for cancellation. + ResetTypoToleranceWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdatePagination updates the pagination settings of the index. + UpdatePagination(request *Pagination) (*TaskInfo, error) + + // UpdatePaginationWithContext updates the pagination settings of the index using the provided context for cancellation. + UpdatePaginationWithContext(ctx context.Context, request *Pagination) (*TaskInfo, error) + + // ResetPagination resets the pagination settings of the index to default values. + ResetPagination() (*TaskInfo, error) + + // ResetPaginationWithContext resets the pagination settings of the index to default values using the provided context for cancellation. + ResetPaginationWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateFaceting updates the faceting settings of the index. + UpdateFaceting(request *Faceting) (*TaskInfo, error) + + // UpdateFacetingWithContext updates the faceting settings of the index using the provided context for cancellation. + UpdateFacetingWithContext(ctx context.Context, request *Faceting) (*TaskInfo, error) + + // ResetFaceting resets the faceting settings of the index to default values. + ResetFaceting() (*TaskInfo, error) + + // ResetFacetingWithContext resets the faceting settings of the index to default values using the provided context for cancellation. + ResetFacetingWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateEmbedders updates the embedders of the index. + UpdateEmbedders(request map[string]Embedder) (*TaskInfo, error) + + // UpdateEmbeddersWithContext updates the embedders of the index using the provided context for cancellation. + UpdateEmbeddersWithContext(ctx context.Context, request map[string]Embedder) (*TaskInfo, error) + + // ResetEmbedders resets the embedders of the index to default values. + ResetEmbedders() (*TaskInfo, error) + + // ResetEmbeddersWithContext resets the embedders of the index to default values using the provided context for cancellation. + ResetEmbeddersWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateSearchCutoffMs updates the search cutoff time in milliseconds. + UpdateSearchCutoffMs(request int64) (*TaskInfo, error) + + // UpdateSearchCutoffMsWithContext updates the search cutoff time in milliseconds using the provided context for cancellation. + UpdateSearchCutoffMsWithContext(ctx context.Context, request int64) (*TaskInfo, error) + + // ResetSearchCutoffMs resets the search cutoff time in milliseconds to default value. + ResetSearchCutoffMs() (*TaskInfo, error) + + // ResetSearchCutoffMsWithContext resets the search cutoff time in milliseconds to default value using the provided context for cancellation. + ResetSearchCutoffMsWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateSeparatorTokens update separator tokens + // https://www.meilisearch.com/docs/reference/api/settings#update-separator-tokens + UpdateSeparatorTokens(tokens []string) (*TaskInfo, error) + + // UpdateSeparatorTokensWithContext update separator tokens and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#update-separator-tokens + UpdateSeparatorTokensWithContext(ctx context.Context, tokens []string) (*TaskInfo, error) + + // ResetSeparatorTokens reset separator tokens + // https://www.meilisearch.com/docs/reference/api/settings#reset-separator-tokens + ResetSeparatorTokens() (*TaskInfo, error) + + // ResetSeparatorTokensWithContext reset separator tokens and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#reset-separator-tokens + ResetSeparatorTokensWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateNonSeparatorTokens update non-separator tokens + // https://www.meilisearch.com/docs/reference/api/settings#update-non-separator-tokens + UpdateNonSeparatorTokens(tokens []string) (*TaskInfo, error) + + // UpdateNonSeparatorTokensWithContext update non-separator tokens and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#update-non-separator-tokens + UpdateNonSeparatorTokensWithContext(ctx context.Context, tokens []string) (*TaskInfo, error) + + // ResetNonSeparatorTokens reset non-separator tokens + // https://www.meilisearch.com/docs/reference/api/settings#reset-non-separator-tokens + ResetNonSeparatorTokens() (*TaskInfo, error) + + // ResetNonSeparatorTokensWithContext reset non-separator tokens and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#reset-non-separator-tokens + ResetNonSeparatorTokensWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateDictionary update user dictionary + // https://www.meilisearch.com/docs/reference/api/settings#update-dictionary + UpdateDictionary(words []string) (*TaskInfo, error) + + // UpdateDictionaryWithContext update user dictionary and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#update-dictionary + UpdateDictionaryWithContext(ctx context.Context, words []string) (*TaskInfo, error) + + // ResetDictionary reset user dictionary + // https://www.meilisearch.com/docs/reference/api/settings#reset-dictionary + ResetDictionary() (*TaskInfo, error) + + // ResetDictionaryWithContext reset user dictionary and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#reset-dictionary + ResetDictionaryWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateProximityPrecision set ProximityPrecision value ByWord or ByAttribute + // https://www.meilisearch.com/docs/reference/api/settings#update-proximity-precision-settings + UpdateProximityPrecision(proximityType ProximityPrecisionType) (*TaskInfo, error) + + // UpdateProximityPrecisionWithContext set ProximityPrecision value ByWord or ByAttribute and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#update-proximity-precision-settings + UpdateProximityPrecisionWithContext(ctx context.Context, proximityType ProximityPrecisionType) (*TaskInfo, error) + + // ResetProximityPrecision reset ProximityPrecision to default ByWord + // https://www.meilisearch.com/docs/reference/api/settings#reset-proximity-precision-settings + ResetProximityPrecision() (*TaskInfo, error) + + // ResetProximityPrecisionWithContext reset ProximityPrecision to default ByWord and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#reset-proximity-precision-settings + ResetProximityPrecisionWithContext(ctx context.Context) (*TaskInfo, error) + + // UpdateLocalizedAttributes update the localized attributes settings of an index + // https://www.meilisearch.com/docs/reference/api/settings#update-localized-attribute-settings + UpdateLocalizedAttributes(request []*LocalizedAttributes) (*TaskInfo, error) + + // UpdateLocalizedAttributesWithContext update the localized attributes settings of an index using the provided context for cancellation + // https://www.meilisearch.com/docs/reference/api/settings#update-localized-attribute-settings + UpdateLocalizedAttributesWithContext(ctx context.Context, request []*LocalizedAttributes) (*TaskInfo, error) + + // ResetLocalizedAttributes reset the localized attributes settings + ResetLocalizedAttributes() (*TaskInfo, error) + + // ResetLocalizedAttributesWithContext reset the localized attributes settings using the provided context for cancellation + ResetLocalizedAttributesWithContext(ctx context.Context) (*TaskInfo, error) +} + +type SettingsReader interface { + // GetSettings retrieves the settings of the index. + GetSettings() (*Settings, error) + + // GetSettingsWithContext retrieves the settings of the index using the provided context for cancellation. + GetSettingsWithContext(ctx context.Context) (*Settings, error) + + // GetRankingRules retrieves the ranking rules of the index. + GetRankingRules() (*[]string, error) + + // GetRankingRulesWithContext retrieves the ranking rules of the index using the provided context for cancellation. + GetRankingRulesWithContext(ctx context.Context) (*[]string, error) + + // GetDistinctAttribute retrieves the distinct attribute of the index. + GetDistinctAttribute() (*string, error) + + // GetDistinctAttributeWithContext retrieves the distinct attribute of the index using the provided context for cancellation. + GetDistinctAttributeWithContext(ctx context.Context) (*string, error) + + // GetSearchableAttributes retrieves the searchable attributes of the index. + GetSearchableAttributes() (*[]string, error) + + // GetSearchableAttributesWithContext retrieves the searchable attributes of the index using the provided context for cancellation. + GetSearchableAttributesWithContext(ctx context.Context) (*[]string, error) + + // GetDisplayedAttributes retrieves the displayed attributes of the index. + GetDisplayedAttributes() (*[]string, error) + + // GetDisplayedAttributesWithContext retrieves the displayed attributes of the index using the provided context for cancellation. + GetDisplayedAttributesWithContext(ctx context.Context) (*[]string, error) + + // GetStopWords retrieves the stop words of the index. + GetStopWords() (*[]string, error) + + // GetStopWordsWithContext retrieves the stop words of the index using the provided context for cancellation. + GetStopWordsWithContext(ctx context.Context) (*[]string, error) + + // GetSynonyms retrieves the synonyms of the index. + GetSynonyms() (*map[string][]string, error) + + // GetSynonymsWithContext retrieves the synonyms of the index using the provided context for cancellation. + GetSynonymsWithContext(ctx context.Context) (*map[string][]string, error) + + // GetFilterableAttributes retrieves the filterable attributes of the index. + GetFilterableAttributes() (*[]string, error) + + // GetFilterableAttributesWithContext retrieves the filterable attributes of the index using the provided context for cancellation. + GetFilterableAttributesWithContext(ctx context.Context) (*[]string, error) + + // GetSortableAttributes retrieves the sortable attributes of the index. + GetSortableAttributes() (*[]string, error) + + // GetSortableAttributesWithContext retrieves the sortable attributes of the index using the provided context for cancellation. + GetSortableAttributesWithContext(ctx context.Context) (*[]string, error) + + // GetTypoTolerance retrieves the typo tolerance settings of the index. + GetTypoTolerance() (*TypoTolerance, error) + + // GetTypoToleranceWithContext retrieves the typo tolerance settings of the index using the provided context for cancellation. + GetTypoToleranceWithContext(ctx context.Context) (*TypoTolerance, error) + + // GetPagination retrieves the pagination settings of the index. + GetPagination() (*Pagination, error) + + // GetPaginationWithContext retrieves the pagination settings of the index using the provided context for cancellation. + GetPaginationWithContext(ctx context.Context) (*Pagination, error) + + // GetFaceting retrieves the faceting settings of the index. + GetFaceting() (*Faceting, error) + + // GetFacetingWithContext retrieves the faceting settings of the index using the provided context for cancellation. + GetFacetingWithContext(ctx context.Context) (*Faceting, error) + + // GetEmbedders retrieves the embedders of the index. + GetEmbedders() (map[string]Embedder, error) + + // GetEmbeddersWithContext retrieves the embedders of the index using the provided context for cancellation. + GetEmbeddersWithContext(ctx context.Context) (map[string]Embedder, error) + + // GetSearchCutoffMs retrieves the search cutoff time in milliseconds. + GetSearchCutoffMs() (int64, error) + + // GetSearchCutoffMsWithContext retrieves the search cutoff time in milliseconds using the provided context for cancellation. + GetSearchCutoffMsWithContext(ctx context.Context) (int64, error) + + // GetSeparatorTokens returns separators tokens + // https://www.meilisearch.com/docs/reference/api/settings#get-separator-tokens + GetSeparatorTokens() ([]string, error) + + // GetSeparatorTokensWithContext returns separator tokens and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#get-separator-tokens + GetSeparatorTokensWithContext(ctx context.Context) ([]string, error) + + // GetNonSeparatorTokens returns non-separator tokens + // https://www.meilisearch.com/docs/reference/api/settings#get-non-separator-tokens + GetNonSeparatorTokens() ([]string, error) + + // GetNonSeparatorTokensWithContext returns non-separator tokens and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#get-non-separator-tokens + GetNonSeparatorTokensWithContext(ctx context.Context) ([]string, error) + + // GetDictionary returns user dictionary + // + //Allows users to instruct Meilisearch to consider groups of strings as a + //single term by adding a supplementary dictionary of user-defined terms. + //This is particularly useful when working with datasets containing many domain-specific + //words, and in languages where words are not separated by whitespace such as Japanese. + //Custom dictionaries are also useful in a few use-cases for space-separated languages, + //such as datasets with names such as "J. R. R. Tolkien" and "W. E. B. Du Bois". + // + // https://www.meilisearch.com/docs/reference/api/settings#get-dictionary + GetDictionary() ([]string, error) + + // GetDictionaryWithContext returns user dictionary and support parent context + // + //Allows users to instruct Meilisearch to consider groups of strings as a + //single term by adding a supplementary dictionary of user-defined terms. + //This is particularly useful when working with datasets containing many domain-specific + //words, and in languages where words are not separated by whitespace such as Japanese. + //Custom dictionaries are also useful in a few use-cases for space-separated languages, + //such as datasets with names such as "J. R. R. Tolkien" and "W. E. B. Du Bois". + // + // https://www.meilisearch.com/docs/reference/api/settings#get-dictionary + GetDictionaryWithContext(ctx context.Context) ([]string, error) + + // GetProximityPrecision returns ProximityPrecision configuration value + // https://www.meilisearch.com/docs/reference/api/settings#get-proximity-precision-settings + GetProximityPrecision() (ProximityPrecisionType, error) + + // GetProximityPrecisionWithContext returns ProximityPrecision configuration value and support parent context + // https://www.meilisearch.com/docs/reference/api/settings#get-proximity-precision-settings + GetProximityPrecisionWithContext(ctx context.Context) (ProximityPrecisionType, error) + + // GetLocalizedAttributes get the localized attributes settings of an index + // https://www.meilisearch.com/docs/reference/api/settings#get-localized-attributes-settings + GetLocalizedAttributes() ([]*LocalizedAttributes, error) + + // GetLocalizedAttributesWithContext get the localized attributes settings of an index using the provided context for cancellation + // https://www.meilisearch.com/docs/reference/api/settings#get-localized-attributes-settings + GetLocalizedAttributesWithContext(ctx context.Context) ([]*LocalizedAttributes, error) +} diff --git a/index_task.go b/index_task.go new file mode 100644 index 00000000..a5f6c853 --- /dev/null +++ b/index_task.go @@ -0,0 +1,75 @@ +package meilisearch + +import ( + "context" + "net/http" + "strconv" + "strings" + "time" +) + +func (i *index) GetTask(taskUID int64) (*Task, error) { + return i.GetTaskWithContext(context.Background(), taskUID) +} + +func (i *index) GetTaskWithContext(ctx context.Context, taskUID int64) (*Task, error) { + return getTask(ctx, i.client, taskUID) +} + +func (i *index) GetTasks(param *TasksQuery) (*TaskResult, error) { + return i.GetTasksWithContext(context.Background(), param) +} + +func (i *index) GetTasksWithContext(ctx context.Context, param *TasksQuery) (*TaskResult, error) { + resp := new(TaskResult) + req := &internalRequest{ + endpoint: "/tasks", + method: http.MethodGet, + withRequest: nil, + withResponse: resp, + withQueryParams: map[string]string{}, + acceptedStatusCodes: []int{http.StatusOK}, + functionName: "GetTasks", + } + if param != nil { + if param.Limit != 0 { + req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10) + } + if param.From != 0 { + req.withQueryParams["from"] = strconv.FormatInt(param.From, 10) + } + if len(param.Statuses) != 0 { + statuses := make([]string, len(param.Statuses)) + for i, status := range param.Statuses { + statuses[i] = string(status) + } + req.withQueryParams["statuses"] = strings.Join(statuses, ",") + } + + if len(param.Types) != 0 { + types := make([]string, len(param.Types)) + for i, t := range param.Types { + types[i] = string(t) + } + req.withQueryParams["types"] = strings.Join(types, ",") + } + if len(param.IndexUIDS) != 0 { + param.IndexUIDS = append(param.IndexUIDS, i.uid) + req.withQueryParams["indexUids"] = strings.Join(param.IndexUIDS, ",") + } else { + req.withQueryParams["indexUids"] = i.uid + } + } + if err := i.client.executeRequest(ctx, req); err != nil { + return nil, err + } + return resp, nil +} + +func (i *index) WaitForTask(taskUID int64, interval time.Duration) (*Task, error) { + return waitForTask(context.Background(), i.client, taskUID, interval) +} + +func (i *index) WaitForTaskWithContext(ctx context.Context, taskUID int64, interval time.Duration) (*Task, error) { + return waitForTask(ctx, i.client, taskUID, interval) +} diff --git a/index_task_test.go b/index_task_test.go new file mode 100644 index 00000000..b89b2ede --- /dev/null +++ b/index_task_test.go @@ -0,0 +1,264 @@ +package meilisearch + +import ( + "context" + "crypto/tls" + "github.com/stretchr/testify/require" + "testing" + "time" +) + +func TestIndex_GetTask(t *testing.T) { + sv := setup(t, "") + customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{ + InsecureSkipVerify: true, + })) + + type args struct { + UID string + client ServiceManager + taskUID int64 + document []docTest + } + tests := []struct { + name string + args args + }{ + { + name: "TestIndexBasicGetTask", + args: args{ + UID: "TestIndexBasicGetTask", + client: sv, + taskUID: 0, + document: []docTest{ + {ID: "123", Name: "Pride and Prejudice"}, + }, + }, + }, + { + name: "TestIndexGetTaskWithCustomClient", + args: args{ + UID: "TestIndexGetTaskWithCustomClient", + client: customSv, + taskUID: 0, + document: []docTest{ + {ID: "123", Name: "Pride and Prejudice"}, + }, + }, + }, + { + name: "TestIndexGetTask", + args: args{ + UID: "TestIndexGetTask", + client: sv, + taskUID: 0, + document: []docTest{ + {ID: "456", Name: "Le Petit Prince"}, + {ID: "1", Name: "Alice In Wonderland"}, + }, + }, + }, + } + + t.Cleanup(cleanup(sv, customSv)) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := tt.args.client + i := c.Index(tt.args.UID) + t.Cleanup(cleanup(c)) + + task, err := i.AddDocuments(tt.args.document) + require.NoError(t, err) + + _, err = c.WaitForTask(task.TaskUID, 0) + require.NoError(t, err) + + gotResp, err := i.GetTask(task.TaskUID) + require.NoError(t, err) + require.NotNil(t, gotResp) + require.GreaterOrEqual(t, gotResp.UID, tt.args.taskUID) + require.Equal(t, gotResp.IndexUID, tt.args.UID) + require.Equal(t, gotResp.Status, TaskStatusSucceeded) + + // Make sure that timestamps are also retrieved + require.NotZero(t, gotResp.EnqueuedAt) + require.NotZero(t, gotResp.StartedAt) + require.NotZero(t, gotResp.FinishedAt) + }) + } +} + +func TestIndex_GetTasks(t *testing.T) { + sv := setup(t, "") + customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{ + InsecureSkipVerify: true, + })) + + type args struct { + UID string + client ServiceManager + document []docTest + query *TasksQuery + } + tests := []struct { + name string + args args + }{ + { + name: "TestIndexBasicGetTasks", + args: args{ + UID: "indexUID", + client: sv, + document: []docTest{ + {ID: "123", Name: "Pride and Prejudice"}, + }, + }, + }, + { + name: "TestIndexGetTasksWithCustomClient", + args: args{ + UID: "indexUID", + client: customSv, + document: []docTest{ + {ID: "123", Name: "Pride and Prejudice"}, + }, + }, + }, + { + name: "TestIndexBasicGetTasksWithFilters", + args: args{ + UID: "indexUID", + client: sv, + document: []docTest{ + {ID: "123", Name: "Pride and Prejudice"}, + }, + query: &TasksQuery{ + Statuses: []TaskStatus{TaskStatusSucceeded}, + Types: []TaskType{TaskTypeDocumentAdditionOrUpdate}, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := tt.args.client + i := c.Index(tt.args.UID) + t.Cleanup(cleanup(c)) + + task, err := i.AddDocuments(tt.args.document) + require.NoError(t, err) + + _, err = c.WaitForTask(task.TaskUID, 0) + require.NoError(t, err) + + gotResp, err := i.GetTasks(nil) + require.NoError(t, err) + require.NotNil(t, (*gotResp).Results[0].Status) + require.NotZero(t, (*gotResp).Results[0].UID) + require.NotNil(t, (*gotResp).Results[0].Type) + }) + } +} + +func TestIndex_WaitForTask(t *testing.T) { + sv := setup(t, "") + customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{ + InsecureSkipVerify: true, + })) + + type args struct { + UID string + client ServiceManager + interval time.Duration + timeout time.Duration + document []docTest + } + tests := []struct { + name string + args args + want TaskStatus + }{ + { + name: "TestWaitForTask50", + args: args{ + UID: "TestWaitForTask50", + client: sv, + interval: time.Millisecond * 50, + timeout: time.Second * 5, + document: []docTest{ + {ID: "123", Name: "Pride and Prejudice"}, + {ID: "456", Name: "Le Petit Prince"}, + {ID: "1", Name: "Alice In Wonderland"}, + }, + }, + want: "succeeded", + }, + { + name: "TestWaitForTask50WithCustomClient", + args: args{ + UID: "TestWaitForTask50WithCustomClient", + client: customSv, + interval: time.Millisecond * 50, + timeout: time.Second * 5, + document: []docTest{ + {ID: "123", Name: "Pride and Prejudice"}, + {ID: "456", Name: "Le Petit Prince"}, + {ID: "1", Name: "Alice In Wonderland"}, + }, + }, + want: "succeeded", + }, + { + name: "TestWaitForTask10", + args: args{ + UID: "TestWaitForTask10", + client: sv, + interval: time.Millisecond * 10, + timeout: time.Second * 5, + document: []docTest{ + {ID: "123", Name: "Pride and Prejudice"}, + {ID: "456", Name: "Le Petit Prince"}, + {ID: "1", Name: "Alice In Wonderland"}, + }, + }, + want: "succeeded", + }, + { + name: "TestWaitForTaskWithTimeout", + args: args{ + UID: "TestWaitForTaskWithTimeout", + client: sv, + interval: time.Millisecond * 50, + timeout: time.Millisecond * 10, + document: []docTest{ + {ID: "123", Name: "Pride and Prejudice"}, + {ID: "456", Name: "Le Petit Prince"}, + {ID: "1", Name: "Alice In Wonderland"}, + }, + }, + want: "succeeded", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := tt.args.client + i := c.Index(tt.args.UID) + t.Cleanup(cleanup(c)) + + task, err := i.AddDocuments(tt.args.document) + require.NoError(t, err) + + ctx, cancelFunc := context.WithTimeout(context.Background(), tt.args.timeout) + defer cancelFunc() + + gotTask, err := i.WaitForTaskWithContext(ctx, task.TaskUID, 0) + if tt.args.timeout < tt.args.interval { + require.Error(t, err) + } else { + require.NoError(t, err) + require.Equal(t, tt.want, gotTask.Status) + } + }) + } +} diff --git a/index_test.go b/index_test.go index bdf6e750..adbe3cd3 100644 --- a/index_test.go +++ b/index_test.go @@ -1,11 +1,9 @@ package meilisearch import ( - "context" "crypto/tls" "github.com/stretchr/testify/require" "testing" - "time" ) func TestIndex_Delete(t *testing.T) { @@ -211,261 +209,6 @@ func Test_newIndex(t *testing.T) { } } -func TestIndex_GetTask(t *testing.T) { - sv := setup(t, "") - customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{ - InsecureSkipVerify: true, - })) - - type args struct { - UID string - client ServiceManager - taskUID int64 - document []docTest - } - tests := []struct { - name string - args args - }{ - { - name: "TestIndexBasicGetTask", - args: args{ - UID: "TestIndexBasicGetTask", - client: sv, - taskUID: 0, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - }, - }, - }, - { - name: "TestIndexGetTaskWithCustomClient", - args: args{ - UID: "TestIndexGetTaskWithCustomClient", - client: customSv, - taskUID: 0, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - }, - }, - }, - { - name: "TestIndexGetTask", - args: args{ - UID: "TestIndexGetTask", - client: sv, - taskUID: 0, - document: []docTest{ - {ID: "456", Name: "Le Petit Prince"}, - {ID: "1", Name: "Alice In Wonderland"}, - }, - }, - }, - } - - t.Cleanup(cleanup(sv, customSv)) - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - c := tt.args.client - i := c.Index(tt.args.UID) - t.Cleanup(cleanup(c)) - - task, err := i.AddDocuments(tt.args.document) - require.NoError(t, err) - - _, err = c.WaitForTask(task.TaskUID, 0) - require.NoError(t, err) - - gotResp, err := i.GetTask(task.TaskUID) - require.NoError(t, err) - require.NotNil(t, gotResp) - require.GreaterOrEqual(t, gotResp.UID, tt.args.taskUID) - require.Equal(t, gotResp.IndexUID, tt.args.UID) - require.Equal(t, gotResp.Status, TaskStatusSucceeded) - - // Make sure that timestamps are also retrieved - require.NotZero(t, gotResp.EnqueuedAt) - require.NotZero(t, gotResp.StartedAt) - require.NotZero(t, gotResp.FinishedAt) - }) - } -} - -func TestIndex_GetTasks(t *testing.T) { - sv := setup(t, "") - customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{ - InsecureSkipVerify: true, - })) - - type args struct { - UID string - client ServiceManager - document []docTest - query *TasksQuery - } - tests := []struct { - name string - args args - }{ - { - name: "TestIndexBasicGetTasks", - args: args{ - UID: "indexUID", - client: sv, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - }, - }, - }, - { - name: "TestIndexGetTasksWithCustomClient", - args: args{ - UID: "indexUID", - client: customSv, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - }, - }, - }, - { - name: "TestIndexBasicGetTasksWithFilters", - args: args{ - UID: "indexUID", - client: sv, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - }, - query: &TasksQuery{ - Statuses: []TaskStatus{TaskStatusSucceeded}, - Types: []TaskType{TaskTypeDocumentAdditionOrUpdate}, - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - c := tt.args.client - i := c.Index(tt.args.UID) - t.Cleanup(cleanup(c)) - - task, err := i.AddDocuments(tt.args.document) - require.NoError(t, err) - - _, err = c.WaitForTask(task.TaskUID, 0) - require.NoError(t, err) - - gotResp, err := i.GetTasks(nil) - require.NoError(t, err) - require.NotNil(t, (*gotResp).Results[0].Status) - require.NotZero(t, (*gotResp).Results[0].UID) - require.NotNil(t, (*gotResp).Results[0].Type) - }) - } -} - -func TestIndex_WaitForTask(t *testing.T) { - sv := setup(t, "") - customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{ - InsecureSkipVerify: true, - })) - - type args struct { - UID string - client ServiceManager - interval time.Duration - timeout time.Duration - document []docTest - } - tests := []struct { - name string - args args - want TaskStatus - }{ - { - name: "TestWaitForTask50", - args: args{ - UID: "TestWaitForTask50", - client: sv, - interval: time.Millisecond * 50, - timeout: time.Second * 5, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - {ID: "456", Name: "Le Petit Prince"}, - {ID: "1", Name: "Alice In Wonderland"}, - }, - }, - want: "succeeded", - }, - { - name: "TestWaitForTask50WithCustomClient", - args: args{ - UID: "TestWaitForTask50WithCustomClient", - client: customSv, - interval: time.Millisecond * 50, - timeout: time.Second * 5, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - {ID: "456", Name: "Le Petit Prince"}, - {ID: "1", Name: "Alice In Wonderland"}, - }, - }, - want: "succeeded", - }, - { - name: "TestWaitForTask10", - args: args{ - UID: "TestWaitForTask10", - client: sv, - interval: time.Millisecond * 10, - timeout: time.Second * 5, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - {ID: "456", Name: "Le Petit Prince"}, - {ID: "1", Name: "Alice In Wonderland"}, - }, - }, - want: "succeeded", - }, - { - name: "TestWaitForTaskWithTimeout", - args: args{ - UID: "TestWaitForTaskWithTimeout", - client: sv, - interval: time.Millisecond * 50, - timeout: time.Millisecond * 10, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - {ID: "456", Name: "Le Petit Prince"}, - {ID: "1", Name: "Alice In Wonderland"}, - }, - }, - want: "succeeded", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - c := tt.args.client - i := c.Index(tt.args.UID) - t.Cleanup(cleanup(c)) - - task, err := i.AddDocuments(tt.args.document) - require.NoError(t, err) - - ctx, cancelFunc := context.WithTimeout(context.Background(), tt.args.timeout) - defer cancelFunc() - - gotTask, err := i.WaitForTaskWithContext(ctx, task.TaskUID, 0) - if tt.args.timeout < tt.args.interval { - require.Error(t, err) - } else { - require.NoError(t, err) - require.Equal(t, tt.want, gotTask.Status) - } - }) - } -} - func TestIndex_FetchInfo(t *testing.T) { sv := setup(t, "") customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{ diff --git a/meilisearch.go b/meilisearch.go index 2d85895b..64457893 100644 --- a/meilisearch.go +++ b/meilisearch.go @@ -14,161 +14,6 @@ type meilisearch struct { client *client } -type ServiceManager interface { - // Index retrieves an IndexManager for a specific index. - Index(uid string) IndexManager - - // GetIndex fetches the details of a specific index. - GetIndex(indexID string) (*IndexResult, error) - - // GetIndexWithContext fetches the details of a specific index with a context for cancellation. - GetIndexWithContext(ctx context.Context, indexID string) (*IndexResult, error) - - // GetRawIndex fetches the raw JSON representation of a specific index. - GetRawIndex(uid string) (map[string]interface{}, error) - - // GetRawIndexWithContext fetches the raw JSON representation of a specific index with a context for cancellation. - GetRawIndexWithContext(ctx context.Context, uid string) (map[string]interface{}, error) - - // ListIndexes lists all indexes. - ListIndexes(param *IndexesQuery) (*IndexesResults, error) - - // ListIndexesWithContext lists all indexes with a context for cancellation. - ListIndexesWithContext(ctx context.Context, param *IndexesQuery) (*IndexesResults, error) - - // GetRawIndexes fetches the raw JSON representation of all indexes. - GetRawIndexes(param *IndexesQuery) (map[string]interface{}, error) - - // GetRawIndexesWithContext fetches the raw JSON representation of all indexes with a context for cancellation. - GetRawIndexesWithContext(ctx context.Context, param *IndexesQuery) (map[string]interface{}, error) - - // CreateIndex creates a new index. - CreateIndex(config *IndexConfig) (*TaskInfo, error) - - // CreateIndexWithContext creates a new index with a context for cancellation. - CreateIndexWithContext(ctx context.Context, config *IndexConfig) (*TaskInfo, error) - - // DeleteIndex deletes a specific index. - DeleteIndex(uid string) (*TaskInfo, error) - - // DeleteIndexWithContext deletes a specific index with a context for cancellation. - DeleteIndexWithContext(ctx context.Context, uid string) (*TaskInfo, error) - - // MultiSearch performs a multi-index search. - MultiSearch(queries *MultiSearchRequest) (*MultiSearchResponse, error) - - // MultiSearchWithContext performs a multi-index search with a context for cancellation. - MultiSearchWithContext(ctx context.Context, queries *MultiSearchRequest) (*MultiSearchResponse, error) - - // CreateKey creates a new API key. - CreateKey(request *Key) (*Key, error) - - // CreateKeyWithContext creates a new API key with a context for cancellation. - CreateKeyWithContext(ctx context.Context, request *Key) (*Key, error) - - // GetKey fetches the details of a specific API key. - GetKey(identifier string) (*Key, error) - - // GetKeyWithContext fetches the details of a specific API key with a context for cancellation. - GetKeyWithContext(ctx context.Context, identifier string) (*Key, error) - - // GetKeys lists all API keys. - GetKeys(param *KeysQuery) (*KeysResults, error) - - // GetKeysWithContext lists all API keys with a context for cancellation. - GetKeysWithContext(ctx context.Context, param *KeysQuery) (*KeysResults, error) - - // UpdateKey updates a specific API key. - UpdateKey(keyOrUID string, request *Key) (*Key, error) - - // UpdateKeyWithContext updates a specific API key with a context for cancellation. - UpdateKeyWithContext(ctx context.Context, keyOrUID string, request *Key) (*Key, error) - - // DeleteKey deletes a specific API key. - DeleteKey(keyOrUID string) (bool, error) - - // DeleteKeyWithContext deletes a specific API key with a context for cancellation. - DeleteKeyWithContext(ctx context.Context, keyOrUID string) (bool, error) - - // GetTask fetches the details of a specific task. - GetTask(taskUID int64) (*Task, error) - - // GetTaskWithContext fetches the details of a specific task with a context for cancellation. - GetTaskWithContext(ctx context.Context, taskUID int64) (*Task, error) - - // GetTasks lists all tasks. - GetTasks(param *TasksQuery) (*TaskResult, error) - - // GetTasksWithContext lists all tasks with a context for cancellation. - GetTasksWithContext(ctx context.Context, param *TasksQuery) (*TaskResult, error) - - // CancelTasks cancels specific tasks. - CancelTasks(param *CancelTasksQuery) (*TaskInfo, error) - - // CancelTasksWithContext cancels specific tasks with a context for cancellation. - CancelTasksWithContext(ctx context.Context, param *CancelTasksQuery) (*TaskInfo, error) - - // DeleteTasks deletes specific tasks. - DeleteTasks(param *DeleteTasksQuery) (*TaskInfo, error) - - // DeleteTasksWithContext deletes specific tasks with a context for cancellation. - DeleteTasksWithContext(ctx context.Context, param *DeleteTasksQuery) (*TaskInfo, error) - - // WaitForTask waits for a specific task to complete. - WaitForTask(taskUID int64, interval time.Duration) (*Task, error) - - // WaitForTaskWithContext waits for a specific task to complete with a context for cancellation. - WaitForTaskWithContext(ctx context.Context, taskUID int64, interval time.Duration) (*Task, error) - - // SwapIndexes swaps the positions of two indexes. - SwapIndexes(param []*SwapIndexesParams) (*TaskInfo, error) - - // SwapIndexesWithContext swaps the positions of two indexes with a context for cancellation. - SwapIndexesWithContext(ctx context.Context, param []*SwapIndexesParams) (*TaskInfo, error) - - // GenerateTenantToken generates a tenant token for multi-tenancy. - GenerateTenantToken(apiKeyUID string, searchRules map[string]interface{}, options *TenantTokenOptions) (string, error) - - // GetStats fetches global stats. - GetStats() (*Stats, error) - - // GetStatsWithContext fetches global stats with a context for cancellation. - GetStatsWithContext(ctx context.Context) (*Stats, error) - - // CreateDump creates a database dump. - CreateDump() (*TaskInfo, error) - - // CreateDumpWithContext creates a database dump with a context for cancellation. - CreateDumpWithContext(ctx context.Context) (*TaskInfo, error) - - // Version fetches the version of the Meilisearch server. - Version() (*Version, error) - - // VersionWithContext fetches the version of the Meilisearch server with a context for cancellation. - VersionWithContext(ctx context.Context) (*Version, error) - - // Health checks the health of the Meilisearch server. - Health() (*Health, error) - - // HealthWithContext checks the health of the Meilisearch server with a context for cancellation. - HealthWithContext(ctx context.Context) (*Health, error) - - // IsHealthy checks if the Meilisearch server is healthy. - IsHealthy() bool - - // CreateSnapshot create database snapshot from meilisearch - CreateSnapshot() (*TaskInfo, error) - - // CreateSnapshotWithContext create database snapshot from meilisearch and support parent context - CreateSnapshotWithContext(ctx context.Context) (*TaskInfo, error) - - // ExperimentalFeatures returns the experimental features manager. - ExperimentalFeatures() *ExperimentalFeatures - - // Close closes the connection to the Meilisearch server. - Close() -} - // New create new service manager for operating on meilisearch func New(host string, options ...Option) ServiceManager { defOpt := defaultMeiliOpt @@ -204,6 +49,26 @@ func Connect(host string, options ...Option) (ServiceManager, error) { return meili, nil } +func (m *meilisearch) GetServiceReader() ServiceReader { + return m +} + +func (m *meilisearch) GetTaskManager() TaskManager { + return m +} + +func (m *meilisearch) GetTaskReader() TaskReader { + return m +} + +func (m *meilisearch) GetKeyManager() KeyManager { + return m +} + +func (m *meilisearch) GetKeyReader() KeyReader { + return m +} + func (m *meilisearch) Index(uid string) IndexManager { return newIndex(m.client, uid) } diff --git a/meilisearch_interface.go b/meilisearch_interface.go new file mode 100644 index 00000000..5af9e882 --- /dev/null +++ b/meilisearch_interface.go @@ -0,0 +1,185 @@ +package meilisearch + +import ( + "context" + "time" +) + +type ServiceManager interface { + ServiceReader + KeyManager + TaskManager + + GetServiceReader() ServiceReader + GetTaskManager() TaskManager + GetTaskReader() TaskReader + GetKeyManager() KeyManager + GetKeyReader() KeyReader + + // CreateIndex creates a new index. + CreateIndex(config *IndexConfig) (*TaskInfo, error) + + // CreateIndexWithContext creates a new index with a context for cancellation. + CreateIndexWithContext(ctx context.Context, config *IndexConfig) (*TaskInfo, error) + + // DeleteIndex deletes a specific index. + DeleteIndex(uid string) (*TaskInfo, error) + + // DeleteIndexWithContext deletes a specific index with a context for cancellation. + DeleteIndexWithContext(ctx context.Context, uid string) (*TaskInfo, error) + + // SwapIndexes swaps the positions of two indexes. + SwapIndexes(param []*SwapIndexesParams) (*TaskInfo, error) + + // SwapIndexesWithContext swaps the positions of two indexes with a context for cancellation. + SwapIndexesWithContext(ctx context.Context, param []*SwapIndexesParams) (*TaskInfo, error) + + // GenerateTenantToken generates a tenant token for multi-tenancy. + GenerateTenantToken(apiKeyUID string, searchRules map[string]interface{}, options *TenantTokenOptions) (string, error) + + // CreateDump creates a database dump. + CreateDump() (*TaskInfo, error) + + // CreateDumpWithContext creates a database dump with a context for cancellation. + CreateDumpWithContext(ctx context.Context) (*TaskInfo, error) + + // CreateSnapshot create database snapshot from meilisearch + CreateSnapshot() (*TaskInfo, error) + + // CreateSnapshotWithContext create database snapshot from meilisearch and support parent context + CreateSnapshotWithContext(ctx context.Context) (*TaskInfo, error) + + // ExperimentalFeatures returns the experimental features manager. + ExperimentalFeatures() *ExperimentalFeatures + + // Close closes the connection to the Meilisearch server. + Close() +} + +type ServiceReader interface { + // Index retrieves an IndexManager for a specific index. + Index(uid string) IndexManager + + // GetIndex fetches the details of a specific index. + GetIndex(indexID string) (*IndexResult, error) + + // GetIndexWithContext fetches the details of a specific index with a context for cancellation. + GetIndexWithContext(ctx context.Context, indexID string) (*IndexResult, error) + + // GetRawIndex fetches the raw JSON representation of a specific index. + GetRawIndex(uid string) (map[string]interface{}, error) + + // GetRawIndexWithContext fetches the raw JSON representation of a specific index with a context for cancellation. + GetRawIndexWithContext(ctx context.Context, uid string) (map[string]interface{}, error) + + // ListIndexes lists all indexes. + ListIndexes(param *IndexesQuery) (*IndexesResults, error) + + // ListIndexesWithContext lists all indexes with a context for cancellation. + ListIndexesWithContext(ctx context.Context, param *IndexesQuery) (*IndexesResults, error) + + // GetRawIndexes fetches the raw JSON representation of all indexes. + GetRawIndexes(param *IndexesQuery) (map[string]interface{}, error) + + // GetRawIndexesWithContext fetches the raw JSON representation of all indexes with a context for cancellation. + GetRawIndexesWithContext(ctx context.Context, param *IndexesQuery) (map[string]interface{}, error) + + // MultiSearch performs a multi-index search. + MultiSearch(queries *MultiSearchRequest) (*MultiSearchResponse, error) + + // MultiSearchWithContext performs a multi-index search with a context for cancellation. + MultiSearchWithContext(ctx context.Context, queries *MultiSearchRequest) (*MultiSearchResponse, error) + + // GetStats fetches global stats. + GetStats() (*Stats, error) + + // GetStatsWithContext fetches global stats with a context for cancellation. + GetStatsWithContext(ctx context.Context) (*Stats, error) + + // Version fetches the version of the Meilisearch server. + Version() (*Version, error) + + // VersionWithContext fetches the version of the Meilisearch server with a context for cancellation. + VersionWithContext(ctx context.Context) (*Version, error) + + // Health checks the health of the Meilisearch server. + Health() (*Health, error) + + // HealthWithContext checks the health of the Meilisearch server with a context for cancellation. + HealthWithContext(ctx context.Context) (*Health, error) + + // IsHealthy checks if the Meilisearch server is healthy. + IsHealthy() bool +} + +type KeyManager interface { + KeyReader + + // CreateKey creates a new API key. + CreateKey(request *Key) (*Key, error) + + // CreateKeyWithContext creates a new API key with a context for cancellation. + CreateKeyWithContext(ctx context.Context, request *Key) (*Key, error) + + // UpdateKey updates a specific API key. + UpdateKey(keyOrUID string, request *Key) (*Key, error) + + // UpdateKeyWithContext updates a specific API key with a context for cancellation. + UpdateKeyWithContext(ctx context.Context, keyOrUID string, request *Key) (*Key, error) + + // DeleteKey deletes a specific API key. + DeleteKey(keyOrUID string) (bool, error) + + // DeleteKeyWithContext deletes a specific API key with a context for cancellation. + DeleteKeyWithContext(ctx context.Context, keyOrUID string) (bool, error) +} + +type KeyReader interface { + // GetKey fetches the details of a specific API key. + GetKey(identifier string) (*Key, error) + + // GetKeyWithContext fetches the details of a specific API key with a context for cancellation. + GetKeyWithContext(ctx context.Context, identifier string) (*Key, error) + + // GetKeys lists all API keys. + GetKeys(param *KeysQuery) (*KeysResults, error) + + // GetKeysWithContext lists all API keys with a context for cancellation. + GetKeysWithContext(ctx context.Context, param *KeysQuery) (*KeysResults, error) +} + +type TaskManager interface { + TaskReader + + // CancelTasks cancels specific tasks. + CancelTasks(param *CancelTasksQuery) (*TaskInfo, error) + + // CancelTasksWithContext cancels specific tasks with a context for cancellation. + CancelTasksWithContext(ctx context.Context, param *CancelTasksQuery) (*TaskInfo, error) + + // DeleteTasks deletes specific tasks. + DeleteTasks(param *DeleteTasksQuery) (*TaskInfo, error) + + // DeleteTasksWithContext deletes specific tasks with a context for cancellation. + DeleteTasksWithContext(ctx context.Context, param *DeleteTasksQuery) (*TaskInfo, error) +} + +type TaskReader interface { + // GetTask retrieves a task by its UID. + GetTask(taskUID int64) (*Task, error) + + // GetTaskWithContext retrieves a task by its UID using the provided context for cancellation. + GetTaskWithContext(ctx context.Context, taskUID int64) (*Task, error) + + // GetTasks retrieves multiple tasks based on query parameters. + GetTasks(param *TasksQuery) (*TaskResult, error) + + // GetTasksWithContext retrieves multiple tasks based on query parameters using the provided context for cancellation. + GetTasksWithContext(ctx context.Context, param *TasksQuery) (*TaskResult, error) + + // WaitForTask waits for a task to complete by its UID with the given interval. + WaitForTask(taskUID int64, interval time.Duration) (*Task, error) + + // WaitForTaskWithContext waits for a task to complete by its UID with the given interval using the provided context for cancellation. + WaitForTaskWithContext(ctx context.Context, taskUID int64, interval time.Duration) (*Task, error) +} From b43b49b62c0bf9337346cc7a4c7622ec87161679 Mon Sep 17 00:00:00 2001 From: Javad Date: Wed, 6 Nov 2024 11:16:24 +0330 Subject: [PATCH 2/4] fix: add test for get interface manager and reader --- index_test.go | 13 +++++++++++++ meilisearch_test.go | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/index_test.go b/index_test.go index adbe3cd3..a6c9e717 100644 --- a/index_test.go +++ b/index_test.go @@ -403,3 +403,16 @@ func TestIndex_UpdateIndex(t *testing.T) { }) } } + +func TestIndexManagerAndReaders(t *testing.T) { + c := setup(t, "") + idx := c.Index("indexUID") + require.NotNil(t, idx) + require.NotNil(t, idx.GetIndexReader()) + require.NotNil(t, idx.GetTaskReader()) + require.NotNil(t, idx.GetSettingsManager()) + require.NotNil(t, idx.GetSettingsReader()) + require.NotNil(t, idx.GetSearch()) + require.NotNil(t, idx.GetDocumentManager()) + require.NotNil(t, idx.GetDocumentReader()) +} diff --git a/meilisearch_test.go b/meilisearch_test.go index cfd42e8a..8dc8edbb 100644 --- a/meilisearch_test.go +++ b/meilisearch_test.go @@ -2332,3 +2332,12 @@ func Test_CreateSnapshot(t *testing.T) { require.NoError(t, err) testWaitForTask(t, c.Index("indexUID"), task) } + +func TestGetServiceManagerAndReaders(t *testing.T) { + c := setup(t, "") + require.NotNil(t, c.GetServiceReader()) + require.NotNil(t, c.GetTaskManager()) + require.NotNil(t, c.GetTaskReader()) + require.NotNil(t, c.GetKeyManager()) + require.NotNil(t, c.GetKeyReader()) +} From 359433e070dabf88585be5838bd7dd3e0da58d33 Mon Sep 17 00:00:00 2001 From: Javad Date: Wed, 6 Nov 2024 11:21:29 +0330 Subject: [PATCH 3/4] chore: improve test get tasks --- index_task_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/index_task_test.go b/index_task_test.go index b89b2ede..80e53c19 100644 --- a/index_task_test.go +++ b/index_task_test.go @@ -139,6 +139,22 @@ func TestIndex_GetTasks(t *testing.T) { }, }, }, + { + name: "TestTasksWithParams", + args: args{ + UID: "indexUID", + client: sv, + document: []docTest{ + {ID: "123", Name: "Pride and Prejudice"}, + }, + query: &TasksQuery{ + Limit: 5, + From: 1, + Statuses: []TaskStatus{TaskStatusSucceeded}, + Types: []TaskType{TaskTypeDocumentAdditionOrUpdate}, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From eb62ceb6d8e60482f6d821851466c0e5f5ab8cc1 Mon Sep 17 00:00:00 2001 From: Javad Date: Wed, 6 Nov 2024 11:28:42 +0330 Subject: [PATCH 4/4] fix: covered task query --- index_task_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index_task_test.go b/index_task_test.go index 80e53c19..da0151bc 100644 --- a/index_task_test.go +++ b/index_task_test.go @@ -148,10 +148,11 @@ func TestIndex_GetTasks(t *testing.T) { {ID: "123", Name: "Pride and Prejudice"}, }, query: &TasksQuery{ - Limit: 5, - From: 1, - Statuses: []TaskStatus{TaskStatusSucceeded}, - Types: []TaskType{TaskTypeDocumentAdditionOrUpdate}, + IndexUIDS: []string{"indexUID"}, + Limit: 10, + From: 1, + Statuses: []TaskStatus{TaskStatusSucceeded}, + Types: []TaskType{TaskTypeDocumentAdditionOrUpdate}, }, }, }, @@ -168,10 +169,9 @@ func TestIndex_GetTasks(t *testing.T) { _, err = c.WaitForTask(task.TaskUID, 0) require.NoError(t, err) - gotResp, err := i.GetTasks(nil) + gotResp, err := i.GetTasks(tt.args.query) require.NoError(t, err) require.NotNil(t, (*gotResp).Results[0].Status) - require.NotZero(t, (*gotResp).Results[0].UID) require.NotNil(t, (*gotResp).Results[0].Type) }) }