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 2 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 Value
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved

#### [Search for facet value](#)
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved

```ts
client.index<T>('xxx').searchForFacetValue(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": "0.33.0-prototype-search-for-facet-values.0",
"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 searchForFacetValue(
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
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 = '0.33.0-prototype-search-for-facet-values.0'
23 changes: 23 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@ export type FieldDistribution = {
[field: string]: number
}

/*
* Facet search
*/

export type SearchForFacetValuesParams = SearchParams & {
facetName: string
facetQuery?: string
q?: string
filter?: Filter
matchingStrategy?: MatchingStrategies
}

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

export type SearchForFacetValuesResponse = {
hits: FacetHit[]
query: string | null
processingTimeMs: number
}

/*
** Documents
*/
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).searchForFacetValue(params)
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved

expect(response.hits.length).toEqual(2)
expect(response.query).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).searchForFacetValue(params)
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved

expect(response.hits.length).toEqual(4)
expect(response.query).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).searchForFacetValue(params)
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved

expect(response.hits.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).searchForFacetValue(params)
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved

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

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

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