diff --git a/README.textile b/README.textile index 52285cc8a2c6475b8af8985698902548d7721fe6..b76af65c3759695c264f59b2b442836bd3c2883a 100644 --- a/README.textile +++ b/README.textile @@ -223,6 +223,25 @@ here is the query result have fun. +热更新IK分词使用方法 +---------- +目前该插件支持热更新IK分词,通过上文在ik配置文件中提到的如下配置 + +
+ ++ +其中`location`是指一个url,比如`http://yoursite.com/getCustomDict`,该请求只需满足一下两点即可完成分词热更新。 + +1. 该http请求需要返回两个头部,一个是`Last-Modified`,一个是`ETags`,这两者都是字符串类型,只要有一个发生变化,该插件就会去抓取新的分词进而更新词库。 + +2. 该http请求返回的内容格式是一行一个分词,换行符用`\n`即可。 + +满足上面两点要求就可以实现热更新分词了,不需要重启es实例。 + 常见问题: ------------- diff --git a/src/main/java/org/wltea/analyzer/dic/Dictionary.java b/src/main/java/org/wltea/analyzer/dic/Dictionary.java index b1bc64c3f302c4c990c07c5d2f9738c9861b6e4e..0248d327797444ffb5fe08215d92459e5528999e 100644 --- a/src/main/java/org/wltea/analyzer/dic/Dictionary.java +++ b/src/main/java/org/wltea/analyzer/dic/Dictionary.java @@ -77,7 +77,7 @@ public class Dictionary { * 配置对象 */ private Configuration configuration; - private ESLogger logger=null; + public static ESLogger logger=Loggers.getLogger("ik-analyzer"); private static ScheduledExecutorService pool = Executors.newScheduledThreadPool(1); @@ -89,8 +89,7 @@ public class Dictionary { public static final String PATH_DIC_STOP = "ik/stopword.dic"; private Dictionary(){ - - logger = Loggers.getLogger("ik-analyzer"); + } /** @@ -367,11 +366,11 @@ public class Dictionary { } response.close(); } catch (ClientProtocolException e) { - e.printStackTrace(); + logger.error( "getRemoteWords {} error" , e , location); } catch (IllegalStateException e) { - e.printStackTrace(); + logger.error( "getRemoteWords {} error" , e , location ); } catch (IOException e) { - e.printStackTrace(); + logger.error( "getRemoteWords {} error" , e , location ); } return buffer; } @@ -638,8 +637,14 @@ public class Dictionary { public void reLoadMainDict(){ logger.info("重新加载词典..."); - loadMainDict(); - loadStopWordDict(); + // 新开一个实例加载词典,减少加载过程对当前词典使用的影响 + Dictionary tmpDict = new Dictionary(); + tmpDict.configuration = getSingleton().configuration; + tmpDict.loadMainDict(); + tmpDict.loadStopWordDict(); + _MainDict = tmpDict._MainDict; + _StopWords = tmpDict._StopWords; + logger.info("重新加载词典完毕..."); } } diff --git a/src/main/java/org/wltea/analyzer/dic/Monitor.java b/src/main/java/org/wltea/analyzer/dic/Monitor.java index 65bc744623f042763776477de12e18485fbc94b4..ee1d99c6a6137a622ec7ee31f996226168652239 100644 --- a/src/main/java/org/wltea/analyzer/dic/Monitor.java +++ b/src/main/java/org/wltea/analyzer/dic/Monitor.java @@ -9,7 +9,7 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class Monitor implements Runnable { - + private static CloseableHttpClient httpclient = HttpClients.createDefault(); /* * 上次更改时间 @@ -40,7 +40,7 @@ public class Monitor implements Runnable { */ public void run() { - + //超时设置 RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10*1000) .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(); 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) { - e.printStackTrace(); + Dictionary.logger.error("remote_ext_dict {} error!",e , location); }finally{ try { - response.close(); + if (response != null) { + response.close(); + } } catch (IOException e) { e.printStackTrace(); }location + +location +