2424import org .elasticsearch .common .io .stream .StreamInput ;
2525import org .elasticsearch .common .io .stream .StreamOutput ;
2626import org .elasticsearch .common .unit .Fuzziness ;
27+ import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
2728import org .elasticsearch .common .xcontent .ObjectParser ;
2829import org .elasticsearch .common .xcontent .ToXContent ;
2930import org .elasticsearch .common .xcontent .XContentBuilder ;
3031import org .elasticsearch .common .xcontent .XContentFactory ;
32+ import org .elasticsearch .common .xcontent .XContentHelper ;
3133import org .elasticsearch .common .xcontent .XContentParser ;
3234import org .elasticsearch .common .xcontent .XContentType ;
3335import org .elasticsearch .index .mapper .CompletionFieldMapper ;
5254 * indexing.
5355 */
5456public class CompletionSuggestionBuilder extends SuggestionBuilder <CompletionSuggestionBuilder > {
57+ private static final XContentType CONTEXT_BYTES_XCONTENT_TYPE = XContentType .JSON ;
5558 static final String SUGGESTION_NAME = "completion" ;
5659 static final ParseField CONTEXTS_FIELD = new ParseField ("contexts" , "context" );
5760
@@ -86,7 +89,7 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug
8689 PARSER .declareInt (CompletionSuggestionBuilder .InnerBuilder ::shardSize , SHARDSIZE_FIELD );
8790 PARSER .declareField ((p , v , c ) -> {
8891 // Copy the current structure. We will parse, once the mapping is provided
89- XContentBuilder builder = XContentFactory .contentBuilder (XContentType . JSON );
92+ XContentBuilder builder = XContentFactory .contentBuilder (CONTEXT_BYTES_XCONTENT_TYPE );
9093 builder .copyCurrentStructure (p );
9194 v .contextBytes = builder .bytes ();
9295 p .skipChildren ();
@@ -186,7 +189,7 @@ public CompletionSuggestionBuilder regex(String regex, RegexOptions regexOptions
186189 public CompletionSuggestionBuilder contexts (Map <String , List <? extends ToXContent >> queryContexts ) {
187190 Objects .requireNonNull (queryContexts , "contexts must not be null" );
188191 try {
189- XContentBuilder contentBuilder = XContentFactory .jsonBuilder ( );
192+ XContentBuilder contentBuilder = XContentFactory .contentBuilder ( CONTEXT_BYTES_XCONTENT_TYPE );
190193 contentBuilder .startObject ();
191194 for (Map .Entry <String , List <? extends ToXContent >> contextEntry : queryContexts .entrySet ()) {
192195 contentBuilder .startArray (contextEntry .getKey ());
@@ -255,33 +258,16 @@ public SuggestionContext build(QueryShardContext context) throws IOException {
255258 suggestionContext .setFuzzyOptions (fuzzyOptions );
256259 suggestionContext .setRegexOptions (regexOptions );
257260 MappedFieldType mappedFieldType = mapperService .fullName (suggestionContext .getField ());
258- if (mappedFieldType == null ||
259- mappedFieldType instanceof CompletionFieldMapper .CompletionFieldType == false ) {
261+ if (mappedFieldType == null || mappedFieldType instanceof CompletionFieldMapper .CompletionFieldType == false ) {
260262 throw new IllegalArgumentException ("Field [" + suggestionContext .getField () + "] is not a completion suggest field" );
261263 }
262264 if (mappedFieldType instanceof CompletionFieldMapper .CompletionFieldType ) {
263265 CompletionFieldMapper .CompletionFieldType type = (CompletionFieldMapper .CompletionFieldType ) mappedFieldType ;
264266 suggestionContext .setFieldType (type );
265267 if (type .hasContextMappings () && contextBytes != null ) {
266- try (XContentParser contextParser = XContentFactory .xContent (contextBytes ).createParser (context .getXContentRegistry (),
267- contextBytes )) {
268- if (type .hasContextMappings () && contextParser != null ) {
269- ContextMappings contextMappings = type .getContextMappings ();
270- contextParser .nextToken ();
271- Map <String , List <ContextMapping .InternalQueryContext >> queryContexts = new HashMap <>(contextMappings .size ());
272- assert contextParser .currentToken () == XContentParser .Token .START_OBJECT ;
273- XContentParser .Token currentToken ;
274- String currentFieldName ;
275- while ((currentToken = contextParser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
276- if (currentToken == XContentParser .Token .FIELD_NAME ) {
277- currentFieldName = contextParser .currentName ();
278- final ContextMapping mapping = contextMappings .get (currentFieldName );
279- queryContexts .put (currentFieldName , mapping .parseQueryContext (contextParser ));
280- }
281- }
282- suggestionContext .setQueryContexts (queryContexts );
283- }
284- }
268+ Map <String , List <ContextMapping .InternalQueryContext >> queryContexts = parseContextBytes (contextBytes ,
269+ context .getXContentRegistry (), type .getContextMappings ());
270+ suggestionContext .setQueryContexts (queryContexts );
285271 } else if (contextBytes != null ) {
286272 throw new IllegalArgumentException ("suggester [" + type .name () + "] doesn't expect any context" );
287273 }
@@ -290,6 +276,25 @@ public SuggestionContext build(QueryShardContext context) throws IOException {
290276 return suggestionContext ;
291277 }
292278
279+ static Map <String , List <ContextMapping .InternalQueryContext >> parseContextBytes (BytesReference contextBytes ,
280+ NamedXContentRegistry xContentRegistry , ContextMappings contextMappings ) throws IOException {
281+ try (XContentParser contextParser = XContentHelper .createParser (xContentRegistry , contextBytes , CONTEXT_BYTES_XCONTENT_TYPE )) {
282+ contextParser .nextToken ();
283+ Map <String , List <ContextMapping .InternalQueryContext >> queryContexts = new HashMap <>(contextMappings .size ());
284+ assert contextParser .currentToken () == XContentParser .Token .START_OBJECT ;
285+ XContentParser .Token currentToken ;
286+ String currentFieldName ;
287+ while ((currentToken = contextParser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
288+ if (currentToken == XContentParser .Token .FIELD_NAME ) {
289+ currentFieldName = contextParser .currentName ();
290+ final ContextMapping <?> mapping = contextMappings .get (currentFieldName );
291+ queryContexts .put (currentFieldName , mapping .parseQueryContext (contextParser ));
292+ }
293+ }
294+ return queryContexts ;
295+ }
296+ }
297+
293298 @ Override
294299 public String getWriteableName () {
295300 return SUGGESTION_NAME ;
0 commit comments