23
23
import org .elasticsearch .common .io .stream .StreamInput ;
24
24
import org .elasticsearch .common .io .stream .StreamOutput ;
25
25
import org .elasticsearch .common .io .stream .Streamable ;
26
- import org .elasticsearch .common .lucene .BytesRefs ;
27
26
import org .elasticsearch .common .xcontent .XContentBuilder ;
28
27
29
28
import java .io .IOException ;
30
29
import java .util .Objects ;
31
30
32
31
public abstract class BaseTermQueryBuilder <QB extends BoostableQueryBuilder <QB >> extends QueryBuilder implements Streamable , BoostableQueryBuilder <QB > {
33
-
32
+
34
33
/** Name of field to match against. */
35
34
protected String fieldName ;
36
35
@@ -105,17 +104,16 @@ public BaseTermQueryBuilder(String fieldName, boolean value) {
105
104
106
105
/**
107
106
* Constructs a new base term query.
107
+ * In case value is assigned to a string, we internally convert it to a {@link BytesRef}
108
+ * because in {@link TermQueryParser} and {@link SpanTermQueryParser} string values are parsed to {@link BytesRef}
109
+ * and we want internal representation of query to be equal regardless of whether it was created from XContent or via Java API.
108
110
*
109
111
* @param fieldName The name of the field
110
112
* @param value The value of the term
111
113
*/
112
114
public BaseTermQueryBuilder (String fieldName , Object value ) {
113
115
this .fieldName = fieldName ;
114
- if (value instanceof String ) {
115
- this .value = BytesRefs .toBytesRef (value );
116
- } else {
117
- this .value = value ;
118
- }
116
+ this .value = convertToBytesRefIfString (value );
119
117
}
120
118
121
119
BaseTermQueryBuilder () {
@@ -127,11 +125,14 @@ public String fieldName() {
127
125
return this .fieldName ;
128
126
}
129
127
130
- /** Returns the value used in this query. */
128
+ /**
129
+ * Returns the value used in this query.
130
+ * If necessary, converts internal {@link BytesRef} representation back to string.
131
+ */
131
132
public Object value () {
132
- return this .value ;
133
+ return convertToStringIfBytesRef ( this .value ) ;
133
134
}
134
-
135
+
135
136
/** Returns the query name for the query. */
136
137
public String queryName () {
137
138
return this .queryName ;
@@ -144,7 +145,7 @@ public QB queryName(String queryName) {
144
145
this .queryName = queryName ;
145
146
return (QB ) this ;
146
147
}
147
-
148
+
148
149
/** Returns the boost for this query. */
149
150
public float boost () {
150
151
return this .boost ;
@@ -163,16 +164,11 @@ public QB boost(float boost) {
163
164
@ Override
164
165
protected void doXContent (XContentBuilder builder , Params params ) throws IOException {
165
166
builder .startObject (parserName ());
166
- Object valueToWrite = value ;
167
- if (value instanceof BytesRef ) {
168
- valueToWrite = ((BytesRef ) value ).utf8ToString ();
169
- }
170
-
171
167
if (boost == 1.0f && queryName == null ) {
172
- builder .field (fieldName , valueToWrite );
168
+ builder .field (fieldName , convertToStringIfBytesRef ( this . value ) );
173
169
} else {
174
170
builder .startObject (fieldName );
175
- builder .field ("value" , valueToWrite );
171
+ builder .field ("value" , convertToStringIfBytesRef ( this . value ) );
176
172
if (boost != 1.0f ) {
177
173
builder .field ("boost" , boost );
178
174
}
@@ -183,7 +179,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
183
179
}
184
180
builder .endObject ();
185
181
}
186
-
182
+
187
183
/** Returns a {@link QueryValidationException} if fieldName is null or empty, or if value is null. */
188
184
@ Override
189
185
public QueryValidationException validate () {
@@ -194,7 +190,7 @@ public QueryValidationException validate() {
194
190
if (value == null ) {
195
191
validationException = QueryValidationException .addValidationError ("value cannot be null." , validationException );
196
192
}
197
- return validationException ;
193
+ return validationException ;
198
194
}
199
195
200
196
@ Override
0 commit comments