Skip to content

Commit 6c073ea

Browse files
authored
Merge pull request #1454 from mattgotteiner/matt/fix-semantic-ranker-doc-update-3
Fix semantic ranker semanticQuery documentation
2 parents ac6062e + c54428e commit 6c073ea

File tree

1 file changed

+87
-29
lines changed

1 file changed

+87
-29
lines changed

articles/search/semantic-how-to-query-request.md

Lines changed: 87 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,28 @@ A few query capabilities bypass relevance scoring, which makes them incompatible
5151

5252
## Set up the query
5353

54-
In this step, add parameters to the query request. To be successful, your query should be full text search (using the `search` parameter to pass in a string) or a vector query, and the index should contain text fields with rich semantic content and a semantic configuration.
54+
By default, queries don't use semantic ranking. To use semantic ranking, two different parameters may be used. Each parameter supports a different set of scenarios:
55+
56+
1. Set `queryType` to `semantic`:
57+
+ [Text search](search-lucene-query-architecture.md) with a simple plain text query. Empty queries result in no semantic ranking being applied to the results.
58+
+ [Hybrid search](hybrid-search-overview.md).
59+
+ [Simple](query-simple-syntax.md) or [full](query-lucene-syntax.md) syntax can't be used.
60+
1. Specify `semanticQuery`:
61+
+ [Text search](search-lucene-query-architecture.md) using the [simple](query-simple-syntax.md) or [full](query-lucene-syntax.md) syntax.
62+
+ [Vector search](vector-search-overview.md).
63+
+ [Hybrid search](hybrid-search-overview.md).
64+
+ The query specified for `semanticQuery` must be a plain text query. Empty queries aren't supported.
65+
66+
The following table illustrates the supported scenarios depending on which semantic ranking parameter is used:
67+
68+
| Semantic Ranker Parameter | Plain text search | [Simple text search syntax](query-simple-syntax.md) | [Full text search syntax](query-lucene-syntax.md) | [Vector search](vector-search-overview.md) | [Hybrid Search](hybrid-search-overview.md) | [Semantic answers](semantic-answers.md) and captions |
69+
|-|-|-|-|-|-|-|
70+
| Set `queryType` to `semantic` |||||||
71+
| Set `semanticQuery`<sup>1</sup> |||||||
72+
73+
<sup>1</sup> `semanticQuery` isn't supported in the portal [search explorer](search-explorer.md).
74+
75+
Regardless of the parameter chosen, the index should contain text fields with rich semantic content and a [semantic configuration](semantic-how-to-configure.md).
5576

5677
### [**Azure portal**](#tab/portal-query)
5778

@@ -67,11 +88,11 @@ In this step, add parameters to the query request. To be successful, your query
6788

6889
1. Enter a query, such as "historic hotel with good food", and select **Search**.
6990

70-
1. Alternatively, select **JSON view** and paste definitions into the query editor:
91+
1. Alternatively, select **JSON view** and paste definitions into the query editor. The Azure portal doesn't support using `semanticQuery`, so setting `queryType` to `"semantic"` is required:
7192

7293
:::image type="content" source="./media/semantic-search-overview/semantic-portal-json-query.png" alt-text="Screenshot showing JSON query syntax in the Azure portal." border="true":::
7394

74-
Here's a JSON example that you can paste into the view:
95+
JSON example for setting query type to semantic that you can paste into the view:
7596

