Skip to content

Commit d3695c2

Browse files
bidoubiwagithub-actions[bot]meili-botmeili-bors[bot]
authored
Prototype beta/prototype search for facet values (#1144)
* Add prototype beta changeset * Add compatibility with searchable on facetRefinements * Add changeset for searchable parameter * Merge * Add a facet search context * Update changeset * Update pre mode * Update pre mode * Update pre mode * Version Packages (prototype-search-for-facet-values) (#1158) Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> * Update README.md * Remove pre changeset mode --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: meili-bot <[email protected]> Co-authored-by: meili-bors[bot] <89034592+meili-bors[bot]@users.noreply.github.com>
1 parent facd110 commit d3695c2

File tree

16 files changed

+124
-28
lines changed

16 files changed

+124
-28
lines changed

.changeset/config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"@meilisearch/angular-playground",
1414
"@meilisearch/vue3-ts-playground",
1515
"@meilisearch/react-playground",
16-
"@meilisearch/local-react-playground"
16+
"@meilisearch/local-react-playground",
17+
"@meilisearch/node-playground",
18+
"@meilisearch/autocomplete-playground"
1719
]
1820
}

.changeset/curly-elephants-juggle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@meilisearch/instant-meilisearch": patch
3+
---
4+
5+
Add compatibility with the `searchable` parameter of the [`RefinementList`](https://www.algolia.com/doc/api-reference/widgets/refinement-list/js/) widget

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ For general information on how to use Meilisearch—such as our API reference, t
3838

3939
## ⚡ Supercharge your Meilisearch experience
4040

41-
Say goodbye to server deployment and manual updates with [Meilisearch Cloud](https://www.meilisearch.com/pricing?utm_campaign=oss&utm_source=integration&utm_medium=meilisearch-js-plugins). No credit card required.
41+
Say goodbye to server deployment and manual updates with [Meilisearch Cloud](https://www.meilisearch.com/pricing?utm_campaign=oss&utm_source=integration&utm_medium=meilisearch-js-plugins). Get started with a 14-day free trial! No credit card required.
4242

4343
## 🐱 Plugins
4444

packages/autocomplete-client/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @meilisearch/autocomplete-client
22

3+
## 0.2.2-prototype-search-for-facet-values.0
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [458bbf3]
8+
- @meilisearch/instant-meilisearch@0.13.3-prototype-search-for-facet-values.0
9+
310
## 0.2.1
411

512
### Patch Changes

packages/autocomplete-client/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@meilisearch/autocomplete-client",
3-
"version": "0.2.1",
3+
"version": "0.2.2-prototype-search-for-facet-values.0",
44
"private": false,
55
"description": "The search client to use Meilisearch with autocomplete.js.",
66
"homepage": "https://github.com/meilisearch/meilisearch-js-plugins/tree/main/packages/autocomplete-client",
@@ -47,7 +47,7 @@
4747
"url": "https://github.com/meilisearch/meilisearch-js-plugins.git"
4848
},
4949
"dependencies": {
50-
"@meilisearch/instant-meilisearch": "*"
50+
"@meilisearch/instant-meilisearch": "0.13.3-prototype-search-for-facet-values.0"
5151
},
5252
"devDependencies": {
5353
"@algolia/autocomplete-js": "^1.7.4",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const PACKAGE_VERSION = '0.2.1'
1+
export const PACKAGE_VERSION = '0.2.2-prototype-search-for-facet-values.0'

packages/instant-meilisearch/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,11 +707,11 @@ The `refinementList` widget is one of the most common widgets you can find in a
707707
- ✅ limit: How many facet values to retrieve.
708708
- ✅ showMore: Whether to display a button that expands the number of items.
709709
- ✅ showMoreLimit: The maximum number of displayed items. Does not work when showMoreLimit > limit.
710-
- searchable: Whether to add a search input to let the user search for more facet values. Not supported by Meilisearch. If you'd like to see it implemented [please vote](https://roadmap.meilisearch.com/c/64-search-for-facet-values?utm_medium=social&utm_source=portal_share).
711-
- searchablePlaceholder: The value of the search input’s placeholder. Not supported, see `searchable`.
712-
- searchableIsAlwaysActive: When false, disables the facet search input. Not supported, see `searchable`.
713-
- ❌ searchableEscapeFacetValues: When true, escapes the facet values. Not supported, see `searchable`.
714-
- ❌ sortBy: Not supported natively but can be implemented manually using `transformItems` options.
710+
- searchable: Whether to add a search input to let the user search for more facet values. Not supported by Meilisearch. If you'd like to see it implemented [please vote](https://roadmap.meilisearch.com/c/64-search-for-facet-values?utm_medium=social&utm_source=portal_share).
711+
- searchablePlaceholder: The value of the search input’s placeholder. Not supported, see `searchable`.
712+
- searchableIsAlwaysActive: When false, disables the facet search input. Not supported, see `searchable`.
713+
- ❌ searchableEscapeFacetValues: When true, escapes the facet values.
714+
- ❌ sortBy: Not supported but can be implemented manually using `transformItems` options.
715715
- ✅ transformItems: A function to transform the items passed to the templates.
716716
- ✅ templates: The templates to use for the widget.
717717
- ✅ cssClasses: The CSS classes to override.

packages/instant-meilisearch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"templates"
5151
],
5252
"dependencies": {
53-
"meilisearch": "^0.33.0"
53+
"meilisearch": "0.33.0-prototype-search-for-facet-values.1"
5454
},
5555
"devDependencies": {
5656
"@babel/cli": "^7.21.0",

packages/instant-meilisearch/src/client/instant-meilisearch-client.ts

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
FacetDistribution,
99
PaginationState,
1010
MeilisearchConfig,
11+
AlgoliaSearchForFacetValuesRequests,
12+
AlgoliaSearchForFacetValuesResponse,
1113
} from '../types'
1214
import {
1315
getApiKey,
@@ -19,7 +21,7 @@ import {
1921
adaptSearchParams,
2022
SearchResolver,
2123
} from '../adapter'
22-
import { createSearchContext } from '../contexts'
24+
import { createSearchContext, createFacetSearchContext } from '../contexts'
2325
import {
2426
SearchCache,
2527
initFacetDistribution,
@@ -132,13 +134,48 @@ export function instantMeiliSearch(
132134
throw new Error(e)
133135
}
134136
},
135-
searchForFacetValues: async function (_: any) {
136-
return await new Promise((resolve, reject) => {
137-
reject(
138-
new Error('SearchForFacetValues is not compatible with Meilisearch')
137+
searchForFacetValues: async function (
138+
requests: AlgoliaSearchForFacetValuesRequests
139+
): Promise<AlgoliaSearchForFacetValuesResponse[]> {
140+
console.log(requests)
141+
142+
const results = []
143+
for (const request of requests) {
144+
const searchContext: SearchContext = createFacetSearchContext(
145+
request,
146+
instantMeiliSearchOptions
139147
)
140-
resolve([]) // added here to avoid compilation error
141-
})
148+
149+
const meilisearchSearchQuery = adaptSearchParams(searchContext)
150+
151+
const index = request.indexName
152+
const meilisearchRequest: any = {
153+
...meilisearchSearchQuery,
154+
facetQuery: request.params.facetQuery,
155+
facetName: request.params.facetName,
156+
}
157+
158+
delete meilisearchRequest.indexUid
159+
160+
const meilisearchResponse = await meilisearchClient
161+
.index(index)
162+
.searchForFacetValues(meilisearchRequest)
163+
164+
const facetHits = meilisearchResponse.facetHits.map((facetHit) => ({
165+
...facetHit,
166+
// not currently supported
167+
highlighted: facetHit.value,
168+
}))
169+
const result = {
170+
facetHits,
171+
exhaustiveFacetsCount: false,
172+
processingTimeMS: meilisearchResponse.processingTimeMs,
173+
}
174+
175+
results.push(result)
176+
}
177+
178+
return results
142179
},
143180
}
144181
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { createSearchContext } from './search-context'
1+
export { createSearchContext, createFacetSearchContext } from './search-context'

packages/instant-meilisearch/src/contexts/search-context.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
InstantMeiliSearchOptions,
33
AlgoliaMultipleQueriesQuery,
44
SearchContext,
5+
AlgoliaSearchForFacetValuesRequest,
56
} from '../types'
67
import { splitSortString } from './sort-context'
78
import { createPaginationState } from './pagination-context'
@@ -54,3 +55,37 @@ export function createSearchContext(
5455
}
5556
return searchContext
5657
}
58+
59+
// TODO: avoid code duplication
60+
/**
61+
* @param {AlgoliaMultipleQueriesQuery} searchRequest
62+
* @param {Context} options
63+
* @returns {SearchContext}
64+
*/
65+
export function createFacetSearchContext(
66+
searchRequest: AlgoliaSearchForFacetValuesRequest,
67+
options: InstantMeiliSearchOptions
68+
): SearchContext {
69+
// Split index name and possible sorting rules
70+
const [indexUid, ...sortByArray] = searchRequest.indexName.split(':')
71+
const { params: instantSearchParams } = searchRequest
72+
73+
const paginationState = createPaginationState(
74+
options.finitePagination,
75+
instantSearchParams?.hitsPerPage,
76+
instantSearchParams?.page
77+
)
78+
79+
const sortState = createSortState(sortByArray.join(':'))
80+
81+
const searchContext: SearchContext = {
82+
...options,
83+
...instantSearchParams,
84+
sort: sortState,
85+
indexUid,
86+
pagination: paginationState,
87+
placeholderSearch: options.placeholderSearch !== false, // true by default
88+
keepZeroFacets: !!options.keepZeroFacets, // false by default
89+
}
90+
return searchContext
91+
}

packages/instant-meilisearch/src/types/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { SearchClient } from 'instantsearch.js'
2-
import type { MultipleQueriesQuery as AlgoliaMultipleQueriesQuery } from '@algolia/client-search'
2+
import type {
3+
MultipleQueriesQuery as AlgoliaMultipleQueriesQuery,
4+
multipleSearchForFacetValues,
5+
} from '@algolia/client-search'
36
import type {
47
MultiSearchQuery as MeiliSearchMultiSearchParams,
58
MultiSearchResult,
@@ -12,6 +15,13 @@ export type {
1215
SearchForFacetValuesResponse as AlgoliaSearchForFacetValuesResponse,
1316
} from '@algolia/client-search'
1417

18+
export type AlgoliaSearchForFacetValuesRequests = Parameters<
19+
ReturnType<typeof multipleSearchForFacetValues>
20+
>[0]
21+
22+
export type AlgoliaSearchForFacetValuesRequest =
23+
AlgoliaSearchForFacetValuesRequests[0]
24+
1525
export type {
1626
Filter,
1727
FacetDistribution,

playgrounds/autocomplete/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@algolia/autocomplete-plugin-query-suggestions": "^1.9.3",
2727
"@algolia/autocomplete-plugin-tags": "^1.9.2",
2828
"@algolia/autocomplete-theme-classic": "^1.7.4",
29-
"@meilisearch/autocomplete-client": "*"
29+
"@meilisearch/autocomplete-client": "0.2.2-prototype-search-for-facet-values.0"
3030
},
3131
"devDependencies": {
3232
"@babel/core": "^7.13.1",

playgrounds/local-react/src/components/SingleIndex.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ const SingleIndex = () => (
5050
]}
5151
/>
5252
<h2>Genres</h2>
53-
<RefinementList attribute="genres" />
53+
<RefinementList attribute="genres" searchable={true} />
5454
<h2>Players</h2>
55-
<RefinementList attribute="players" />
55+
<RefinementList attribute="players" searchable={true} />
5656
<h2>Platforms</h2>
5757
<RefinementList attribute="platforms" />
5858
<h2>Misc</h2>

playgrounds/node-env/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
],
1616
"license": "MIT",
1717
"dependencies": {
18-
"@meilisearch/instant-meilisearch": "*"
18+
"@meilisearch/instant-meilisearch": "0.13.3-prototype-search-for-facet-values.0"
1919
}
2020
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10269,10 +10269,10 @@ [email protected]:
1026910269
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
1027010270
integrity sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==
1027110271

10272-
meilisearch@^0.33.0:
10273-
version "0.33.0"
10274-
resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.33.0.tgz#25982b193cdd22e9ec534a022dbde89c42951dc4"
10275-
integrity sha512-bYPb9WyITnJfzf92e7QFK8Rc50DmshFWxypXCs3ILlpNh8pT15A7KSu9Xgnnk/K3G/4vb3wkxxtFS4sxNkWB8w==
10272+
[email protected]-prototype-search-for-facet-values.1:
10273+
version "0.33.0-prototype-search-for-facet-values.1"
10274+
resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.33.0-prototype-search-for-facet-values.1.tgz#e8cfd380880dbf7c2c0de9e52ca581c05062a65f"
10275+
integrity sha512-tP2NHj4LIG/L7RQc8IdCOnn3u7MU5RDITf4vEjwToCREtoqadEGahypPfnjRMD7Iw/2j3ELk81SpbFArrSXpCg==
1027610276
dependencies:
1027710277
cross-fetch "^3.1.6"
1027810278

0 commit comments

Comments
 (0)