You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/search/tutorial-csharp-create-mvc-app.md
+20-29Lines changed: 20 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,19 +20,18 @@ In this tutorial, create a basic ASP.NET Core (Model-View-Controller) app that r
20
20
> + Filter results
21
21
> + Sort results
22
22
23
-
This tutorial puts the focus on several operations that execute server-side using the Search APIs. Although it's common to sort and filter in client-side script, knowing how to invoke these operations on the server gives you more options when designing the search experience.
23
+
This tutorial puts the focus on server-side operations called through the Search APIs. Although it's common to sort and filter in client-side script, knowing how to invoke these operations on the server gives you more options when designing the search experience.
24
24
25
25
Sample code for this tutorial can be found in the [azure-search-dotnet-samples](https://github.com/Azure-Samples/azure-search-dotnet-samples/tree/main/create-mvc-app) repository on GitHub.
<sup>1</sup> The search service can be any tier, but it must have public network access.
34
+
<sup>1</sup> The search service can be any tier, but it must have public network access for this tutorial.
36
35
37
36
<sup>2</sup> To complete this tutorial, you need to create the hotels-sample-index on your search service. Make sure the search index name is`hotels-sample-index`, or change the index name in the `HomeController.cs` file.
38
37
@@ -56,13 +55,13 @@ Sample code for this tutorial can be found in the [azure-search-dotnet-samples](
56
55
57
56
1. Browse for `Azure.Search.Documents` and install the latest stable version.
58
57
59
-
1. Browse for and install the `Microsoft.Spatial` package. The sample index includes a GeoPoint data type. Installing this package avoids run time errors. Alternatively, remove the field from the Hotels class if you don't want to install the package.
58
+
1. Browse for and install the `Microsoft.Spatial` package. The sample index includes a GeographyPoint data type. Installing this package avoids run time errors. Alternatively, remove the "Location" field from the Hotels class if you don't want to install the package. It's not used in this tutorial.
60
59
61
60
### Add service information
62
61
63
-
For the connection, the app presents a query API key to your fully qualified search URL specified in the `appsettings.json` file.
62
+
For the connection, the app presents a query API key to your fully qualified search URL. Both are specified in the `appsettings.json` file.
64
63
65
-
Modify `appsettings.json` to specify your search service and query API key.
64
+
Modify `appsettings.json` to specify your search service and [query API key](search-security-api-keys.md).
66
65
67
66
```json
68
67
{
@@ -75,7 +74,7 @@ Modify `appsettings.json` to specify your search service and query API key.
75
74
76
75
In this step, create models that represent the schema of the hotels-search-index.
77
76
78
-
1. In Solution explorer, right-select **Models** and add a new class named "Hotel".
77
+
1. In Solution explorer, right-select **Models** and add a new class named "Hotel" for the following code:
79
78
80
79
```csharp
81
80
usingAzure.Search.Documents.Indexes.Models;
@@ -122,7 +121,6 @@ In this step, create models that represent the schema of the hotels-search-index
122
121
123
122
publicRooms[] Rooms { get; set; }
124
123
}
125
-
126
124
}
127
125
```
128
126
@@ -149,7 +147,6 @@ In this step, create models that represent the schema of the hotels-search-index
@@ -193,7 +190,7 @@ In this step, create models that represent the schema of the hotels-search-index
193
190
}
194
191
```
195
192
196
-
1. Add a class named "SearchData". and replace it with the following code:
193
+
1. Add a class named "SearchData" and replace it with the following code:
197
194
198
195
```csharp
199
196
usingAzure.Search.Documents.Models;
@@ -293,8 +290,8 @@ For this tutorial, modify the default `HomeController` to contain methods that e
293
290
IncludeTotalCount=true
294
291
};
295
292
296
-
// Enter Hotel property names into this list so only these values will be returned.
297
-
// If Select is empty, all values will be returned, which can be inefficient.
293
+
// Enter Hotel property names to specify which fields are returned.
294
+
// If Select is empty, all "retrievable" fields are returned.
298
295
options.Select.Add("HotelName");
299
296
options.Select.Add("Category");
300
297
options.Select.Add("Rating");
@@ -334,11 +331,7 @@ For this tutorial, modify the default `HomeController` to contain methods that e
334
331
<img src="~/images/azure-logo.png" width="80" />
335
332
<h2>Search for Hotels</h2>
336
333
337
-
<p>Use this demo app to test server-side behaviors, including sorting, filters, and relevance tuning. Modify the RunQueryAsnc method to change the operation.
338
-
</p>
339
-
340
-
<p>The app connects to the sample hotels index on your search service using the default search configuration (simple search, with searchMode=Any).
341
-
</p>
334
+
<p>Use this demo app to test server-side sorting and filtering. Modify the RunQueryAsync method to change the operation. The app uses the default search configuration (simple search syntax, with searchMode=Any).</p>
342
335
343
336
<form asp-controller="Home" asp-action="Index">
344
337
<p>
@@ -409,7 +402,7 @@ Index field attributes determine which fields are searchable, filterable, sortab
409
402
410
403
A filter always executes first, followed by a query assuming one is specified.
411
404
412
-
1. Open the `HomeController` and replace **RunQueryAsync** method with the following version:
405
+
1. Open the `HomeController` and find the **RunQueryAsync** method. Add [Filter](/dotnet/api/azure.search.documents.searchoptions.filter) to `var options = new SearchOptions()`:
1. Select **Search** to run an empty query. The filter criteria returns 18 documents instead of the original 50.
435
+
436
+
For more information about filter expressions, see [Filters in Azure Cognitive Search](search-filters.md) and [OData $filter syntax in Azure Cognitive Search](search-query-odata-filter.md).
437
+
445
438
## Sort results
446
439
447
-
In the hotels-sample-index, sortable fields include "Rating" and "LastRenovated". This example adds an [$OrderBy](search-query-odata-orderby.md) expression to the "Rating" field.
440
+
In the hotels-sample-index, sortable fields include "Rating" and "LastRenovated". This example adds an [$OrderBy](/dotnet/api/azure.search.documents.searchoptions.orderby) expression to the "Rating" field.
448
441
449
442
1. Open the `HomeController` and replace **RunQueryAsync** method with the following version:
450
443
@@ -460,25 +453,23 @@ In the hotels-sample-index, sortable fields include "Rating" and "LastRenovated"
460
453
461
454
options.OrderBy.Add("Rating desc");
462
455
463
-
// Enter Hotel property names into this list so only these values will be returned.
464
-
// If Select is empty, all values will be returned, which can be inefficient.
465
456
options.Select.Add("HotelName");
466
457
options.Select.Add("Category");
467
458
options.Select.Add("Rating");
468
459
options.Select.Add("Tags");
469
460
options.Select.Add("Address/City");
470
461
options.Select.Add("Address/StateProvince");
471
462
options.Select.Add("Description");
472
-
473
-
// For efficiency, the search call should be asynchronous, so use SearchAsync rather than Search.
0 commit comments