Skip to content

Commit 11bc166

Browse files
bidoubiwadependabot-preview[bot]curquiza
authored
Add placeholder search doc and tests (#544)
* Add placeholder doc and tests * build(deps-dev): bump rollup-plugin-terser from 6.1.0 to 7.0.0 Bumps [rollup-plugin-terser](https://github.com/TrySound/rollup-plugin-terser) from 6.1.0 to 7.0.0. - [Release notes](https://github.com/TrySound/rollup-plugin-terser/releases) - [Commits](TrySound/rollup-plugin-terser@v6.1.0...v7.0.0) Signed-off-by: dependabot-preview[bot] <[email protected]> * Update README.md Co-authored-by: Clémentine Urquizar <[email protected]> * Update tests/search_tests.ts * Add placeholder doc and tests * Update README.md Co-authored-by: Clémentine Urquizar <[email protected]> * Update tests/search_tests.ts Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Clémentine Urquizar <[email protected]>
1 parent 1ca6cf3 commit 11bc166

File tree

5 files changed

+120
-6
lines changed

5 files changed

+120
-6
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,44 @@ await index.search('prince', { limit: 1, attributesToHighlight: '*' })
300300
}
301301
```
302302

303+
#### Placeholder Search
304+
305+
Placeholder search makes it possible to receive hits based on your parameters without having any query (`q`).
306+
To enable this behavior, instead of sending an empty string, the query should be `null` or `undefined`.
307+
308+
```javascript
309+
await index.search(null, {
310+
facetFilters: ['genre:fantasy'],
311+
facetsDistribution: ['genre']
312+
})
313+
```
314+
315+
```json
316+
{
317+
"hits": [
318+
{
319+
"genre": "fantasy",
320+
"id": 4,
321+
"title": "Harry Potter and the Half-Blood Prince",
322+
"comment": "The best book"
323+
},
324+
{
325+
"genre": "fantasy",
326+
"id": 42,
327+
"title": "The Hitchhiker's Guide to the Galaxy"
328+
}
329+
],
330+
"offset": 0,
331+
"limit": 20,
332+
"nbHits": 2,
333+
"exhaustiveNbHits": false,
334+
"processingTimeMs": 0,
335+
"query": "",
336+
"facetsDistribution": { "genre": { "fantasy": 2, "romance": 0, "sci fi": 0, "adventure": 0 } },
337+
"exhaustiveFacetsCount": true
338+
}
339+
```
340+
303341
## ⚙️ Development Workflow and Contributing
304342

305343
Any new contribution is more than welcome in this project!

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Index<T> extends MeiliAxiosWrapper implements Types.IndexInterface<T> {
7474
* @method search
7575
*/
7676
async search<P extends Types.SearchParams<T>>(
77-
query?: string,
77+
query?: string | null,
7878
options?: P,
7979
method: Types.Methods = 'POST'
8080
): Promise<Types.SearchResponse<T, P>> {

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export interface SearchParams<T> {
6969
}
7070

7171
export interface SearchRequest {
72-
q?: string
72+
q?: string | null
7373
offset?: number
7474
limit?: number
7575
cropLength?: number
@@ -83,7 +83,7 @@ export interface SearchRequest {
8383
}
8484

8585
export interface GetSearchRequest {
86-
q?: string
86+
q?: string | null
8787
offset?: number
8888
limit?: number
8989
attributesToRetrieve?: string
@@ -258,7 +258,7 @@ export interface IndexInterface<T = any> extends MeiliAxiosWrapperInterface {
258258
getUpdateStatus: (updateId: number) => Promise<Update>
259259
getAllUpdateStatus: () => Promise<Update[]>
260260
search: <P extends SearchParams<T>>(
261-
query: string,
261+
query?: string | null,
262262
options?: P,
263263
method?: Methods
264264
) => Promise<SearchResponse<T, P>>

tests/search_tests.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ describe.each([
344344
})
345345
})
346346

347-
test(`${permission} key: ${method} search with facetFilters and facetDistribution`, async () => {
347+
test(`${permission} key: ${method} search with facetFilters and facetsDistribution`, async () => {
348348
await client
349349
.getIndex(index.uid)
350350
.search(
@@ -402,6 +402,44 @@ describe.each([
402402
})
403403
})
404404

405+
test(`${permission} key: ${method} search with multiple facetFilters and placeholder search`, async () => {
406+
await client
407+
.getIndex(index.uid)
408+
.search(
409+
undefined,
410+
{
411+
facetFilters: ['genre:fantasy'],
412+
facetsDistribution: ['genre'],
413+
},
414+
method
415+
)
416+
.then((response) => {
417+
expect(response).toHaveProperty('facetsDistribution', {
418+
genre: { adventure: 0, fantasy: 2, romance: 0, 'sci fi': 0 },
419+
})
420+
expect(response.hits.length).toEqual(2)
421+
})
422+
})
423+
424+
test(`${permission} key: ${method} search with multiple facetFilters and placeholder search`, async () => {
425+
await client
426+
.getIndex(index.uid)
427+
.search(
428+
null,
429+
{
430+
facetFilters: ['genre:fantasy'],
431+
facetsDistribution: ['genre'],
432+
},
433+
method
434+
)
435+
.then((response) => {
436+
expect(response).toHaveProperty('facetsDistribution', {
437+
genre: { adventure: 0, fantasy: 2, romance: 0, 'sci fi': 0 },
438+
})
439+
expect(response.hits.length).toEqual(2)
440+
})
441+
})
442+
405443
test(`${permission} key: ${method} search on index with no documents and no primary key`, async () => {
406444
await client
407445
.getIndex(emptyIndex.uid)

tests/typed_search_tests.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ describe.each([
350350
})
351351
})
352352

353-
test(`${permission} key: Search with facetFilters and facetDistribution`, async () => {
353+
test(`${permission} key: Search with facetFilters and facetsDistribution`, async () => {
354354
await client
355355
.getIndex<Movie>(index.uid)
356356
.search(
@@ -408,6 +408,44 @@ describe.each([
408408
})
409409
})
410410

411+
test(`${permission} key: ${method} search with multiple facetFilters and placeholder search`, async () => {
412+
await client
413+
.getIndex<Movie>(index.uid)
414+
.search(
415+
undefined,
416+
{
417+
facetFilters: ['genre:fantasy'],
418+
facetsDistribution: ['genre'],
419+
},
420+
method
421+
)
422+
.then((response) => {
423+
expect(response).toHaveProperty('facetsDistribution', {
424+
genre: { adventure: 0, fantasy: 2, romance: 0, 'sci fi': 0 },
425+
})
426+
expect(response.hits.length).toEqual(2)
427+
})
428+
})
429+
430+
test(`${permission} key: ${method} search with multiple facetFilters and placeholder search`, async () => {
431+
await client
432+
.getIndex<Movie>(index.uid)
433+
.search(
434+
null,
435+
{
436+
facetFilters: ['genre:fantasy'],
437+
facetsDistribution: ['genre'],
438+
},
439+
method
440+
)
441+
.then((response) => {
442+
expect(response).toHaveProperty('facetsDistribution', {
443+
genre: { adventure: 0, fantasy: 2, romance: 0, 'sci fi': 0 },
444+
})
445+
expect(response.hits.length).toEqual(2)
446+
})
447+
})
448+
411449
test(`${permission} key: Search on index with no documents and no primary key`, async () => {
412450
await client
413451
.getIndex(emptyIndex.uid)

0 commit comments

Comments
 (0)