Merge pull request #56 from rockybean/master
optimize Dictionary reload process
This commit is contained in:
commit
beb4755367
@ -223,6 +223,25 @@ here is the query result
|
|||||||
|
|
||||||
have fun.
|
have fun.
|
||||||
|
|
||||||
|
热更新IK分词使用方法
|
||||||
|
----------
|
||||||
|
目前该插件支持热更新IK分词,通过上文在ik配置文件中提到的如下配置
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<!--用户可以在这里配置远程扩展字典 -->
|
||||||
|
<entry key="remote_ext_dict">location</entry>
|
||||||
|
<!--用户可以在这里配置远程扩展停止词字典-->
|
||||||
|
<entry key="remote_ext_stopwords">location</entry>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
其中`location`是指一个url,比如`http://yoursite.com/getCustomDict`,该请求只需满足一下两点即可完成分词热更新。
|
||||||
|
|
||||||
|
1. 该http请求需要返回两个头部,一个是`Last-Modified`,一个是`ETags`,这两者都是字符串类型,只要有一个发生变化,该插件就会去抓取新的分词进而更新词库。
|
||||||
|
|
||||||
|
2. 该http请求返回的内容格式是一行一个分词,换行符用`\n`即可。
|
||||||
|
|
||||||
|
满足上面两点要求就可以实现热更新分词了,不需要重启es实例。
|
||||||
|
|
||||||
|
|
||||||
常见问题:
|
常见问题:
|
||||||
-------------
|
-------------
|
||||||
|
@ -77,7 +77,7 @@ public class Dictionary {
|
|||||||
* 配置对象
|
* 配置对象
|
||||||
*/
|
*/
|
||||||
private Configuration configuration;
|
private Configuration configuration;
|
||||||
private ESLogger logger=null;
|
public static ESLogger logger=Loggers.getLogger("ik-analyzer");
|
||||||
|
|
||||||
private static ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
|
private static ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
|
||||||
|
|
||||||
@ -89,8 +89,7 @@ public class Dictionary {
|
|||||||
public static final String PATH_DIC_STOP = "ik/stopword.dic";
|
public static final String PATH_DIC_STOP = "ik/stopword.dic";
|
||||||
|
|
||||||
private Dictionary(){
|
private Dictionary(){
|
||||||
|
|
||||||
logger = Loggers.getLogger("ik-analyzer");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -367,11 +366,11 @@ public class Dictionary {
|
|||||||
}
|
}
|
||||||
response.close();
|
response.close();
|
||||||
} catch (ClientProtocolException e) {
|
} catch (ClientProtocolException e) {
|
||||||
e.printStackTrace();
|
logger.error( "getRemoteWords {} error" , e , location);
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
e.printStackTrace();
|
logger.error( "getRemoteWords {} error" , e , location );
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
logger.error( "getRemoteWords {} error" , e , location );
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
@ -638,8 +637,14 @@ public class Dictionary {
|
|||||||
|
|
||||||
public void reLoadMainDict(){
|
public void reLoadMainDict(){
|
||||||
logger.info("重新加载词典...");
|
logger.info("重新加载词典...");
|
||||||
loadMainDict();
|
// 新开一个实例加载词典,减少加载过程对当前词典使用的影响
|
||||||
loadStopWordDict();
|
Dictionary tmpDict = new Dictionary();
|
||||||
|
tmpDict.configuration = getSingleton().configuration;
|
||||||
|
tmpDict.loadMainDict();
|
||||||
|
tmpDict.loadStopWordDict();
|
||||||
|
_MainDict = tmpDict._MainDict;
|
||||||
|
_StopWords = tmpDict._StopWords;
|
||||||
|
logger.info("重新加载词典完毕...");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
|||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
|
||||||
public class Monitor implements Runnable {
|
public class Monitor implements Runnable {
|
||||||
|
|
||||||
private static CloseableHttpClient httpclient = HttpClients.createDefault();
|
private static CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||||
/*
|
/*
|
||||||
* 上次更改时间
|
* 上次更改时间
|
||||||
@ -40,7 +40,7 @@ public class Monitor implements Runnable {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
//超时设置
|
//超时设置
|
||||||
RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10*1000)
|
RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10*1000)
|
||||||
.setConnectTimeout(10*1000).setSocketTimeout(15*1000).build();
|
.setConnectTimeout(10*1000).setSocketTimeout(15*1000).build();
|
||||||
@ -72,13 +72,17 @@ public class Monitor implements Runnable {
|
|||||||
last_modified = response.getLastHeader("Last-Modified")==null?null:response.getLastHeader("Last-Modified").getValue();
|
last_modified = response.getLastHeader("Last-Modified")==null?null:response.getLastHeader("Last-Modified").getValue();
|
||||||
eTags = response.getLastHeader("ETags")==null?null:response.getLastHeader("ETags").getValue();
|
eTags = response.getLastHeader("ETags")==null?null:response.getLastHeader("ETags").getValue();
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
|
Dictionary.logger.info("remote_ext_dict {} return bad code {}" , location , response.getStatusLine().getStatusCode() );
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
Dictionary.logger.error("remote_ext_dict {} error!",e , location);
|
||||||
}finally{
|
}finally{
|
||||||
try {
|
try {
|
||||||
response.close();
|
if (response != null) {
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user