private void prepareFulltext(KiWiConfiguration configuration) {
try {
if(configuration.isFulltextEnabled()) {
KiWiConnection connection = parent.getPersistence().getConnection();
try {
if(configuration.getDialect() instanceof PostgreSQLDialect) {
// for postgres, we need to create
// - a stored procedure for mapping ISO language codes to PostgreSQL fulltext configuration names
// - if languages are not null, for each configured language as well as for the generic configuration
// an index over nodes.svalue
ScriptRunner runner = new ScriptRunner(connection.getJDBCConnection(), false, false);
if(connection.getMetadata("ft.lookup") == null) {
log.info("PostgreSQL: creating language configuration lookup function");
StringBuilder script = new StringBuilder();
for(String line : IOUtils.readLines(PostgreSQLDialect.class.getResourceAsStream("create_fulltext_langlookup.sql"))) {
if(!line.startsWith("--")) {
script.append(line);
script.append(" ");
}
}
log.debug("PostgreSQL: running SQL script '{}'", script.toString());
runner.runScript(new StringReader(script.toString()));
}
// language specific indexes
if(configuration.getFulltextLanguages() != null) {
StringBuilder script = new StringBuilder();
for(String line : IOUtils.readLines(PostgreSQLDialect.class.getResourceAsStream("create_fulltext_index.sql"))) {
if(!line.startsWith("--")) {
script.append(line);
script.append(" ");
}
}
for(String lang : configuration.getFulltextLanguages()) {
if(connection.getMetadata("ft.idx."+lang) == null) {
String pg_configuration = POSTGRES_LANG_MAPPINGS.get(lang);
if(pg_configuration != null) {
log.info("PostgreSQL: creating fulltext index for language {}", lang);
String script_lang = script.toString().replaceAll("@LANGUAGE@", lang).replaceAll("@CONFIGURATION@",pg_configuration);
log.debug("PostgreSQL: running SQL script '{}'", script_lang);
runner.runScript(new StringReader(script_lang));
}
}
}
}
// generic index
if(configuration.getFulltextLanguages() != null) {
if(connection.getMetadata("ft.idx.generic") == null) {
StringBuilder script = new StringBuilder();
for(String line : IOUtils.readLines(PostgreSQLDialect.class.getResourceAsStream("create_fulltext_index_generic.sql"))) {
if(!line.startsWith("--")) {
script.append(line);
script.append(" ");
}
}
log.info("PostgreSQL: creating generic fulltext index ");
log.debug("PostgreSQL: running SQL script '{}'", script.toString());
runner.runScript(new StringReader(script.toString()));
}
}
/*
} else if(configuration.getDialect() instanceof MySQLDialect) {
// for MySQL, just create a fulltext index (no language support)
if(connection.getMetadata("ft.idx") == null) {
ScriptRunner runner = new ScriptRunner(connection.getJDBCConnection(), false, false);
String script = IOUtils.toString(MySQLDialect.class.getResourceAsStream("create_fulltext_index.sql"));
log.info("MySQL: creating generic fulltext index ");
log.debug("MySQL: running SQL script '{}'", script.toString());
runner.runScript(new StringReader(script));
}
/*
} else if(configuration.getDialect() instanceof H2Dialect) {
// for H2, just create a fulltext index (no language support)
if(connection.getMetadata("fulltext.index") == null) {
ScriptRunner runner = new ScriptRunner(connection.getJDBCConnection(), false, false);
String script = IOUtils.toString(H2Dialect.class.getResourceAsStream("create_fulltext_index.sql"));
runner.runScript(new StringReader(script));
}
*/
}
} finally {
connection.close();
}
}
} catch (IOException | SQLException ex) {
log.error("error while preparing fulltext support",ex);
}