Top Description Inners Fields Constructors Methods
org.h2.fulltext

public Class FullText

extends Object
Class Inheritance
Known Direct Subclasses
org.h2.fulltext.FullTextLucene
Imports
java.io.IOException, .Reader, .StreamTokenizer, java.sql.Clob, .Connection, .DatabaseMetaData, .PreparedStatement, .ResultSet, .SQLException, .Statement, .Types, java.util.ArrayList, .Arrays, .Collections, .HashSet, .Set, .StringTokenizer, .UUID, org.h2.api.Trigger, org.h2.command.Parser, org.h2.engine.SessionLocal, org.h2.expression.Expression, .ExpressionColumn, .ValueExpression, org.h2.expression.condition.Comparison, .ConditionAndOr, org.h2.jdbc.JdbcConnection, org.h2.message.DbException, org.h2.tools.SimpleResultSet, org.h2.util.IOUtils, .StringUtils, .Utils

This class implements the native full text search. Most methods can be called using SQL statements as well.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
FullText.FullTextTrigger

Trigger updates the index when a inserting, updating, or deleting a row.

Field Summary

Modifier and TypeField and Description
private static final String
FIELD_COLUMNS

A column name of the result set returned by the searchData method.

private static final String
FIELD_KEYS

A column name of the result set returned by the searchData method.

private static final String
FIELD_QUERY

The column name of the result set returned by the search method.

private static final String
FIELD_SCHEMA

A column name of the result set returned by the searchData method.

private static final String
FIELD_SCORE

The hit score.

private static final String
FIELD_TABLE

A column name of the result set returned by the searchData method.

private static final String
private static final String
private static final String
private static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static void
protected static void
addWords(FullTextSettings
the fulltext settings
setting
,
Set<String>
the hash set
set
,
Reader
the reader
reader
)

Add all words in the given text to the hash set.

protected static void
addWords(FullTextSettings
the fulltext settings
setting
,
Set<String>
the hash set
set
,
String
the text
text
)

Add all words in the given text to the hash set.

protected static String

Returns:

the string
asString
(Object
the object
data
,
int
the SQL type
type
)

INTERNAL.

public static void
closeAll()

INTERNAL Close all fulltext settings, freeing up memory.

public static void
createIndex(Connection
the connection
conn
,
String
the schema name of the table (case sensitive)
schema
,
String
the table name (case sensitive)
table
,
String
the column list (null for all columns)
columnList
)

Create a new full text index for a table and column list.

private static void
createOrDropTrigger(Connection conn, String schema, String table, boolean create)

protected static SimpleResultSet

Returns:

the empty result set
createResultSet
(boolean
true if the result set should contain the primary key data as an array.
data
)

Create an empty search result and initialize the columns.

private static void
createTrigger(Connection
the database connection
conn
,
String
the schema name
schema
,
String
the table name
table
)

Create the trigger.

public static void
dropAll(Connection
the connection
conn
)

Drops all full text indexes from the database.

public static void
dropIndex(Connection
the connection
conn
,
String
the schema name of the table (case sensitive)
schema
,
String
the table name (case sensitive)
table
)

Drop an existing full text index for a table.

protected static boolean

Returns:

true if the indexed columns don't match
hasChanged
(Object[]
the old row
oldRow
,
Object[]
the new row
newRow
,
int[]
the indexed columns
indexColumns
)

Check if a the indexed columns of a row probably have changed.

private static void
indexExistingRows(Connection
the database connection
conn
,
String
the schema name
schema
,
String
the table name
table
)

Add the existing data to the index.

public static void
init(Connection
the connection
conn
)

Initializes full text search functionality for this database.

protected static String[][]

Returns:

an array containing the column name list and the data list
parseKey
(Connection
the database connection
conn
,
String
the primary key condition as a string
key
)

Parse a primary key condition into the primary key columns.

protected static String

Returns:

the SQL String
quoteSQL
(Object
the object
data
,
int
the SQL type
type
)

INTERNAL.

private static String
public static void
reindex(Connection
the connection
conn
)

Re-creates the full text index for this database.

protected static void
removeAllTriggers(Connection
the database connection
conn
,
String
the prefix
prefix
)

Remove all triggers that start with the given prefix.

public static ResultSet

Returns:

the result set
search
(Connection
the connection
conn
,
String
the search query
text
,
int
the maximum number of rows or 0 for no limit
limit
,
int
the offset or 0 for no offset
offset
)

Searches from the full text index for this database.

protected static ResultSet

Returns:

the result set
search
(Connection
the database connection
conn
,
String
the query
text
,
int
the limit
limit
,
int
the offset
offset
,
boolean
whether the raw data should be returned
data
)

Do the search.

public static ResultSet