7697
```json
7798
{
@@ -81,7 +102,6 @@ In this step, add parameters to the query request. To be successful, your query
81102
"semanticConfiguration": "my-semantic-config",
82103
"captions": "extractive|highlight-true",
83104
"answers": "extractive|count-3",
84-
"queryLanguage": "en-us",
85105
"highlightPreTag": "<strong>",
86106
"highlightPostTag": "</strong>",
87107
"select": "HotelId,HotelName,Description,Category"
@@ -92,50 +112,72 @@ In this step, add parameters to the query request. To be successful, your query
92112

93113
Use [Search Documents](/rest/api/searchservice/documents/search-post) to formulate the request.
94114

95-
A response includes an `@search.rerankerScore` automatically. If you want captions or answers in the response, add captions and answers to the request.
115+
A response includes an `@search.rerankerScore` automatically. If you want captions or answers in the response, enable semantic ranking by setting `queryType` to `semantic` or setting `semanticQuery` and adding captions and answers to the request.
96116

97117
The following example in this section uses the [hotels-sample-index](search-get-started-portal.md) to demonstrate semantic ranking with semantic answers and captions.
98118

99-
1. Paste the following request into a web client as a template. Replace `search-service-name` with your search service name and replace `hotels-sample-index` if you have a different index name.
119+
If you want to set `queryType` to `semantic`, paste the following request into a web client as a template. Replace `search-service-name` with your search service name and replace `hotels-sample-index` if you have a different index name.
100120

101-
```http
102-
POST https://[search-service-name].search.windows.net/indexes/hotels-sample-index/docs/search?api-version=2024-07-01     
103-
{
104-
"search": "interesting hotel with restaurant on site and cozy lobby or shared area",
105-
"count": true,
106-
"queryType": "semantic",
107-
"semanticConfiguration": "semantic-config",
108-
"captions": "extractive|highlight-true",
109-
"answers": "extractive|count-3",
110-
"queryLanguage": "en-us",
111-
"highlightPreTag": "<strong>",
112-
"highlightPostTag": "</strong>",
113-
"select": "HotelId,HotelName,Description,Category"
114-
}
115-
```
121+
```http
122+
POST https://[search-service-name].search.windows.net/indexes/hotels-sample-index/docs/search?api-version=2024-07-01
123+
{
124+
"search": "interesting hotel with restaurant on site and cozy lobby or shared area",
125+
"count": true,
126+
"queryType": "semantic",
127+
"semanticConfiguration": "semantic-config",
128+
"captions": "extractive|highlight-true",
129+
"answers": "extractive|count-3",
130+
"highlightPreTag": "<strong>",
131+
"highlightPostTag": "</strong>",
132+
"select": "HotelId,HotelName,Description,Category"
133+
}
134+
```
116135

117-
1. Set "queryType" to "semantic".
136+
1. Set `queryType` to `semantic`.
118137

119-
1. Set "search" to a full text search query. Your search string can support either the [simple syntax](query-simple-syntax.md) or [full Lucene syntax](query-lucene-syntax.md). Semantic ranking is an extension of full text search, so while "search" isn't required, you won't get an expected outcome if it's an empty search (`"search": "*"`).
138+
1. Set `search` to a simple plain text query. Since the `queryType` is set to `semantic`, [simple syntax](query-simple-syntax.md) or [full Lucene syntax](query-lucene-syntax.md) aren't supported. Supplying `*` or an empty string results in no semantic ranking being applied to the query.
120139

121-
1. Set "semanticConfiguration" to a [predefined semantic configuration](semantic-how-to-configure.md) that's embedded in your index.
140+
1. Set `semanticConfiguration` to a [predefined semantic configuration](semantic-how-to-configure.md) that's embedded in your index.
122141

123-
1. Set "answers" to specify whether [semantic answers](semantic-answers.md) are included in the result. Currently, the only valid value for this parameter is `extractive`. Answers can be configured to return a maximum of 10. The default is one. This example shows a count of three answers: `extractive|count-3`.
142+
1. Set `answers` to specify whether [semantic answers](semantic-answers.md) are included in the result. Currently, the only valid value for this parameter is `extractive`. Answers can be configured to return a maximum of 10. The default is one. This example shows a count of three answers: `extractive|count-3`.
124143

125144
Answers aren't guaranteed on every request. To get an answer, the query must look like a question and the content must include text that looks like an answer.
126145

127-
1. Set "captions" to specify whether semantic captions are included in the result. Currently, the only valid value for this parameter is `extractive`. Captions can be configured to return results with or without highlights. The default is for highlights to be returned. This example returns captions without highlights: `extractive|highlight-false`.
146+
1. Set `captions` to specify whether semantic captions are included in the result. Currently, the only valid value for this parameter is `extractive`. Captions can be configured to return results with or without highlights. The default is for highlights to be returned. This example returns captions without highlights: `extractive|highlight-false`.
128147

129148
The basis for captions and answers are the fields referenced in the "semanticConfiguration". These fields are under a combined limit in the range of 2,000 tokens or approximately 20,000 characters. If you anticipate a token count exceeding this limit, consider a [data chunking step](vector-search-how-to-chunk-documents.md) using the [Text split skill](cognitive-search-skill-textsplit.md). This approach introduces a dependency on an [AI enrichment pipeline](cognitive-search-concept-intro.md) and [indexers](search-indexer-overview.md).
130149

131-
1. Set "highlightPreTag" and "highlightPostTag" if you want to override the default highlight formatting that's applied to captions.
150+
1. Set `highlightPreTag` and `highlightPostTag` if you want to override the default highlight formatting that's applied to captions.
132151

133152
Captions apply highlight formatting over key passages in the document that summarize the response. The default is `<em>`. If you want to specify the type of formatting (for example, yellow background), you can set the highlightPreTag and highlightPostTag.
134153

135-
1. Set ["select"](search-query-odata-select.md) to specify which fields are returned in the response, and "count" to return the number of matches in the index. These parameters improve the quality of the request and readability of the response.
154+
1. Set [select](search-query-odata-select.md) to specify which fields are returned in the response, and "count" to return the number of matches in the index. These parameters improve the quality of the request and readability of the response.
136155

137156
1. Send the request to execute the query and return results.
138157

158+
If you want to use "semanticQuery" in order to use [vector search](vector-search-overview.md), [simple text syntax](query-simple-syntax.md), or [full text syntax](query-lucene-syntax.md), adjust your request to the following JSON:
159+
160+
```http
161+
POST https://[search-service-name].search.windows.net/indexes/hotels-sample-index/docs/search?api-version=2024-07-01
162+
{
163+
"search": "Description:breakfast",
164+
"semanticQuery": "interesting hotel with restaurant on site and cozy lobby or shared area",
165+
"count": true,
166+
"queryType": "full",
167+
"semanticConfiguration": "semantic-config",
168+
"captions": "extractive|highlight-true",
169+
"answers": "extractive|count-3",
170+
"highlightPreTag": "<strong>",
171+
"highlightPostTag": "</strong>",
172+
"select": "HotelId,HotelName,Description,Category"
173+
}
174+
```
175+
176+
1. Set `queryType` to the search syntax you're using, either [simple](query-simple-syntax.md) or [full](query-lucene-syntax.md).
177+
178+
1. Set `semanticQuery` to the simple plain text query you want to use for semantic ranking. Empty queries aren't supported.
179+
180+
139181
### [**.NET SDK**](#tab/dotnet-query)
140182
141183
Use QueryType or SemanticQuery to invoke semantic ranker on a semantic query. The [following example](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/search/Azure.Search.Documents/samples/Sample08_SemanticSearch.md) is from the Azure SDK team.
@@ -151,7 +193,6 @@ SearchResults<Hotel> response = await searchClient.SearchAsync<Hotel>(
151193
QueryCaption = new(QueryCaptionType.Extractive),
152194
QueryAnswer = new(QueryAnswerType.Extractive)
153195
},
154-
QueryLanguage = QueryLanguage.EnUs,
155196
QueryType = SearchQueryType.Semantic
156197
});
157198
@@ -187,6 +228,23 @@ await foreach (SearchResult<Hotel> result in response.GetResultsAsync())
187228
Console.WriteLine($"Total number of search results:{count}");
188229
```
189230

231+
To use `semanticQuery` instead of setting `queryType` to `semantic`, the search code snippet can be replaced with the following code snippet:
232+
233+
```csharp
234+
SearchResults<Hotel> response = await searchClient.SearchAsync<Hotel>(
235+
"Luxury hotel",
236+
new SearchOptions
237+
{
238+
SemanticSearch = new()
239+
{
240+
SemanticConfigurationName = "my-semantic-config",
241+
QueryCaption = new(QueryCaptionType.Extractive),
242+
QueryAnswer = new(QueryAnswerType.Extractive),
243+
SemanticQuery = "Is there any hotel located on the main commercial artery of the city in the heart of New York?"
244+
}
245+
});
246+
```
247+
190248
---
191249

192250
## Evaluate the response

0 commit comments

Comments
 (0)