Skip to content

Apache Lucene open-source search software

LUCENE-7468: ASCIIFoldingFilter should not emit duplicated tokens when preserve original is on.

Adrien Grand28d187acd1e3
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index a1273d7fede..4437792b8c1 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -58,6 +58,9 @@ Bug Fixes
* LUCENE-7456: PerFieldPostings/DocValues was failing to delegate the
merge method (Julien MASSENET via Mike McCandless)
+* LUCENE-7468: ASCIIFoldingFilter should not emit duplicated tokens when
+ preserve original is on. (David Causse via Adrien Grand)
+
Improvements
* LUCENE-7439: FuzzyQuery now matches all terms within the specified
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/ASCIIFoldingFilter.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/ASCIIFoldingFilter.java
index a327d175d6b..686c7a6b428 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/ASCIIFoldingFilter.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/ASCIIFoldingFilter.java
@@ -134,9 +134,6 @@ public final class ASCIIFoldingFilter extends TokenFilter {
*/
public void foldToASCII(char[] input, int length)
{
- if (preserveOriginal) {
- state = captureState();
- }
// Worst-case length required:
final int maxSizeNeeded = 4 * length;
if (output.length < maxSizeNeeded) {
@@ -144,6 +141,27 @@ public final class ASCIIFoldingFilter extends TokenFilter {
}
outputPos = foldToASCII(input, 0, output, 0, length);
+ if (preserveOriginal && needToPreserve(input, length)) {
+ state = captureState();
+ }
+ }
+
+ /**
+ * Check if foldToASCII generated a different token.
+ * @param input original term
+ * @param inputLength length of the original term
+ * @return true if foldToASCII generated a different token
+ */
+ private boolean needToPreserve(char[] input, int inputLength) {
+ if(inputLength != outputPos) {
+ return true;
+ }
+ for(int i = 0; i < inputLength; i++) {
+ if(input[i] != output[i]) {
+ return true;
+ }
+ }
+ return false;
}
/**
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestASCIIFoldingFilter.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestASCIIFoldingFilter.java
index 5225aaa1fdb..0e6e4fba243 100644
--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestASCIIFoldingFilter.java
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestASCIIFoldingFilter.java
@@ -131,6 +131,19 @@ public class TestASCIIFoldingFilter extends BaseTokenStreamTestCase {
assertFalse(filter.incrementToken());
}
+ // Test that we do not emit duplicated tokens when preserve original is on
+ public void testUnmodifiedLetters() throws Exception {
+ TokenStream stream = whitespaceMockTokenizer("§ ¦ ¤ END");
+ ASCIIFoldingFilter filter = new ASCIIFoldingFilter(stream, true);
+
+ CharTermAttribute termAtt = filter.getAttribute(CharTermAttribute.class);
+ filter.reset();
+ assertNextTerms("§", "§", filter, termAtt);
+ assertNextTerms("¦", "¦", filter, termAtt);
+ assertNextTerms("¤", "¤", filter, termAtt);
+ assertNextTerms("END", "END", filter, termAtt);
+ assertFalse(filter.incrementToken());
+ }
// The following Perl script generated the foldings[] array automatically
// from ASCIIFoldingFilter.java: