Top Description Inners Fields Constructors Methods
org.h2.fulltext

public Class FullTextLucene

extends FullText
Class Inheritance
Imports
java.io.IOException, java.nio.file.Paths, java.sql.Connection, .DatabaseMetaData, .PreparedStatement, .ResultSet, .SQLException, .Statement, java.util.ArrayList, .Collections, .HashMap, org.apache.lucene.analysis.Analyzer, org.apache.lucene.analysis.standard.StandardAnalyzer, org.apache.lucene.document.DateTools, org.apache.lucene.document.Document, org.apache.lucene.document.Field, org.apache.lucene.document.FieldType, org.apache.lucene.document.TextField, org.apache.lucene.index.DirectoryReader, org.apache.lucene.index.IndexFormatTooOldException, org.apache.lucene.index.IndexReader, org.apache.lucene.index.IndexWriter, org.apache.lucene.index.IndexWriterConfig, org.apache.lucene.index.Term, org.apache.lucene.queryparser.flexible.standard.StandardQueryParser, org.apache.lucene.search.IndexSearcher, org.apache.lucene.search.Query, org.apache.lucene.search.ScoreDoc, org.apache.lucene.search.TopDocs, org.apache.lucene.store.ByteBuffersDirectory, org.apache.lucene.store.Directory, org.apache.lucene.store.FSDirectory, org.h2.api.Trigger, org.h2.command.Parser, org.h2.engine.SessionLocal, org.h2.expression.ExpressionColumn, org.h2.jdbc.JdbcConnection, org.h2.store.fs.FileUtils, org.h2.tools.SimpleResultSet, org.h2.util.StringUtils, .Utils

This class implements the full text search based on Apache Lucene. Most methods can be called using SQL statements as well.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
FullTextLucene.FullTextTrigger

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

private static class
FullTextLucene.IndexAccess

A wrapper for the Lucene writer and searcher.

Field Summary

Modifier and TypeField and Description
private static final String
IN_MEMORY_PREFIX

The prefix for a in-memory path.

private static final HashMap<String, FullTextLucene.IndexAccess>
private static final String
private static final String
private static final String
private static final String
private static final String
protected static final boolean
STORE_DOCUMENT_TEXT_IN_INDEX

Whether the text content should be stored in the Lucene index.

private static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected static SQLException

Returns:

the converted SQL exception
convertException
(Exception
the original exception
e
)

Convert an exception to a fulltext exception.

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
)

Hides org.h2.fulltext.FullText.createIndex.

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

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

Hides org.h2.fulltext.FullText.createTrigger.

Create the trigger.

public static void
dropAll(Connection
the connection
conn
)

Hides org.h2.fulltext.FullText.dropAll.

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
)

Hides org.h2.fulltext.FullText.dropIndex.

Drop an existing full text index for a table.

protected static FullTextLucene.IndexAccess

Returns:

the index access wrapper
getIndexAccess
(Connection
the connection
conn
)

Get the index writer/searcher wrapper for the given connection.

protected static String

Returns:

the path
getIndexPath
(Connection
the database connection
conn
)

Get the path of the Lucene index for this database.

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

Hides org.h2.fulltext.FullText.indexExistingRows.

Add the existing data to the index.

public static void
init(Connection
the connection
conn
)

Hides org.h2.fulltext.FullText.init.

Initializes full text search functionality for this database.

public static void
reindex(Connection
the connection
conn
)

Hides org.h2.fulltext.FullText.reindex.

Re-creates the full text index for this database.

protected static void
removeIndexAccess(String
the index path
indexPath
)

Close the index writer and searcher and remove them from the index access set.

private static void
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
)

Hides org.h2.fulltext.FullText.search.

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
)

Hides org.h2.fulltext.FullText.search.

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
)

Hides org.h2.fulltext.FullText.searchData.

Searches from the full text index for this database.

