Skip to content

Apache Lucene open-source search software

introduce new parameter onlyLongestMatchNoSubwords replacing onlyLongestMatch (#14311)

Renato Haeberlidb9679afb97f
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 24b0182553f..f9091382913 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -93,9 +93,10 @@ New Features
histogram of the distribution of the values of a field, for documents
matching a given query. (Adrien Grand)
-* GITHUB#14278: Added `reuseChars` option to DictionaryCompoundWordTokenFilter, for use with
- `longestMatch = true`. The value can be set to false to reduce the number of extracted subwords. For example,
- if the word "schwein" is extracted, the sub-word "wein" is not extracted anymore. (Renato Haeberli)
+* GITHUB#14278: Added `onlyLongestMatchNoSubwords` option to DictionaryCompoundWordTokenFilter.
+ This replaces the `onlyLongestMatch` option, and actually only extracts longest matches. For example,
+ if the word "schwein" is extracted, the sub-word "wein" is not extracted anymore.
+ Deprecate `onlyLongestMatch`. (Renato Haeberli)
* GITHUB#14300: Add support JDK 24 to the Panama Vectorization Provider. (Chris Hegarty)
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/DictionaryCompoundWordTokenFilter.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/DictionaryCompoundWordTokenFilter.java
index 5a997e375d3..f3afe51c62f 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/DictionaryCompoundWordTokenFilter.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/DictionaryCompoundWordTokenFilter.java
@@ -28,7 +28,7 @@ import org.apache.lucene.analysis.TokenStream;
*/
public class DictionaryCompoundWordTokenFilter extends CompoundWordTokenFilterBase {
- private boolean reuseChars = true;
+ private boolean onlyLongestMatchNoSubwords = false;
/**
* Creates a new {@link DictionaryCompoundWordTokenFilter}
@@ -51,11 +51,12 @@ public class DictionaryCompoundWordTokenFilter extends CompoundWordTokenFilterBa
* @param minWordSize only words longer than this get processed
* @param minSubwordSize only subwords longer than this get to the output stream
* @param maxSubwordSize only subwords shorter than this get to the output stream
- * @param onlyLongestMatch Add only the longest matching subword to the stream
- * @param reuseChars Characters are reused for multiple matching words, e.g. if a word contains
- * 'schwein', the word 'schwein' and 'wein' will be extracted. If set to false, only the
- * longer word, 'schwein' in this case, will be extracted.
+ * @param onlyLongestMatch deprecated, use parameter onlyLongestMatchIgnoreSubwords instead
+ * @param onlyLongestMatchIgnoreSubwords Subwords are igored, e.g. if a word contains 'schwein',
+ * only the longer word 'schwein' will be extracted, the subword 'wein' will be ignored.
+ * Supersede parameter onlyLongestMatch
*/
+ @Deprecated
public DictionaryCompoundWordTokenFilter(
TokenStream input,
CharArraySet dictionary,
@@ -63,22 +64,45 @@ public class DictionaryCompoundWordTokenFilter extends CompoundWordTokenFilterBa
int minSubwordSize,
int maxSubwordSize,
boolean onlyLongestMatch,
- boolean reuseChars) {
+ boolean onlyLongestMatchIgnoreSubwords) {
super(input, dictionary, minWordSize, minSubwordSize, maxSubwordSize, onlyLongestMatch);
- this.reuseChars = reuseChars;
+ this.onlyLongestMatchNoSubwords = onlyLongestMatchIgnoreSubwords;
if (dictionary == null) {
throw new IllegalArgumentException("dictionary must not be null");
}
+ }
+
+ /**
+ * Creates a new {@link DictionaryCompoundWordTokenFilter}
+ *
+ * @param input the {@link org.apache.lucene.analysis.TokenStream} to process
+ * @param dictionary the word dictionary to match against.
+ * @param minWordSize only words longer than this get processed
+ * @param minSubwordSize only subwords longer than this get to the output stream
+ * @param maxSubwordSize only subwords shorter than this get to the output stream
+ * @param onlyLongestMatchIgnoreSubwords Subwords are igored, e.g. if a word contains 'schwein',
+ * only the longer word 'schwein' will be extracted, the subword 'wein' will be ignored.
+ * Supersede parameter onlyLongestMatch
+ */
+ public DictionaryCompoundWordTokenFilter(
+ TokenStream input,
+ CharArraySet dictionary,
+ int minWordSize,
+ int minSubwordSize,
+ int maxSubwordSize,
+ boolean onlyLongestMatchIgnoreSubwords) {
+ super(input, dictionary, minWordSize, minSubwordSize, maxSubwordSize, false);
+ this.onlyLongestMatchNoSubwords = onlyLongestMatchIgnoreSubwords;
- if (!reuseChars && !onlyLongestMatch) {
- throw new IllegalArgumentException(
- "reuseChars can only be set to false if onlyLongestMatch is set to true");
+ if (dictionary == null) {
+ throw new IllegalArgumentException("dictionary must not be null");
}
}
@Override
protected void decompose() {
+ boolean onlyLongestMatch = this.onlyLongestMatch || onlyLongestMatchNoSubwords;
final int len = termAtt.length();
for (int i = 0; i <= len - this.minSubwordSize; ++i) {
CompoundToken longestMatchToken = null;
@@ -87,7 +111,7 @@ public class DictionaryCompoundWordTokenFilter extends CompoundWordTokenFilterBa
break;
}
if (dictionary.contains(termAtt.buffer(), i, j)) {
- if (this.onlyLongestMatch) {
+ if (onlyLongestMatch) {
if (longestMatchToken != null) {
if (longestMatchToken.txt.length() < j) {
longestMatchToken = new CompoundToken(i, j);
@@ -101,12 +125,11 @@ public class DictionaryCompoundWordTokenFilter extends CompoundWordTokenFilterBa
}
}
- if (longestMatchToken != null && !reuseChars) {
- i += longestMatchToken.txt.length() - 1;
- }
-
- if (this.onlyLongestMatch && longestMatchToken != null) {
+ if (longestMatchToken != null) {
tokens.add(longestMatchToken);
+ if (onlyLongestMatchNoSubwords) {
+ i += longestMatchToken.txt.length() - 1;
+ }
}
}
}
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/DictionaryCompoundWordTokenFilterFactory.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/DictionaryCompoundWordTokenFilterFactory.java
index effe6e7379d..b72a9fa9009 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/DictionaryCompoundWordTokenFilterFactory.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/DictionaryCompoundWordTokenFilterFactory.java
@@ -51,7 +51,7 @@ public class DictionaryCompoundWordTokenFilterFactory extends TokenFilterFactory
private final int minSubwordSize;
private final int maxSubwordSize;
private final boolean onlyLongestMatch;
- private final boolean reuseChars;
+ private final boolean onlyLongestMatchIgnoreSubwords;
/** Creates a new DictionaryCompoundWordTokenFilterFactory */
public DictionaryCompoundWordTokenFilterFactory(Map<String, String> args) {
@@ -63,7 +63,8 @@ public class DictionaryCompoundWordTokenFilterFactory extends TokenFilterFactory
maxSubwordSize =
getInt(args, "maxSubwordSize", CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE);
onlyLongestMatch = getBoolean(args, "onlyLongestMatch", true);
- reuseChars = getBoolean(args, "reuseChars", true);
+ onlyLongestMatchIgnoreSubwords = getBoolean(args, "onlyLongestMatchIgnoreSubwords", true);
+
if (!args.isEmpty()) {
throw new IllegalArgumentException("Unknown parameters: " + args);
}
@@ -92,6 +93,6 @@ public class DictionaryCompoundWordTokenFilterFactory extends TokenFilterFactory
minSubwordSize,
maxSubwordSize,
onlyLongestMatch,
- reuseChars);
+ onlyLongestMatchIgnoreSubwords);
}
}
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java
index 7e5dda0c57a..fbf811b29b0 100644
--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java
@@ -20,7 +20,6 @@ import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
-import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.CharArraySet;
import org.apache.lucene.analysis.TokenFilter;
@@ -253,7 +252,7 @@ public class TestCompoundWordTokenFilter extends BaseTokenStreamTestCase {
CompoundWordTokenFilterBase.DEFAULT_MIN_SUBWORD_SIZE,
CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE,
true,
- true);
+ false);
assertTokenStreamContents(
tf,
@@ -278,7 +277,7 @@ public class TestCompoundWordTokenFilter extends BaseTokenStreamTestCase {
CompoundWordTokenFilterBase.DEFAULT_MIN_SUBWORD_SIZE,
CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE,
false,
- true);
+ false);
assertTokenStreamContents(
tf,
@@ -301,7 +300,7 @@ public class TestCompoundWordTokenFilter extends BaseTokenStreamTestCase {
CompoundWordTokenFilterBase.DEFAULT_MIN_SUBWORD_SIZE,
CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE,
false,
- true);
+ false);
// since "d" is shorter than the minimum subword size, it should not be added to the token
// stream
@@ -328,7 +327,7 @@ public class TestCompoundWordTokenFilter extends BaseTokenStreamTestCase {
CompoundWordTokenFilterBase.DEFAULT_MIN_SUBWORD_SIZE,
CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE,
false,
- true);
+ false);
CharTermAttribute termAtt = tf.getAttribute(CharTermAttribute.class);
tf.reset();
@@ -357,7 +356,7 @@ public class TestCompoundWordTokenFilter extends BaseTokenStreamTestCase {
CompoundWordTokenFilterBase.DEFAULT_MIN_SUBWORD_SIZE,
CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE,
false,
- true);
+ false);
MockRetainAttribute retAtt = stream.addAttribute(MockRetainAttribute.class);
stream.reset();
while (stream.incrementToken()) {
@@ -713,16 +712,6 @@ public class TestCompoundWordTokenFilter extends BaseTokenStreamTestCase {
assertTokenStreamContents(tf, new String[] {searchTerm, "kampf", "waffe"});
}
- public void testDecompoundingWithInvalidParameterCombination() {
-
- Tokenizer tokenizer = new MockTokenizer(MockTokenizer.WHITESPACE, false);
- expectThrows(
- IllegalArgumentException.class,
- () ->
- getDictionaryCompoundWordTokenFilter(
- tokenizer, "", new CharArraySet(List.of(), true), false));
- }
-
private DictionaryCompoundWordTokenFilter getDictionaryCompoundWordTokenFilter(
Tokenizer tokenizer, String searchTerm, CharArraySet dict, boolean onlyLongestMatch) {
tokenizer.setReader(new StringReader(searchTerm));
@@ -733,6 +722,6 @@ public class TestCompoundWordTokenFilter extends BaseTokenStreamTestCase {
CompoundWordTokenFilterBase.DEFAULT_MIN_SUBWORD_SIZE,
CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE,
onlyLongestMatch,
- false);
+ true);
}
}