4747import org .elasticsearch .search .aggregations .support .CoreValuesSourceType ;
4848
4949import java .io .IOException ;
50+ import java .io .UncheckedIOException ;
5051import java .util .Collections ;
5152import java .util .Iterator ;
5253import java .util .List ;
@@ -373,25 +374,9 @@ protected void parseCreateField(ParseContext context) throws IOException {
373374 return ;
374375 }
375376
376- final NamedAnalyzer normalizer = fieldType ().normalizer ();
377+ NamedAnalyzer normalizer = fieldType ().normalizer ();
377378 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 );
395380 }
396381
397382 // convert to utf8 only once before feeding postings/dv/stored fields
@@ -410,6 +395,26 @@ protected void parseCreateField(ParseContext context) throws IOException {
410395 }
411396 }
412397
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+
413418 @ Override
414419 protected String parseSourceValue (Object value , String format ) {
415420 if (format != null ) {
@@ -420,7 +425,17 @@ protected String parseSourceValue(Object value, String format) {
420425 if (keywordValue .length () > ignoreAbove ) {
421426 return null ;
422427 }
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+ }
424439 }
425440
426441 @ Override
0 commit comments