27
27
28
28
namespace Elastic . Clients . Elasticsearch . QueryDsl ;
29
29
30
+ internal sealed partial class TextExpansionQueryConverter : JsonConverter < TextExpansionQuery >
31
+ {
32
+ public override TextExpansionQuery Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
33
+ {
34
+ if ( reader . TokenType != JsonTokenType . StartObject )
35
+ throw new JsonException ( "Unexpected JSON detected." ) ;
36
+ reader . Read ( ) ;
37
+ var fieldName = reader . GetString ( ) ;
38
+ reader . Read ( ) ;
39
+ var variant = new TextExpansionQuery ( fieldName ) ;
40
+ while ( reader . Read ( ) && reader . TokenType != JsonTokenType . EndObject )
41
+ {
42
+ if ( reader . TokenType == JsonTokenType . PropertyName )
43
+ {
44
+ var property = reader . GetString ( ) ;
45
+ if ( property == "_name" )
46
+ {
47
+ variant . QueryName = JsonSerializer . Deserialize < string ? > ( ref reader , options ) ;
48
+ continue ;
49
+ }
50
+
51
+ if ( property == "boost" )
52
+ {
53
+ variant . Boost = JsonSerializer . Deserialize < float ? > ( ref reader , options ) ;
54
+ continue ;
55
+ }
56
+
57
+ if ( property == "model_id" )
58
+ {
59
+ variant . ModelId = JsonSerializer . Deserialize < string > ( ref reader , options ) ;
60
+ continue ;
61
+ }
62
+
63
+ if ( property == "model_text" )
64
+ {
65
+ variant . ModelText = JsonSerializer . Deserialize < string > ( ref reader , options ) ;
66
+ continue ;
67
+ }
68
+ }
69
+ }
70
+
71
+ reader . Read ( ) ;
72
+ return variant ;
73
+ }
74
+
75
+ public override void Write ( Utf8JsonWriter writer , TextExpansionQuery value , JsonSerializerOptions options )
76
+ {
77
+ if ( value . Field is null )
78
+ throw new JsonException ( "Unable to serialize TextExpansionQuery because the `Field` property is not set. Field name queries must include a valid field name." ) ;
79
+ if ( options . TryGetClientSettings ( out var settings ) )
80
+ {
81
+ writer . WriteStartObject ( ) ;
82
+ writer . WritePropertyName ( settings . Inferrer . Field ( value . Field ) ) ;
83
+ writer . WriteStartObject ( ) ;
84
+ if ( ! string . IsNullOrEmpty ( value . QueryName ) )
85
+ {
86
+ writer . WritePropertyName ( "_name" ) ;
87
+ writer . WriteStringValue ( value . QueryName ) ;
88
+ }
89
+
90
+ if ( value . Boost . HasValue )
91
+ {
92
+ writer . WritePropertyName ( "boost" ) ;
93
+ writer . WriteNumberValue ( value . Boost . Value ) ;
94
+ }
95
+
96
+ writer . WritePropertyName ( "model_id" ) ;
97
+ writer . WriteStringValue ( value . ModelId ) ;
98
+ writer . WritePropertyName ( "model_text" ) ;
99
+ writer . WriteStringValue ( value . ModelText ) ;
100
+ writer . WriteEndObject ( ) ;
101
+ writer . WriteEndObject ( ) ;
102
+ return ;
103
+ }
104
+
105
+ throw new JsonException ( "Unable to retrieve client settings required to infer field." ) ;
106
+ }
107
+ }
108
+
109
+ [ JsonConverter ( typeof ( TextExpansionQueryConverter ) ) ]
30
110
public sealed partial class TextExpansionQuery : SearchQuery
31
111
{
32
- [ JsonInclude , JsonPropertyName ( "_name" ) ]
112
+ public TextExpansionQuery ( Field field )
113
+ {
114
+ if ( field is null )
115
+ throw new ArgumentNullException ( nameof ( field ) ) ;
116
+ Field = field ;
117
+ }
118
+
33
119
public string ? QueryName { get ; set ; }
34
- [ JsonInclude , JsonPropertyName ( "boost" ) ]
35
120
public float ? Boost { get ; set ; }
36
121
37
122
/// <summary>
38
123
/// <para>The text expansion NLP model to use</para>
39
124
/// </summary>
40
- [ JsonInclude , JsonPropertyName ( "model_id" ) ]
41
125
public string ModelId { get ; set ; }
42
126
43
127
/// <summary>
44
128
/// <para>The query text</para>
45
129
/// </summary>
46
- [ JsonInclude , JsonPropertyName ( "model_text" ) ]
47
130
public string ModelText { get ; set ; }
48
-
49
- /// <summary>
50
- /// <para>The name of the rank features field to search against</para>
51
- /// </summary>
52
- [ JsonInclude , JsonPropertyName ( "value" ) ]
53
- public Elastic . Clients . Elasticsearch . Field Value { get ; set ; }
131
+ public Elastic . Clients . Elasticsearch . Field Field { get ; set ; }
54
132
55
133
public static implicit operator Query ( TextExpansionQuery textExpansionQuery ) => QueryDsl . Query . TextExpansion ( textExpansionQuery ) ;
56
134
@@ -61,15 +139,29 @@ public sealed partial class TextExpansionQueryDescriptor<TDocument> : Serializab
61
139
{
62
140
internal TextExpansionQueryDescriptor ( Action < TextExpansionQueryDescriptor < TDocument > > configure ) => configure . Invoke ( this ) ;
63
141
64
- public TextExpansionQueryDescriptor ( ) : base ( )
142
+ internal TextExpansionQueryDescriptor ( ) : base ( )
143
+ {
144
+ }
145
+
146
+ public TextExpansionQueryDescriptor ( Field field )
147
+ {
148
+ if ( field is null )
149
+ throw new ArgumentNullException ( nameof ( field ) ) ;
150
+ FieldValue = field ;
151
+ }
152
+
153
+ public TextExpansionQueryDescriptor ( Expression < Func < TDocument , object > > field )
65
154
{
155
+ if ( field is null )
156
+ throw new ArgumentNullException ( nameof ( field ) ) ;
157
+ FieldValue = field ;
66
158
}
67
159
68
160
private string ? QueryNameValue { get ; set ; }
69
161
private float ? BoostValue { get ; set ; }
162
+ private Elastic . Clients . Elasticsearch . Field FieldValue { get ; set ; }
70
163
private string ModelIdValue { get ; set ; }
71
164
private string ModelTextValue { get ; set ; }
72
- private Elastic . Clients . Elasticsearch . Field ValueValue { get ; set ; }
73
165
74
166
public TextExpansionQueryDescriptor < TDocument > QueryName ( string ? queryName )
75
167
{
@@ -83,44 +175,42 @@ public TextExpansionQueryDescriptor<TDocument> Boost(float? boost)
83
175
return Self ;
84
176
}
85
177
86
- /// <summary>
87
- /// <para>The text expansion NLP model to use</para>
88
- /// </summary>
89
- public TextExpansionQueryDescriptor < TDocument > ModelId ( string modelId )
178
+ public TextExpansionQueryDescriptor < TDocument > Field ( Elastic . Clients . Elasticsearch . Field field )
90
179
{
91
- ModelIdValue = modelId ;
180
+ FieldValue = field ;
92
181
return Self ;
93
182
}
94
183
95
- /// <summary>
96
- /// <para>The query text</para>
97
- /// </summary>
98
- public TextExpansionQueryDescriptor < TDocument > ModelText ( string modelText )
184
+ public TextExpansionQueryDescriptor < TDocument > Field < TValue > ( Expression < Func < TDocument , TValue > > field )
99
185
{
100
- ModelTextValue = modelText ;
186
+ FieldValue = field ;
101
187
return Self ;
102
188
}
103
189
104
190
/// <summary>
105
- /// <para>The name of the rank features field to search against </para>
191
+ /// <para>The text expansion NLP model to use </para>
106
192
/// </summary>
107
- public TextExpansionQueryDescriptor < TDocument > Value ( Elastic . Clients . Elasticsearch . Field value )
193
+ public TextExpansionQueryDescriptor < TDocument > ModelId ( string modelId )
108
194
{
109
- ValueValue = value ;
195
+ ModelIdValue = modelId ;
110
196
return Self ;
111
197
}
112
198
113
199
/// <summary>
114
- /// <para>The name of the rank features field to search against </para>
200
+ /// <para>The query text </para>
115
201
/// </summary>
116
- public TextExpansionQueryDescriptor < TDocument > Value < TValue > ( Expression < Func < TDocument , TValue > > value )
202
+ public TextExpansionQueryDescriptor < TDocument > ModelText ( string modelText )
117
203
{
118
- ValueValue = value ;
204
+ ModelTextValue = modelText ;
119
205
return Self ;
120
206
}
121
207
122
208
protected override void Serialize ( Utf8JsonWriter writer , JsonSerializerOptions options , IElasticsearchClientSettings settings )
123
209
{
210
+ if ( FieldValue is null )
211
+ throw new JsonException ( "Unable to serialize field name query descriptor with a null field. Ensure you use a suitable descriptor constructor or call the Field method, passing a non-null value for the field argument." ) ;
212
+ writer . WriteStartObject ( ) ;
213
+ writer . WritePropertyName ( settings . Inferrer . Field ( FieldValue ) ) ;
124
214
writer . WriteStartObject ( ) ;
125
215
if ( ! string . IsNullOrEmpty ( QueryNameValue ) )
126
216
{
@@ -138,8 +228,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
138
228
writer . WriteStringValue ( ModelIdValue ) ;
139
229
writer . WritePropertyName ( "model_text" ) ;
140
230
writer . WriteStringValue ( ModelTextValue ) ;
141
- writer . WritePropertyName ( "value" ) ;
142
- JsonSerializer . Serialize ( writer , ValueValue , options ) ;
231
+ writer . WriteEndObject ( ) ;
143
232
writer . WriteEndObject ( ) ;
144
233
}
145
234
}
@@ -148,15 +237,22 @@ public sealed partial class TextExpansionQueryDescriptor : SerializableDescripto
148
237
{
149
238
internal TextExpansionQueryDescriptor ( Action < TextExpansionQueryDescriptor > configure ) => configure . Invoke ( this ) ;
150
239
151
- public TextExpansionQueryDescriptor ( ) : base ( )
240
+ internal TextExpansionQueryDescriptor ( ) : base ( )
241
+ {
242
+ }
243
+
244
+ public TextExpansionQueryDescriptor ( Field field )
152
245
{
246
+ if ( field is null )
247
+ throw new ArgumentNullException ( nameof ( field ) ) ;
248
+ FieldValue = field ;
153
249
}
154
250
155
251
private string ? QueryNameValue { get ; set ; }
156
252
private float ? BoostValue { get ; set ; }
253
+ private Elastic . Clients . Elasticsearch . Field FieldValue { get ; set ; }
157
254
private string ModelIdValue { get ; set ; }
158
255
private string ModelTextValue { get ; set ; }
159
- private Elastic . Clients . Elasticsearch . Field ValueValue { get ; set ; }
160
256
161
257
public TextExpansionQueryDescriptor QueryName ( string ? queryName )
162
258
{
@@ -170,53 +266,48 @@ public TextExpansionQueryDescriptor Boost(float? boost)
170
266
return Self ;
171
267
}
172
268
173
- /// <summary>
174
- /// <para>The text expansion NLP model to use</para>
175
- /// </summary>
176
- public TextExpansionQueryDescriptor ModelId ( string modelId )
269
+ public TextExpansionQueryDescriptor Field ( Elastic . Clients . Elasticsearch . Field field )
177
270
{
178
- ModelIdValue = modelId ;
271
+ FieldValue = field ;
179
272
return Self ;
180
273
}
181
274
182
- /// <summary>
183
- /// <para>The query text</para>
184
- /// </summary>
185
- public TextExpansionQueryDescriptor ModelText ( string modelText )
275
+ public TextExpansionQueryDescriptor Field < TDocument , TValue > ( Expression < Func < TDocument , TValue > > field )
186
276
{
187
- ModelTextValue = modelText ;
277
+ FieldValue = field ;
188
278
return Self ;
189
279
}
190
280
191
- /// <summary>
192
- /// <para>The name of the rank features field to search against</para>
193
- /// </summary>
194
- public TextExpansionQueryDescriptor Value ( Elastic . Clients . Elasticsearch . Field value )
281
+ public TextExpansionQueryDescriptor Field < TDocument > ( Expression < Func < TDocument , object > > field )
195
282
{
196
- ValueValue = value ;
283
+ FieldValue = field ;
197
284
return Self ;
198
285
}
199
286
200
287
/// <summary>
201
- /// <para>The name of the rank features field to search against </para>
288
+ /// <para>The text expansion NLP model to use </para>
202
289
/// </summary>
203
- public TextExpansionQueryDescriptor Value < TDocument , TValue > ( Expression < Func < TDocument , TValue > > value )
290
+ public TextExpansionQueryDescriptor ModelId ( string modelId )
204
291
{
205
- ValueValue = value ;
292
+ ModelIdValue = modelId ;
206
293
return Self ;
207
294
}
208
295
209
296
/// <summary>
210
- /// <para>The name of the rank features field to search against </para>
297
+ /// <para>The query text </para>
211
298
/// </summary>
212
- public TextExpansionQueryDescriptor Value < TDocument > ( Expression < Func < TDocument , object > > value )
299
+ public TextExpansionQueryDescriptor ModelText ( string modelText )
213
300
{
214
- ValueValue = value ;
301
+ ModelTextValue = modelText ;
215
302
return Self ;
216
303
}
217
304
218
305
protected override void Serialize ( Utf8JsonWriter writer , JsonSerializerOptions options , IElasticsearchClientSettings settings )
219
306
{
307
+ if ( FieldValue is null )
308
+ throw new JsonException ( "Unable to serialize field name query descriptor with a null field. Ensure you use a suitable descriptor constructor or call the Field method, passing a non-null value for the field argument." ) ;
309
+ writer . WriteStartObject ( ) ;
310
+ writer . WritePropertyName ( settings . Inferrer . Field ( FieldValue ) ) ;
220
311
writer . WriteStartObject ( ) ;
221
312
if ( ! string . IsNullOrEmpty ( QueryNameValue ) )
222
313
{
@@ -234,8 +325,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
234
325
writer . WriteStringValue ( ModelIdValue ) ;
235
326
writer . WritePropertyName ( "model_text" ) ;
236
327
writer . WriteStringValue ( ModelTextValue ) ;
237
- writer . WritePropertyName ( "value" ) ;
238
- JsonSerializer . Serialize ( writer , ValueValue , options ) ;
328
+ writer . WriteEndObject ( ) ;
239
329
writer . WriteEndObject ( ) ;
240
330
}
241
331
}
0 commit comments