Inherited from org.h2.fulltext.FullText:
addWordsaddWordsasStringcloseAllcreateResultSethasChangedparseKeyquoteSQLremoveAllTriggerssetColumnssetIgnoreListsetWhitespaceCharsthrowException

Field Detail

IN_MEMORY_PREFIXback to summary
private static final String IN_MEMORY_PREFIX

The prefix for a in-memory path. This prefix is only used internally within this class and not related to the database URL.

INDEX_ACCESSback to summary
private static final HashMap<String, FullTextLucene.IndexAccess> INDEX_ACCESS
LUCENE_FIELD_COLUMN_PREFIXback to summary
private static final String LUCENE_FIELD_COLUMN_PREFIX
LUCENE_FIELD_DATAback to summary
private static final String LUCENE_FIELD_DATA
LUCENE_FIELD_MODIFIEDback to summary
private static final String LUCENE_FIELD_MODIFIED
LUCENE_FIELD_QUERYback to summary
private static final String LUCENE_FIELD_QUERY
SCHEMAback to summary
private static final String SCHEMA

Hides org.h2.fulltext.FullText.SCHEMA.

STORE_DOCUMENT_TEXT_IN_INDEXback to summary
protected static final boolean STORE_DOCUMENT_TEXT_IN_INDEX

Whether the text content should be stored in the Lucene index.

TRIGGER_PREFIXback to summary
private static final String TRIGGER_PREFIX

Hides org.h2.fulltext.FullText.TRIGGER_PREFIX.

Constructor Detail

FullTextLuceneback to summary
public FullTextLucene()

Method Detail

convertExceptionback to summary
protected static SQLException convertException(Exception e)

Convert an exception to a fulltext exception.

Parameters
e:Exception

the original exception

Returns:SQLException

the converted SQL exception

createIndexback to summary
public static void createIndex(Connection conn, String schema, String table, String columnList) throws SQLException

Hides org.h2.fulltext.FullText.createIndex.

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

Hides org.h2.fulltext.FullText.createOrDropTrigger.

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

Hides org.h2.fulltext.FullText.createTrigger.

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

Hides org.h2.fulltext.FullText.dropAll.

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

Hides org.h2.fulltext.FullText.dropIndex.

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
getIndexAccessback to summary
protected static FullTextLucene.IndexAccess getIndexAccess(Connection conn) throws SQLException

Get the index writer/searcher wrapper for the given connection.

Parameters
conn:Connection

the connection

Returns:FullTextLucene.IndexAccess

the index access wrapper

Exceptions
SQLException:
on failure
getIndexPathback to summary
protected static String getIndexPath(Connection conn) throws SQLException

Get the path of the Lucene index for this database.

Parameters
conn:Connection

the database connection

Returns:String

the path

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

Hides org.h2.fulltext.FullText.indexExistingRows.

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

Hides org.h2.fulltext.FullText.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()
It also adds a schema FTL 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 FTL_INIT FOR
     "org.h2.fulltext.FullTextLucene.init";
CALL FTL_INIT();
Parameters
conn:Connection

the connection

Exceptions
SQLException:
on failure
reindexback to summary
public static void reindex(Connection conn) throws SQLException

Hides org.h2.fulltext.FullText.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:Connection

the connection

Exceptions
SQLException:
on failure
removeIndexAccessback to summary
protected static void removeIndexAccess(String indexPath) throws SQLException

Close the index writer and searcher and remove them from the index access set.

Parameters
indexPath:String

the index path

Exceptions
SQLException:
on failure
removeIndexFilesback to summary
private static void removeIndexFiles(Connection conn) throws SQLException
searchback to summary
public static ResultSet search(Connection conn, String text, int limit, int offset) throws SQLException

Hides org.h2.fulltext.FullText.search.

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

Hides org.h2.fulltext.FullText.search.

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

Hides org.h2.fulltext.FullText.searchData.

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: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
org.h2.fulltext back to summary

public final Class FullTextLucene.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 String[]
private int[]
private final org.apache.lucene.document.FieldType
private FullTextLucene.IndexAccess
private int[]
private String
private int[]
private String
private 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

