Skip to content

Commit b895f03

Browse files
author
PeterTurcan
committed
type-ahead test 1
1 parent eac2fd7 commit b895f03

File tree

2 files changed

+37
-42
lines changed

2 files changed

+37
-42
lines changed

articles/search/tutorial-csharp-paging.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,15 @@ This variable is a string, which holds "next" if the next page of results should
423423
{
424424
return 3;
425425
}
426-
}
426+
}
427427
}
428428
```
429429

430430
### Add a vertical scroll bar to the view
431431

432432
1. Locate the section of the index.cshtml file that displays the results (it starts with the **@if (Model != null)**).
433433

434-
1. Replace the section with the code below. The new **<div>** section is around the area that should be scrollable and adds both an **overflow-y** attribute and a call to an **onscroll** function called "scrolled()" (or any name you want to give it), like so.
434+
1. Replace the section with the code below. The new **<div>** section is around the area that should be scrollable, and adds both an **overflow-y** attribute and a call to an **onscroll** function called "scrolled()", like so.
435435

436436
```cs
437437
@if (Model != null)
@@ -454,7 +454,7 @@ This variable is a string, which holds "next" if the next page of results should
454454
}
455455
```
456456

457-
3. Directly underneath the loop, after the </div> tag, add the scroll function.
457+
3. Directly underneath the loop, after the </div> tag, add the **scrolled** function.
458458

459459
```cs
460460
<script>

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ We have connected this script to the search box via the same id. Also, a minimum
5959

6060
The autocomplete function called in the script above is not something we have to write ourselves as it is available in the jquery library.
6161

62-
1. To access the jquery library, add the following lines to the top of the &lt;head&gt; section of the view file, so the beginning of this section looks similar to the following code.
62+
1. To access the jquery library, change the &lt;head&gt; section of the view file to the following code.
6363

6464
```cs
6565
<head>
@@ -69,6 +69,8 @@ The autocomplete function called in the script above is not something we have to
6969
rel="stylesheet">
7070
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
7171
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
72+
<link rel="stylesheet" href="~/css/hotels.css" />
73+
</head>
7274
```
7375

7476
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.
@@ -90,33 +92,30 @@ Now we can use the predefined autocomplete jquery functions.
9092
```cs
9193
public async Task<ActionResult> Suggest(bool highlights, bool fuzzy, string term)
9294
{
93-
// Use static variables to set up the configuration and Azure service and index clients, for efficiency.
94-
_builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
95-
_configuration = _builder.Build();
96-
97-
_serviceClient = CreateSearchServiceClient(_configuration);
98-
_indexClient = _serviceClient.Indexes.GetClient("hotels");
95+
InitSearch();
9996

10097
// Setup the suggest parameters.
101-
SuggestParameters sp = new SuggestParameters()
98+
var parameters = new SuggestParameters()
10299
{
103100
UseFuzzyMatching = fuzzy,
104101
Top = 8,
105102
};
106103

107104
if (highlights)
108105
{
109-
sp.HighlightPreTag = "<b>";
110-
sp.HighlightPostTag = "</b>";
106+
parameters.HighlightPreTag = "<b>";
107+
parameters.HighlightPostTag = "</b>";
111108
}
112109

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

117114
// Convert the suggest query results to a list that can be displayed in the client.
118115
List<string> suggestions = suggestResult.Results.Select(x => x.Text).ToList();
119-
return new JsonResult((object)suggestions);
116+
117+
// Return the list of suggestions.
118+
return new JsonResult(suggestions);
120119
}
121120
```
122121

