grailsgrails-ormgorm-mongodb

grails gorm mongodb `like` functionality in criteria


Is like or rlike supported for searching a string in a collection's property value?
Does the collection need to define text type index for this to work? Unfortunately I can not create a text index for the property. There are 100 million documents and text index killed the performance (MongoDB is on single node).
If this is not do-able without text index, its fine with me. I will look for alternatives.

Given below collection:

Message {
    'payload' : 'XML or JSON string' 
    //few other properties
}

In grails, I created a Criteria to return me a list of documents which contain a specific string in the payload

Message.list {
  projections {
    like('payload' : searchString)
  }
}

I tried using rlike('payload' : ".*${searchString}.*") as well. It did not result in any doc to me.

Note: I was able to get the document when I fired the native query on Mongo shell.

db.Message.find({payload : { $regex : ".*My search string.*" }}).pretty()

Solution

  • I got it working in a round about way. I believe there is a much better grails solution. Criteria approach did not work. So used the low level API converted the DBObjects to Domain objects.

            def query = ['payload' : [ '$regex' : /${searchString}/ ]  ]
            def dbObjects = Message.collection.find(query).skip(offset).limit(defaultPageSize).toArray()
            dbObjects?.collect { new Message(new JsonSlurper().parseText(it.toString()))}