Skip to content

Commit a4d8c3f

Browse files
authored
Merge pull request #55 from wangguanquan/fix#54
Fix#54
2 parents 9a572a7 + 420375a commit a4d8c3f

File tree

5 files changed

+303
-17
lines changed

5 files changed

+303
-17
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,24 @@
5959
<artifactId>httpclient</artifactId>
6060
<version>4.4.1</version>
6161
</dependency>
62+
<dependency>
63+
<groupId>org.apache.logging.log4j</groupId>
64+
<artifactId>log4j-core</artifactId>
65+
<version>2.7</version>
66+
<scope>provided</scope>
67+
</dependency>
6268
<dependency>
6369
<groupId>org.apache.logging.log4j</groupId>
6470
<artifactId>log4j-api</artifactId>
6571
<version>2.7</version>
6672
<scope>provided</scope>
6773
</dependency>
74+
<dependency>
75+
<groupId>org.codelibs</groupId>
76+
<artifactId>elasticsearch-cluster-runner</artifactId>
77+
<version>${project.version}.0</version>
78+
<scope>test</scope>
79+
</dependency>
6880
</dependencies>
6981

7082

src/main/java/com/bellszhu/elasticsearch/plugin/synonym/analysis/LocalSynonymFile.java

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
import java.io.InputStreamReader;
1717
import java.io.Reader;
1818
import java.io.StringReader;
19+
import java.net.URL;
20+
import java.nio.file.Files;
1921
import 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
}

src/main/java/com/bellszhu/elasticsearch/plugin/synonym/analysis/RemoteSynonymFile.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public SynonymMap reloadSynonymMap() {
9292
parser = getSynonymParser(rulesReader, format, expand, analyzer);
9393
return parser.build();
9494
} catch (Exception e) {
95-
logger.error("reload remote synonym {} error!", e, location);
95+
logger.error("reload remote synonym {} error!", location, e);
9696
throw new IllegalArgumentException(
9797
"could not reload remote synonyms file to build synonyms",
9898
e);
@@ -112,7 +112,7 @@ private CloseableHttpResponse executeHttpRequest(HttpUriRequest httpUriRequest)
112112
try {
113113
return httpclient.execute(httpUriRequest);
114114
} catch (IOException e) {
115-
logger.error("Unable to execute HTTP request: {}", e);
115+
logger.error("Unable to execute HTTP request.", e);
116116
}
117117
return null;
118118
});
@@ -122,7 +122,7 @@ private CloseableHttpResponse executeHttpRequest(HttpUriRequest httpUriRequest)
122122
* Download custom terms from a remote server
123123
*/
124124
public Reader getReader() {
125-
Reader reader = null;
125+
Reader reader;
126126
RequestConfig rc = RequestConfig.custom()
127127
.setConnectionRequestTimeout(10 * 1000)
128128
.setConnectTimeout(10 * 1000).setSocketTimeout(60 * 1000)
@@ -145,19 +145,21 @@ public Reader getReader() {
145145

146146
br = new BufferedReader(new InputStreamReader(response
147147
.getEntity().getContent(), charset));
148-
StringBuffer sb = new StringBuffer();
148+
StringBuilder sb = new StringBuilder();
149149
String line;
150150
while ((line = br.readLine()) != null) {
151151
logger.info("reload remote synonym: {}", line);
152152
sb.append(line)
153153
.append(System.getProperty("line.separator"));
154154
}
155155
reader = new StringReader(sb.toString());
156-
}
156+
} else reader = new StringReader("");
157157
} catch (Exception e) {
158-
logger.error("get remote synonym reader {} error!", e, location);
159-
throw new IllegalArgumentException(
160-
"Exception while reading remote synonyms file", e);
158+
logger.error("get remote synonym reader {} error!", location, e);
159+
// throw new IllegalArgumentException(
160+
// "Exception while reading remote synonyms file", e);
161+
// Fix #54 Returns blank if synonym file has be deleted.
162+
reader = new StringReader("");
161163
} finally {
162164
try {
163165
if (br != null) {

0 commit comments

Comments
 (0)