-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Allow reloading of search time analyzers #42888
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
Merged
cbuescher
merged 13 commits into
elastic:reloadable-analyzers-feature
from
cbuescher:reloadable-analyzers
Jun 14, 2019
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a5842b5
Allow reloading of search time analyzers
2557dc5
Addressing review comments
eae870f
Pull out interface that allows access to AnalyzerComponents
6a4669a
Merge branch 'reloadable-analyzers-feature' into reloadable-analyzers
9cc3fc7
Add ReloadableCustomAnalyzerTest
bb0c3fc
Fix test naming convention
2365f9c
Merge branch 'reloadable-analyzers-feature' into reloadable-analyzers
9966020
Fix problem in TransportAnalyzeAction
828e926
Merge branch 'reloadable-analyzers-feature' into reloadable-analyzers
604445d
Fix problem with fake analyzer map in #checkMappingsCompatibility
a7e8c6f
Revert previous changes in IndexAnalyzers ctor
72a0081
iter
d43a34e
iter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
123 changes: 123 additions & 0 deletions
123
...es/analysis-common/src/test/java/org/elasticsearch/analysis/common/SynonymAnalyzerIT.java
This file contains hidden or 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,123 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.analysis.common; | ||
|
|
||
| import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction.AnalyzeToken; | ||
| import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction.Response; | ||
| import org.elasticsearch.action.admin.indices.reloadanalyzer.ReloadAnalyzersResponse; | ||
| import org.elasticsearch.action.search.SearchResponse; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.env.Environment; | ||
| import org.elasticsearch.index.query.QueryBuilders; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.test.ESIntegTestCase; | ||
| import org.elasticsearch.test.InternalTestCluster; | ||
|
|
||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.io.OutputStreamWriter; | ||
| import java.io.PrintWriter; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; | ||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; | ||
|
|
||
| public class SynonymAnalyzerIT extends ESIntegTestCase { | ||
|
|
||
| @Override | ||
| protected Collection<Class<? extends Plugin>> nodePlugins() { | ||
| return Arrays.asList(CommonAnalysisPlugin.class); | ||
| } | ||
|
|
||
| /** | ||
| * This test needs to write to the config directory, this is difficult in an external cluster so we overwrite this to force running with | ||
| * {@link InternalTestCluster} | ||
| */ | ||
| @Override | ||
| protected boolean ignoreExternalCluster() { | ||
| return true; | ||
| } | ||
|
|
||
| public void testSynonymsUpdateable() throws FileNotFoundException, IOException, InterruptedException { | ||
| Path config = internalCluster().getInstance(Environment.class).configFile(); | ||
| String synonymsFileName = "synonyms.txt"; | ||
| Path synonymsFile = config.resolve(synonymsFileName); | ||
| Files.createFile(synonymsFile); | ||
| assertTrue(Files.exists(synonymsFile)); | ||
| try (PrintWriter out = new PrintWriter( | ||
| new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.CREATE), StandardCharsets.UTF_8))) { | ||
| out.println("foo, baz"); | ||
| } | ||
| assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder() | ||
| .put("index.number_of_shards", cluster().numDataNodes() * 2) | ||
| .put("index.number_of_replicas", 1) | ||
| .put("analysis.analyzer.my_synonym_analyzer.tokenizer", "standard") | ||
| .put("analysis.analyzer.my_synonym_analyzer.filter", "my_synonym_filter") | ||
| .put("analysis.filter.my_synonym_filter.type", "synonym") | ||
| .put("analysis.filter.my_synonym_filter.updateable", "true") | ||
| .put("analysis.filter.my_synonym_filter.synonyms_path", synonymsFileName)) | ||
| .addMapping("_doc", "field", "type=text,analyzer=standard,search_analyzer=my_synonym_analyzer")); | ||
|
|
||
| client().prepareIndex("test", "_doc", "1").setSource("field", "foo").get(); | ||
| assertNoFailures(client().admin().indices().prepareRefresh("test").execute().actionGet()); | ||
|
|
||
| SearchResponse response = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("field", "baz")).get(); | ||
| assertHitCount(response, 1L); | ||
| response = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("field", "buzz")).get(); | ||
| assertHitCount(response, 0L); | ||
| Response analyzeResponse = client().admin().indices().prepareAnalyze("test", "foo").setAnalyzer("my_synonym_analyzer").get(); | ||
| assertEquals(2, analyzeResponse.getTokens().size()); | ||
| assertEquals("foo", analyzeResponse.getTokens().get(0).getTerm()); | ||
| assertEquals("baz", analyzeResponse.getTokens().get(1).getTerm()); | ||
|
|
||
| // now update synonyms file several times and trigger reloading | ||
| for (int i = 0; i < 10; i++) { | ||
| String testTerm = randomAlphaOfLength(10); | ||
| try (PrintWriter out = new PrintWriter( | ||
| new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8))) { | ||
| out.println("foo, baz, " + testTerm); | ||
| } | ||
| ReloadAnalyzersResponse reloadResponse = client().admin().indices().prepareReloadAnalyzers("test").execute().actionGet(); | ||
| assertNoFailures(reloadResponse); | ||
| assertEquals(cluster().numDataNodes(), reloadResponse.getSuccessfulShards()); | ||
|
|
||
| analyzeResponse = client().admin().indices().prepareAnalyze("test", "foo").setAnalyzer("my_synonym_analyzer").get(); | ||
| assertEquals(3, analyzeResponse.getTokens().size()); | ||
| Set<String> tokens = new HashSet<>(); | ||
| analyzeResponse.getTokens().stream().map(AnalyzeToken::getTerm).forEach(t -> tokens.add(t)); | ||
| assertTrue(tokens.contains("foo")); | ||
| assertTrue(tokens.contains("baz")); | ||
| assertTrue(tokens.contains(testTerm)); | ||
|
|
||
| response = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("field", "baz")).get(); | ||
| assertHitCount(response, 1L); | ||
| response = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("field", testTerm)).get(); | ||
| assertHitCount(response, 1L); | ||
| } | ||
| } | ||
| } |
111 changes: 111 additions & 0 deletions
111
...analysis-common/src/test/java/org/elasticsearch/analysis/common/SynonymAnalyzerTests.java
This file contains hidden or 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,111 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.analysis.common; | ||
|
|
||
| import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction.AnalyzeToken; | ||
| import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction.Response; | ||
| import org.elasticsearch.action.search.SearchResponse; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.index.query.QueryBuilders; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.test.ESSingleNodeTestCase; | ||
|
|
||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.io.OutputStreamWriter; | ||
| import java.io.PrintWriter; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; | ||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; | ||
|
|
||
| public class SynonymAnalyzerTests extends ESSingleNodeTestCase { | ||
|
|
||
| @Override | ||
| protected Collection<Class<? extends Plugin>> getPlugins() { | ||
| return Arrays.asList(CommonAnalysisPlugin.class); | ||
| } | ||
|
|
||
| public void testSynonymsUpdateable() throws FileNotFoundException, IOException { | ||
| String synonymsFileName = "synonyms.txt"; | ||
| Path configDir = node().getEnvironment().configFile(); | ||
| if (Files.exists(configDir) == false) { | ||
| Files.createDirectory(configDir); | ||
| } | ||
| Path synonymsFile = configDir.resolve(synonymsFileName); | ||
| if (Files.exists(synonymsFile) == false) { | ||
| Files.createFile(synonymsFile); | ||
| } | ||
| try (PrintWriter out = new PrintWriter( | ||
| new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8))) { | ||
| out.println("foo, baz"); | ||
| } | ||
|
|
||
| assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder() | ||
| .put("index.number_of_shards", 5) | ||
| .put("index.number_of_replicas", 0) | ||
| .put("analysis.analyzer.my_synonym_analyzer.tokenizer", "standard") | ||
| .putList("analysis.analyzer.my_synonym_analyzer.filter", "lowercase", "my_synonym_filter") | ||
| .put("analysis.filter.my_synonym_filter.type", "synonym") | ||
| .put("analysis.filter.my_synonym_filter.updateable", "true") | ||
| .put("analysis.filter.my_synonym_filter.synonyms_path", synonymsFileName)) | ||
| .addMapping("_doc", "field", "type=text,analyzer=standard,search_analyzer=my_synonym_analyzer")); | ||
|
|
||
| client().prepareIndex("test", "_doc", "1").setSource("field", "Foo").get(); | ||
| assertNoFailures(client().admin().indices().prepareRefresh("test").execute().actionGet()); | ||
|
|
||
| SearchResponse response = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("field", "baz")).get(); | ||
| assertHitCount(response, 1L); | ||
| response = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("field", "buzz")).get(); | ||
| assertHitCount(response, 0L); | ||
| Response analyzeResponse = client().admin().indices().prepareAnalyze("test", "foo").setAnalyzer("my_synonym_analyzer").get(); | ||
| assertEquals(2, analyzeResponse.getTokens().size()); | ||
| assertEquals("foo", analyzeResponse.getTokens().get(0).getTerm()); | ||
| assertEquals("baz", analyzeResponse.getTokens().get(1).getTerm()); | ||
|
|
||
| // now update synonyms file and trigger reloading | ||
| try (PrintWriter out = new PrintWriter( | ||
| new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8))) { | ||
| out.println("foo, baz, buzz"); | ||
| } | ||
| assertNoFailures(client().admin().indices().prepareReloadAnalyzers("test").execute().actionGet()); | ||
|
|
||
| analyzeResponse = client().admin().indices().prepareAnalyze("test", "Foo").setAnalyzer("my_synonym_analyzer").get(); | ||
| assertEquals(3, analyzeResponse.getTokens().size()); | ||
| Set<String> tokens = new HashSet<>(); | ||
| analyzeResponse.getTokens().stream().map(AnalyzeToken::getTerm).forEach(t -> tokens.add(t)); | ||
| assertTrue(tokens.contains("foo")); | ||
| assertTrue(tokens.contains("baz")); | ||
| assertTrue(tokens.contains("buzz")); | ||
|
|
||
| response = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("field", "baz")).get(); | ||
| assertHitCount(response, 1L); | ||
| response = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("field", "buzz")).get(); | ||
| assertHitCount(response, 1L); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.