1+ /*
2+ * Licensed to Elasticsearch under one or more contributor
3+ * license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright
5+ * ownership. Elasticsearch licenses this file to you under
6+ * the Apache License, Version 2.0 (the "License"); you may
7+ * not use this file except in compliance with the License.
8+ * You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing,
13+ * software distributed under the License is distributed on an
14+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ * KIND, either express or implied. See the License for the
16+ * specific language governing permissions and limitations
17+ * under the License.
18+ */
19+
20+ package org .elasticsearch .analysis .common ;
21+
22+ import org .apache .lucene .util .LuceneTestCase .AwaitsFix ;
23+ import org .elasticsearch .action .admin .indices .analyze .AnalyzeResponse ;
24+ import org .elasticsearch .action .admin .indices .analyze .AnalyzeResponse .AnalyzeToken ;
25+ import org .elasticsearch .action .search .SearchResponse ;
26+ import org .elasticsearch .common .settings .Settings ;
27+ import org .elasticsearch .index .query .QueryBuilders ;
28+ import org .elasticsearch .plugins .Plugin ;
29+ import org .elasticsearch .test .ESIntegTestCase ;
30+ import org .junit .BeforeClass ;
31+
32+ import java .io .FileNotFoundException ;
33+ import java .io .IOException ;
34+ import java .io .OutputStreamWriter ;
35+ import java .io .PrintWriter ;
36+ import java .nio .charset .StandardCharsets ;
37+ import java .nio .file .Files ;
38+ import java .nio .file .Path ;
39+ import java .nio .file .StandardOpenOption ;
40+ import java .util .Arrays ;
41+ import java .util .Collection ;
42+ import java .util .HashSet ;
43+ import java .util .Set ;
44+
45+ import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
46+ import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertHitCount ;
47+ import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertNoFailures ;
48+
49+ @ AwaitsFix (bugUrl ="Cannot be run outside IDE yet" )
50+ public class SynonymAnalyzerIT extends ESIntegTestCase {
51+
52+ private static Path config ;
53+ private static Path synonymsFile ;
54+ private static final String synonymsFileName = "synonyms.txt" ;
55+
56+ @ BeforeClass
57+ public static void initConfigDir () throws IOException {
58+ config = createTempDir ().resolve ("config" );
59+ if (Files .exists (config ) == false ) {
60+ Files .createDirectory (config );
61+ }
62+ synonymsFile = config .resolve (synonymsFileName );
63+ Files .createFile (synonymsFile );
64+ assertTrue (Files .exists (synonymsFile ));
65+ }
66+
67+
68+ @ Override
69+ protected Collection <Class <? extends Plugin >> nodePlugins () {
70+ return Arrays .asList (CommonAnalysisPlugin .class );
71+ }
72+
73+ @ Override
74+ protected Path nodeConfigPath (int nodeOrdinal ) {
75+ return config ;
76+ }
77+
78+ public void testSynonymsUpdateable () throws FileNotFoundException , IOException {
79+ try (PrintWriter out = new PrintWriter (
80+ new OutputStreamWriter (Files .newOutputStream (synonymsFile , StandardOpenOption .CREATE ), StandardCharsets .UTF_8 ))) {
81+ out .println ("foo, baz" );
82+ }
83+ assertTrue (Files .exists (synonymsFile ));
84+ assertAcked (client ().admin ().indices ().prepareCreate ("test" ).setSettings (Settings .builder ()
85+ .put ("index.number_of_shards" , 1 )
86+ .put ("index.number_of_replicas" , 0 )
87+ .put ("analysis.analyzer.my_synonym_analyzer.tokenizer" , "standard" )
88+ .put ("analysis.analyzer.my_synonym_analyzer.filter" , "my_synonym_filter" )
89+ .put ("analysis.filter.my_synonym_filter.type" , "synonym" )
90+ .put ("analysis.filter.my_synonym_filter.updateable" , "true" )
91+ .put ("analysis.filter.my_synonym_filter.synonyms_path" , synonymsFileName ))
92+ .addMapping ("_doc" , "field" , "type=text,analyzer=standard,search_analyzer=my_synonym_analyzer" ));
93+
94+ client ().prepareIndex ("test" , "_doc" , "1" ).setSource ("field" , "foo" ).get ();
95+ assertNoFailures (client ().admin ().indices ().prepareRefresh ("test" ).execute ().actionGet ());
96+
97+ SearchResponse response = client ().prepareSearch ("test" ).setQuery (QueryBuilders .matchQuery ("field" , "baz" )).get ();
98+ assertHitCount (response , 1L );
99+ response = client ().prepareSearch ("test" ).setQuery (QueryBuilders .matchQuery ("field" , "buzz" )).get ();
100+ assertHitCount (response , 0L );
101+ AnalyzeResponse analyzeResponse = client ().admin ().indices ().prepareAnalyze ("test" , "foo" ).setAnalyzer ("my_synonym_analyzer" ).get ();
102+ assertEquals (2 , analyzeResponse .getTokens ().size ());
103+ assertEquals ("foo" , analyzeResponse .getTokens ().get (0 ).getTerm ());
104+ assertEquals ("baz" , analyzeResponse .getTokens ().get (1 ).getTerm ());
105+
106+ // now update synonyms file and trigger reloading
107+ try (PrintWriter out = new PrintWriter (
108+ new OutputStreamWriter (Files .newOutputStream (synonymsFile , StandardOpenOption .WRITE ), StandardCharsets .UTF_8 ))) {
109+ out .println ("foo, baz, buzz" );
110+ }
111+ // TODO don't use refresh here but something more specific
112+ assertNoFailures (client ().admin ().indices ().prepareRefresh ("test" ).execute ().actionGet ());
113+
114+ analyzeResponse = client ().admin ().indices ().prepareAnalyze ("test" , "foo" ).setAnalyzer ("my_synonym_analyzer" ).get ();
115+ assertEquals (3 , analyzeResponse .getTokens ().size ());
116+ Set <String > tokens = new HashSet <>();
117+ analyzeResponse .getTokens ().stream ().map (AnalyzeToken ::getTerm ).forEach (t -> tokens .add (t ));
118+ assertTrue (tokens .contains ("foo" ));
119+ assertTrue (tokens .contains ("baz" ));
120+ assertTrue (tokens .contains ("buzz" ));
121+
122+ response = client ().prepareSearch ("test" ).setQuery (QueryBuilders .matchQuery ("field" , "baz" )).get ();
123+ assertHitCount (response , 1L );
124+ response = client ().prepareSearch ("test" ).setQuery (QueryBuilders .matchQuery ("field" , "buzz" )).get ();
125+ assertHitCount (response , 1L );
126+ }
127+ }
0 commit comments