Skip to content

Commit

Permalink
Add the searchForFacetValues method for Meilisearch v1.3.0 (#1513)
Browse files Browse the repository at this point in the history
* Add the searchForFacetValue method

* Update version for the next release (v0.33.0-prototype-search-for-facet-values.0) (#1514)

* Update package.json

* Update src/package-version.ts

* Change name of searchForFacetValue to searchForFacetValues

* Add error codes

* Remove only label on test

* Fix the typing of the search parameters

* Remove unecessary INVALID_SEARCH_FACET error

* Fix tests

* Update returned fields from hits to facetHits and query to facetQuery

* Update version for the next release (vv0.33.0-prototype-search-for-facet-values.1) (#1515)

* Update package.json

* Update src/package-version.ts

---------

Co-authored-by: cvermand <33010418+bidoubiwa@users.noreply.github.com>

* Apply suggestions from code review

* Update README.md

Co-authored-by: Bruno Casali <brunoocasali@gmail.com>

---------

Co-authored-by: meili-bot <74670311+meili-bot@users.noreply.github.com>
Co-authored-by: Bruno Casali <brunoocasali@gmail.com>
  • Loading branch information
3 people authored Jul 12, 2023
1 parent 13846f0 commit b61a8e5
Show file tree
Hide file tree
Showing 5 changed files with 369 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,14 @@ client.multiSearch(queries?: MultiSearchParams, config?: Partial<Request>): Prom

`multiSearch` uses the `POST` method when performing its request to Meilisearch.

### Search For Facet Values

#### [Search for facet values](#)

```ts
client.index<T>('myIndex').searchForFacetValues(params: SearchForFacetValuesParams, config?: Partial<Request>): Promise<SearchForFacetValuesResponse>
```

### Documents <!-- omit in toc -->

#### [Add or replace multiple documents](https://www.meilisearch.com/docs/reference/api/documents#add-or-replace-documents)
Expand Down
23 changes: 23 additions & 0 deletions src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {
ContentType,
DocumentsIds,
DocumentsDeletionQuery,
SearchForFacetValuesParams,
SearchForFacetValuesResponse,
} from './types'
import { removeUndefinedFromObject } from './utils'
import { HttpRequests } from './http-requests'
Expand Down Expand Up @@ -148,6 +150,27 @@ class Index<T extends Record<string, any> = Record<string, any>> {
)
}

/**
* Search for facet values
*
* @param params - Parameters used to search on the facets
* @param config - Additional request configuration options
* @returns Promise containing the search response
*/
async searchForFacetValues(
params: SearchForFacetValuesParams,
config?: Partial<Request>
): Promise<SearchForFacetValuesResponse> {
const url = `indexes/${this.uid}/facet-search`

return await this.httpRequest.post(
url,
removeUndefinedFromObject(params),
undefined,
config
)
}

///
/// INDEX
///
Expand Down
27 changes: 27 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ export type Crop = {
cropMarker?: string
}

// `facetName` becomes mandatory when using `searchForFacetValues`
export type SearchForFacetValuesParams = Omit<SearchParams, 'facetName'> & {
facetName: string
}

export type FacetHit = {
value: string
count: number
}

export type SearchForFacetValuesResponse = {
facetHits: FacetHit[]
facetQuery: string | null
processingTimeMs: number
}

export type SearchParams = Query &
Pagination &
Highlight &
Expand All @@ -90,6 +106,8 @@ export type SearchParams = Query &
matchingStrategy?: MatchingStrategies
hitsPerPage?: number
page?: number
facetName?: string
facetQuery?: string
vector?: number[] | null
attributesToSearchOn?: string[] | null
}
Expand Down Expand Up @@ -835,6 +853,15 @@ export const enum ErrorStatusCode {

/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_api_key_offset */
INVALID_API_KEY_OFFSET = 'invalid_api_key_offset',

/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_facet_search_facet_name */
INVALID_FACET_SEARCH_FACET_NAME = 'invalid_facet_search_facet_name',

/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#missing_facet_search_facet_name */
MISSING_FACET_SEARCH_FACET_NAME = 'missing_facet_search_facet_name',

/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_facet_search_facet_query */
INVALID_FACET_SEARCH_FACET_QUERY = 'invalid_facet_search_facet_query',
}

export type TokenIndexRules = {
Expand Down
205 changes: 205 additions & 0 deletions tests/__snapshots__/facet_search.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test on POST search Admin key: basic facet value search 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "action",
},
Object {
"count": 2,
"value": "adventure",
},
],
"facetQuery": "a",
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Admin key: facet value search with filter 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "action",
},
],
"facetQuery": "a",
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Admin key: facet value search with no facet query 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "action",
},
Object {
"count": 2,
"value": "adventure",
},
Object {
"count": 1,
"value": "comedy",
},
Object {
"count": 2,
"value": "romance",
},
],
"facetQuery": null,
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Admin key: facet value search with search query 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "adventure",
},
],
"facetQuery": "a",
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Master key: basic facet value search 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "action",
},
Object {
"count": 2,
"value": "adventure",
},
],
"facetQuery": "a",
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Master key: facet value search with filter 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "action",
},
],
"facetQuery": "a",
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Master key: facet value search with no facet query 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "action",
},
Object {
"count": 2,
"value": "adventure",
},
Object {
"count": 1,
"value": "comedy",
},
Object {
"count": 2,
"value": "romance",
},
],
"facetQuery": null,
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Master key: facet value search with search query 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "adventure",
},
],
"facetQuery": "a",
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Search key: basic facet value search 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "action",
},
Object {
"count": 2,
"value": "adventure",
},
],
"facetQuery": "a",
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Search key: facet value search with filter 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "action",
},
],
"facetQuery": "a",
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Search key: facet value search with no facet query 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "action",
},
Object {
"count": 2,
"value": "adventure",
},
Object {
"count": 1,
"value": "comedy",
},
Object {
"count": 2,
"value": "romance",
},
],
"facetQuery": null,
"processingTimeMs": 0,
}
`;

exports[`Test on POST search Search key: facet value search with search query 1`] = `
Object {
"facetHits": Array [
Object {
"count": 1,
"value": "adventure",
},
],
"facetQuery": "a",
"processingTimeMs": 0,
}
`;
Loading

0 comments on commit b61a8e5

Please sign in to comment.