1616import java .io .InputStreamReader ;
1717import java .io .Reader ;
1818import java .io .StringReader ;
19+ import java .net .URL ;
20+ import java .nio .file .Files ;
1921import java .nio .file .Path ;
22+ import java .nio .file .Paths ;
2023
2124
2225/**
@@ -51,57 +54,102 @@ public class LocalSynonymFile implements SynonymFile {
5154 this .env = env ;
5255 this .location = location ;
5356
54- this .synonymFilePath = env . configFile (). resolve ( location );
57+ this .synonymFilePath = deepSearch ( );
5558 isNeedReloadSynonymMap ();
5659 }
5760
5861 @ Override
5962 public SynonymMap reloadSynonymMap () {
6063 try {
61- logger .info ("start reload local synonym from {}." , location );
64+ logger .info ("start reload local synonym from {}." , synonymFilePath );
6265 Reader rulesReader = getReader ();
6366 SynonymMap .Builder parser = RemoteSynonymFile .getSynonymParser (rulesReader , format , expand , analyzer );
6467 return parser .build ();
6568 } catch (Exception e ) {
66- logger .error ("reload local synonym {} error!" , e , location );
69+ logger .error ("reload local synonym {} error!" , synonymFilePath , e );
6770 throw new IllegalArgumentException (
6871 "could not reload local synonyms file to build synonyms" , e );
6972 }
7073
7174 }
7275
7376 public Reader getReader () {
77+ /*
78+ Just deleted when reading the file, Returns empty synonym
79+ keyword if file not exists.
80+ A small probability event.
81+ */
82+ if (!Files .exists (synonymFilePath )) {
83+ return new StringReader ("" );
84+ }
7485 try (BufferedReader br = new BufferedReader (new InputStreamReader (
7586 synonymFilePath .toUri ().toURL ().openStream (), Charsets .UTF_8 ))) {
76- StringBuffer sb = new StringBuffer ();
87+ StringBuilder sb = new StringBuilder ();
7788 String line ;
7889 while ((line = br .readLine ()) != null ) {
7990 logger .info ("reload local synonym: {}" , line );
8091 sb .append (line ).append (System .getProperty ("line.separator" ));
8192 }
8293 return new StringReader (sb .toString ());
8394 } catch (IOException e ) {
84- logger .error ("get local synonym reader {} error!" , e , location );
85- throw new IllegalArgumentException (
86- "IOException while reading local synonyms file" , e );
95+ logger .error ("get local synonym reader {} error!" , location , e );
96+ // throw new IllegalArgumentException(
97+ // "IOException while reading local synonyms file", e);
98+ // Fix #54 Returns blank if synonym file has be deleted.
99+ return new StringReader ("" );
87100 }
88101 }
89102
90103 @ Override
91104 public boolean isNeedReloadSynonymMap () {
92105 try {
106+ /*
107+ If the file does not exist, it will be scanned every time
108+ until the file is restored.
109+ */
110+ if (!Files .exists (synonymFilePath ) && !Files .exists (synonymFilePath = deepSearch ())) {
111+ return false ;
112+ }
93113 File synonymFile = synonymFilePath .toFile ();
94114 if (synonymFile .exists ()
95115 && lastModified < synonymFile .lastModified ()) {
96116 lastModified = synonymFile .lastModified ();
97117 return true ;
98118 }
99119 } catch (Exception e ) {
100- logger .error ("check need reload local synonym {} error!" , e ,
101- location );
120+ logger .error ("check need reload local synonym {} error!" , location , e );
102121 }
103122
104123 return false ;
105124 }
106125
126+ /**
127+ * Deep search synonym file.
128+ * Step 1. Query the 'sysnonym_path' parameter as an absolute path
129+ * Step 2. Query the es config path
130+ * Step 3. Query in current relative path
131+ * <p>
132+ * Override this method to expend search path
133+ *
134+ * @return the synonym path.
135+ */
136+ protected Path deepSearch () {
137+ Path path ;
138+ // Load setting config as absolute path
139+ if (Files .exists (Paths .get (location ))) {
140+ path = Paths .get (location );
141+ // Load from setting config path
142+ } else if (Files .exists (env .configFile ().resolve (location ))) {
143+ path = env .configFile ().resolve (location );
144+ // Load from current relative path
145+ } else {
146+ URL url = getClass ().getClassLoader ().getResource (location );
147+ if (url != null ) {
148+ path = Paths .get (url .getFile ());
149+ } else {
150+ path = env .configFile ().resolve (location );
151+ }
152+ }
153+ return path ;
154+ }
107155}
0 commit comments