Search in sources :

Example 86 with TermQuery

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();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) AttributeType(edu.uci.ics.textdb.api.schema.AttributeType) DataFlowException(edu.uci.ics.textdb.api.exception.DataFlowException) Term(org.apache.lucene.index.Term)

Example 87 with TermQuery

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;
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) DataReader(edu.uci.ics.textdb.storage.DataReader) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) IField(edu.uci.ics.textdb.api.field.IField) Tuple(edu.uci.ics.textdb.api.tuple.Tuple)

Example 88 with TermQuery

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;
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) Tuple(edu.uci.ics.textdb.api.tuple.Tuple)

Example 89 with TermQuery

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);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) IDField(edu.uci.ics.textdb.api.field.IDField) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) Attribute(edu.uci.ics.textdb.api.schema.Attribute) Schema(edu.uci.ics.textdb.api.schema.Schema) Term(org.apache.lucene.index.Term) StringField(edu.uci.ics.textdb.api.field.StringField) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) Test(org.junit.Test)

Example 90 with TermQuery

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();
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) StorageException(edu.uci.ics.textdb.api.exception.StorageException)

Aggregations

TermQuery (org.apache.lucene.search.TermQuery)673 Term (org.apache.lucene.index.Term)560 BooleanQuery (org.apache.lucene.search.BooleanQuery)343 Query (org.apache.lucene.search.Query)275 IndexSearcher (org.apache.lucene.search.IndexSearcher)252 Document (org.apache.lucene.document.Document)210 TopDocs (org.apache.lucene.search.TopDocs)164 Directory (org.apache.lucene.store.Directory)164 IndexReader (org.apache.lucene.index.IndexReader)125 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)125 PhraseQuery (org.apache.lucene.search.PhraseQuery)122 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)116 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)114 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)97 BoostQuery (org.apache.lucene.search.BoostQuery)85 Field (org.apache.lucene.document.Field)81 Test (org.junit.Test)75 PrefixQuery (org.apache.lucene.search.PrefixQuery)74 ArrayList (java.util.ArrayList)72 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)62