@@ -147,7 +146,7 @@ If you are interested, the [Lucene query syntax in Azure Search](https://docs.mi
147146

148147
We can improve the appearance of the suggestions to the user a bit, by setting the **highlights** parameter to true. However, first we need to add some code to the view to display the bolded text.
149148

150-
1. In the view (index.cshtml), add the following script (no need to delete the **azureautosuggest** script, as we are using different ids) after the script you entered above.
149+
1. In the view (index.cshtml), add the following script after the **azureautosuggest** script you entered above.
151150

152151
```cs
153152
<script>
@@ -184,16 +183,15 @@ We can improve the appearance of the suggestions to the user a bit, by setting t
184183

185184
3. Run the app again, and you should see your entered text bolded in the suggestions. Say, try typing "pa".
186185

186+
![Typing "pa" with highlighting](./media/tutorial-csharp-create-first-app/azure-search-suggest-highlight.png)
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-
![Typing "mo" with highlighting](./media/tutorial-csharp-create-first-app/azure-search-suggest-highlight.png)
191-
192190
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.
193191

194192
## Add autocompletion
195193

196-
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.
197195

198196
1. Enter the following script into the view, following your previous scripts.
199197

@@ -219,17 +217,12 @@ Another variation that is slightly different from suggestions is autocompletion
219217
3. In the home controller we need to enter the **Autocomplete** action, say, below the **Suggest** action.
220218

221219
```cs
222-
public async Task<ActionResult> AutoComplete(string term)
220+
public async Task<ActionResult> AutoComplete(string term)
223221
{
224-
// Use static variables to set up the configuration and Azure service and index clients, for efficiency.
225-
_builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
226-
_configuration = _builder.Build();
227-
228-
_serviceClient = CreateSearchServiceClient(_configuration);
229-
_indexClient = _serviceClient.Indexes.GetClient("hotels");
222+
InitSearch();
230223

231224
// Setup the autocomplete parameters.
232-
AutocompleteParameters ap = new AutocompleteParameters()
225+
var ap = new AutocompleteParameters()
233226
{
234227
AutocompleteMode = AutocompleteMode.OneTermWithContext,
235228
Top = 6
@@ -264,23 +257,18 @@ There are libraries that offer this functionality - often called "inline autocom
264257
```cs
265258
public async Task<ActionResult> AutocompleteAndSuggest(string term)
266259
{
267-
// Use static variables to set up the configuration and Azure service and index clients, for efficiency.
268-
_builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
269-
_configuration = _builder.Build();
270-
271-
_serviceClient = CreateSearchServiceClient(_configuration);
272-
_indexClient = _serviceClient.Indexes.GetClient("hotels");
260+
InitSearch();
273261

274262
// Setup the type-ahead search parameters.
275-
AutocompleteParameters ap = new AutocompleteParameters()
263+
var ap = new AutocompleteParameters()
276264
{
277265
AutocompleteMode = AutocompleteMode.OneTermWithContext,
278266
Top = 1,
279267
};
280268
AutocompleteResult autocompleteResult = await _indexClient.Documents.AutocompleteAsync(term, "sg", ap);
281269

282270
// Setup the suggest search parameters.
283-
SuggestParameters sp = new SuggestParameters()
271+
var sp = new SuggestParameters()
284272
{
285273
Top = 8,
286274
};
@@ -289,21 +277,26 @@ There are libraries that offer this functionality - often called "inline autocom
289277
// The suggester for the hotel database is called "sg" and simply searches the hotel name.
290278
DocumentSuggestResult<Hotel> suggestResult = await _indexClient.Documents.SuggestAsync<Hotel>(term, "sg", sp);
291279

292-
List<string> results = new List<string>();
280+
// Create an empty list.
281+
var results = new List<string>();
282+
293283
if (autocompleteResult.Results.Count > 0)
294284
{
295285
// Add the top result for type-ahead.
296286
results.Add(autocompleteResult.Results[0].Text);
297-
} else
287+
}
288+
else
298289
{
299290
// There were no type-ahead suggestions, so add an empty string.
300291
results.Add("");
301292
}
302-
for (int n=0; n<suggestResult.Results.Count; n++)
293+
for (int n = 0; n < suggestResult.Results.Count; n++)
303294
{
304-
// Now add up to eight suggestions.
295+
// Now add the suggestions.
305296
results.Add(suggestResult.Results[n].Text);
306297
}
298+
299+
// Return the list.
307300
return new JsonResult(results);
308301
}
309302
```
@@ -321,10 +314,12 @@ One autocompletion option is returned at the top of the **results** list, follow
321314
</div>
322315
```
323316

317+
Note we are changing the id again, to **azureautocomplete** in this case.
318+
324319
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.
325320

326321
```cs
327-
<script>
322+
<script>
328323
$('#azureautocomplete').autocomplete({
329324
delay: 500,
330325
minLength: 2,
@@ -435,7 +430,7 @@ Notice the clever use of the **interval** function to both clear the underlying
435430
Read through the comments in the script to get a fuller understanding.
436431

437432

438-
4. Finally, we need to make a minor adjustment to the HTML style **searchBox** to make it transparent. Add the following line to this style.
433+
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.
439434

440435
```cs
441436
background: rgba(0,0,0,0);
@@ -460,7 +455,7 @@ Consider the following takeaways from this project:
460455

461456
## Next steps
462457

463-
One of the issues with autocompletion and suggestions is that they involve repeated calls to the server (one on every key stroke after the minimum number of characters typed is reached). If this results in slower than expected responses then the user experience diminishes. Facets are an interesting alternative to avoid these repeated calls, which we will look at next.
458+
One of the issues with autocompletion and suggestions is that they involve repeated calls to the server (one on every key stroke after the minimum number of characters typed is reached). If this results in slower than expected responses, then the user experience diminishes. Using facets provides an interesting alternative to avoid these repeated calls, which we will look at next.
464459

465460
> [!div class="nextstepaction"]
466461
> [C# Tutorial: Use facets to improve network efficiency - Azure Search](tutorial-csharp-facets.md)

0 commit comments

Comments
 (0)