Skip to content

Commit 047e7b1

Browse files
authored
Merge pull request #111355 from HeidiSteen/heidist-master
[Azure Cog Search] Add autocomplete/suggestions to a client app
2 parents 1203b63 + 4222a80 commit 047e7b1

File tree

4 files changed

+154
-230
lines changed

4 files changed

+154
-230
lines changed

articles/search/TOC.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
items:
233233
- name: Create a suggester
234234
href: index-add-suggesters.md
235-
- name: Add suggestions or autocomplete
235+
- name: Add autocomplete and suggestions
236236
href: search-autocomplete-tutorial.md
237237
- name: Query from Power Apps
238238
href: search-howto-powerapps.md

articles/search/index-add-suggesters.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ author: HeidiSteen
88
ms.author: heidist
99
ms.service: cognitive-search
1010
ms.topic: conceptual
11-
ms.date: 04/10/2020
11+
ms.date: 04/14/2020
1212
---
1313

1414
# Create a suggester to enable autocomplete and suggested results in a query
@@ -38,15 +38,15 @@ When creating prefixes, a suggester has its own analysis chain, similar to the o
3838
3939
## Define a suggester
4040

41-
Although a suggester has several properties, it is primarily a collection of fields for which you are enabling a search-as-you-type experience. For example, a travel app might want to enable autocomplete on destinations, cities, and attractions. As such, all three fields would go in the fields collection.
41+
To create a suggester, add one to an [index schema](https://docs.microsoft.com/rest/api/searchservice/create-index) and [set each property](#property-reference). In the index, you can have one suggester (specifically, one suggester in the suggesters collection). The best time to create a suggester is when you are also defining the field that will use it.
4242

43-
To create a suggester, add one to an index schema. You can have one suggester in an index (specifically, one suggester in the suggesters collection). A suggester takes a list of fields.
43+
### Choose fields
4444

45-
+ For suggestions, choose fields that best represent a single result. Names, titles, or other unique fields that distinguish among documents work best. If fields consist of similar or identical values, the suggestions will be composed of identical results and a user won't know which one to click.
45+
Although a suggester has several properties, it is primarily a collection of fields for which you are enabling a search-as-you-type experience. For suggestions in particular, choose fields that best represent a single result. Names, titles, or other unique fields that distinguish among multiple matches work best. If fields consist of repetitive values, the suggestions consist of identical results and a user won't know which one to click.
4646

47-
+ Make sure each field in the suggester `sourceFields` list uses either the default standard Lucene analyzer (`"analyzer": null`) or a [language analyzer](index-add-language-analyzers.md) (for example, `"analyzer": "en.Microsoft"`).
47+
Make sure each field uses an analyzer that performs lexical analysis during indexing. You can use either the default standard Lucene analyzer (`"analyzer": null`) or a [language analyzer](index-add-language-analyzers.md) (for example, `"analyzer": "en.Microsoft"`).
4848

49-
Your choice of an analyzer determines how fields are tokenized and subsequently prefixed. For example, for a hyphenated string like "context-sensitive", using a language analyzer will result in these token combinations: "context", "sensitive", "context-sensitive". Had you used the standard Lucene analyzer, the hyphenated string would not exist.
49+
Your choice of an analyzer determines how fields are tokenized and subsequently prefixed. For example, for a hyphenated string like "context-sensitive", using a language analyzer will result in these token combinations: "context", "sensitive", "context-sensitive". Had you used the standard Lucene analyzer, the hyphenated string would not exist.
5050

5151
> [!TIP]
5252
> Consider using the [Analyze Text API](https://docs.microsoft.com/rest/api/searchservice/test-analyzer) for insight into how terms are tokenized and subsequently prefixed. Once you build an index, you can try various analyzers on a string to view the tokens it emits.
@@ -57,7 +57,7 @@ The best time to create a suggester is when you are also creating the field defi
5757

5858
If you try to create a suggester using pre-existing fields, the API will disallow it. Prefixes are generated during indexing, when partial terms in two or more character combinations are tokenized alongside whole terms. Given that existing fields are already tokenized, you will have to rebuild the index if you want to add them to a suggester. For more information, see [How to rebuild an Azure Cognitive Search index](search-howto-reindex.md).
5959

60-
### Create using the REST API
60+
## Create using REST
6161

6262
In the REST API, add suggesters through [Create Index](https://docs.microsoft.com/rest/api/searchservice/create-index) or
6363
[Update Index](https://docs.microsoft.com/rest/api/searchservice/update-index).
@@ -96,7 +96,7 @@ In the REST API, add suggesters through [Create Index](https://docs.microsoft.co
9696
}
9797
```
9898

99-
### Create using the .NET SDK
99+
## Create using .NET
100100

101101
In C#, define a [Suggester object](https://docs.microsoft.com/dotnet/api/microsoft.azure.search.models.suggester?view=azure-dotnet). `Suggesters` is a collection but it can only take one item.
102102

@@ -119,37 +119,40 @@ private static void CreateHotelsIndex(SearchServiceClient serviceClient)
119119
}
120120
```
121121

122-
### Property reference
122+
## Property reference
123123

124124
|Property |Description |
125125
|--------------|-----------------|
126126
|`name` |The name of the suggester.|
127-
|`searchMode` |The strategy used to search for candidate phrases. The only mode currently supported is `analyzingInfixMatching`, which performs flexible matching of phrases at the beginning or in the middle of sentences.|
127+
|`searchMode` |The strategy used to search for candidate phrases. The only mode currently supported is `analyzingInfixMatching`, which currently matches on the beginning of a term.|
128128
|`sourceFields`|A list of one or more fields that are the source of the content for suggestions. Fields must be of type `Edm.String` and `Collection(Edm.String)`. If an analyzer is specified on the field, it must be a named analyzer from [this list](https://docs.microsoft.com/dotnet/api/microsoft.azure.search.models.analyzername?view=azure-dotnet) (not a custom analyzer).<p/> As a best practice, specify only those fields that lend themselves to an expected and appropriate response, whether it's a completed string in a search bar or a dropdown list.<p/>A hotel name is a good candidate because it has precision. Verbose fields like descriptions and comments are too dense. Similarly, repetitive fields, such as categories and tags, are less effective. In the examples, we include "category" anyway to demonstrate that you can include multiple fields. |
129129

130130
<a name="how-to-use-a-suggester"></a>
131131

132132
## Use a suggester
133133

134-
A suggester is used in a query. After a suggester is created, call the appropriate API in your query logic to invoke the feature.
134+
A suggester is used in a query. After a suggester is created, call one of the following APIs for a search-as-you-type experience:
135135

136136
+ [Suggestions REST API](https://docs.microsoft.com/rest/api/searchservice/suggestions)
137137
+ [Autocomplete REST API](https://docs.microsoft.com/rest/api/searchservice/autocomplete)
138138
+ [SuggestWithHttpMessagesAsync method](https://docs.microsoft.com/dotnet/api/microsoft.azure.search.idocumentsoperations.suggestwithhttpmessagesasync?view=azure-dotnet)
139139
+ [AutocompleteWithHttpMessagesAsync method](https://docs.microsoft.com/dotnet/api/microsoft.azure.search.idocumentsoperations.autocompletewithhttpmessagesasync?view=azure-dotnet&viewFallbackFrom=azure-dotnet)
140140

141-
API usage is illustrated in the following call to the Autocomplete REST API. There are two takeaways from this example. First, as with all queries, the operation is against the documents collection of an index. Second, you can add query parameters. While many query parameters are common to both APIs, the list is different for each one.
141+
In a search application, client code should leverage a library like [jQuery UI Autocomplete](https://jqueryui.com/autocomplete/) to collect the partial query and provide the match. For more information about this task, see [Add autocomplete or suggested results to client code](search-autocomplete-tutorial.md).
142+
143+
API usage is illustrated in the following call to the Autocomplete REST API. There are two takeaways from this example. First, as with all queries, the operation is against the documents collection of an index and the query includes a **search** parameter, which in this case provides the partial query. Second, you must add **suggesterName** to the request. If a suggester is not defined in the index, a call to autocomplete or suggestions will fail.
142144

143145
```http
144-
GET https://[service name].search.windows.net/indexes/[index name]/docs/autocomplete?[query parameters]
145-
api-key: [admin or query key]
146+
POST /indexes/myxboxgames/docs/autocomplete?search&api-version=2019-05-06
147+
{
148+
"search": "minecraf",
149+
"suggesterName": "sg"
150+
}
146151
```
147152

148-
If a suggester is not defined in the index, a call to autocomplete or suggestions will fail.
149-
150153
## Sample code
151154

152-
+ [Create your first app in C#](tutorial-csharp-type-ahead-and-suggestions.md) sample demonstrates a suggester construction, suggested queries, autocomplete, and faceted navigation. This code sample runs on a sandbox Azure Cognitive Search service and uses a pre-loaded Hotels index so all you have to do is press F5 to run the application. No subscription or sign-in is necessary.
155+
+ [Create your first app in C# (lesson 3 - Add search-as-you-type)](tutorial-csharp-type-ahead-and-suggestions.md) sample demonstrates a suggester construction, suggested queries, autocomplete, and faceted navigation. This code sample runs on a sandbox Azure Cognitive Search service and uses a pre-loaded Hotels index so all you have to do is press F5 to run the application. No subscription or sign-in is necessary.
153156

154157
+ [DotNetHowToAutocomplete](https://github.com/Azure-Samples/search-dotnet-getting-started/tree/master/DotNetHowToAutocomplete) is an older sample containing both C# and Java code. It also demonstrates a suggester construction, suggested queries, autocomplete, and faceted navigation. This code sample uses the hosted [NYCJobs](https://github.com/Azure-Samples/search-dotnet-asp-net-mvc-jobs) sample data.
155158

@@ -158,4 +161,4 @@ If a suggester is not defined in the index, a call to autocomplete or suggestion
158161
We recommend the following example to see how the requests are formulated.
159162

160163
> [!div class="nextstepaction"]
161-
> [Suggestions and autocomplete examples](search-autocomplete-tutorial.md)
164+
> [Add autocomplete and suggestions to client code](search-autocomplete-tutorial.md)

0 commit comments

Comments
 (0)