Returns:

the result set
searchData
(Connection
the connection
conn
,
String
the search query
text
,
int
the maximum number of rows or 0 for no limit
limit
,
int
the offset or 0 for no offset
offset
)

Searches from the full text index for this database.

protected static void
setColumns(int[]
the column indices (will be modified)
index
,
ArrayList<String>
the key list
keys
,
ArrayList<String>
the column list
columns
)

Set the column indices of a set of keys.

public static void
setIgnoreList(Connection
the connection
conn
,
String
the list
commaSeparatedList
)

Change the ignore list.

private static void
setIgnoreList(FullTextSettings setting, String commaSeparatedList)

public static void
setWhitespaceChars(Connection
the connection
conn
,
String
the list of characters
whitespaceChars
)

Change the whitespace characters.

protected static SQLException

Returns:

never returns normally
throwException
(String
the message
message
)

Throw a SQLException with the given message.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FIELD_COLUMNSback to summary
private static final String FIELD_COLUMNS

A column name of the result set returned by the searchData method.

FIELD_KEYSback to summary
private static final String FIELD_KEYS

A column name of the result set returned by the searchData method.

FIELD_QUERYback to summary
private static final String FIELD_QUERY

The column name of the result set returned by the search method.

FIELD_SCHEMAback to summary
private static final String FIELD_SCHEMA

A column name of the result set returned by the searchData method.

FIELD_SCOREback to summary
private static final String FIELD_SCORE

The hit score.

FIELD_TABLEback to summary
private static final String FIELD_TABLE

A column name of the result set returned by the searchData method.

SCHEMAback to summary
private static final String SCHEMA
SELECT_MAP_BY_WORD_IDback to summary
private static final String SELECT_MAP_BY_WORD_ID
SELECT_ROW_BY_IDback to summary
private static final String SELECT_ROW_BY_ID
TRIGGER_PREFIXback to summary
private static final String TRIGGER_PREFIX

Constructor Detail

FullTextback to summary
public FullText()

Method Detail

addColumnDataback to summary
private static void addColumnData(SessionLocal session, ArrayList<String> columns, ArrayList<String> data, Expression expr)
addWordsback to summary
protected static void addWords(FullTextSettings setting, Set<String> set, Reader reader)

Add all words in the given text to the hash set.

Parameters
setting:FullTextSettings

the fulltext settings

set:Set<String>

the hash set

reader:Reader

the reader

addWordsback to summary
protected static void addWords(FullTextSettings setting, Set<String> set, String text)

Add all words in the given text to the hash set.

Parameters
setting:FullTextSettings

the fulltext settings

set:Set<String>

the hash set

text:String

the text

asStringback to summary
protected static String asString(Object data, int type) throws SQLException

INTERNAL. Convert the object to a string.

Parameters
data:Object

the object

type:int

the SQL type

Returns:String

the string

Exceptions
SQLException:
on failure
closeAllback to summary
public static void closeAll()

INTERNAL Close all fulltext settings, freeing up memory.

createIndexback to summary
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:Connection

the connection

schema:String

the schema name of the table (case sensitive)

table:String

the table name (case sensitive)

columnList:String

the column list (null for all columns)

Exceptions
SQLException:
on failure
createOrDropTriggerback to summary
private static void createOrDropTrigger(Connection conn, String schema, String table, boolean create) throws SQLException
createResultSetback to summary
protected static SimpleResultSet createResultSet(boolean data)

Create an empty search result and initialize the columns.

Parameters
data:boolean

true if the result set should contain the primary key data as an array.

Returns:SimpleResultSet

the empty result set

createTriggerback to summary
private static void createTrigger(Connection conn, String schema, String table) throws SQLException

Create the trigger.

Parameters
conn:Connection

the database connection

schema:String

the schema name

table:String

the table name

Exceptions
SQLException:
on failure
dropAllback to summary
public static void dropAll(Connection conn) throws SQLException

Drops all full text indexes from the database.

Parameters
conn:Connection

the connection

Exceptions
SQLException:
on failure
dropIndexback to summary
public static void dropIndex(Connection conn, String schema, String table) throws SQLException

Drop an existing full text index for a table. This method returns silently if no index for this table exists.

Parameters
conn:Connection

the connection

schema:String

the schema name of the table (case sensitive)

table:String

the table name (case sensitive)

Exceptions
SQLException:
on failure
hasChangedback to summary
protected static boolean hasChanged(Object[] oldRow, Object[] newRow, int[] indexColumns)

Check if a the indexed columns of a row probably have changed. It may return true even if the change was minimal (for example from 0.0 to 0.00).

Parameters
oldRow:Object[]

the old row

newRow:Object[]

the new row

indexColumns:int[]

