use of org.apache.derby.optional.api.LuceneIndexDescriptor in project derby by apache.
the class LuceneSupport method createOrRecreateIndex.
/**
* Create or re-create a Lucene index on the specified column.
*
* @param schema The schema of the column to index
* @param table The table of the column to index
* @param textcol The column to create the Lucene index on
* @param indexDescriptorMaker name of static method which instantiates the index configuration. may be null.
* @param create True if the index is to be created, false if it is to be recreated
* @throws SQLException
* @throws IOException
*/
private static void createOrRecreateIndex(Connection conn, String schema, String table, String textcol, String indexDescriptorMaker, boolean create, String... keyColumns) throws SQLException, IOException, PrivilegedActionException {
VTITemplate.ColumnDescriptor[] primaryKeys = new VTITemplate.ColumnDescriptor[0];
// can't override keys when the index is updated
if (!create) {
primaryKeys = getKeys(conn, schema, table, textcol);
} else // use the supplied keys if possible
if ((keyColumns != null) && (keyColumns.length > 0)) {
primaryKeys = getKeys(conn, schema, table, keyColumns);
} else {
primaryKeys = getPrimaryKeys(conn, schema, table);
}
// can't create an index without specifying keys for joining it back to Derby data
if (primaryKeys.length == 0) {
throw ToolUtilities.newSQLException(SQLState.LUCENE_NO_PRIMARY_KEY);
}
// don't let the user create a table function with duplicate column names
vetColumnName(ToolUtilities.derbyIdentifier(textcol));
for (VTITemplate.ColumnDescriptor key : primaryKeys) {
vetColumnName(key.columnName);
}
int keyCount = 0;
StorageFile propertiesFile = getIndexPropertiesFile(conn, schema, table, textcol);
//
if (!create) {
dropIndexDirectories(schema, table, textcol);
}
Version luceneVersion = LuceneUtils.currentVersion();
// create the new directory
DerbyLuceneDir derbyLuceneDir = getDerbyLuceneDir(conn, schema, table, textcol);
// get the Analyzer and the field names. use the default if the user didn't specify an override
if (indexDescriptorMaker == null) {
indexDescriptorMaker = LuceneUtils.class.getName() + ".defaultIndexDescriptor";
}
LuceneIndexDescriptor indexDescriptor = getIndexDescriptor(indexDescriptorMaker);
String[] fieldNames = indexDescriptor.getFieldNames();
Analyzer analyzer = indexDescriptor.getAnalyzer();
// make sure the field names don't overlap with the key names
sortAndVetFieldNames(fieldNames, primaryKeys);
Properties indexProperties = new Properties();
indexProperties.setProperty(LUCENE_VERSION, luceneVersion.toString());
indexProperties.setProperty(UPDATE_TIMESTAMP, Long.toString(System.currentTimeMillis()));
indexProperties.setProperty(INDEX_DESCRIPTOR_MAKER, indexDescriptorMaker);
indexProperties.setProperty(ANALYZER, analyzer.getClass().getName());
StringBuilder tableFunction = new StringBuilder();
tableFunction.append("create function " + makeTableFunctionName(schema, table, textcol) + "\n");
tableFunction.append("( query varchar( 32672 ), windowSize int, scoreCeiling real )\n");
tableFunction.append("returns table\n(");
writeIndexProperties(propertiesFile, indexProperties);
PreparedStatement ps = null;
ResultSet rs = null;
IndexWriter iw = null;
try {
iw = getIndexWriter(luceneVersion, analyzer, derbyLuceneDir);
// select all keys and the textcol from this column, add to lucene index
StringBuilder query = new StringBuilder("select ");
for (VTITemplate.ColumnDescriptor keyDesc : primaryKeys) {
String keyName = delimitID(keyDesc.columnName);
if (keyCount > 0) {
query.append(", ");
}
query.append(keyName);
String keyType = mapType(keyDesc);
if (keyCount > 0) {
tableFunction.append(",");
}
tableFunction.append("\n\t" + keyName + " " + keyType);
keyCount++;
}
tableFunction.append(",\n\t" + DOCUMENT_ID + " int");
tableFunction.append(",\n\t" + SCORE + " real");
tableFunction.append("\n)\nlanguage java parameter style derby_jdbc_result_set contains sql\n");
tableFunction.append("external name '" + LuceneSupport.class.getName() + ".luceneQuery'");
// now create the table function for this text column
if (create) {
conn.prepareStatement(tableFunction.toString()).execute();
}
query.append(", ");
query.append(delimitID(ToolUtilities.derbyIdentifier(textcol)));
query.append(" from " + makeTableName(schema, table));
ps = conn.prepareStatement(query.toString());
rs = ps.executeQuery();
while (rs.next()) {
Document doc = new Document();
for (int i = 0; i < keyCount; i++) {
VTITemplate.ColumnDescriptor keyDescriptor = primaryKeys[i];
addValue(doc, keyDescriptor, rs, i + 1);
}
String textcolValue = rs.getString(keyCount + 1);
if (textcolValue != null) {
for (String fieldName : fieldNames) {
doc.add(new TextField(fieldName, textcolValue, Store.NO));
}
}
addDocument(iw, doc);
}
} finally {
try {
if (iw != null) {
close(iw);
}
} finally {
try {
if (rs != null) {
rs.close();
}
} finally {
if (ps != null) {
ps.close();
}
}
}
}
}
use of org.apache.derby.optional.api.LuceneIndexDescriptor in project derby by apache.
the class LuceneQueryVTI method initScan.
// ///////////////////////////////////////////////////////////////////
//
// MINIONS
//
// ///////////////////////////////////////////////////////////////////
/**
* Initialize the metadata and scan
*/
private void initScan() throws SQLException {
try {
// read the execution context for this AwareVTI
VTIContext context = getContext();
_schema = context.vtiSchema();
String[] nameParts = LuceneSupport.decodeFunctionName(context.vtiTable());
_table = nameParts[LuceneSupport.TABLE_PART];
_column = nameParts[LuceneSupport.COLUMN_PART];
// divine the column names
VTITemplate.ColumnDescriptor[] returnColumns = getReturnTableSignature(_connection);
String[] columnNames = new String[returnColumns.length];
for (int i = 0; i < returnColumns.length; i++) {
columnNames[i] = returnColumns[i].columnName;
}
setColumnNames(columnNames);
_scoreColumnID = getColumnCount();
_docIDColumnID = _scoreColumnID - 1;
_maxKeyID = _docIDColumnID - 1;
_minKeyID = 1;
// make sure the user has SELECT privilege on all relevant columns of the underlying table
vetPrivileges();
String delimitedColumnName = LuceneSupport.delimitID(_column);
DerbyLuceneDir derbyLuceneDir = LuceneSupport.getDerbyLuceneDir(_connection, _schema, _table, delimitedColumnName);
StorageFile propertiesFile = LuceneSupport.getIndexPropertiesFile(derbyLuceneDir);
Properties indexProperties = readIndexProperties(propertiesFile);
String indexDescriptorMaker = indexProperties.getProperty(LuceneSupport.INDEX_DESCRIPTOR_MAKER);
LuceneIndexDescriptor indexDescriptor = getIndexDescriptor(indexDescriptorMaker);
Analyzer analyzer = indexDescriptor.getAnalyzer();
QueryParser qp = indexDescriptor.getQueryParser();
vetLuceneVersion(indexProperties.getProperty(LuceneSupport.LUCENE_VERSION));
_indexReader = getIndexReader(derbyLuceneDir);
_searcher = new IndexSearcher(_indexReader);
Query luceneQuery = qp.parse(_queryText);
TopScoreDocCollector tsdc = TopScoreDocCollector.create(_windowSize, true);
if (_scoreCeiling != null) {
tsdc = TopScoreDocCollector.create(_windowSize, new ScoreDoc(0, _scoreCeiling), true);
}
searchAndScore(luceneQuery, tsdc);
} catch (IOException ioe) {
throw ToolUtilities.wrap(ioe);
} catch (ParseException pe) {
throw ToolUtilities.wrap(pe);
} catch (PrivilegedActionException pae) {
throw ToolUtilities.wrap(pae);
}
}
use of org.apache.derby.optional.api.LuceneIndexDescriptor in project derby by apache.
the class LuceneSupport method getIndexDescriptorNoPrivs.
/**
* Invoke a static method (possibly supplied by the user) to instantiate an index descriptor.
* The method has no arguments.
*/
static LuceneIndexDescriptor getIndexDescriptorNoPrivs(String indexDescriptorMaker) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
int lastDotIdx = indexDescriptorMaker.lastIndexOf(".");
String className = indexDescriptorMaker.substring(0, lastDotIdx);
ClassInspector ci = getClassFactory().getClassInspector();
Class<? extends Object> klass = ci.getClass(className);
String methodName = indexDescriptorMaker.substring(lastDotIdx + 1, indexDescriptorMaker.length());
Method method = klass.getDeclaredMethod(methodName);
return (LuceneIndexDescriptor) method.invoke(null);
}
Aggregations