Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the searchForFacetValues method for Meilisearch v1.3.0 #1513

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,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>('xxx').searchForFacetValues(params: SearchForFacetValuesParams, config?: Partial<Request>): Promise<SearchForFacetValuesResponse>
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
```

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

#### [Add or replace multiple documents](https://www.meilisearch.com/docs/reference/api/documents#add-or-replace-documents)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meilisearch",
"version": "0.33.0",
"version": "v0.33.0-prototype-search-for-facet-values.1",
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
"description": "The Meilisearch JS client for Node.js and the browser.",
"keywords": [
"meilisearch",
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 @@ -146,6 +148,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
2 changes: 1 addition & 1 deletion src/package-version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const PACKAGE_VERSION = '0.33.0'
export const PACKAGE_VERSION = 'v0.33.0-prototype-search-for-facet-values.1'
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 37 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ export type Crop = {
cropMarker?: string
}

export const SortFacetValuesBy = {
COUNT: 'error',
ALPHA: 'alpha',
}

export type SortFacetValuesBy = typeof SortFacetValuesBy[keyof typeof SortFacetValuesBy]

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
Comment on lines +92 to +94
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this only these 3 keys that will be responded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

 {
      facetHits: [ { value: 'adventure', count: 1 } ],
      facetQuery: 'a',
      processingTimeMs: 0
    }

}

export type SearchParams = Query &
Pagination &
Highlight &
Expand All @@ -90,6 +112,9 @@ export type SearchParams = Query &
matchingStrategy?: MatchingStrategies
hitsPerPage?: number
page?: number
sortFacetValuesBy?: SortFacetValuesBy
facetName?: string
facetQuery?: string
}

// Search parameters for searches made with the GET method
Expand Down Expand Up @@ -816,6 +841,18 @@ 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#invalid_facet_search_query */
INVALID_FACET_SEARCH_QUERY = 'invalid_facet_search_query',

/** @see https://www.meilisearch.com/docs/reference/errors/error_codes#invalid_facet_search_name */
INVALID_FACET_SEARCH_NAME = 'invalid_facet_search_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',
}

export type TokenIndexRules = {
Expand Down
108 changes: 108 additions & 0 deletions tests/facet_search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
clearAllIndexes,
config,
getClient,
} from './utils/meilisearch-test-utils'

const index = {
uid: 'movies_test',
}

const dataset = [
{
id: 123,
title: 'Pride and Prejudice',
genres: ['romance', 'action'],
},
{
id: 456,
title: 'Le Petit Prince',
genres: ['adventure', 'comedy'],
},
{
id: 2,
title: 'Le Rouge et le Noir',
genres: 'romance',
},
{
id: 1,
title: 'Alice In Wonderland',
genres: ['adventure'],
},
]

describe.each([
{ permission: 'Master' },
{ permission: 'Admin' },
{ permission: 'Search' },
])('Test on POST search', ({ permission }) => {
beforeAll(async () => {
await clearAllIndexes(config)
const client = await getClient('Master')
const newFilterableAttributes = ['genres', 'title']
await client.createIndex(index.uid)
await client.index(index.uid).updateSettings({
filterableAttributes: newFilterableAttributes,
})
const { taskUid } = await client.index(index.uid).addDocuments(dataset)
await client.waitForTask(taskUid)
})

test(`${permission} key: basic facet value search`, async () => {
const client = await getClient(permission)

const params = {
facetQuery: 'a',
facetName: 'genres',
}
const response = await client.index(index.uid).searchForFacetValues(params)

expect(response.facetHits.length).toEqual(2)
expect(response.facetQuery).toEqual('a')
})

test(`${permission} key: facet value search with no facet query`, async () => {
const client = await getClient(permission)

const params = {
facetName: 'genres',
}
const response = await client.index(index.uid).searchForFacetValues(params)

expect(response.facetHits.length).toEqual(4)
expect(response.facetQuery).toEqual(null)
})

test(`${permission} key: facet value search with filter`, async () => {
const client = await getClient(permission)

const params = {
facetName: 'genres',
facetQuery: 'a',
brunoocasali marked this conversation as resolved.
Show resolved Hide resolved
filter: ['genres = action'],
}

const response = await client.index(index.uid).searchForFacetValues(params)

expect(response.facetHits.length).toEqual(1)
})

test(`${permission} key: facet value search with search query`, async () => {
const client = await getClient(permission)

const params = {
facetName: 'genres',
facetQuery: 'a',
q: 'Alice',
}
const response = await client.index(index.uid).searchForFacetValues(params)

expect(response.facetHits.length).toEqual(1)
})
})

jest.setTimeout(100 * 1000)
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved

afterAll(() => {
return clearAllIndexes(config)
})