New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for char filters in the analyze API #5148
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
554a08a
Added support for char filters in the analyze API
brusic e236b33
Made change for #5148 suggested by @javanna
brusic 187846c
Added chained token filter and char filter tests.
brusic d0dc1e3
Opt for empty arrays over null ones. Use Stream string array methods.
brusic 36235ac
Check for nulls in setters.
brusic 1fe04bf
Enforce null checks with setters only
brusic 8eb4170
Move null checks back into the validate method
brusic File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,12 +23,27 @@ | |
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequestBuilder; | ||
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse; | ||
import org.elasticsearch.common.Priority; | ||
import org.elasticsearch.common.inject.Injector; | ||
import org.elasticsearch.common.inject.ModulesBuilder; | ||
import org.elasticsearch.common.settings.ImmutableSettings; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.settings.SettingsModule; | ||
import org.elasticsearch.common.xcontent.XContentFactory; | ||
import org.elasticsearch.env.Environment; | ||
import org.elasticsearch.env.EnvironmentModule; | ||
import org.elasticsearch.index.Index; | ||
import org.elasticsearch.index.IndexNameModule; | ||
import org.elasticsearch.index.analysis.AnalysisModule; | ||
import org.elasticsearch.index.analysis.AnalysisService; | ||
import org.elasticsearch.index.settings.IndexSettingsModule; | ||
import org.elasticsearch.indices.analysis.IndicesAnalysisModule; | ||
import org.elasticsearch.indices.analysis.IndicesAnalysisService; | ||
import org.elasticsearch.test.ElasticsearchIntegrationTest; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
/** | ||
|
@@ -106,6 +121,50 @@ public void analyzeWithNoIndex() throws Exception { | |
analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setTokenizer("keyword").setTokenFilters("lowercase").execute().actionGet(); | ||
assertThat(analyzeResponse.getTokens().size(), equalTo(1)); | ||
assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("this is a test")); | ||
|
||
analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setTokenizer("standard").setTokenFilters("lowercase", "reverse").execute().actionGet(); | ||
assertThat(analyzeResponse.getTokens().size(), equalTo(4)); | ||
AnalyzeResponse.AnalyzeToken token = analyzeResponse.getTokens().get(0); | ||
assertThat(token.getTerm(), equalTo("siht")); | ||
token = analyzeResponse.getTokens().get(1); | ||
assertThat(token.getTerm(), equalTo("si")); | ||
token = analyzeResponse.getTokens().get(2); | ||
assertThat(token.getTerm(), equalTo("a")); | ||
token = analyzeResponse.getTokens().get(3); | ||
assertThat(token.getTerm(), equalTo("tset")); | ||
} | ||
|
||
@Test | ||
public void analyzeWithCharFilters() throws Exception { | ||
|
||
ImmutableSettings.Builder settings = settingsBuilder() | ||
.put("index.analysis.char_filter.custom_mapping.type", "mapping") | ||
.putArray("index.analysis.char_filter.custom_mapping.mappings", "ph=>f", "qu=>q") | ||
.put("index.analysis.analyzer.custom_with_char_filter.tokenizer", "standard") | ||
.putArray("index.analysis.analyzer.custom_with_char_filter.char_filter", "custom_mapping"); | ||
|
||
prepareCreate("test", 1, settings).execute().actionGet(); | ||
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); | ||
|
||
AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze("<h2><b>THIS</b> IS A</h2> <a href=\"#\">TEST</a>").setTokenizer("standard").setCharFilters("html_strip").execute().actionGet(); | ||
assertThat(analyzeResponse.getTokens().size(), equalTo(4)); | ||
|
||
analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A <b>TEST</b>").setTokenizer("keyword").setTokenFilters("lowercase").setCharFilters("html_strip").execute().actionGet(); | ||
assertThat(analyzeResponse.getTokens().size(), equalTo(1)); | ||
assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("this is a test")); | ||
|
||
analyzeResponse = client().admin().indices().prepareAnalyze("test", "jeff quit phish").setTokenizer("keyword").setTokenFilters("lowercase").setCharFilters("custom_mapping").execute().actionGet(); | ||
assertThat(analyzeResponse.getTokens().size(), equalTo(1)); | ||
assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("jeff qit fish")); | ||
|
||
analyzeResponse = client().admin().indices().prepareAnalyze("test", "<a href=\"#\">jeff quit fish</a>").setTokenizer("standard").setCharFilters("html_strip", "custom_mapping").execute().actionGet(); | ||
assertThat(analyzeResponse.getTokens().size(), equalTo(3)); | ||
AnalyzeResponse.AnalyzeToken token = analyzeResponse.getTokens().get(0); | ||
assertThat(token.getTerm(), equalTo("jeff")); | ||
token = analyzeResponse.getTokens().get(1); | ||
assertThat(token.getTerm(), equalTo("qit")); | ||
token = analyzeResponse.getTokens().get(2); | ||
assertThat(token.getTerm(), equalTo("fish")); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could also test the case where we have a custom char filter registered under a specific index? |
||
@Test | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above we have a
writeStringArray
as well