Skip to content

Commit acd8fda

Browse files
committed
Add compatibility with searchable on facetRefinements
1 parent 10fdf73 commit acd8fda

File tree

6 files changed

+56
-20
lines changed

6 files changed

+56
-20
lines changed

packages/instant-meilisearch/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,11 @@ The `refinementList` widget is one of the most common widgets you can find in a
701701
- ✅ limit: How many facet values to retrieve.
702702
- ✅ showMore: Whether to display a button that expands the number of items.
703703
- ✅ showMoreLimit: The maximum number of displayed items. Does not work when showMoreLimit > limit.
704-
- 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).
705-
- searchablePlaceholder: The value of the search input’s placeholder. Not supported, see `searchable`.
706-
- searchableIsAlwaysActive: When false, disables the facet search input. Not supported, see `searchable`.
707-
- ❌ searchableEscapeFacetValues: When true, escapes the facet values. Not supported, see `searchable`.
708-
- ❌ sortBy: Not supported natively but can be implemented manually using `transformItems` options.
704+
- 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).
705+
- searchablePlaceholder: The value of the search input’s placeholder. Not supported, see `searchable`.
706+
- searchableIsAlwaysActive: When false, disables the facet search input. Not supported, see `searchable`.
707+
- ❌ searchableEscapeFacetValues: When true, escapes the facet values.
708+
- ❌ sortBy: Not supported but can be implemented manually using `transformItems` options.
709709
- ✅ transformItems: A function to transform the items passed to the templates.
710710
- ✅ templates: The templates to use for the widget.
711711
- ✅ 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
@@ -47,7 +47,7 @@
4747
"templates"
4848
],
4949
"dependencies": {
50-
"meilisearch": "^0.33.0"
50+
"meilisearch": "0.33.0-prototype-search-for-facet-values.1"
5151
},
5252
"devDependencies": {
5353
"@babel/cli": "^7.21.0",

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
FacetDistribution,
99
PaginationState,
1010
MeilisearchConfig,
11+
AlgoliaSearchForFacetValuesRequest,
12+
AlgoliaSearchForFacetValuesResponse,
1113
} from '../types'
1214
import {
1315
getApiKey,
@@ -132,13 +134,38 @@ 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')
139-
)
140-
resolve([]) // added here to avoid compilation error
141-
})
137+
searchForFacetValues: async function (
138+
requests: AlgoliaSearchForFacetValuesRequest
139+
): Promise<AlgoliaSearchForFacetValuesResponse[]> {
140+
console.log(requests)
141+
142+
const results = []
143+
for (const request of requests) {
144+
const index = request.indexName
145+
const meilisearchRequest = {
146+
facetQuery: request.params.facetQuery,
147+
facetName: request.params.facetName,
148+
}
149+
150+
const meilisearchResponse = await meilisearchClient
151+
.index(index)
152+
.searchForFacetValues(meilisearchRequest)
153+
154+
const facetHits = meilisearchResponse.facetHits.map((facetHit) => ({
155+
...facetHit,
156+
// not currently supported
157+
highlighted: facetHit.value,
158+
}))
159+
const result = {
160+
facetHits,
161+
exhaustiveFacetsCount: false,
162+
processingTimeMS: meilisearchResponse.processingTimeMs,
163+
}
164+
165+
results.push(result)
166+
}
167+
168+
return results
142169
},
143170
}
144171
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
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+
SearchForFacetValuesQueryParams as AlgoliaSearchForFacetValuesQueryParams,
5+
SearchOptions as AlgoliaSearchOptions,
6+
} from '@algolia/client-search'
37
import type {
48
MultiSearchQuery as MeiliSearchMultiSearchParams,
59
MultiSearchResult,
@@ -12,6 +16,11 @@ export type {
1216
SearchForFacetValuesResponse as AlgoliaSearchForFacetValuesResponse,
1317
} from '@algolia/client-search'
1418

19+
export type AlgoliaSearchForFacetValuesRequest = Array<{
20+
readonly indexName: string
21+
readonly params: AlgoliaSearchForFacetValuesQueryParams & AlgoliaSearchOptions
22+
}>
23+
1524
export type {
1625
Filter,
1726
FacetDistribution,

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>

yarn.lock

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

10139-
meilisearch@^0.33.0:
10140-
version "0.33.0"
10141-
resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.33.0.tgz#25982b193cdd22e9ec534a022dbde89c42951dc4"
10142-
integrity sha512-bYPb9WyITnJfzf92e7QFK8Rc50DmshFWxypXCs3ILlpNh8pT15A7KSu9Xgnnk/K3G/4vb3wkxxtFS4sxNkWB8w==
10139+
[email protected]-prototype-search-for-facet-values.1:
10140+
version "0.33.0-prototype-search-for-facet-values.1"
10141+
resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.33.0-prototype-search-for-facet-values.1.tgz#e8cfd380880dbf7c2c0de9e52ca581c05062a65f"
10142+
integrity sha512-tP2NHj4LIG/L7RQc8IdCOnn3u7MU5RDITf4vEjwToCREtoqadEGahypPfnjRMD7Iw/2j3ELk81SpbFArrSXpCg==
1014310143
dependencies:
1014410144
cross-fetch "^3.1.6"
1014510145

0 commit comments

Comments
 (0)