47
47
import org .elasticsearch .search .aggregations .support .CoreValuesSourceType ;
48
48
49
49
import java .io .IOException ;
50
+ import java .io .UncheckedIOException ;
50
51
import java .util .Collections ;
51
52
import java .util .Iterator ;
52
53
import java .util .List ;
@@ -361,25 +362,9 @@ protected void parseCreateField(ParseContext context) throws IOException {
361
362
return ;
362
363
}
363
364
364
- final NamedAnalyzer normalizer = fieldType ().normalizer ();
365
+ NamedAnalyzer normalizer = fieldType ().normalizer ();
365
366
if (normalizer != null ) {
366
- try (TokenStream ts = normalizer .tokenStream (name (), value )) {
367
- final CharTermAttribute termAtt = ts .addAttribute (CharTermAttribute .class );
368
- ts .reset ();
369
- if (ts .incrementToken () == false ) {
370
- throw new IllegalStateException ("The normalization token stream is "
371
- + "expected to produce exactly 1 token, but got 0 for analyzer "
372
- + normalizer + " and input \" " + value + "\" " );
373
- }
374
- final String newValue = termAtt .toString ();
375
- if (ts .incrementToken ()) {
376
- throw new IllegalStateException ("The normalization token stream is "
377
- + "expected to produce exactly 1 token, but got 2+ for analyzer "
378
- + normalizer + " and input \" " + value + "\" " );
379
- }
380
- ts .end ();
381
- value = newValue ;
382
- }
367
+ value = normalizeValue (normalizer , value );
383
368
}
384
369
385
370
// convert to utf8 only once before feeding postings/dv/stored fields
@@ -398,6 +383,26 @@ protected void parseCreateField(ParseContext context) throws IOException {
398
383
}
399
384
}
400
385
386
+ private String normalizeValue (NamedAnalyzer normalizer , String value ) throws IOException {
387
+ try (TokenStream ts = normalizer .tokenStream (name (), value )) {
388
+ final CharTermAttribute termAtt = ts .addAttribute (CharTermAttribute .class );
389
+ ts .reset ();
390
+ if (ts .incrementToken () == false ) {
391
+ throw new IllegalStateException ("The normalization token stream is "
392
+ + "expected to produce exactly 1 token, but got 0 for analyzer "
393
+ + normalizer + " and input \" " + value + "\" " );
394
+ }
395
+ final String newValue = termAtt .toString ();
396
+ if (ts .incrementToken ()) {
397
+ throw new IllegalStateException ("The normalization token stream is "
398
+ + "expected to produce exactly 1 token, but got 2+ for analyzer "
399
+ + normalizer + " and input \" " + value + "\" " );
400
+ }
401
+ ts .end ();
402
+ return newValue ;
403
+ }
404
+ }
405
+
401
406
@ Override
402
407
protected String parseSourceValue (Object value , String format ) {
403
408
if (format != null ) {
@@ -408,7 +413,17 @@ protected String parseSourceValue(Object value, String format) {
408
413
if (keywordValue .length () > ignoreAbove ) {
409
414
return null ;
410
415
}
411
- return keywordValue ;
416
+
417
+ NamedAnalyzer normalizer = fieldType ().normalizer ();
418
+ if (normalizer == null ) {
419
+ return keywordValue ;
420
+ }
421
+
422
+ try {
423
+ return normalizeValue (normalizer , keywordValue );
424
+ } catch (IOException e ) {
425
+ throw new UncheckedIOException (e );
426
+ }
412
427
}
413
428
414
429
@ Override
0 commit comments