the indexed columns

Returns:boolean

true if the indexed columns don't match

indexExistingRowsback to summary
private static void indexExistingRows(Connection conn, String schema, String table) throws SQLException

Add the existing data to the index.

Parameters
conn:Connection

the database connection

schema:String

the schema name

table:String

the table name

Exceptions
SQLException:
on failure
initback to summary
public static void init(Connection conn) throws SQLException

Initializes full text search functionality for this database. This adds the following Java functions to the database:

  • FT_CREATE_INDEX(schemaNameString, tableNameString, columnListString)
  • FT_SEARCH(queryString, limitInt, offsetInt): result set
  • FT_REINDEX()
  • FT_DROP_ALL()
It also adds a schema FT to the database where bookkeeping information is stored. This function may be called from a Java application, or by using the SQL statements:
CREATE ALIAS IF NOT EXISTS FT_INIT FOR
     "org.h2.fulltext.FullText.init";
CALL FT_INIT();
Parameters
conn:Connection

the connection

Exceptions
SQLException:
on failure
parseKeyback to summary
protected static String[][] parseKey(Connection conn, String key)

Parse a primary key condition into the primary key columns.

Parameters
conn:Connection

the database connection

key:String

the primary key condition as a string

Returns:String[][]

an array containing the column name list and the data list

quoteSQLback to summary
protected static String quoteSQL(Object data, int type) throws SQLException

INTERNAL. Convert an object to a String as used in a SQL statement.

Parameters
data:Object

the object

type:int

the SQL type

Returns:String

the SQL String

Exceptions
SQLException:
on failure
quoteStringback to summary
private static String quoteString(String data)
reindexback to summary
public static void reindex(Connection conn) throws SQLException

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:Connection

the connection

Exceptions
SQLException:
on failure
removeAllTriggersback to summary
protected static void removeAllTriggers(Connection conn, String prefix) throws SQLException

Remove all triggers that start with the given prefix.

Parameters
conn:Connection

the database connection

prefix:String

the prefix

Exceptions
SQLException:
on failure
searchback to summary
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. This value is always 1.0 for the native fulltext search.
Parameters
conn:Connection

the connection

text:String

the search query

limit:int

the maximum number of rows or 0 for no limit

offset:int

the offset or 0 for no offset

Returns:ResultSet

the result set

Exceptions
SQLException:
on failure
searchback to summary
protected static ResultSet search(Connection conn, String text, int limit, int offset, boolean data) throws SQLException

Do the search.

Parameters
conn:Connection

the database connection

text:String

the query

limit:int

the limit

offset:int

the offset

data:boolean

whether the raw data should be returned

Returns:ResultSet

the result set

Exceptions
SQLException:
on failure
searchDataback to summary
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. This value is always 1.0 for the native fulltext search.
Parameters
conn:Connection

the connection

text:String

the search query

limit:int

the maximum number of rows or 0 for no limit

offset:int

the offset or 0 for no offset

Returns:ResultSet

the result set

Exceptions
SQLException:
on failure
setColumnsback to summary
protected static void setColumns(int[] index, ArrayList<String> keys, ArrayList<String> columns) throws SQLException

Set the column indices of a set of keys.

Parameters
index:int[]

the column indices (will be modified)

keys:ArrayList<String>

the key list

columns:ArrayList<String>

the column list

Exceptions
SQLException:
on failure
setIgnoreListback to summary
public static void setIgnoreList(Connection conn, String commaSeparatedList) throws SQLException

Change the ignore list. The ignore list is a comma separated list of common words that must not be indexed. The default ignore list is empty. If indexes already exist at the time this list is changed, reindex must be called.

Parameters
conn:Connection

the connection

commaSeparatedList:String

the list

Exceptions
SQLException:
on failure
setIgnoreListback to summary
private static void setIgnoreList(FullTextSettings setting, String commaSeparatedList)
setWhitespaceCharsback to summary
public static void setWhitespaceChars(Connection conn, String whitespaceChars) throws SQLException

Change the whitespace characters. The whitespace characters are used to separate words. If indexes already exist at the time this list is changed, reindex must be called.

Parameters
conn:Connection

the connection

whitespaceChars:String

the list of characters

Exceptions
SQLException:
on failure
throwExceptionback to summary
protected static SQLException throwException(String message) throws SQLException

Throw a SQLException with the given message.

Parameters
message:String

the message

Returns:SQLException

never returns normally

Exceptions
SQLException:
the exception
org.h2.fulltext back to summary

public final Class FullText.FullTextTrigger

extends Object
implements Trigger
Class Inheritance
All Implemented Interfaces
org.h2.api.Trigger

Trigger updates the index when a inserting, updating, or deleting a row.

