forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose DelimitedTermFrequencyTokenFilter (opensearch-project#9479)
* Expose DelimitedTermFrequencyTokenFilter Relates: opensearch-project#9413 This commit exposes Lucene's delimited term frequency token filter to be able to provide term frequencies along with terms. Signed-off-by: Russ Cam <russcam@canva.com> * fix format violations Signed-off-by: Russ Cam <russcam@canva.com> * fix test and add to changelog Signed-off-by: Russ Cam <russcam@canva.com> * Address PR feedback - Add unit tests for DelimitedTermFrequencyTokenFilterFactory - Remove IllegalArgumentException as caught exception - Add skip to yaml rest tests to skip for version < 2.10 Signed-off-by: Russ Cam <russcam@canva.com> * formatting Signed-off-by: Russ Cam <russcam@canva.com> * Rename filter Signed-off-by: Russ Cam <russcam@canva.com> * update naming in REST tests Signed-off-by: Russ Cam <russcam@canva.com> --------- Signed-off-by: Russ Cam <russcam@canva.com> Signed-off-by: Shivansh Arora <hishiv@amazon.com>
- Loading branch information
Showing
7 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...rc/main/java/org/opensearch/analysis/common/DelimitedTermFrequencyTokenFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.analysis.common; | ||
|
||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.miscellaneous.DelimitedTermFrequencyTokenFilter; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.env.Environment; | ||
import org.opensearch.index.IndexSettings; | ||
import org.opensearch.index.analysis.AbstractTokenFilterFactory; | ||
|
||
public class DelimitedTermFrequencyTokenFilterFactory extends AbstractTokenFilterFactory { | ||
public static final char DEFAULT_DELIMITER = '|'; | ||
private static final String DELIMITER = "delimiter"; | ||
private final char delimiter; | ||
|
||
DelimitedTermFrequencyTokenFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) { | ||
super(indexSettings, name, settings); | ||
delimiter = parseDelimiter(settings); | ||
} | ||
|
||
@Override | ||
public TokenStream create(TokenStream tokenStream) { | ||
return new DelimitedTermFrequencyTokenFilter(tokenStream, delimiter); | ||
} | ||
|
||
private static char parseDelimiter(Settings settings) { | ||
String delimiter = settings.get(DELIMITER); | ||
if (delimiter == null) { | ||
return DEFAULT_DELIMITER; | ||
} else if (delimiter.length() == 1) { | ||
return delimiter.charAt(0); | ||
} | ||
|
||
throw new IllegalArgumentException( | ||
"Setting [" + DELIMITER + "] must be a single, non-null character. [" + delimiter + "] was provided." | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...st/java/org/opensearch/analysis/common/DelimitedTermFrequencyTokenFilterFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.analysis.common; | ||
|
||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.Tokenizer; | ||
import org.apache.lucene.analysis.core.WhitespaceTokenizer; | ||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; | ||
import org.apache.lucene.analysis.tokenattributes.TermFrequencyAttribute; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.env.Environment; | ||
import org.opensearch.index.analysis.AnalysisTestsHelper; | ||
import org.opensearch.index.analysis.TokenFilterFactory; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
import org.opensearch.test.OpenSearchTokenStreamTestCase; | ||
|
||
import java.io.StringReader; | ||
|
||
public class DelimitedTermFrequencyTokenFilterFactoryTests extends OpenSearchTokenStreamTestCase { | ||
|
||
public void testDefault() throws Exception { | ||
OpenSearchTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( | ||
Settings.builder() | ||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) | ||
.put("index.analysis.filter.my_delimited_term_freq.type", "delimited_term_freq") | ||
.build(), | ||
new CommonAnalysisModulePlugin() | ||
); | ||
doTest(analysis, "cat|4 dog|5"); | ||
} | ||
|
||
public void testDelimiter() throws Exception { | ||
OpenSearchTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( | ||
Settings.builder() | ||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) | ||
.put("index.analysis.filter.my_delimited_term_freq.type", "delimited_term_freq") | ||
.put("index.analysis.filter.my_delimited_term_freq.delimiter", ":") | ||
.build(), | ||
new CommonAnalysisModulePlugin() | ||
); | ||
doTest(analysis, "cat:4 dog:5"); | ||
} | ||
|
||
public void testDelimiterLongerThanOneCharThrows() { | ||
IllegalArgumentException ex = expectThrows( | ||
IllegalArgumentException.class, | ||
() -> AnalysisTestsHelper.createTestAnalysisFromSettings( | ||
Settings.builder() | ||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) | ||
.put("index.analysis.filter.my_delimited_term_freq.type", "delimited_term_freq") | ||
.put("index.analysis.filter.my_delimited_term_freq.delimiter", "^^") | ||
.build(), | ||
new CommonAnalysisModulePlugin() | ||
) | ||
); | ||
|
||
assertEquals("Setting [delimiter] must be a single, non-null character. [^^] was provided.", ex.getMessage()); | ||
} | ||
|
||
private void doTest(OpenSearchTestCase.TestAnalysis analysis, String source) throws Exception { | ||
TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_delimited_term_freq"); | ||
Tokenizer tokenizer = new WhitespaceTokenizer(); | ||
tokenizer.setReader(new StringReader(source)); | ||
|
||
TokenStream stream = tokenFilter.create(tokenizer); | ||
|
||
CharTermAttribute termAtt = stream.getAttribute(CharTermAttribute.class); | ||
TermFrequencyAttribute tfAtt = stream.getAttribute(TermFrequencyAttribute.class); | ||
stream.reset(); | ||
assertTermEquals("cat", stream, termAtt, tfAtt, 4); | ||
assertTermEquals("dog", stream, termAtt, tfAtt, 5); | ||
assertFalse(stream.incrementToken()); | ||
stream.end(); | ||
stream.close(); | ||
} | ||
|
||
void assertTermEquals(String expected, TokenStream stream, CharTermAttribute termAtt, TermFrequencyAttribute tfAtt, int expectedTf) | ||
throws Exception { | ||
assertTrue(stream.incrementToken()); | ||
assertEquals(expected, termAtt.toString()); | ||
assertEquals(expectedTf, tfAtt.getTermFrequency()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters