3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Diagnostics ;
6
+ using System . Text ;
7
7
using Azure . Core ;
8
8
using Azure . Search . Documents . Models ;
9
9
@@ -18,9 +18,7 @@ namespace Azure.Search.Documents
18
18
[ CodeGenModel ( "SearchRequest" ) ]
19
19
public partial class SearchOptions
20
20
{
21
- private const string QueryAnswerCountRawSplitter = "|count-" ;
22
21
private const string QueryAnswerCountRaw = "count-" ;
23
- private const string QueryAnswerThresholdRawSplitter = "|threshold-" ;
24
22
private const string QueryAnswerThresholdRaw = "threshold-" ;
25
23
private const string QueryCaptionRawSplitter = "|highlight-" ;
26
24
@@ -197,72 +195,71 @@ internal string OrderByRaw
197
195
/// <summary> A value that specifies the threshold of <see cref="SearchResults{T}.Answers"/> that should be returned as part of the search response. </summary>
198
196
public double ? QueryAnswerThreshold { get ; set ; }
199
197
200
- /// <summary> Constructed from <see cref="QueryAnswer"/>, <see cref="QueryAnswerCount"/> and <see cref="QueryAnswerThreshold"/>.</summary>
198
+ /// <summary> Constructed from <see cref="QueryAnswer"/>, <see cref="QueryAnswerCount"/> and <see cref="QueryAnswerThreshold"/>. For example: "extractive|count-1,threshold-0.7" </summary>
201
199
[ CodeGenMember ( "Answers" ) ]
202
200
internal string QueryAnswerRaw
203
201
{
204
202
get
205
203
{
206
- string queryAnswerStringValue = null ;
207
-
208
204
if ( QueryAnswer . HasValue )
209
205
{
210
- queryAnswerStringValue = $ "{ QueryAnswer . Value } { QueryAnswerCountRawSplitter } { QueryAnswerCount . GetValueOrDefault ( 1 ) } { QueryAnswerThresholdRawSplitter } { QueryAnswerThreshold . GetValueOrDefault ( 0.7 ) } ";
206
+ StringBuilder queryAnswerStringValue = new ( QueryAnswer . Value . ToString ( ) ) ;
207
+
208
+ int tokens = 0 ;
209
+ char NextToken ( ) => tokens ++ == 0 ? '|' : ',' ;
210
+
211
+ if ( QueryAnswerCount . HasValue )
212
+ {
213
+ queryAnswerStringValue . Append ( NextToken ( ) ) . Append ( $ "{ QueryAnswerCountRaw } { QueryAnswerCount . Value } ") ;
214
+ tokens = 1 ;
215
+ }
216
+
217
+ if ( QueryAnswerThreshold . HasValue )
218
+ {
219
+ queryAnswerStringValue . Append ( NextToken ( ) ) . Append ( $ "{ QueryAnswerThresholdRaw } { QueryAnswerThreshold . Value } ") ;
220
+ }
221
+
222
+ return queryAnswerStringValue . ToString ( ) ;
211
223
}
212
224
213
- return queryAnswerStringValue ;
225
+ return null ;
214
226
}
215
-
216
227
set
217
228
{
218
- if ( string . IsNullOrEmpty ( value ) )
219
- {
220
- QueryAnswer = null ;
221
- QueryAnswerCount = null ;
222
- QueryAnswerThreshold = null ;
223
- }
224
- else
229
+ if ( ! string . IsNullOrEmpty ( value ) ) // If the value is - "extractive" or "extractive|count-1" or "extractive|threshold-0.7" or "extractive|count-5,threshold-0.9" or "extractive|threshold-0.8,count-4"
225
230
{
226
- if ( value . Contains ( QueryAnswerCountRawSplitter ) || value . Contains ( QueryAnswerThresholdRawSplitter ) )
231
+ string [ ] queryAnswerValues = value . Split ( '|' ) ;
232
+ if ( ! string . IsNullOrEmpty ( queryAnswerValues [ 0 ] ) )
227
233
{
228
- string [ ] queryAnswerValues = value . Split ( '|' ) ;
229
-
230
- var queryAnswerPart = queryAnswerValues [ 0 ] ;
231
- if ( string . IsNullOrEmpty ( queryAnswerPart ) )
232
- {
233
- QueryAnswer = null ;
234
- }
235
- else
236
- {
237
- QueryAnswer = new QueryAnswerType ( queryAnswerPart ) ;
238
- }
234
+ QueryAnswer = new QueryAnswerType ( queryAnswerValues [ 0 ] ) ;
235
+ }
239
236
240
- foreach ( var queryAnswerValue in queryAnswerValues )
237
+ if ( queryAnswerValues . Length == 2 )
238
+ {
239
+ var queryAnswerParams = queryAnswerValues [ 1 ] . Split ( ',' ) ;
240
+ if ( queryAnswerParams . Length <= 2 )
241
241
{
242
- if ( queryAnswerValue . Contains ( QueryAnswerCountRaw ) )
242
+ foreach ( var param in queryAnswerParams )
243
243
{
244
- var countPart = queryAnswerValue . Substring ( queryAnswerValue . IndexOf ( QueryAnswerCountRaw , StringComparison . OrdinalIgnoreCase ) + QueryAnswerCountRaw . Length ) ;
245
- if ( int . TryParse ( countPart , out int countValue ) )
244
+ if ( param . Contains ( QueryAnswerCountRaw ) )
246
245
{
247
- QueryAnswerCount = countValue ;
246
+ var countPart = param . Substring ( param . IndexOf ( QueryAnswerCountRaw , StringComparison . OrdinalIgnoreCase ) + QueryAnswerCountRaw . Length ) ;
247
+ if ( int . TryParse ( countPart , out int countValue ) )
248
+ {
249
+ QueryAnswerCount = countValue ;
250
+ }
248
251
}
249
- }
250
- else if ( queryAnswerValue . Contains ( QueryAnswerThresholdRaw ) )
251
- {
252
- var thresholdPart = queryAnswerValue . Substring ( queryAnswerValue . IndexOf ( QueryAnswerThresholdRaw , StringComparison . OrdinalIgnoreCase ) + QueryAnswerThresholdRaw . Length ) ;
253
- if ( double . TryParse ( thresholdPart , out double thresholdValue ) )
252
+ else if ( param . Contains ( QueryAnswerThresholdRaw ) )
254
253
{
255
- QueryAnswerThreshold = thresholdValue ;
254
+ var thresholdPart = param . Substring ( param . IndexOf ( QueryAnswerThresholdRaw , StringComparison . OrdinalIgnoreCase ) + QueryAnswerThresholdRaw . Length ) ;
255
+ if ( double . TryParse ( thresholdPart , out double thresholdValue ) )
256
+ {
257
+ QueryAnswerThreshold = thresholdValue ;
258
+ }
256
259
}
257
260
}
258
261
}
259
262
}
260
- else
261
- {
262
- QueryAnswer = new QueryAnswerType ( value ) ;
263
- QueryAnswerCount = null ;
264
- QueryAnswerThreshold = null ;
265
- }
266
263
}
267
264
}
268
265
}
0 commit comments