Package nxt.db
Class FullTextTrigger
- java.lang.Object
-
- nxt.db.FullTextTrigger
-
- All Implemented Interfaces:
TransactionalDb.TransactionCallback
,org.h2.api.Trigger
public class FullTextTrigger extends java.lang.Object implements org.h2.api.Trigger, TransactionalDb.TransactionCallback
FullTextTrigger provides Lucene search support. Each searchable database has a database trigger defined. The Lucene index is updated whenever a row is inserted, updated or deleted. The DB_ID column is used to identify each row and will be returned as the COLUMNS and KEYS values in the search results. Schema, table and column names are converted to uppercase to match the way H2 stores the information. Function aliases and triggers are created in the default schema (PUBLIC). The database aliases are defined as follows: CREATE ALIAS FTL_CREATE_INDEX FOR "nxt.db.FullTextTrigger.createIndex" CALL FTL_CREATE(schema, table, columnList) CREATE ALIAS FTL_DROP_INDEX FOR "nxt.db.FullTextTrigger.dropIndex" CALL FTL_DROP(schema, table) CREATE ALIAS FTL_SEARCH FOR "nxt.db.FullTextTrigger.search" CALL FTL_SEARCH(schema, table, query, limit, offset) FTL_CREATE_INDEX is called to create a fulltext index for a table. It is provided as a convenience for use in NxtDbVersion when creating a new index after the database has been created. FTL_DROP_INDEX is called to drop a fulltext index for a table. It is provided as a convenience for use in NxtDbVersion when dropping a table after the database has been created. FTL_SEARCH is used to return the search result set as part of a SELECT statement. The result set columns are the following: SCHEMA - the schema name (String) TABLE - the table name (String) COLUMNS - the primary key columns (String[]) - this is always DB_ID for NRS KEYS - the primary key values (Long[]) - DB_ID value for the row SCORE - the search hit score (Float) The table index trigger is defined as follows: CREATE TRIGGER trigger_name AFTER INSERT,UPDATE,DELETE ON table_name FOR EACH ROW CALL "nxt.db.FullTextTrigger"
-
-
Constructor Summary
Constructors Constructor Description FullTextTrigger()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close the trigger (Trigger interface)void
commit()
Commit the table changes for the current transaction (TransactionCallback interface)static void
createIndex(java.sql.Connection conn, java.lang.String schema, java.lang.String table, java.lang.String columnList)
Create the fulltext index for a tablestatic void
dropAll(java.sql.Connection conn)
Drop all fulltext indexesstatic void
dropIndex(java.sql.Connection conn, java.lang.String schema, java.lang.String table)
Drop the fulltext index for a tablevoid
fire(java.sql.Connection conn, java.lang.Object[] oldRow, java.lang.Object[] newRow)
Trigger has fired (Trigger interface)static void
init()
Initialize the fulltext support for a new database This method should be called from NxtDbVersion when performing the database version update that enables NRS fulltext search supportvoid
init(java.sql.Connection conn, java.lang.String schema, java.lang.String trigger, java.lang.String table, boolean before, int type)
Initialize the trigger (Trigger interface)static void
reindex(java.sql.Connection conn)
Reindex all of the indexed tablesvoid
remove()
Remove the trigger (Trigger interface)void
rollback()
Discard the table changes for the current transaction (TransactionCallback interface)static java.sql.ResultSet
search(java.sql.Connection conn, java.lang.String schema, java.lang.String table, java.lang.String queryText, int limit, int offset)
Search the Lucene index The result set will have the following columns: SCHEMA - Schema name (String) TABLE - Table name (String) COLUMNS - Primary key column names (String[]) - this is always DB_ID KEYS - Primary key values (Long[]) - this is always the DB_ID value for the table row SCORE - Lucene score (Float)static void
setActive(boolean active)
This method is called by NRS initialization to indicate NRS is active.
-
-
-
Method Detail
-
setActive
public static void setActive(boolean active)
This method is called by NRS initialization to indicate NRS is active. This is required since database triggers will be initialized when the database is opened outside the NRS environment (for example, from the H2 console) The database triggers cannot be re-activated after they have been deactivated.- Parameters:
active
- TRUE to enable database triggers
-
init
public static void init()
Initialize the fulltext support for a new database This method should be called from NxtDbVersion when performing the database version update that enables NRS fulltext search support
-
reindex
public static void reindex(java.sql.Connection conn) throws java.sql.SQLException
Reindex all of the indexed tables- Parameters:
conn
- SQL connection- Throws:
java.sql.SQLException
- Unable to reindex tables
-
createIndex
public static void createIndex(java.sql.Connection conn, java.lang.String schema, java.lang.String table, java.lang.String columnList) throws java.sql.SQLException
Create the fulltext index for a table- Parameters:
conn
- SQL connectionschema
- Schema nametable
- Table namecolumnList
- Indexed column names separated by commas- Throws:
java.sql.SQLException
- Unable to create fulltext index
-
dropIndex
public static void dropIndex(java.sql.Connection conn, java.lang.String schema, java.lang.String table) throws java.sql.SQLException
Drop the fulltext index for a table- Parameters:
conn
- SQL connectionschema
- Schema nametable
- Table name- Throws:
java.sql.SQLException
- Unable to drop fulltext index
-
dropAll
public static void dropAll(java.sql.Connection conn) throws java.sql.SQLException
Drop all fulltext indexes- Parameters:
conn
- SQL connection- Throws:
java.sql.SQLException
- Unable to drop fulltext indexes
-
search
public static java.sql.ResultSet search(java.sql.Connection conn, java.lang.String schema, java.lang.String table, java.lang.String queryText, int limit, int offset) throws java.sql.SQLException
Search the Lucene index The result set will have the following columns: SCHEMA - Schema name (String) TABLE - Table name (String) COLUMNS - Primary key column names (String[]) - this is always DB_ID KEYS - Primary key values (Long[]) - this is always the DB_ID value for the table row SCORE - Lucene score (Float)- Parameters:
conn
- SQL connectionschema
- Schema nametable
- Table namequeryText
- Query expressionlimit
- Number of rows to returnoffset
- Offset with result set- Returns:
- Search results
- Throws:
java.sql.SQLException
- Unable to search the index
-
init
public void init(java.sql.Connection conn, java.lang.String schema, java.lang.String trigger, java.lang.String table, boolean before, int type) throws java.sql.SQLException
Initialize the trigger (Trigger interface)- Specified by:
init
in interfaceorg.h2.api.Trigger
- Parameters:
conn
- Database connectionschema
- Database schema nametrigger
- Database trigger nametable
- Database table namebefore
- TRUE if trigger is called before database operationtype
- Trigger type- Throws:
java.sql.SQLException
- A SQL error occurred
-
close
public void close() throws java.sql.SQLException
Close the trigger (Trigger interface)- Specified by:
close
in interfaceorg.h2.api.Trigger
- Throws:
java.sql.SQLException
- A SQL error occurred
-
remove
public void remove() throws java.sql.SQLException
Remove the trigger (Trigger interface)- Specified by:
remove
in interfaceorg.h2.api.Trigger
- Throws:
java.sql.SQLException
- A SQL error occurred
-
fire
public void fire(java.sql.Connection conn, java.lang.Object[] oldRow, java.lang.Object[] newRow) throws java.sql.SQLException
Trigger has fired (Trigger interface)- Specified by:
fire
in interfaceorg.h2.api.Trigger
- Parameters:
conn
- Database connectionoldRow
- The old row or nullnewRow
- The new row or null- Throws:
java.sql.SQLException
- A SQL error occurred
-
commit
public void commit()
Commit the table changes for the current transaction (TransactionCallback interface)- Specified by:
commit
in interfaceTransactionalDb.TransactionCallback
-
rollback
public void rollback()
Discard the table changes for the current transaction (TransactionCallback interface)- Specified by:
rollback
in interfaceTransactionalDb.TransactionCallback
-
-