- でソースコードをダウンロードし、解凍してideaにダンプしてパッケージがダウンロードされるのを待ちます。パッケージのダウンロードには数分かかるかもしれません。 pom.xmlファイルに赤いポップアップが出るかもしれませんが、コンパイルには影響しないので気にしないでください。 
- public static synchronized Dictionary initial(Configuration cfg) { if (singleton == null) { synchronized (Dictionary.class) { if (singleton == null) { singleton = new Dictionary(cfg); singleton.loadMainDict(); singleton.loadSurnameDict(); singleton.loadQuantifierDict(); singleton.loadSuffixDict(); singleton.loadPrepDict(); singleton.loadStopWordDict(); pool.scheduleAtFixedRate(new MysqlHotWordMonitor(), 10, 60, TimeUnit.SECONDS); if(cfg.isEnableRemoteDict()){ // 監視スレッドを作成する for (String location : singleton.getRemoteExtDictionarys()) { // 10 secondsは初期遅延時間を変更できる 60はインターバル時間を秒単位で指定する pool.scheduleAtFixedRate(new Monitor(location), 10, 60, TimeUnit.SECONDS); } for (String location : singleton.getRemoteExtStopWordDictionarys()) { pool.scheduleAtFixedRate(new Monitor(location), 10, 60, TimeUnit.SECONDS); } } return singleton; } } } return singleton; }
- 同じディレクトリにMysqlHotWordMonitorクラスを作成し、以下のコードを直接コピーしてください。ここではpostgresデータベースに接続し、あらかじめライブラリにトークン・テーブルを作成しておき、分詞を保存するためのキー・フィールドがあります。同様に、ここのコードはmysqlのような他のデータベースを使用するために変更することができます。 - public class MysqlHotWordMonitor implements Runnable{ - private static final Logger logger = ESPluginLoggerFactory.getLogger(Monitor.class.getName()); private static final String DATABASE_CONF = "database_conf.txt"; public static Map loadConf(){ Map map = new HashMap<String,String>(); Path file = PathUtils.get(org.wltea.analyzer.dic.Dictionary.getSingleton().conf_dir.toAbsolutePath().toString(), DATABASE_CONF); try (InputStream is = new FileInputStream(file.toFile())) { BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"), 512); String line = br.readLine(); while (line!= null && !line.equals("")) { String [] words = line.split(": "); map.put(words[0], words[1]); line = br.readLine(); } } catch (FileNotFoundException e) { logger.error("ik-analyzer: not found", e); } catch (IOException e) { logger.error("ik-analyzer: loading failed", e); } return map; } static List<String> loadMysqlDict(){ Statement statement; Connection conn; List<String> strings = new ArrayList<>(); Map<String,String> map = loadConf(); try { Class.forName("org.postgresql.Driver"); System.out.println(" !"); conn = DriverManager.getConnection(String.format("jdbc:postgresql://%s:%s/%s", map.get("host"), map.get("port"), map.get("database")), map.get("user"), map.get("password")); System.out.println("接続する " + map.get("host") + " サーバー " +map.get("database") +" データベースの成功!"); String sql = "select distinct(key) FROM "+map.get("tableName"); statement = conn.createStatement(); ResultSet rs = statement.executeQuery(sql); System.out.println("ロードする "+map.get("tableName")+" テーブルのセグメンテーション"); while (rs.next()){ String word = rs.getString("key"); if (word != null && !"".equals(word.trim())) { // 拡張辞書のデータを主記憶辞書に読み込む strings.add(word); } } }catch (Exception e){ e.printStackTrace(); System.out.println("loadMysqlDict 例外"); } return strings; } @Override public void run() { Statement statement = null; Connection conn = null; try { Dictionary.getSingleton().reLoadMainDict(); } catch (Exception e) { e.printStackTrace(); System.out.println("run 例外"); }finally { try { if(statement!=null&&!statement.isClosed()){ statement.close(); } if(conn!=null&&!conn.isClosed()){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } }- 4.Dictionaryに戻って、loadMainDictメソッドを見つけ、次のコードに置き換えます。 - private void loadMainDict() { // マスター辞書インスタンスを作成する _MainDict = new DictSegment((char) 0); // メイン辞書ファイルを読む Path file = PathUtils.get(getDictRoot(), Dictionary.PATH_DIC_MAIN); loadDictFile(_MainDict, file, false, "Main Dict"); // 拡張辞書を読み込む this.loadExtDict(); // リモート・カスタム・シソーラスを読み込む this.loadRemoteExtDict(); // ロードmysqlシソーラスを追加変更する this.loadMysqlDict(); }
- 次のコードで新しいメソッド loadMysqlDict を作成します。 - private void loadMysqlDict() { try { List<String> words = MysqlHotWordMonitor.loadMysqlDict(); for (String word : words){ _MainDict.fillSegment(word.trim().toLowerCase().toCharArray()); } }catch (Exception e){ e.printStackTrace(); System.out.println("loadMysqlDict 例外"); } }
- pom.xmlに、使用するデータベース(私の場合はpostgres)の依存関係を追加します。 - <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version></version> </dependency>
- を変更します。 - <includes> <include>org.apache.httpcomponents:httpclient</include> </includes>- 変える - <includes> <include>org.apache.httpcomponents:httpclient</include> <include>org.postgresql:postgresql</include> </includes>
- 読み込むデータベース情報を示す、以下の内容の database_conf.txt ファイルを config 配下に作成します。 - host: データベース ip port: データベースポート user: password: database: データベース名 tableName:
- mavenパッケージの実行 
- 新しく作成したzipパッケージをreleasesディレクトリから/elasticsearchホームディレクトリ/pluginsのikというフォルダに解凍します。 
- // データベースに接続する permission java.net.SocketPermission "*", "connect,resolve";
JAVA_HOME%/jre/lib/security/java.policyで同じ操作をして、esを再起動し、コードに表示すべき情報が表示できれば、正常に読み込めるということです。





