Start a new topic
Answered

Trouble creating a query using "CONTAINS"

I'm following the syntax from https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html


let containsPredicate = NSPredicate(format: "self.name CONTAINS[c] %@", searchString)

Using the above, I get an error with the description: Regular expression in queries must be anchored at the beginning (they must start with ^)


Any idea why I'm running into this problem?


Best Answer

Hi Shea and Wood,


I have investigated this thoroughly and I wanted to bring some important information to your attention. We have placed restrictions on regex queries because they are very computationally expensive given our current platform setup. We ONLY support regex for querying only to the beginning of a string and it should be case sensitive. We do not recommend the full-text regular expression searches as they are very computationally expensive. Please check this documentation link for more information.


Now coming to your NSPredicate question, "Contains" will not work because it does not begin with "^". You will have to use "beginswith" as below (it will fetch all records for which "title" column value starts with "FRI"):

 

let dataStore = try DataStore<Book>.collection(type: .auto)
let predicate = NSPredicate(format: "title beginswith %@", "FRI")
let sortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
let query = Query(predicate: predicate, sortDescriptors: [sortDescriptor])

dataStore.find(query) { (result: Result<AnyRandomAccessCollection<Book>, Swift.Error>) in
                        switch result {
                        case .success(let books):
                            //print("Entered block \(num)");
                            //num+=1;
                            print("\nBooks: \(books)")
                            print("\nBook count: \(books.count)")


                            for record in books {
                                print(record)
                            }

                        case .failure(let error):
                            print("Error: \(error)")
                        }
                    }

 

 

The generated Mongo query will be like this: /appdata/kid_xyz/Book?query={"title":{"$regex":"^FRI"}}. You can test this on the Kinvey API console.




Thanks,

Pranav

Kinvey


1 person has this question

Have the same issue 

Any news ? I need this fix ASAP

Do you even check the feedback forum?

Yes, I do. However, no one has gave any answer so I'm not too sure how I can help you since we both have the same problem.

Hi Shea and Wood,


Sincere apologies as we couldn't answer your question in time. I have started working on this one and will get back to you soon.


Thanks,

Pranav

Kinvey


1 person likes this
Answer

Hi Shea and Wood,


I have investigated this thoroughly and I wanted to bring some important information to your attention. We have placed restrictions on regex queries because they are very computationally expensive given our current platform setup. We ONLY support regex for querying only to the beginning of a string and it should be case sensitive. We do not recommend the full-text regular expression searches as they are very computationally expensive. Please check this documentation link for more information.


Now coming to your NSPredicate question, "Contains" will not work because it does not begin with "^". You will have to use "beginswith" as below (it will fetch all records for which "title" column value starts with "FRI"):

 

let dataStore = try DataStore<Book>.collection(type: .auto)
let predicate = NSPredicate(format: "title beginswith %@", "FRI")
let sortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
let query = Query(predicate: predicate, sortDescriptors: [sortDescriptor])

dataStore.find(query) { (result: Result<AnyRandomAccessCollection<Book>, Swift.Error>) in
                        switch result {
                        case .success(let books):
                            //print("Entered block \(num)");
                            //num+=1;
                            print("\nBooks: \(books)")
                            print("\nBook count: \(books.count)")


                            for record in books {
                                print(record)
                            }

                        case .failure(let error):
                            print("Error: \(error)")
                        }
                    }

 

 

The generated Mongo query will be like this: /appdata/kid_xyz/Book?query={"title":{"$regex":"^FRI"}}. You can test this on the Kinvey API console.




Thanks,

Pranav

Kinvey


1 person likes this
Login or Signup to post a comment