Skip to content

Commit 4734517

Browse files
committed
Feature prototype: define strategy on optional words
1 parent 3f9ccc6 commit 4734517

File tree

7 files changed

+46
-25
lines changed

7 files changed

+46
-25
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const searchClient = instantMeiliSearch(
8383
- [`finitePagination`](#finite-pagination): Used to work with the [`pagination`](#-pagination) widget (default: `false`) .
8484
- [`primaryKey`](#primary-key): Specify the primary key of your documents (default `undefined`).
8585
- [`keepZeroFacets`](#keep-zero-facets): Show the facets value even when they have 0 matches (default `false`).
86+
- [`optionalWords`](#optional-words): Determine the search strategy on words matching (default `last`). [See extented explaination](https://github.com/meilisearch/meilisearch/discussions/2639).
8687

8788
The options are added as the third parameter of the `instantMeilisearch` function.
8889

@@ -170,6 +171,19 @@ genres:
170171
{ keepZeroFacets : true } // default: false
171172
```
172173

174+
### Optional words
175+
176+
`optionalWords` gives you the possibility to chose how Meilisearch should handle the presence of multiple query words.
177+
178+
For example, if your query is `Hello world` by default Meilisearch returns documents containing either both `Hello` and `world` or documents that only contain `hello`. This is the `last` strategy, where words are stripped from the right.
179+
The other strategy is `none`, where both `hello` and `worlds` **must** be present in a document for it to be returned.
180+
181+
```js
182+
{
183+
optionalWords: 'none' // default last
184+
}
185+
```
186+
173187
## 🪡 Example with InstantSearch
174188

175189
The open-source [InstantSearch](https://www.algolia.com/doc/api-reference/widgets/js/) library powered by Algolia provides all the front-end tools you need to highly customize your search bar environment.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@meilisearch/instant-meilisearch",
3-
"version": "0.8.1",
3+
"version": "0.8.1-optional-words-beta.0",
44
"private": false,
55
"description": "The search client to use Meilisearch with InstantSearch.",
66
"scripts": {
@@ -57,7 +57,7 @@
5757
"url": "https://github.com/meilisearch/instant-meilisearch.git"
5858
},
5959
"dependencies": {
60-
"meilisearch": "^0.27.0"
60+
"meilisearch": "0.27.0-optional-words-beta.0"
6161
},
6262
"devDependencies": {
6363
"@babel/cli": "^7.18.9",

src/adapter/search-request-adapter/search-params-adapter.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function MeiliParamsCreator(searchContext: SearchContext) {
3232
finitePagination,
3333
sort,
3434
pagination,
35+
optionalWords,
3536
} = searchContext
3637

3738
return {
@@ -119,6 +120,11 @@ function MeiliParamsCreator(searchContext: SearchContext) {
119120
}
120121
}
121122
},
123+
addOptionalWords() {
124+
if (optionalWords) {
125+
meiliSearchParams.optionalWords = optionalWords
126+
}
127+
},
122128
}
123129
}
124130

@@ -144,6 +150,7 @@ export function adaptSearchParams(
144150
meilisearchParams.addFilters()
145151
meilisearchParams.addSort()
146152
meilisearchParams.addGeoSearchRules()
153+
meilisearchParams.addOptionalWords()
147154

148155
return meilisearchParams.getParams()
149156
}

src/contexts/search-context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function createSearchContext(
3737
placeholderSearch: options.placeholderSearch !== false, // true by default
3838
keepZeroFacets: !!options.keepZeroFacets, // false by default
3939
finitePagination: !!options.finitePagination, // false by default
40+
optionalWords: options.optionalWords || 'last',
4041
}
4142
return searchContext
4243
}

src/package-version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const PACKAGE_VERSION = '0.8.1'
1+
export const PACKAGE_VERSION = '0.8.1-optional-words-beta.0'

src/types/types.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type InstantMeiliSearchOptions = {
3434
keepZeroFacets?: boolean
3535
finitePagination?: boolean
3636
clientAgents?: string[]
37+
optionalWords?: string
3738
}
3839

3940
export type SearchCacheInterface = {
@@ -45,15 +46,6 @@ export type SearchCacheInterface = {
4546

4647
export type InsideBoundingBox = string | ReadonlyArray<readonly number[]>
4748

48-
type ClientParams = {
49-
primaryKey?: string
50-
placeholderSearch?: boolean
51-
sort?: string
52-
indexUid: string
53-
paginationTotalHits: number
54-
finitePagination: boolean
55-
}
56-
5749
export type GeoSearchContext = {
5850
aroundLatLng?: string
5951
aroundLatLngViaIP?: boolean
@@ -77,15 +69,22 @@ export type PaginationParams = {
7769
}
7870

7971
export type SearchContext = Omit<
80-
InstantSearchParams & ClientParams,
81-
'insideBoundingBox' | 'paginationTotalHits'
82-
> & {
83-
insideBoundingBox?: InsideBoundingBox
84-
keepZeroFacets?: boolean
85-
cropMarker?: string
86-
defaultFacetDistribution: FacetDistribution
87-
pagination: PaginationContext
88-
}
72+
InstantSearchParams,
73+
'insideBoundingBox' | 'optionalWords'
74+
> &
75+
InstantSearchParams & {
76+
insideBoundingBox?: InsideBoundingBox
77+
keepZeroFacets?: boolean
78+
cropMarker?: string
79+
defaultFacetDistribution: FacetDistribution
80+
pagination: PaginationContext
81+
finitePagination: boolean
82+
sort?: string
83+
indexUid: string
84+
placeholderSearch: boolean
85+
primaryKey?: string
86+
optionalWords?: string
87+
}
8988

9089
export type InstantMeiliSearchInstance = SearchClient & {
9190
clearCache: () => void

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5169,10 +5169,10 @@ [email protected]:
51695169
resolved "https://registry.yarnpkg.com/mdn-data/-/mdn-data-2.0.4.tgz#699b3c38ac6f1d728091a64650b65d388502fd5b"
51705170
integrity sha512-iV3XNKw06j5Q7mi6h+9vbx23Tv7JkjEVgKHW4pimwyDGWm0OIQntJJ+u1C6mg6mK1EaTv42XQ7w76yuzH7M2cA==
51715171

5172-
meilisearch@^0.27.0:
5173-
version "0.27.0"
5174-
resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.27.0.tgz#8bd57ddb77b975f93e054cb977b951c488ece297"
5175-
integrity sha512-kZOZFIuSO7c6xRf+Y2/9/h6A9pl0sCl/G44X4KuaSwxGbruOZPhmxbeVEgLHBv4pUFvQ56rNVTA/2d/5GCU1YA==
5172+
[email protected].0-optional-words-beta.0:
5173+
version "0.27.0-optional-words-beta.0"
5174+
resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.27.0-optional-words-beta.0.tgz#ddfd83fcfa1cc27edda542e677e445c077233938"
5175+
integrity sha512-Wg93vbYEj7OlKL4IQmyAeRW2wcZo7SR5kXDoZBe6wBjaJ641iPDhgngVofjjGhAHHsZEOCM+84ebZ8iAUq1CcA==
51765176
dependencies:
51775177
cross-fetch "^3.1.5"
51785178

0 commit comments

Comments
 (0)