How to perform queries filtering records like using the SQL LIKE operator? (Android OS)


#1

I’m new to CBL. I need to perform search on the db using criteria similar to the SQL LIKE operator but it seems i can’t do something like this with CBL. It seems that the only way to do it is to emit any single document and perform programmatically filtering, but this seems to me like an extraordinary slow approach considering i may have 10000s of documents.
How can i achieve such a common task?
To be clearer, i have a List of documents with product names and user must be allowed to filter them specifying few letters.
Thanks in advance.


#2

@andretti1977, Here is the code snippet. I have been using this for fetching documents.

By using predicate, you can build your own queries to fetch data.
//Methods Calling

 let predicate : NSPredicate = NSPredicate(format: "documentType == %@", argumentArray: [NSStringFromClass(PettyCashModel)])
 let sortDesc : NSSortDescriptor = NSSortDescriptor(key: "updatedDate", ascending: true)
 let result = self.eventsObj.getMatchedDocsWithPredicate(predicate, withOrderList: [sortDesc], andWithStartIndex:self.skipIndex, andWithLimit: Constants.kLimitRecords)

//Method Body

 func getMatchedDocsWithPredicate(predicate: NSPredicate, withOrderList orderlist: [AnyObject]!, andWithStartIndex startindex: AnyObject!, andWithLimit limit: UInt) -> [AnyObject]! {
    
    let query = try! CBLQueryBuilder(database: Constants.appDeleRef.database, select: nil, wherePredicate: predicate, orderBy: orderlist)
    
    
    let queryObj : CBLQuery  = query.createQueryWithContext(nil)
    queryObj.descending = true
    
    queryObj.skip = startindex as! UInt
    queryObj.limit = limit
    let result : CBLQueryEnumerator = try! queryObj.run()
    
    var listData = [AnyObject]()
    
    while let row = result.nextRow() {
        
        listData.append(row)
        
        
    }
    return listData
}

#3

Thank itssrinadh, i forgot to specify one important requirement: my app is for an android os (now i have updated the topic title).
I tried to find the same API you used (especially the query builder) for android but it seems the ios and android apis are quite different so it seems to me that there is no query builder at all for android, am i right?


#4

@andretti1977 Hmm, Sorry i don’t know about CBLQueryBuilder for Android. lets wait you will get some more response from experts here.