use of org.apache.lucene.search.TermQuery in project textdb by TextDB.
the class KeywordMatcherSourceOperator method buildConjunctionQuery.
private Query buildConjunctionQuery() throws DataFlowException {
BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
for (String attributeName : this.predicate.getAttributeNames()) {
AttributeType attributeType = this.inputSchema.getAttribute(attributeName).getAttributeType();
// types other than TEXT and STRING: throw Exception for now
if (attributeType != AttributeType.STRING && attributeType != AttributeType.TEXT) {
throw new DataFlowException("KeywordPredicate: Fields other than STRING and TEXT are not supported yet");
}
if (attributeType == AttributeType.STRING) {
Query termQuery = new TermQuery(new Term(attributeName, predicate.getQuery()));
booleanQueryBuilder.add(termQuery, BooleanClause.Occur.SHOULD);
}
if (attributeType == AttributeType.TEXT) {
BooleanQuery.Builder fieldQueryBuilder = new BooleanQuery.Builder();
for (String token : queryTokenSet) {
Query termQuery = new TermQuery(new Term(attributeName, token.toLowerCase()));
fieldQueryBuilder.add(termQuery, BooleanClause.Occur.MUST);
}
booleanQueryBuilder.add(fieldQueryBuilder.build(), BooleanClause.Occur.SHOULD);
}
}
return booleanQueryBuilder.build();
}
use of org.apache.lucene.search.TermQuery in project textdb by TextDB.
the class PlanStore method getPlan.
/**
* Retrieves a plan by given name from plan store.
*
* @param planName, the name of the plan.
* @Return ITuple, the tuple consisting of fields of the plan.
* @throws TextDBException
*/
public Tuple getPlan(String planName) throws TextDBException {
Query q = new TermQuery(new Term(PlanStoreConstants.NAME, planName));
DataReader reader = relationManager.getTableDataReader(PlanStoreConstants.TABLE_NAME, q);
reader.open();
Tuple inputTuple = null;
while ((inputTuple = reader.getNextTuple()) != null) {
IField nameField = inputTuple.getField(PlanStoreConstants.NAME);
if (nameField.getValue().toString().equals(planName)) {
reader.close();
return inputTuple;
}
}
reader.close();
return null;
}
use of org.apache.lucene.search.TermQuery in project textdb by TextDB.
the class RelationManager method getTupleByID.
/**
* Gets a tuple in a table by its _id field.
* Returns null if the tuple doesn't exist.
*
* @param tableName, the name of the table, case insensitive
* @param idValue, the IDField to lookup
* @return
* @throws StorageException
*/
public Tuple getTupleByID(String tableName, IDField idField) throws StorageException {
// construct the ID query
Query tupleIDQuery = new TermQuery(new Term(SchemaConstants._ID, idField.getValue().toString()));
// find the tuple using DataReader
DataReader dataReader = getTableDataReader(tableName, tupleIDQuery);
dataReader.setPayloadAdded(false);
dataReader.open();
Tuple tuple = dataReader.getNextTuple();
dataReader.close();
return tuple;
}
use of org.apache.lucene.search.TermQuery in project textdb by TextDB.
the class RelationManagerTest method test10.
/*
* Test inserting multiple tuples to a table, getting them by a query, then deleting them by a query
*/
@Test
public void test10() throws Exception {
String tableName = "relation_manager_test_table";
String tableDirectory = "./index/test_table";
Schema tableSchema = new Schema(new Attribute("content", AttributeType.STRING), new Attribute("number", AttributeType.STRING));
RelationManager relationManager = RelationManager.getRelationManager();
relationManager.deleteTable(tableName);
relationManager.createTable(tableName, tableDirectory, tableSchema, LuceneAnalyzerConstants.standardAnalyzerString());
DataWriter dataWriter = relationManager.getTableDataWriter(tableName);
dataWriter.open();
Tuple insertedTuple = new Tuple(tableSchema, new StringField("test"), new StringField("1"));
dataWriter.insertTuple(insertedTuple);
Tuple insertedTuple2 = new Tuple(tableSchema, new StringField("test"), new StringField("2"));
IDField idField2 = dataWriter.insertTuple(insertedTuple2);
Tuple insertedTuple3 = new Tuple(tableSchema, new StringField("test"), new StringField("3"));
dataWriter.insertTuple(insertedTuple3);
dataWriter.close();
// test should match all 3 tuples
Query allTupleQuery = new TermQuery(new Term("content", "test"));
DataReader allTupleReader = relationManager.getTableDataReader(tableName, allTupleQuery);
int tupleCounter = 0;
allTupleReader.open();
while (allTupleReader.getNextTuple() != null) {
tupleCounter++;
}
allTupleReader.close();
Assert.assertEquals(3, tupleCounter);
// tuple 2 should be deleted
Query tuple2Query = new TermQuery(new Term("number", "2"));
dataWriter.open();
dataWriter.deleteTuple(tuple2Query);
dataWriter.close();
Tuple deletedTuple = relationManager.getTupleByID(tableName, idField2);
Assert.assertNull(deletedTuple);
relationManager.deleteTable(tableName);
}
use of org.apache.lucene.search.TermQuery in project textdb by TextDB.
the class RelationManager method deleteTable.
/**
* Deletes a table by its name.
* If the table doesn't exist, it won't do anything.
* Deleting system catalog tables is prohibited.
*
* @param tableName, the name of a table, case insensitive
* @throws StorageException
*/
public void deleteTable(String tableName) throws StorageException {
tableName = tableName.toLowerCase();
// User can't delete catalog table
if (isSystemCatalog(tableName)) {
throw new StorageException("Deleting a system catalog table is prohibited.");
}
// if table doesn't exist, then do nothing
if (!checkTableExistence(tableName)) {
return;
}
// try to clear all data in the table
DataWriter dataWriter = getTableDataWriter(tableName);
dataWriter.open();
dataWriter.clearData();
dataWriter.close();
StorageUtils.deleteDirectory(getTableDirectory(tableName));
// generate a query for the table name
Query catalogTableNameQuery = new TermQuery(new Term(CatalogConstants.TABLE_NAME, tableName));
// delete the table from table catalog
DataWriter tableCatalogWriter = new DataWriter(CatalogConstants.TABLE_CATALOG_DATASTORE, LuceneAnalyzerConstants.getStandardAnalyzer());
tableCatalogWriter.open();
tableCatalogWriter.deleteTuple(catalogTableNameQuery);
tableCatalogWriter.close();
// delete the table from schema catalog
DataWriter schemaCatalogWriter = new DataWriter(CatalogConstants.SCHEMA_CATALOG_DATASTORE, LuceneAnalyzerConstants.getStandardAnalyzer());
schemaCatalogWriter.open();
schemaCatalogWriter.deleteTuple(catalogTableNameQuery);
schemaCatalogWriter.close();
}
Aggregations