Skip to content

Commit ddec39a

Browse files
author
PeterTurcan
committed
few edits to type ahead tutorial
1 parent 2ac96b8 commit ddec39a

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

articles/search/tutorial-csharp-paging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ This variable is a string, which holds "next" if the next page of results should
455455
});
456456
}
457457
}
458-
</script>
458+
</script>
459459
```
460460

461461
The **if** statement in the script above tests to see if the user has scrolled to the bottom of the vertical scroll bar. If they have, a call to the **Home** controller is made to an action called **Next**. No other information is needed by the controller, it will return the next page of data. This data is then formatted using identical HTML styles as the original page. If no results are returned, nothing is appended and things stay as they are.

articles/search/tutorial-csharp-type-ahead-and-suggestions.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The key here is that we have set the **id** of the search box to **azureautosugg
4141
2. Following this statement, after the closing **&lt;/div&gt;**, enter this script.
4242

4343
```cs
44-
<script>
44+
<script>
4545
$("#azureautosuggest").autocomplete({
4646
source: "/Home/Suggest?highlights=false&fuzzy=false",
4747
minLength: 2,
@@ -76,7 +76,7 @@ The autocomplete function called in the script above is not something we have to
7676
2. We also need to remove, or comment out, a line referencing jquery in the _Layout.cshtml file (in the **Views/Shared** folder). Locate the following lines, and comment out the first script line as shown. This avoids clashing references to jquery.
7777

7878
```cs
79-
<environment include="Development">
79+
<environment include="Development">
8080
<!-- <script src="~/lib/jquery/dist/jquery.js"></script> -->
8181
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
8282
<script src="~/js/site.js" asp-append-version="true"></script>
@@ -87,7 +87,7 @@ Now we can use the predefined autocomplete jquery functions.
8787

8888
### Add the Suggest action to the controller
8989

90-
1. In the home controller add the **Suggest** action (say, after the **Page** action).
90+
1. In the home controller, add the **Suggest** action (say, after the **Page** action).
9191

9292
```cs
9393
public async Task<ActionResult> Suggest(bool highlights, bool fuzzy, string term)
@@ -108,7 +108,7 @@ Now we can use the predefined autocomplete jquery functions.
108108
}
109109

110110
// Only one suggester can be specified per index. The name of the suggester is set when the suggester is specified by other API calls.
111-
// The suggester for the hotel database is called "sg" and simply searches the hotel name.
111+
// The suggester for the hotel database is called "sg", and simply searches the hotel name.
112112
DocumentSuggestResult<Hotel> suggestResult = await _indexClient.Documents.SuggestAsync<Hotel>(term, "sg", parameters);
113113

114114
// Convert the suggest query results to a list that can be displayed in the client.
@@ -119,9 +119,9 @@ Now we can use the predefined autocomplete jquery functions.
119119
}
120120
```
121121

122-
The **Top** parameter specifies how many results to return (if unspecified, the default is 5). A _suggester_ is specified on the Azure index, which is done when the data is set up, and not by a client app such as this tutorial. In this case, the suggester is called "sg" and simply searches the **HotelName** field - nothing else.
122+
The **Top** parameter specifies how many results to return (if unspecified, the default is 5). A _suggester_ is specified on the Azure index, which is done when the data is set up, and not by a client app such as this tutorial. In this case, the suggester is called "sg" and it simply searches the **HotelName** field - nothing else.
123123

124-
Fuzzy matching allows "near misses" to be included in the output. If the **highlights** parameter is set to true then bold HTML tags are added to the output. We will set these two parameters to true in the next section.
124+
Fuzzy matching allows "near misses" to be included in the output. If the **highlights** parameter is set to true, then bold HTML tags are added to the output. We will set these two parameters to true in the next section.
125125

126126
2. You may get some syntax errors. If so, add the following two **using** statements to the top of the file.
127127

@@ -134,7 +134,7 @@ using System.Linq;
134134

135135
![Typing "po" reveals two suggestions](./media/tutorial-csharp-create-first-app/azure-search-suggest-po.png)
136136

137-
Notice that the letters you enter must start a word, not simply be included within the word.
137+
Notice that the letters you enter _must_ start a word, not simply be included within the word.
138138

139139
4. In the view script, set **&fuzzy** to true, and run the app again. Now enter "po". Notice that the search assumes you got one letter wrong!
140140

@@ -149,7 +149,7 @@ We can improve the appearance of the suggestions to the user a bit, by setting t
149149
1. In the view (index.cshtml), add the following script after the **azureautosuggest** script you entered above.
150150

151151
```cs
152-
<script>
152+
<script>
153153
var updateTextbox = function (event, ui) {
154154
var result = ui.item.value.replace(/<\/?[^>]+(>|$)/g, "");
155155
$("#azuresuggesthighlights").val(result);
@@ -187,16 +187,16 @@ We can improve the appearance of the suggestions to the user a bit, by setting t
187187

188188
4. The logic used in the highlighting script above is not foolproof. If you enter a term that appears twice in the same name, the bolded results are not quite what you would want. Try typing "mo".
189189

190-
One of the questions a developer needs to answer is when is a script working "well enough" and when should its quirks be addressed. We will not be taking highlighting any further in this tutorial, but finding a precise algorithm is something to consider if taking highlighting further.
190+
One of the questions a developer needs to answer is, when is a script working "well enough", and when should its quirks be addressed. We will not be taking highlighting any further in this tutorial, but finding a precise algorithm is something to consider if taking highlighting further.
191191

192192
## Add autocompletion
193193

194-
Another variation that is slightly different from suggestions is autocompletion (sometimes called "type-ahead"). Again, we will start with the simplest implementation before moving onto improving the user experience.
194+
Another variation, that is slightly different from suggestions, is autocompletion (sometimes called "type-ahead"). Again, we will start with the simplest implementation, before moving onto improving the user experience.
195195

196196
1. Enter the following script into the view, following your previous scripts.
197197

198198
```cs
199-
<script>
199+
<script>
200200
$("#azureautocompletebasic").autocomplete({
201201
source: "/Home/Autocomplete",
202202
minLength: 2,
@@ -208,7 +208,7 @@ Another variation that is slightly different from suggestions is autocompletion
208208
</script>
209209
```
210210

211-
2. Now change the id of the text box so it reads as follows.
211+
2. Now change the **id** of the text box, so it reads as follows.
212212

213213
```cs
214214
@Html.TextBoxFor(m => m.searchText, new { @class = "searchBox", @id = "azureautocompletebasic" }) <input value="" class="searchBoxSubmit" type="submit">
@@ -229,28 +229,29 @@ Another variation that is slightly different from suggestions is autocompletion
229229
};
230230
AutocompleteResult autocompleteResult = await _indexClient.Documents.AutocompleteAsync(term, "sg", ap);
231231

232-
// Convert the autocompleteResult results to a list that can be displayed in the client.
232+
// Convert the results to a list that can be displayed in the client.
233233
List<string> autocomplete = autocompleteResult.Results.Select(x => x.Text).ToList();
234234

235+
// Return the list.
235236
return new JsonResult(autocomplete);
236237
}
237238
```
238239

239-
Notice that we are using the same *suggester* function called "sg" in the autocomplete search as we did for suggestions (so we are only trying to autocomplete the hotel names).
240+
Notice that we are using the same *suggester* function, called "sg", in the autocomplete search as we did for suggestions (so we are only trying to autocomplete the hotel names).
240241

241-
There are a range of **AutocompleteMode** settings and we are using **OneTermWithContext**. Refer to [Azure Autocomplete](https://docs.microsoft.com/en-us/rest/api/searchservice/autocomplete) for a description of the range of options here.
242+
There are a range of **AutocompleteMode** settings, and we are using **OneTermWithContext**. Refer to [Azure Autocomplete](https://docs.microsoft.com/en-us/rest/api/searchservice/autocomplete) for a description of the range of options here.
242243

243-
4. Run the app. Notice how the range of options are single words. Try typing words starting with "re". Notice how the number of options reduces as more letters are typed.
244+
4. Run the app. Notice how the range of options displayed in the drop-down list are single words. Try typing words starting with "re". Notice how the number of options reduces as more letters are typed.
244245

245246
![Typing with basic autocompletion](./media/tutorial-csharp-create-first-app/azure-search-suggest-autocompletebasic.png)
246247

247248
As it stands, the suggestions script you ran earlier is probably more helpful than this autocompletion script. To make autocompletion more user-friendly, it is best added to the suggestion search.
248249

249250
## Combine autocompletion and suggestions
250251

251-
Combining autocompletion and suggestions is the most complex of our options and probably provides the best user experience. What we want is to display, inline with the text that is being typed, the first choice of Azure Search for autocompleting the text. Also, we want a range of suggestions as a drop-down list.
252+
Combining autocompletion and suggestions is the most complex of our options, and probably provides the best user experience. What we want is to display, inline with the text that is being typed, the first choice of Azure Search for autocompleting the text. Also, we want a range of suggestions as a drop-down list.
252253

253-
There are libraries that offer this functionality - often called "inline autocompletion" or a similar name. However, we are going to natively implement this feature so you can see what is going on. We are going to start work on the controller first in this example.
254+
There are libraries that offer this functionality - often called "inline autocompletion" or a similar name. However, we are going to natively implement this feature, so you can see what is going on. We are going to start work on the controller first in this example.
254255

255256
1. We need to add an action to the controller that returns just one autocompletion result, along with a specified number of suggestions. We will call this action **AutocompleteAndSuggest**. In the home controller, add the following action, following your other new actions.
256257

@@ -314,7 +315,7 @@ One autocompletion option is returned at the top of the **results** list, follow
314315
</div>
315316
```
316317

317-
Note we are changing the id again, to **azureautocomplete** in this case.
318+
Note we are changing the **id** again, to **azureautocomplete** in this case.
318319

319320
3. Also in the view, enter the following script, after all the scripts you have entered so far. There is quite a lot to it.
320321

@@ -425,22 +426,21 @@ Note we are changing the id again, to **azureautocomplete** in this case.
425426
</script>
426427
```
427428

428-
Notice the clever use of the **interval** function to both clear the underlying text when it no longer matches what the user is typing, and also to set the same case (upper or lower) as the user is typing (as "pa" matches "PA", "pA", "Pa" when searching) so that the overlaid text is neat.
429+
Notice the clever use of the **interval** function to both clear the underlying text when it no longer matches what the user is typing, and also to set the same case (upper or lower) as the user is typing (as "pa" matches "PA", "pA", "Pa" when searching), so that the overlaid text is neat.
429430

430431
Read through the comments in the script to get a fuller understanding.
431432

432-
433433
4. Finally, we need to make a minor adjustments to two HTML class to make them transparent. Add the following line to the **searchBoxForm** and **searchBox** classes, in the hotels.css file.
434434

435435
```cs
436-
background: rgba(0,0,0,0);
436+
background: rgba(0,0,0,0);
437437
```
438438

439439
5. Now run the app. Enter "pa" into the search box. Do you get "palace" as the autocomplete suggestion, along with two hotels that contain "pa"?
440440

441441
![Typing with inline autocomplete and suggestions](./media/tutorial-csharp-create-first-app/azure-search-suggest-autocomplete.png)
442442

443-
6. Try tabbing to accept the autocomplete suggestion, and try selecting suggestions using the arrow keys and tab, and try again using the mouse and a single click. Verify that the script handles all these situations neatly.
443+
6. Try tabbing to accept the autocomplete suggestion, and try selecting suggestions using the arrow keys and tab key, and try again using the mouse and a single click. Verify that the script handles all these situations neatly.
444444

445445
Of course, you may decide that it is simpler to load in a library that offers this feature for you, but now you know at least one way of how to get it to work!
446446

0 commit comments

Comments
 (0)