20
20
package org .elasticsearch .analysis .common ;
21
21
22
22
import org .apache .lucene .analysis .Analyzer ;
23
- import org .apache .lucene .analysis .MockTokenizer ;
24
- import org .apache .lucene .analysis .Tokenizer ;
25
23
import org .elasticsearch .Version ;
26
24
import org .elasticsearch .cluster .metadata .IndexMetaData ;
27
25
import org .elasticsearch .common .settings .Settings ;
28
26
import org .elasticsearch .env .Environment ;
29
27
import org .elasticsearch .index .IndexSettings ;
30
28
import org .elasticsearch .index .analysis .IndexAnalyzers ;
31
29
import org .elasticsearch .index .analysis .NamedAnalyzer ;
32
- import org .elasticsearch .index .analysis .TokenFilterFactory ;
33
30
import org .elasticsearch .test .ESTestCase ;
34
31
import org .elasticsearch .test .IndexSettingsModule ;
35
32
import org .elasticsearch .test .VersionUtils ;
36
33
37
34
import java .io .IOException ;
38
- import java .io .StringReader ;
39
- import java .util .Map ;
40
35
41
36
public class CommonAnalysisPluginTests extends ESTestCase {
42
37
@@ -45,42 +40,37 @@ public class CommonAnalysisPluginTests extends ESTestCase {
45
40
*/
46
41
public void testNGramDeprecationWarning () throws IOException {
47
42
Settings settings = Settings .builder ().put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ())
48
- .put (IndexMetaData .SETTING_VERSION_CREATED ,
43
+ .put (IndexMetaData .SETTING_VERSION_CREATED ,
49
44
VersionUtils .randomVersionBetween (random (), Version .V_6_0_0 , VersionUtils .getPreviousVersion (Version .V_7_0_0 )))
50
- .build ();
45
+ .put ("index.analysis.analyzer.custom_analyzer.type" , "custom" )
46
+ .put ("index.analysis.analyzer.custom_analyzer.tokenizer" , "standard" )
47
+ .putList ("index.analysis.analyzer.custom_analyzer.filter" , "nGram" )
48
+ .build ();
51
49
52
- IndexSettings idxSettings = IndexSettingsModule .newIndexSettings ("index" , settings );
53
50
try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin ()) {
54
- Map <String , TokenFilterFactory > tokenFilters = createTestAnalysis (idxSettings , settings , commonAnalysisPlugin ).tokenFilter ;
55
- TokenFilterFactory tokenFilterFactory = tokenFilters .get ("nGram" );
56
- Tokenizer tokenizer = new MockTokenizer ();
57
- tokenizer .setReader (new StringReader ("foo bar" ));
58
- assertNotNull (tokenFilterFactory .create (tokenizer ));
59
- assertWarnings (
60
- "The [nGram] token filter name is deprecated and will be removed in a future version. "
61
- + "Please change the filter name to [ngram] instead." );
51
+ createTestAnalysis (IndexSettingsModule .newIndexSettings ("index" , settings ), settings , commonAnalysisPlugin );
62
52
}
53
+
54
+ assertWarnings ("The [nGram] token filter name is deprecated and will be removed in a future version. "
55
+ + "Please change the filter name to [ngram] instead." );
63
56
}
64
57
65
58
/**
66
59
* Check that the deprecated name "nGram" throws an error since 7.0.0
67
60
*/
68
61
public void testNGramDeprecationError () throws IOException {
69
62
Settings settings = Settings .builder ().put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ())
70
- .put (IndexMetaData .SETTING_VERSION_CREATED , VersionUtils .randomVersionBetween (random (), Version .V_7_0_0 , null ))
71
- .build ();
63
+ .put (IndexMetaData .SETTING_VERSION_CREATED , VersionUtils .randomVersionBetween (random (), Version .V_7_0_0 , null ))
64
+ .put ("index.analysis.analyzer.custom_analyzer.type" , "custom" )
65
+ .put ("index.analysis.analyzer.custom_analyzer.tokenizer" , "standard" )
66
+ .putList ("index.analysis.analyzer.custom_analyzer.filter" , "nGram" )
67
+ .build ();
72
68
73
- IndexSettings idxSettings = IndexSettingsModule .newIndexSettings ("index" , settings );
74
69
try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin ()) {
75
- Map <String , TokenFilterFactory > tokenFilters = createTestAnalysis (idxSettings , settings , commonAnalysisPlugin ).tokenFilter ;
76
- TokenFilterFactory tokenFilterFactory = tokenFilters .get ("nGram" );
77
- Tokenizer tokenizer = new MockTokenizer ();
78
- tokenizer .setReader (new StringReader ("foo bar" ));
79
- IllegalArgumentException ex = expectThrows (IllegalArgumentException .class , () -> tokenFilterFactory .create (tokenizer ));
80
- assertEquals (
81
- "The [nGram] token filter name was deprecated in 6.4 and cannot be used in new indices. Please change the filter"
82
- + " name to [ngram] instead." ,
83
- ex .getMessage ());
70
+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class ,
71
+ () -> createTestAnalysis (IndexSettingsModule .newIndexSettings ("index" , settings ), settings , commonAnalysisPlugin ));
72
+ assertEquals ("The [nGram] token filter name was deprecated in 6.4 and cannot be used in new indices. "
73
+ + "Please change the filter name to [ngram] instead." , e .getMessage ());
84
74
}
85
75
}
86
76
@@ -89,42 +79,36 @@ public void testNGramDeprecationError() throws IOException {
89
79
*/
90
80
public void testEdgeNGramDeprecationWarning () throws IOException {
91
81
Settings settings = Settings .builder ().put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ())
92
- .put (IndexMetaData .SETTING_VERSION_CREATED ,
93
- VersionUtils .randomVersionBetween (random (), Version .V_6_4_0 , VersionUtils .getPreviousVersion (Version .V_7_0_0 )))
94
- .build ();
82
+ .put (IndexMetaData .SETTING_VERSION_CREATED ,
83
+ VersionUtils .randomVersionBetween (random (), Version .V_6_4_0 , VersionUtils .getPreviousVersion (Version .V_7_0_0 )))
84
+ .put ("index.analysis.analyzer.custom_analyzer.type" , "custom" )
85
+ .put ("index.analysis.analyzer.custom_analyzer.tokenizer" , "standard" )
86
+ .putList ("index.analysis.analyzer.custom_analyzer.filter" , "edgeNGram" )
87
+ .build ();
95
88
96
- IndexSettings idxSettings = IndexSettingsModule .newIndexSettings ("index" , settings );
97
89
try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin ()) {
98
- Map <String , TokenFilterFactory > tokenFilters = createTestAnalysis (idxSettings , settings , commonAnalysisPlugin ).tokenFilter ;
99
- TokenFilterFactory tokenFilterFactory = tokenFilters .get ("edgeNGram" );
100
- Tokenizer tokenizer = new MockTokenizer ();
101
- tokenizer .setReader (new StringReader ("foo bar" ));
102
- assertNotNull (tokenFilterFactory .create (tokenizer ));
103
- assertWarnings (
104
- "The [edgeNGram] token filter name is deprecated and will be removed in a future version. "
105
- + "Please change the filter name to [edge_ngram] instead." );
90
+ createTestAnalysis (IndexSettingsModule .newIndexSettings ("index" , settings ), settings , commonAnalysisPlugin );
106
91
}
92
+ assertWarnings ("The [edgeNGram] token filter name is deprecated and will be removed in a future version. "
93
+ + "Please change the filter name to [edge_ngram] instead." );
107
94
}
108
95
109
96
/**
110
97
* Check that the deprecated name "edgeNGram" throws an error for indices created since 7.0.0
111
98
*/
112
99
public void testEdgeNGramDeprecationError () throws IOException {
113
100
Settings settings = Settings .builder ().put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ())
114
- .put (IndexMetaData .SETTING_VERSION_CREATED , VersionUtils .randomVersionBetween (random (), Version .V_7_0_0 , null ))
115
- .build ();
101
+ .put (IndexMetaData .SETTING_VERSION_CREATED , VersionUtils .randomVersionBetween (random (), Version .V_7_0_0 , null ))
102
+ .put ("index.analysis.analyzer.custom_analyzer.type" , "custom" )
103
+ .put ("index.analysis.analyzer.custom_analyzer.tokenizer" , "standard" )
104
+ .putList ("index.analysis.analyzer.custom_analyzer.filter" , "edgeNGram" )
105
+ .build ();
116
106
117
- IndexSettings idxSettings = IndexSettingsModule .newIndexSettings ("index" , settings );
118
107
try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin ()) {
119
- Map <String , TokenFilterFactory > tokenFilters = createTestAnalysis (idxSettings , settings , commonAnalysisPlugin ).tokenFilter ;
120
- TokenFilterFactory tokenFilterFactory = tokenFilters .get ("edgeNGram" );
121
- Tokenizer tokenizer = new MockTokenizer ();
122
- tokenizer .setReader (new StringReader ("foo bar" ));
123
- IllegalArgumentException ex = expectThrows (IllegalArgumentException .class , () -> tokenFilterFactory .create (tokenizer ));
124
- assertEquals (
125
- "The [edgeNGram] token filter name was deprecated in 6.4 and cannot be used in new indices. Please change the filter"
126
- + " name to [edge_ngram] instead." ,
127
- ex .getMessage ());
108
+ IllegalArgumentException ex = expectThrows (IllegalArgumentException .class ,
109
+ () -> createTestAnalysis (IndexSettingsModule .newIndexSettings ("index" , settings ), settings , commonAnalysisPlugin ));
110
+ assertEquals ("The [edgeNGram] token filter name was deprecated in 6.4 and cannot be used in new indices. "
111
+ + "Please change the filter name to [edge_ngram] instead." , ex .getMessage ());
128
112
}
129
113
}
130
114
0 commit comments