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 ;
@@ -373,25 +374,9 @@ protected void parseCreateField(ParseContext context) throws IOException {
373
374
return ;
374
375
}
375
376
376
- final NamedAnalyzer normalizer = fieldType ().normalizer ();
377
+ NamedAnalyzer normalizer = fieldType ().normalizer ();
377
378
if (normalizer != null ) {
378
- try (TokenStream ts = normalizer .tokenStream (name (), value )) {
379
- final CharTermAttribute termAtt = ts .addAttribute (CharTermAttribute .class );
380
- ts .reset ();
381
- if (ts .incrementToken () == false ) {
382
- throw new IllegalStateException ("The normalization token stream is "
383
- + "expected to produce exactly 1 token, but got 0 for analyzer "
384
- + normalizer + " and input \" " + value + "\" " );
385
- }
386
- final String newValue = termAtt .toString ();
387
- if (ts .incrementToken ()) {
388
- throw new IllegalStateException ("The normalization token stream is "
389
- + "expected to produce exactly 1 token, but got 2+ for analyzer "
390
- + normalizer + " and input \" " + value + "\" " );
391
- }
392
- ts .end ();
393
- value = newValue ;
394
- }
379
+ value = normalizeValue (normalizer , value );
395
380
}
396
381
397
382
// convert to utf8 only once before feeding postings/dv/stored fields
@@ -410,6 +395,26 @@ protected void parseCreateField(ParseContext context) throws IOException {
410
395
}
411
396
}
412
397
398
+ private String normalizeValue (NamedAnalyzer normalizer , String value ) throws IOException {
399
+ try (TokenStream ts = normalizer .tokenStream (name (), value )) {
400
+ final CharTermAttribute termAtt = ts .addAttribute (CharTermAttribute .class );
401
+ ts .reset ();
402
+ if (ts .incrementToken () == false ) {
403
+ throw new IllegalStateException ("The normalization token stream is "
404
+ + "expected to produce exactly 1 token, but got 0 for analyzer "
405
+ + normalizer + " and input \" " + value + "\" " );
406
+ }
407
+ final String newValue = termAtt .toString ();
408
+ if (ts .incrementToken ()) {
409
+ throw new IllegalStateException ("The normalization token stream is "
410
+ + "expected to produce exactly 1 token, but got 2+ for analyzer "
411
+ + normalizer + " and input \" " + value + "\" " );
412
+ }
413
+ ts .end ();
414
+ return newValue ;
415
+ }
416
+ }
417
+
413
418
@ Override
414
419
protected String parseSourceValue (Object value , String format ) {
415
420
if (format != null ) {
@@ -420,7 +425,17 @@ protected String parseSourceValue(Object value, String format) {
420
425
if (keywordValue .length () > ignoreAbove ) {
421
426
return null ;
422
427
}
423
- return keywordValue ;
428
+
429
+ NamedAnalyzer normalizer = fieldType ().normalizer ();
430
+ if (normalizer == null ) {
431
+ return keywordValue ;
432
+ }
433
+
434
+ try {
435
+ return normalizeValue (normalizer , keywordValue );
436
+ } catch (IOException e ) {
437
+ throw new UncheckedIOException (e );
438
+ }
424
439
}
425
440
426
441
@ Override
0 commit comments