2727
2828namespace Elastic . Clients . Elasticsearch . QueryDsl ;
2929
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 ) ) ]
30110public sealed partial class TextExpansionQuery : SearchQuery
31111{
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+
33119 public string ? QueryName { get ; set ; }
34- [ JsonInclude , JsonPropertyName ( "boost" ) ]
35120 public float ? Boost { get ; set ; }
36121
37122 /// <summary>
38123 /// <para>The text expansion NLP model to use</para>
39124 /// </summary>
40- [ JsonInclude , JsonPropertyName ( "model_id" ) ]
41125 public string ModelId { get ; set ; }
42126
43127 /// <summary>
44128 /// <para>The query text</para>
45129 /// </summary>
46- [ JsonInclude , JsonPropertyName ( "model_text" ) ]
47130 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 ; }
54132
55133 public static implicit operator Query ( TextExpansionQuery textExpansionQuery ) => QueryDsl . Query . TextExpansion ( textExpansionQuery ) ;
56134
@@ -61,15 +139,29 @@ public sealed partial class TextExpansionQueryDescriptor<TDocument> : Serializab
61139{
62140 internal TextExpansionQueryDescriptor ( Action < TextExpansionQueryDescriptor < TDocument > > configure ) => configure . Invoke ( this ) ;
63141
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 )
65154 {
155+ if ( field is null )
156+ throw new ArgumentNullException ( nameof ( field ) ) ;
157+ FieldValue = field ;
66158 }
67159
68160 private string ? QueryNameValue { get ; set ; }
69161 private float ? BoostValue { get ; set ; }
162+ private Elastic . Clients . Elasticsearch . Field FieldValue { get ; set ; }
70163 private string ModelIdValue { get ; set ; }
71164 private string ModelTextValue { get ; set ; }
72- private Elastic . Clients . Elasticsearch . Field ValueValue { get ; set ; }
73165
74166 public TextExpansionQueryDescriptor < TDocument > QueryName ( string ? queryName )
75167 {
@@ -83,44 +175,42 @@ public TextExpansionQueryDescriptor<TDocument> Boost(float? boost)
83175 return Self ;
84176 }
85177
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 )
90179 {
91- ModelIdValue = modelId ;
180+ FieldValue = field ;
92181 return Self ;
93182 }
94183
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 )
99185 {
100- ModelTextValue = modelText ;
186+ FieldValue = field ;
101187 return Self ;
102188 }
103189
104190 /// <summary>
105- /// <para>The name of the rank features field to search against </para>
191+ /// <para>The text expansion NLP model to use </para>
106192 /// </summary>
107- public TextExpansionQueryDescriptor < TDocument > Value ( Elastic . Clients . Elasticsearch . Field value )
193+ public TextExpansionQueryDescriptor < TDocument > ModelId ( string modelId )
108194 {
109- ValueValue = value ;
195+ ModelIdValue = modelId ;
110196 return Self ;
111197 }
112198
113199 /// <summary>
114- /// <para>The name of the rank features field to search against </para>
200+ /// <para>The query text </para>
115201 /// </summary>
116- public TextExpansionQueryDescriptor < TDocument > Value < TValue > ( Expression < Func < TDocument , TValue > > value )
202+ public TextExpansionQueryDescriptor < TDocument > ModelText ( string modelText )
117203 {
118- ValueValue = value ;
204+ ModelTextValue = modelText ;
119205 return Self ;
120206 }
121207
122208 protected override void Serialize ( Utf8JsonWriter writer , JsonSerializerOptions options , IElasticsearchClientSettings settings )
123209 {
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 ) ) ;
124214 writer . WriteStartObject ( ) ;
125215 if ( ! string . IsNullOrEmpty ( QueryNameValue ) )
126216 {
@@ -138,8 +228,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
138228 writer . WriteStringValue ( ModelIdValue ) ;
139229 writer . WritePropertyName ( "model_text" ) ;
140230 writer . WriteStringValue ( ModelTextValue ) ;
141- writer . WritePropertyName ( "value" ) ;
142- JsonSerializer . Serialize ( writer , ValueValue , options ) ;
231+ writer . WriteEndObject ( ) ;
143232 writer . WriteEndObject ( ) ;
144233 }
145234}
@@ -148,15 +237,22 @@ public sealed partial class TextExpansionQueryDescriptor : SerializableDescripto
148237{
149238 internal TextExpansionQueryDescriptor ( Action < TextExpansionQueryDescriptor > configure ) => configure . Invoke ( this ) ;
150239
151- public TextExpansionQueryDescriptor ( ) : base ( )
240+ internal TextExpansionQueryDescriptor ( ) : base ( )
241+ {
242+ }
243+
244+ public TextExpansionQueryDescriptor ( Field field )
152245 {
246+ if ( field is null )
247+ throw new ArgumentNullException ( nameof ( field ) ) ;
248+ FieldValue = field ;
153249 }
154250
155251 private string ? QueryNameValue { get ; set ; }
156252 private float ? BoostValue { get ; set ; }
253+ private Elastic . Clients . Elasticsearch . Field FieldValue { get ; set ; }
157254 private string ModelIdValue { get ; set ; }
158255 private string ModelTextValue { get ; set ; }
159- private Elastic . Clients . Elasticsearch . Field ValueValue { get ; set ; }
160256
161257 public TextExpansionQueryDescriptor QueryName ( string ? queryName )
162258 {
@@ -170,53 +266,48 @@ public TextExpansionQueryDescriptor Boost(float? boost)
170266 return Self ;
171267 }
172268
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 )
177270 {
178- ModelIdValue = modelId ;
271+ FieldValue = field ;
179272 return Self ;
180273 }
181274
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 )
186276 {
187- ModelTextValue = modelText ;
277+ FieldValue = field ;
188278 return Self ;
189279 }
190280
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 )
195282 {
196- ValueValue = value ;
283+ FieldValue = field ;
197284 return Self ;
198285 }
199286
200287 /// <summary>
201- /// <para>The name of the rank features field to search against </para>
288+ /// <para>The text expansion NLP model to use </para>
202289 /// </summary>
203- public TextExpansionQueryDescriptor Value < TDocument , TValue > ( Expression < Func < TDocument , TValue > > value )
290+ public TextExpansionQueryDescriptor ModelId ( string modelId )
204291 {
205- ValueValue = value ;
292+ ModelIdValue = modelId ;
206293 return Self ;
207294 }
208295
209296 /// <summary>
210- /// <para>The name of the rank features field to search against </para>
297+ /// <para>The query text </para>
211298 /// </summary>
212- public TextExpansionQueryDescriptor Value < TDocument > ( Expression < Func < TDocument , object > > value )
299+ public TextExpansionQueryDescriptor ModelText ( string modelText )
213300 {
214- ValueValue = value ;
301+ ModelTextValue = modelText ;
215302 return Self ;
216303 }
217304
218305 protected override void Serialize ( Utf8JsonWriter writer , JsonSerializerOptions options , IElasticsearchClientSettings settings )
219306 {
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 ) ) ;
220311 writer . WriteStartObject ( ) ;
221312 if ( ! string . IsNullOrEmpty ( QueryNameValue ) )
222313 {
@@ -234,8 +325,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
234325 writer . WriteStringValue ( ModelIdValue ) ;
235326 writer . WritePropertyName ( "model_text" ) ;
236327 writer . WriteStringValue ( ModelTextValue ) ;
237- writer . WritePropertyName ( "value" ) ;
238- JsonSerializer . Serialize ( writer , ValueValue , options ) ;
328+ writer . WriteEndObject ( ) ;
239329 writer . WriteEndObject ( ) ;
240330 }
241331}
0 commit comments