Field Summary

Modifier and TypeField and Description
private int[]
private static final int
private static final int
private IndexInfo
private static final int
private static final int
private static final int
private static final int
private FullTextSettings
private static final String[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides default org.h2.api.Trigger.close.

INTERNAL

private void
delete(Connection
to use
conn
,
Object[]
the row
row
)

Delete a row from the index.

public void
fire(Connection
a connection to the database
conn
,
Object[]
the old row, or null if no old row is available (for INSERT)
oldRow
,
Object[]
the new row, or null if no new row is available (for DELETE)
newRow
)

Implements org.h2.api.Trigger.fire.

INTERNAL

private String
getKey(Object[] row)

private static PreparedStatement
getStatement(Connection conn, int index)

private int[]
public void
init(Connection
a connection to the database (a system connection)
conn
,
String
the name of the schema
schemaName
,
String
the name of the trigger used in the CREATE TRIGGER statement
triggerName
,
String
the name of the table
tableName
,
boolean
whether the fire method is called before or after the operation is performed
before
,
int
the operation type: INSERT, UPDATE, DELETE, SELECT, or a combination (this parameter is a bit field)
type
)

Overrides default org.h2.api.Trigger.init.

INTERNAL

private void
insert(Connection
to use
conn
,
Object[]
the row
row
)

Add a row to the index.

public void
remove()

Overrides default org.h2.api.Trigger.remove.

INTERNAL

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

columnTypesback to summary
private int[] columnTypes
DELETE_MAPback to summary
private static final int DELETE_MAP
DELETE_ROWback to summary
private static final int DELETE_ROW
indexback to summary
private IndexInfo index
INSERT_MAPback to summary
private static final int INSERT_MAP
INSERT_ROWback to summary
private static final int INSERT_ROW
INSERT_WORDback to summary
private static final int INSERT_WORD
SELECT_ROWback to summary
private static final int SELECT_ROW
settingback to summary
private FullTextSettings setting
SQLback to summary
private static final String[] SQL

Constructor Detail

FullTextTriggerback to summary
public FullTextTrigger()

Method Detail

closeback to summary
public void close()

Overrides default org.h2.api.Trigger.close.

INTERNAL

Annotations
@Override
deleteback to summary
private void delete(Connection conn, Object[] row) throws SQLException

Delete a row from the index.

Parameters
conn:Connection

to use

row:Object[]

the row

Exceptions
SQLException:
on failure
fireback to summary
public void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException

Implements org.h2.api.Trigger.fire.

INTERNAL

Parameters
conn:Connection

Doc from org.h2.api.Trigger.fire.

a connection to the database

oldRow:Object[]

Doc from org.h2.api.Trigger.fire.

the old row, or null if no old row is available (for INSERT)

newRow:Object[]

Doc from org.h2.api.Trigger.fire.

the new row, or null if no new row is available (for DELETE)

Annotations
@Override
Exceptions
SQLException:

Doc from org.h2.api.Trigger.fire.

if the operation must be undone

See Also
Trigger#fire(Connection, Object[], Object[])
getKeyback to summary
private String getKey(Object[] row) throws SQLException
getStatementback to summary
private static PreparedStatement getStatement(Connection conn, int index) throws SQLException
getWordIdsback to summary
private int[] getWordIds(Connection conn, Object[] row) throws SQLException
initback to summary
public void init(Connection conn, String schemaName, String triggerName, String tableName, boolean before, int type) throws SQLException

Overrides default org.h2.api.Trigger.init.

INTERNAL

Parameters
conn:Connection

Doc from org.h2.api.Trigger.init.

a connection to the database (a system connection)

schemaName:String

Doc from org.h2.api.Trigger.init.

the name of the schema

triggerName:String

Doc from org.h2.api.Trigger.init.

the name of the trigger used in the CREATE TRIGGER statement

tableName:String

Doc from org.h2.api.Trigger.init.

the name of the table

before:boolean

Doc from org.h2.api.Trigger.init.

whether the fire method is called before or after the operation is performed

type:int

Doc from org.h2.api.Trigger.init.

the operation type: INSERT, UPDATE, DELETE, SELECT, or a combination (this parameter is a bit field)

Annotations
@Override
Exceptions
SQLException:

Doc from org.h2.api.Trigger.init.

on SQL exception

See Also
Trigger#init(Connection, String, String, String, boolean, int)
insertback to summary
private void insert(Connection conn, Object[] row) throws SQLException

Add a row to the index.

Parameters
conn:Connection

to use

row:Object[]

the row

Exceptions
SQLException:
on failure
removeback to summary
public void remove()

Overrides default org.h2.api.Trigger.remove.

INTERNAL

Annotations
@Override