Skip to content

Add support for distinct attribute #1313

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

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,5 @@ data.ms
.turbo

.angular

.idea
123 changes: 62 additions & 61 deletions packages/instant-meilisearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ Nonetheless you might want to still showcase them even if they have 0 matched do

Without `keepZeroFacets` set to `true`:
genres:
- [x] horror (2000)
- [x] thriller (214)
- [ ] comedy (0)
- [x] horror (2000)
- [x] thriller (214)
- [ ] comedy (0)

With `keepZeroFacets` set to `false`, `comedy` disapears:

genres:
- [x] horror (2000)
- [x] thriller (214)
- [x] horror (2000)
- [x] thriller (214)

```js
{ keepZeroFacets : true } // default: false
Expand Down Expand Up @@ -224,6 +224,7 @@ The following options can be overridden:
[`showRankingScore`](https://www.meilisearch.com/docs/reference/api/search#ranking-score),
[`attributesToSearchOn`](https://www.meilisearch.com/docs/reference/api/search#customize-attributes-to-search-on-at-search-time),
[`hybrid`](https://www.meilisearch.com/docs/learn/experimental/vector_search)
[`distinct`](https://www.meilisearch.com/docs/learn/relevancy/distinct_attribute)

```js
instantMeiliSearch(
Expand Down Expand Up @@ -281,20 +282,20 @@ In `index.html`:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>

<body>
<div>
<div id="searchbox"></div>
<div id="hits"></div>
</div>

<script src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script>
<script src="./app.js"></script>
</body>
<head>
<meta charset="utf-8" />
</head>

<body>
<div>
<div id="searchbox"></div>
<div id="hits"></div>
</div>

<script src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script>
<script src="./app.js"></script>
</body>
</html>
```

Expand Down Expand Up @@ -682,11 +683,11 @@ import injectScript from 'scriptjs'
injectScript(
`https://maps.googleapis.com/maps/api/js?v=quarterly&key=${GOOGLE_API}`,
() => {
const search = instantsearch({
const search = instantsearch({
indexName: 'geo',
// ...
})
// ...
})
// ...
})
```

Expand All @@ -700,9 +701,9 @@ The classic usage, with only the `required` elements, renders an embedded Google

```js
instantsearch.widgets.geoSearch({
container: '#maps',
googleReference: window.google,
}),
container: '#maps',
googleReference: window.google,
}),
```

For further customization, for example to determine an initial position for the map. Contrary to `initialZoom` and `initialPosition`, triggers a search request with the provided information.
Expand All @@ -718,24 +719,24 @@ For exemple, by adding `boundingBox` in the [`instantSearch`](#-instantsearch) w

```js
initialUiState: {
geo: {
geoSearch: {
boundingBox:
'50.680720183653065, 3.273798366642514,50.55969330590075, 2.9625244444490253',
},
geo: {
geoSearch: {
boundingBox:
'50.680720183653065, 3.273798366642514,50.55969330590075, 2.9625244444490253',
},
},
},
```
Without providing this parameter, Google Maps will default to a window containing all markers from the provided search results.

Alternatively, the parameters can be passed through the [`searchFunction`](https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/#widget-param-searchfunction) parameter of the [`instantSearch`](#-instantsearch) widget. Contrary to `initialUiState` these parameters overwrite the values on each search.

```js
searchFunction: function (helper) {
helper.setQueryParameter('aroundRadius', 75000)
helper.setQueryParameter('aroundLatLng', '51.1241999, 9.662499900000057');
helper.search()
},
helper.setQueryParameter('aroundRadius', 75000)
helper.setQueryParameter('aroundLatLng', '51.1241999, 9.662499900000057');
helper.search()
},
```

[Read the guide on how GeoSearch works in Meilisearch](https://www.meilisearch.com/docs/learn/getting_started/filtering_and_sorting#geosearch).
Expand Down Expand Up @@ -805,12 +806,12 @@ Example:
Give the following document structure:
```json
{
"id": 1,
"name": "Basic T-shirt",
"categories.lvl0": "Men",
"categories.lvl1": "Men > clothes",
"categories.lvl2": "Men > clothes > t-shirt"
}
"id": 1,
"name": "Basic T-shirt",
"categories.lvl0": "Men",
"categories.lvl1": "Men > clothes",
"categories.lvl2": "Men > clothes > t-shirt"
}
```

You have to add the fields `categories.lvl0`, `categories.lvl1` and `categories.lvl2` in the `filterableAttributes` in your Meilisearch settings.
Expand Down Expand Up @@ -953,8 +954,8 @@ The `clearRefinement` widget displays a button that lets the user clean every re

```js
instantsearch.widgets.clearRefinements({
container: '#clear-refinements',
}),
container: '#clear-refinements',
}),
```

### ✅ Pagination
Expand Down Expand Up @@ -1060,8 +1061,8 @@ The usage of the `SortBy` widget differs from the one found in Algolia's documen
- Different `sort` rules on the same index.

The items list is composed of objects containing every sort possibility you want to provide to your user. Each object must contain two fields:
- `label`: What is showcased on the user interface ex: `Sort by Ascending Price`
- `value`: The sort formula.
- `label`: What is showcased on the user interface ex: `Sort by Ascending Price`
- `value`: The sort formula.

#### Sort formula

Expand Down Expand Up @@ -1090,23 +1091,23 @@ See [relevancy guide](https://www.meilisearch.com/docs/learn/core_concepts/relev

```js
instantsearch.widgets.sortBy({
container: '#sort-by',
items: [
{ value: 'clothes', label: 'Relevant' }, // default index
{
value: 'clothes:price:desc', // Sort on descending price
label: 'Ascending price using query time sort',
},
{
value: 'clothes:price:asc', // Sort on ascending price
label: 'Descending price using query time sort',
},
{
value: 'clothes-sorted', // different index with different ranking rules.
label: 'Custom sort using a different index',
},
],
}),
container: '#sort-by',
items: [
{ value: 'clothes', label: 'Relevant' }, // default index
{
value: 'clothes:price:desc', // Sort on descending price
label: 'Ascending price using query time sort',
},
{
value: 'clothes:price:asc', // Sort on ascending price
label: 'Descending price using query time sort',
},
{
value: 'clothes-sorted', // different index with different ranking rules.
label: 'Custom sort using a different index',
},
],
}),
```

### ❌ RelevantSort
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ describe('Parameters adapter', () => {

expect(searchParams.hybrid).toBe(hybridSearchConfig)
})

test('distinct search configuration can be set via search parameters', () => {
const distinctSearchConfig = 'title'

const searchParams = adaptSearchParams({
...DEFAULT_CONTEXT,
meiliSearchParams: {
distinct: distinctSearchConfig,
},
})

expect(searchParams.distinct).toBe(distinctSearchConfig)
})
})

describe('Geo filter adapter', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
addAttributesToHighlight() {
meiliSearchParams.attributesToHighlight =
overrideParams?.attributesToHighlight ??
<Mutable<typeof attributesToHighlight>>attributesToHighlight ?? ['*']
<Mutable<typeof attributesToHighlight>>attributesToHighlight ?? ['*']

Check failure on line 142 in packages/instant-meilisearch/src/adapter/search-request-adapter/search-params-adapter.ts

View workflow job for this annotation

GitHub Actions / style-check

Insert `··`
},
addPreTag() {
meiliSearchParams.highlightPreTag =
Expand Down Expand Up @@ -226,8 +226,8 @@
overrideParams?.attributesToSearchOn !== undefined
? overrideParams.attributesToSearchOn
: <Mutable<typeof restrictSearchableAttributes>>(
restrictSearchableAttributes
)
restrictSearchableAttributes

Check failure on line 229 in packages/instant-meilisearch/src/adapter/search-request-adapter/search-params-adapter.ts

View workflow job for this annotation

GitHub Actions / style-check

Insert `··`
)

Check failure on line 230 in packages/instant-meilisearch/src/adapter/search-request-adapter/search-params-adapter.ts

View workflow job for this annotation

GitHub Actions / style-check

Insert `··`
if (value !== undefined) {
meiliSearchParams.attributesToSearchOn = value
}
Expand All @@ -238,6 +238,12 @@
meiliSearchParams.hybrid = value
}
},
addDistinct() {
const value = overrideParams?.distinct
if (value !== undefined) {
meiliSearchParams.distinct = value
}
},
}
}

Expand Down Expand Up @@ -270,6 +276,7 @@
meilisearchParams.addShowRankingScore()
meilisearchParams.addAttributesToSearchOn()
meilisearchParams.addHybridSearch()
meilisearchParams.addDistinct()

return meilisearchParams.getParams()
}
13 changes: 7 additions & 6 deletions packages/instant-meilisearch/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
| 'showRankingScore'
| 'attributesToSearchOn'
| 'hybrid'
| 'distinct'
>

type BaseInstantMeiliSearchOptions = {
Expand Down Expand Up @@ -104,15 +105,15 @@

export type MeilisearchMultiSearchResult<T = Record<string, any>> =
MultiSearchResult<T> & {
pagination: PaginationState
}
pagination: PaginationState

Check failure on line 108 in packages/instant-meilisearch/src/types/types.ts

View workflow job for this annotation

GitHub Actions / style-check

Insert `··`
}

Check failure on line 109 in packages/instant-meilisearch/src/types/types.ts

View workflow job for this annotation

GitHub Actions / style-check

Insert `··`

export type SearchContext = InstantMeiliSearchOptions &
InstantSearchParams & {
pagination: PaginationState
indexUid: string
sort?: string | string[]
}
pagination: PaginationState

Check failure on line 113 in packages/instant-meilisearch/src/types/types.ts

View workflow job for this annotation

GitHub Actions / style-check

Insert `··`
indexUid: string

Check failure on line 114 in packages/instant-meilisearch/src/types/types.ts

View workflow job for this annotation

GitHub Actions / style-check

Insert `··`
sort?: string | string[]

Check failure on line 115 in packages/instant-meilisearch/src/types/types.ts

View workflow job for this annotation

GitHub Actions / style-check

Insert `··`
}

Check failure on line 116 in packages/instant-meilisearch/src/types/types.ts

View workflow job for this annotation

GitHub Actions / style-check

Insert `··`

export type InstantSearchGeoParams = {
aroundLatLng?: string
Expand Down
Loading