Skip to content

Add code samples for sorting/filtering nested fields #2050

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 5 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ filtering_guide_3: |-
"q": "Planet of the Apes",
"filter": "rating >= 3 AND (NOT director = \"Tim Burton\")"
}' \
filtering_guide_nested_1: |-
curl \
-X POST 'http://localhost:7700/indexes/movies/search' \
-H 'Content-Type: application/json' \
--data-binary '{
"q": "thriller",
"filter": "rating.users >= 90"
}'
search_parameter_guide_query_1: |-
curl \
-X POST 'http://localhost:7700/indexes/movies/search' \
Expand Down Expand Up @@ -717,6 +725,14 @@ sorting_guide_update_ranking_rules_1: |-
"attribute",
"exactness"
]'
sorting_guide_sort_nested_1: |-
curl \
-X POST 'http://localhost:7700/indexes/books/search' \
-H 'Content-Type: application/json' \
--data-binary '{
"q": "science fiction",
"sort": ["rating.users:asc"]
}'
sorting_guide_sort_parameter_1: |-
curl \
-X POST 'http://localhost:7700/indexes/books/search' \
Expand Down
2 changes: 2 additions & 0 deletions .vuepress/public/sample-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ field_properties_guide_displayed_1: |-
filtering_guide_1: |-
filtering_guide_2: |-
filtering_guide_3: |-
filtering_guide_nested_1: |-
search_parameter_guide_query_1: |-
search_parameter_guide_offset_1: |-
search_parameter_guide_limit_1: |-
Expand Down Expand Up @@ -101,6 +102,7 @@ sorting_guide_update_sortable_attributes_1: |-
sorting_guide_update_ranking_rules_1: |-
sorting_guide_sort_parameter_1: |-
sorting_guide_sort_parameter_2: |-
sorting_guide_sort_nested_1: |-
get_sortable_attributes_1: |-
update_sortable_attributes_1: |-
reset_sortable_attributes_1: |-
Expand Down
21 changes: 17 additions & 4 deletions learn/advanced/filtering_and_faceted_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Suppose you have a collection of movies containing the following fields:
"Horror",
"Mystery"
],
"rating": {
"critics": 86,
"users": 73
},
"overview": "Husband and wife Gabe and Adelaide Wilson take their…"
},
Expand Down Expand Up @@ -182,13 +186,16 @@ Suppose that you have a dataset containing several movies in the following forma
"Horror",
"Thriller"
],
"rating": 4
"rating": {
"critics": 86,
"users": 73
},
},
]
```

If you want to enable filtering using `director`, `release_date`, `genres`, and `rating`, you must add these attributes to the [`filterableAttributes` index setting](//reference/api/settings.md#filterable-attributes).
If you want to enable filtering using `director`, `release_date`, `genres`, and `rating.users`, you must add these attributes to the [`filterableAttributes` index setting](//reference/api/settings.md#filterable-attributes).

You can then restrict a search so it only returns movies released after 18 March 1995 with the following filter containing a single condition:

Expand Down Expand Up @@ -217,7 +224,7 @@ Note that filtering on string values is case-insensitive.
If you only want well-rated movies that weren't directed by `Tim Burton`, you can use this filter:

```SQL
rating >= 3 AND (NOT director = "Tim Burton")
rating.users >= 80 AND (NOT director = "Tim Burton")
```

You can use this filter when searching for `Planet of the Apes`:
Expand All @@ -227,7 +234,7 @@ You can use this filter when searching for `Planet of the Apes`:
`NOT director = "Tim Burton"` will include both documents that do not contain `"Tim Burton"` in its `director` field and documents without a `director` field. To return only documents that have a `director` field, expand the filter expression with the `EXISTS` operator:

```SQL
rating >= 3 AND (NOT director = "Tim Burton" AND director EXISTS)
rating.users >= 80 AND (NOT director = "Tim Burton" AND director EXISTS)
```

## Filtering with `_geoRadius`
Expand All @@ -250,6 +257,12 @@ When using a dataset of restaurants containing geopositioning data, we can filte

[You can read more about filtering results with `_geoRadius` in our geosearch guide.](/learn/advanced/geosearch.md#filtering-results-with-georadius)

#### Filtering by nested fields

Use dot notation to filter results based on a document's nested fields. The following query only returns thrillers with good user reviews:

<CodeSamples id="filtering_guide_nested_1" />

## Faceted search

Meilisearch filters can be used to build **faceted search** interfaces. This type of interface allows users to refine search results based on broad categories or **facets**. For example, a clothing webshop can use faceted search to allow users to easily explore items of a certain size or belonging to a specific brand.
Expand Down
42 changes: 42 additions & 0 deletions learn/advanced/sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Suppose you have collection of books containing the following fields:
"genres": [
"science fiction"
],
"rating": {
"critics": 95,
"users": 87
},
"price": 5.00
},
{
Expand All @@ -54,6 +58,10 @@ Suppose you have collection of books containing the following fields:
"genres": [
"science fiction"
],
"rating": {
"critics": 90,
"users": 92
},
"price": 10.00
},
{
Expand All @@ -64,6 +72,10 @@ Suppose you have collection of books containing the following fields:
"feminism",
"philosophy"
],
"rating": {
"critics": 86,
"users": 73
},
"price": 10.00
},
{
Expand All @@ -73,6 +85,10 @@ Suppose you have collection of books containing the following fields:
"genres": [
"fantasy"
],
"rating": {
"critics": 84,
"users": 80
},
"price": 5.00
},
Expand Down Expand Up @@ -156,6 +172,10 @@ With our example dataset, the results look like this:
"genres": [
"science fiction"
],
"rating": {
"critics": 95,
"users": 87
},
"price": 5.00
},
{
Expand All @@ -165,6 +185,10 @@ With our example dataset, the results look like this:
"genres": [
"science fiction"
],
"rating": {
"critics": 90,
"users": 92
},
"price": 10.00
}
]
Expand All @@ -183,6 +207,10 @@ It is common to search books based on an author's name. `sort` can help grouping
"genres": [
"science fiction"
],
"rating": {
"critics": 90,
"users": 92
},
"price": 10.00
},
{
Expand All @@ -192,6 +220,10 @@ It is common to search books based on an author's name. `sort` can help grouping
"genres": [
"fantasy"
],
"rating": {
"critics": 84,
"users": 80
},
"price": 5.00
},
{
Expand All @@ -202,11 +234,21 @@ It is common to search books based on an author's name. `sort` can help grouping
"feminism",
"philosophy"
],
"rating": {
"critics": 86,
"users": 73
},
"price": 10.00
}
]
```

#### Sorting by nested fields

Use dot notation to sort results based on a document's nested fields. The following query sorts returned documents by their user review scores:

<CodeSamples id="sorting_guide_sort_nested_1" />

## Sorting and custom ranking rules

There is a lot of overlap between sorting and configuring [custom ranking rules](/learn/core_concepts/relevancy.md#custom-rules), as both can greatly influence which results a user will see first.
Expand Down
2 changes: 1 addition & 1 deletion reference/api/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ You can then use the filter in a search query:

<CodeSamples id="faceted_search_walkthrough_filter_1" />

#### Filtering results `_geoRadius`
#### Filtering results with `_geoRadius`

If your documents contain `_geo` data, you can use the `_geoRadius` built-in filter rule to filter results according to their geographic position.

Expand Down