Package org.h2.fulltext
Class FullTextLucene
java.lang.Object
org.h2.fulltext.FullText
org.h2.fulltext.FullTextLucene
This class implements the full text search based on Apache Lucene.
Most methods can be called using SQL statements as well.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Trigger updates the index when a inserting, updating, or deleting a row. -
Field Summary
Modifier and TypeFieldDescriptionprotected static final boolean
Whether the text content should be stored in the Lucene index. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected static SQLException
Convert an exception to a fulltext exception.static void
createIndex
(Connection conn, String schema, String table, String columnList) Create a new full text index for a table and column list.static void
dropAll
(Connection conn) Drops all full text indexes from the database.static void
dropIndex
(Connection conn, String schema, String table) Drop an existing full text index for a table.protected static org.h2.fulltext.FullTextLucene.IndexAccess
getIndexAccess
(Connection conn) Get the index writer/searcher wrapper for the given connection.protected static String
getIndexPath
(Connection conn) Get the path of the Lucene index for this database.static void
init
(Connection conn) Initializes full text search functionality for this database.static void
reindex
(Connection conn) Re-creates the full text index for this database.protected static void
removeIndexAccess
(String indexPath) Close the index writer and searcher and remove them from the index access set.static ResultSet
search
(Connection conn, String text, int limit, int offset) Searches from the full text index for this database.protected static ResultSet
search
(Connection conn, String text, int limit, int offset, boolean data) Do the search.static ResultSet
searchData
(Connection conn, String text, int limit, int offset) Searches from the full text index for this database.Methods inherited from class org.h2.fulltext.FullText
addWords, addWords, asString, closeAll, createResultSet, hasChanged, parseKey, quoteSQL, removeAllTriggers, setColumns, setIgnoreList, setWhitespaceChars, throwException
-
Field Details
-
STORE_DOCUMENT_TEXT_IN_INDEX
protected static final boolean STORE_DOCUMENT_TEXT_IN_INDEXWhether the text content should be stored in the Lucene index.
-
-
Constructor Details
-
FullTextLucene
public FullTextLucene()
-
-
Method Details
-
init
Initializes full text search functionality for this database. This adds the following Java functions to the database:- FTL_CREATE_INDEX(schemaNameString, tableNameString, columnListString)
- FTL_SEARCH(queryString, limitInt, offsetInt): result set
- FTL_REINDEX()
- FTL_DROP_ALL()
CREATE ALIAS IF NOT EXISTS FTL_INIT FOR "org.h2.fulltext.FullTextLucene.init"; CALL FTL_INIT();
- Parameters:
conn
- the connection- Throws:
SQLException
- on failure
-
createIndex
public static void createIndex(Connection conn, String schema, String table, String columnList) throws SQLException Create a new full text index for a table and column list. Each table may only have one index at any time.- Parameters:
conn
- the connectionschema
- the schema name of the table (case sensitive)table
- the table name (case sensitive)columnList
- the column list (null for all columns)- Throws:
SQLException
- on failure
-
dropIndex
Drop an existing full text index for a table. This method returns silently if no index for this table exists.- Parameters:
conn
- the connectionschema
- the schema name of the table (case sensitive)table
- the table name (case sensitive)- Throws:
SQLException
- on failure
-
reindex
Re-creates the full text index for this database. Calling this method is usually not needed, as the index is kept up-to-date automatically.- Parameters:
conn
- the connection- Throws:
SQLException
- on failure
-
dropAll
Drops all full text indexes from the database.- Parameters:
conn
- the connection- Throws:
SQLException
- on failure
-
search
public static ResultSet search(Connection conn, String text, int limit, int offset) throws SQLException Searches from the full text index for this database. The returned result set has the following column:- QUERY (varchar): the query to use to get the data. The query does not include 'SELECT * FROM '. Example: PUBLIC.TEST WHERE ID = 1
- SCORE (float) the relevance score as returned by Lucene.
- Parameters:
conn
- the connectiontext
- the search querylimit
- the maximum number of rows or 0 for no limitoffset
- the offset or 0 for no offset- Returns:
- the result set
- Throws:
SQLException
- on failure
-
searchData
public static ResultSet searchData(Connection conn, String text, int limit, int offset) throws SQLException Searches from the full text index for this database. The result contains the primary key data as an array. The returned result set has the following columns:- SCHEMA (varchar): the schema name. Example: PUBLIC
- TABLE (varchar): the table name. Example: TEST
- COLUMNS (array of varchar): comma separated list of quoted column names. The column names are quoted if necessary. Example: (ID)
- KEYS (array of values): comma separated list of values. Example: (1)
- SCORE (float) the relevance score as returned by Lucene.
- Parameters:
conn
- the connectiontext
- the search querylimit
- the maximum number of rows or 0 for no limitoffset
- the offset or 0 for no offset- Returns:
- the result set
- Throws:
SQLException
- on failure
-
convertException
Convert an exception to a fulltext exception.- Parameters:
e
- the original exception- Returns:
- the converted SQL exception
-
getIndexAccess
protected static org.h2.fulltext.FullTextLucene.IndexAccess getIndexAccess(Connection conn) throws SQLException Get the index writer/searcher wrapper for the given connection.- Parameters:
conn
- the connection- Returns:
- the index access wrapper
- Throws:
SQLException
- on failure
-
getIndexPath
Get the path of the Lucene index for this database.- Parameters:
conn
- the database connection- Returns:
- the path
- Throws:
SQLException
- on failure
-
removeIndexAccess
Close the index writer and searcher and remove them from the index access set.- Parameters:
indexPath
- the index path- Throws:
SQLException
- on failure
-
search
protected static ResultSet search(Connection conn, String text, int limit, int offset, boolean data) throws SQLException Do the search.- Parameters:
conn
- the database connectiontext
- the querylimit
- the limitoffset
- the offsetdata
- whether the raw data should be returned- Returns:
- the result set
- Throws:
SQLException
- on failure
-