Skip to content

Commit 0306cda

Browse files
committed
Apply keyword normalizers in the field retrieval API.
1 parent 2af35f7 commit 0306cda

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java

+34-19
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
4848

4949
import java.io.IOException;
50+
import java.io.UncheckedIOException;
5051
import java.util.Collections;
5152
import java.util.Iterator;
5253
import 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

server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldMapperTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,13 @@ public void testParseSourceValue() {
650650
assertEquals("42", ignoreAboveMapper.parseSourceValue(42L, null));
651651
assertEquals("true", ignoreAboveMapper.parseSourceValue(true, null));
652652

653+
KeywordFieldMapper normalizerMapper = new KeywordFieldMapper.Builder("field")
654+
.normalizer(indexService.getIndexAnalyzers(), "lowercase")
655+
.build(context);
656+
assertEquals("value", normalizerMapper.parseSourceValue("VALUE", null));
657+
assertEquals("42", normalizerMapper.parseSourceValue(42L, null));
658+
assertEquals("value", normalizerMapper.parseSourceValue("value", null));
659+
653660
KeywordFieldMapper nullValueMapper = new KeywordFieldMapper.Builder("field")
654661
.nullValue("NULL")
655662
.build(context);

0 commit comments

Comments
 (0)