Package nxt.db
Class FullTextTrigger
java.lang.Object
nxt.db.FullTextTrigger
- All Implemented Interfaces:
TransactionalDb.TransactionCallback,org.h2.api.Trigger
public class FullTextTrigger
extends 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 expected to be in uppercase to match the
way H2 stores the information. Function aliases are created in the default
schema (PUBLIC) and triggers are created in the schema of the table being
indexed.
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"
-
Field Summary
Fields inherited from interface org.h2.api.Trigger
DELETE, INSERT, SELECT, UPDATE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the trigger (Trigger interface)voidcommit()Commit the table changes for the current transaction (TransactionCallback interface)static voidcreateIndex(Connection conn, String schema, String table, String columnList) Create the fulltext index for a tablestatic voiddropAll(Connection conn) Drop all fulltext indexesstatic voiddropIndex(Connection conn, String schema, String table) Drop the fulltext index for a tablevoidfire(Connection conn, Object[] oldRow, Object[] newRow) Trigger has fired (Trigger interface)voidinit(Connection conn, String schema, String trigger, String table, boolean before, int type) Initialize the trigger (Trigger interface)static voidInitialize 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 supportstatic voidmigrateToV7(BasicDb db) static voidreindex(Connection conn) Reindex all of the indexed tablesvoidremove()Remove the trigger (Trigger interface)voidrollback()Discard the table changes for the current transaction (TransactionCallback interface)static ResultSetsearch(Connection conn, String schema, String table, 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 voidsetActive(boolean active) This method is called by NRS initialization to indicate NRS is active.
-
Constructor Details
-
FullTextTrigger
public FullTextTrigger()
-
-
Method Details
-
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
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- Parameters:
db- BasicDb to init
-
migrateToV7
-
reindex
Reindex all of the indexed tables- Parameters:
conn- SQL connection- Throws:
SQLException- Unable to reindex tables
-
createIndex
public static void createIndex(Connection conn, String schema, String table, String columnList) throws SQLException Create the fulltext index for a table- Parameters:
conn- SQL connectionschema- Schema nametable- Table namecolumnList- Indexed column names separated by commas- Throws:
SQLException- Unable to create fulltext index
-
dropIndex
Drop the fulltext index for a table- Parameters:
conn- SQL connectionschema- Schema nametable- Table name- Throws:
SQLException- Unable to drop fulltext index
-
dropAll
Drop all fulltext indexes- Parameters:
conn- SQL connection- Throws:
SQLException- Unable to drop fulltext indexes
-
search
public static ResultSet search(Connection conn, String schema, String table, String queryText, int limit, int offset) throws 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:
SQLException- Unable to search the index
-
init
public void init(Connection conn, String schema, String trigger, String table, boolean before, int type) throws SQLException Initialize the trigger (Trigger interface)- Specified by:
initin 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:
SQLException- A SQL error occurred
-
close
public void close()Close the trigger (Trigger interface)- Specified by:
closein interfaceorg.h2.api.Trigger
-
remove
public void remove()Remove the trigger (Trigger interface)- Specified by:
removein interfaceorg.h2.api.Trigger
-
fire
Trigger has fired (Trigger interface)- Specified by:
firein interfaceorg.h2.api.Trigger- Parameters:
conn- Database connectionoldRow- The old row or nullnewRow- The new row or null
-
commit
public void commit()Commit the table changes for the current transaction (TransactionCallback interface)- Specified by:
commitin interfaceTransactionalDb.TransactionCallback
-
rollback
public void rollback()Discard the table changes for the current transaction (TransactionCallback interface)- Specified by:
rollbackin interfaceTransactionalDb.TransactionCallback
-