pack-priv void
commitIndex()

Commit all changes to the Lucene index.

private void
delete(Object[]
the row
row
,
boolean
whether to commit the changes to the Lucene index
commitIndex
)

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

pack-priv void
insert(Object[]
the row
row
,
boolean
whether to commit the changes to the Lucene index
commitIndex
)

Add a row to the index.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

columnsback to summary
private String[] columns
columnTypesback to summary
private int[] columnTypes
DOC_ID_FIELD_TYPEback to summary
private final org.apache.lucene.document.FieldType DOC_ID_FIELD_TYPE
indexAccessback to summary
private FullTextLucene.IndexAccess indexAccess
indexColumnsback to summary
private int[] indexColumns
indexPathback to summary
private String indexPath
keysback to summary
private int[] keys
schemaback to summary
private String schema
tableback to summary
private String table

Constructor Detail

FullTextTriggerback to summary
public FullTextTrigger()

Method Detail

closeback to summary
public void close() throws SQLException

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

INTERNAL

Annotations
@Override
Exceptions
SQLException:

Doc from org.h2.api.Trigger.close.

on SQL exception

commitIndexback to summary
pack-priv void commitIndex() throws SQLException

Commit all changes to the Lucene index.

Exceptions
SQLException:
on failure
deleteback to summary
private void delete(Object[] row, boolean commitIndex) throws SQLException

Delete a row from the index.

Parameters
row:Object[]

the row

commitIndex:boolean

whether to commit the changes to the Lucene index

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[])
getQueryback to summary
private String getQuery(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
pack-priv void insert(Object[] row, boolean commitIndex) throws SQLException

Add a row to the index.

Parameters
row:Object[]

the row

commitIndex:boolean

whether to commit the changes to the Lucene index

Exceptions
SQLException:
on failure
org.h2.fulltext back to summary

private final Class FullTextLucene.IndexAccess

extends Object
Class Inheritance

A wrapper for the Lucene writer and searcher.

Field Summary

Modifier and TypeField and Description
private org.apache.lucene.search.IndexSearcher
searcher

The index searcher.

pack-priv final org.apache.lucene.index.IndexWriter
writer

The index writer.

Constructor Summary

AccessConstructor and Description
pack-priv
IndexAccess(org.apache.lucene.index.IndexWriter writer)

Method Summary

Modifier and TypeMethod and Description
public synchronized void
close()

Close the index.

public synchronized void
commit()

Commit the changes.

pack-priv synchronized org.apache.lucene.search.IndexSearcher

Returns:

the searcher
getSearcher
()

Start using the searcher.

private void
pack-priv synchronized void
returnSearcher(org.apache.lucene.search.IndexSearcher
the searcher
searcher
)

Stop using the searcher.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

searcherback to summary
private org.apache.lucene.search.IndexSearcher searcher

The index searcher.

writerback to summary
pack-priv final org.apache.lucene.index.IndexWriter writer

The index writer.

Constructor Detail

IndexAccessback to summary
pack-priv IndexAccess(org.apache.lucene.index.IndexWriter writer) throws IOException

Method Detail

closeback to summary
public synchronized void close() throws IOException

Close the index.

Exceptions
IOException:
on failure
commitback to summary
public synchronized void commit() throws IOException

Commit the changes.

Exceptions
IOException:
on failure
getSearcherback to summary
pack-priv synchronized org.apache.lucene.search.IndexSearcher getSearcher() throws IOException

Start using the searcher.

Returns:org.apache.lucene.search.IndexSearcher

the searcher

Exceptions
IOException:
on failure
initializeSearcherback to summary
private void initializeSearcher() throws IOException
returnSearcherback to summary
pack-priv synchronized void returnSearcher(org.apache.lucene.search.IndexSearcher searcher) throws IOException

Stop using the searcher.

Parameters
searcher:org.apache.lucene.search.IndexSearcher

the searcher

Exceptions
IOException:
on failure