Skip to content

Commit f501a54

Browse files
meili-bors[bot]danFbachcurquiza
authored
Merge #579
579: Update search docs r=ahmednfwela a=danFbach # Pull Request ## Related issue Fixes #577, references #578 ## What does this PR do? - Updates docs to better reflect how results can be consumed. - Some of the existing docs are wrong as `SearchResult<T> result = await index.SearchAsync(...);` is actually invalid since an explicit cast is required from `ISearchable<T>` to the type of result. - There was no context showing that the result should be cast to either `SearchResult<T>` or `PaginatedResult<T>` in order to populate properties `EstimatedTotalHits` or `TotalHits` and `TotalPages` respectively. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? If you have any critiques or anything you want added to the docs for this purpose, let me know. Just hoping to clarify how transform the interface correctly. Technically the proper type could be derive by one of two methods: ```c# var result = (SearchResult<T>)await index.SearchAsync<T>(...); ``` or ```c# var result = await index.SearchAsync<T>(...); if (result is SearchResult<T> searchResult) { //... } ``` I prefer the latter, so that's how i changed the docs. Co-authored-by: Dan Fehrenbach <[email protected]> Co-authored-by: Clémentine <[email protected]>
2 parents 9ee4f3b + 4eb2de4 commit f501a54

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

.code-samples.meilisearch.yaml

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ date_guide_filterable_attributes_1: |-
3434
await client.Index("games").UpdateFilterableAttributesAsync(new string[] { "release_timestamp" });
3535
date_guide_filter_1: |-
3636
var filters = new SearchQuery() { Filter = "release_timestamp >= 1514761200 AND release_timestamp < 1672527600" };
37-
SearchResult<Game> games = await client.Index("games").SearchAsync<Game>("", filters);
37+
var games = await client.Index("games").SearchAsync<Game>("", filters);
3838
date_guide_sortable_attributes_1: |-
3939
await client.Index("games").UpdateSortableAttributesAsync(new string[] { "release_timestamp" });
4040
date_guide_sort_1: |-
4141
SearchQuery sort = new SearchQuery() { Sort = new string[] { "release_timestamp:desc" }};
4242
await client.Index("games").SearchAsync<Game>("", sort);
4343
filtering_guide_nested_1: |-
4444
var filters = new SearchQuery() { Filter = "rating.users >= 90" };
45-
SearchResult<MovieRating> movies = await client.Index("movie_ratings").SearchAsync<MovieRating>("thriller", filters);
45+
var movies = await client.Index("movie_ratings").SearchAsync<MovieRating>("thriller", filters);
4646
sorting_guide_sort_nested_1: |-
4747
SearchQuery sort = new SearchQuery() { Sort = new string[] { "rating.users:asc" }};
4848
await client.Index("books").SearchAsync<Book>("science fiction", sort);
@@ -69,9 +69,15 @@ async_guide_canceled_by_1: |-
6969
swap_indexes_1: |-
7070
await client.SwapIndexesAsync(new List<IndexSwap> { new IndexSwap("indexA", "indexB"), new IndexSwap("indexX", "indexY") } });
7171
search_parameter_guide_hitsperpage_1: |-
72-
await client.Index("movies").SearchAsync<Movie>("", new SearchQuery { HitsPerPage = 15 });
72+
var result = await client.Index("movies").SearchAsync<Movie>("", new SearchQuery { HitsPerPage = 15 });
73+
if(result is PaginatedSearchResult<Movie> pagedResults)
74+
{
75+
}
7376
search_parameter_guide_page_1: |-
74-
await client.Index("movies").SearchAsync<Movie>("", new SearchQuery { Page = 2 });
77+
var result = await client.Index("movies").SearchAsync<Movie>("", new SearchQuery { Page = 2 });
78+
if(result is PaginatedSearchResult<Movie> pagedResults)
79+
{
80+
}
7581
get_one_index_1: |-
7682
await client.GetIndexAsync("movies");
7783
list_all_indexes_1: |-
@@ -269,28 +275,34 @@ field_properties_guide_displayed_1: |-
269275
});
270276
filtering_guide_1: |-
271277
SearchQuery filters = new SearchQuery() { Filter = "release_date > \"795484800\"" };
272-
SearchResult<Movie> movies = await client.Index("movie_ratings").SearchAsync<Movie>("Avengers", filters);
278+
var movies = await client.Index("movie_ratings").SearchAsync<Movie>("Avengers", filters);
273279
filtering_guide_2: |-
274280
SearchQuery filters = new SearchQuery() { Filter = "release_date > 795484800 AND (director =
275281
\"Tim Burton\" OR director = \"Christopher Nolan\")" };
276-
SearchResult<Movie> movies = await client.Index("movie_ratings").SearchAsync<Movie>("Batman", filters);
282+
var movies = await client.Index("movie_ratings").SearchAsync<Movie>("Batman", filters);
277283
filtering_guide_3: |-
278284
SearchQuery filters = new SearchQuery() { Filter = "release_date > 1577884550 AND (NOT director = \"Tim Burton\")" };
279-
SearchResult<Movie> movies = await client.Index("movie_ratings").SearchAsync<Movie>("Planet of the Apes", filters);
285+
var movies = await client.Index("movie_ratings").SearchAsync<Movie>("Planet of the Apes", filters);
280286
search_parameter_guide_query_1: |-
281287
await client.Index("movies").SearchAsync<Movie>("shifu");
282288
search_parameter_guide_offset_1: |-
283289
var sq = new SearchQuery
284290
{
285291
Offset = 1
286292
};
287-
await client.Index("movies").SearchAsync<Movie>("shifu", sq);
293+
var result = await client.Index("movies").SearchAsync<Movie>("shifu", sq);
294+
if(result is SearchResult<Movie> pagedResults)
295+
{
296+
}
288297
search_parameter_guide_limit_1: |-
289298
var sq = new SearchQuery
290299
{
291300
Limit = 2
292301
};
293-
await client.Index("movies").SearchAsync<Movie>("shifu", sq);
302+
var result = await client.Index("movies").SearchAsync<Movie>("shifu", sq);
303+
if(result is SearchResult<Movie> pagedResults)
304+
{
305+
}
294306
search_parameter_guide_retrieve_1: |-
295307
var sq = new SearchQuery
296308
{
@@ -394,7 +406,7 @@ getting_started_search_md: |-
394406
MeilisearchClient client = new MeilisearchClient("http://localhost:7700", "masterKey");
395407
var index = client.Index("movies");
396408
397-
SearchResult<Movie> movies = await index.SearchAsync<Movie>("botman");
409+
var movies = await index.SearchAsync<Movie>("botman");
398410
foreach (var movie in movies.Hits)
399411
{
400412
Console.WriteLine(movie.Title);
@@ -548,14 +560,14 @@ geosearch_guide_filter_settings_1: |-
548560
TaskInfo result = await client.Index("movies").UpdateFilterableAttributesAsync(attributes);
549561
geosearch_guide_filter_usage_1: |-
550562
SearchQuery filters = new SearchQuery() { Filter = "_geoRadius(45.472735, 9.184019, 2000)" };
551-
SearchResult<Restaurant> restaurants = await client.Index("restaurants").SearchAsync<Restaurant>("", filters);
563+
var restaurants = await client.Index("restaurants").SearchAsync<Restaurant>("", filters);
552564
geosearch_guide_filter_usage_2: |-
553565
SearchQuery filters = new SearchQuery()
554566
{
555567
Filter = new string[] { "_geoRadius(45.472735, 9.184019, 2000) AND type = pizza" }
556568
};
557569
558-
SearchResult<Restaurant> restaurants = await client.Index("restaurants").SearchAsync<Restaurant>("restaurants", filters);
570+
var restaurants = await client.Index("restaurants").SearchAsync<Restaurant>("restaurants", filters);
559571
geosearch_guide_sort_settings_1: |-
560572
List<string> attributes = new() { "_geo" };
561573
TaskInfo result = await client.Index("restaurants").UpdateSortableAttributesAsync(attributes);
@@ -565,7 +577,7 @@ geosearch_guide_sort_usage_1: |-
565577
Sort = new string[] { "_geoPoint(48.8561446,2.2978204):asc" }
566578
};
567579
568-
SearchResult<Restaurant> restaurants = await client.Index("restaurants").SearchAsync<Restaurant>("", filters);
580+
var restaurants = await client.Index("restaurants").SearchAsync<Restaurant>("", filters);
569581
geosearch_guide_sort_usage_2: |-
570582
SearchQuery filters = new SearchQuery()
571583
{
@@ -575,13 +587,13 @@ geosearch_guide_sort_usage_2: |-
575587
}
576588
};
577589
578-
SearchResult<Restaurant> restaurants = await client.Index("restaurants").SearchAsync<Restaurant>("restaurants", filters);
590+
var restaurants = await client.Index("restaurants").SearchAsync<Restaurant>("restaurants", filters);
579591
geosearch_guide_filter_usage_3: |-
580592
SearchQuery filters = new SearchQuery()
581593
{
582594
Filter = "_geoBoundingBox([45.494181, 9.214024], [45.449484, 9.179175])"
583595
};
584-
SearchResult<Restaurant> restaurants = await client.Index("restaurants").SearchAsync<Restaurant>("restaurants", filters);
596+
var restaurants = await client.Index("restaurants").SearchAsync<Restaurant>("restaurants", filters);
585597
primary_field_guide_create_index_primary_key: |-
586598
TaskInfo task = await client.CreateIndexAsync("books", "reference_number");
587599
primary_field_guide_update_document_primary_key: |-
@@ -624,7 +636,7 @@ delete_a_key_1: |-
624636
client.DeleteKeyAsync("6062abda-a5aa-4414-ac91-ecd7944c0f8d")
625637
security_guide_search_key_1: |-
626638
MeilisearchClient client = new MeilisearchClient("http://localhost:7700", "apiKey");
627-
SearchResult<Patient> searchResult = await client.Index("patient_medical_records").SearchAsync<Patient>();
639+
var searchResult = await client.Index("patient_medical_records").SearchAsync<Patient>();
628640
security_guide_update_key_1: |-
629641
MeilisearchClient client = new MeilisearchClient("http://localhost:7700", "masterKey");
630642
await client.UpdateKeyAsync("74c9c733-3368-4738-bbe5-1d18a5fecb37", description: "Default Search API Key");
@@ -659,7 +671,7 @@ tenant_token_guide_generate_sdk_1: |-
659671
);
660672
tenant_token_guide_search_sdk_1: |-
661673
frontEndClient = new MeilisearchClient("http://localhost:7700", token);
662-
SearchResult<Patient> searchResult = await frontEndClient.Index("patient_medical_records").SearchAsync<Patient>("blood test");
674+
var searchResult = await frontEndClient.Index("patient_medical_records").SearchAsync<Patient>("blood test");
663675
getting_started_typo_tolerance: |-
664676
var typoTolerance = new TypoTolerance {
665677
MinWordSizeTypos = new TypoTolerance.TypoSize {

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ JSON Output:
146146
All the supported options are described in the [search parameters](https://www.meilisearch.com/docs/reference/api/search#search-parameters) section of the documentation.
147147

148148
```c#
149-
SearchResult<Movie> movies = await index.SearchAsync<Movie>(
149+
var movies = await index.SearchAsync<Movie>(
150150
"car",
151151
new SearchQuery
152152
{
@@ -197,7 +197,7 @@ Note that MeiliSearch will rebuild your index whenever you update `FilterableAtt
197197
Then, you can perform the search:
198198

199199
```c#
200-
SearchResult<Movie> movies = await index.SearchAsync<Movie>(
200+
var movies = await index.SearchAsync<Movie>(
201201
"wonder",
202202
new SearchQuery
203203
{
@@ -225,6 +225,41 @@ JSON Output:
225225
}
226226
```
227227

228+
#### Search with Limit and Offset
229+
230+
You can paginate search results by making queries combining both [offset](https://www.meilisearch.com/docs/reference/api/search#offset) and [limit](https://www.meilisearch.com/docs/reference/api/search#limit).
231+
232+
```c#
233+
var results = await index.SearchAsync<T>(query, new SearchQuery()
234+
{
235+
Limit = 5,
236+
Offset = 0
237+
});
238+
239+
if (results is SearchResult<T> limitedResults)
240+
{
241+
var estimatedTotalHits = limitedResults.EstimatedTotalHits;
242+
}
243+
```
244+
245+
#### Search with defined number of results per page
246+
247+
To get paginated results with page numbers, the [HitsPerPage](https://www.meilisearch.com/docs/reference/api/search#number-of-results-per-page) and [Page](https://www.meilisearch.com/docs/reference/api/search#page) properties must be defined.
248+
249+
```c#
250+
var results = await index.SearchAsync<T>(query, new SearchQuery()
251+
{
252+
HitsPerPage = pageSize,
253+
Page = pageNumber,
254+
});
255+
256+
if (results is PaginatedSearchResult<T> paginatedResults)
257+
{
258+
var totalHits = paginatedResults.TotalHits;
259+
var totalPages = paginatedResults.TotalPages;
260+
}
261+
```
262+
228263
## 🤖 Compatibility with Meilisearch
229264

230265
This package guarantees compatibility with [version v1.x of Meilisearch](https://github.com/meilisearch/meilisearch/releases/latest), but some features may not be present. Please check the [issues](https://github.com/meilisearch/meilisearch-dotnet/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+label%3Aenhancement) for more info.

0 commit comments

Comments
 (0)