If it's in a domain class try:
def matches = Book.findAllByTitleLike("wonderful")
of if you don't want to worry about upper/lower case:
def matches = Book.findAllByTitleILike("wonderful")
If your book titles are in a collection:
def books = ['Wonderful Tonight', 'Atlas Shrugged', 'A Tale of Two Cities']
def matches = books.findAll{it.indexOf("wonderful") > -1} // returns []
matches = books.findAll{it.indexOf("Wonderful") > -1} // returns ['Wonderful Tonight']
matches = books.findAll{it.toLowerCase().indexOf("wonderful") > -1} // returns ['Wonderful Tonight']
Update:
So now that we have a more complete description of your problem, let's figure out a simplified way to solve it.
Today, you want to search by event title and its date so we'll start there then look at how we might generalize this and make more complete solution down the road.
Our general flow is going to be something like:
- You display a search form to your user
- User enters a date and/or title to search for and submits the form
- You find the matching entries
- You display the matching entries to the user
First, let's create a view for the search form, in grails-app/views/entry/ create a file called search.gsp with the following content:
<g:form controller='entry' action='searchResults'>
Entry Title: <g:textField name="title" value="${title}" />
Entry Date: <g:datePicker name="day" value="${day}" precision="day" />
<g:submitButton name="submit" value="Search" />
</g:form>
Next, we'll create the controller. In grails-app/controllers/ create a new controller EntryController.groovy. You can do this by executing the 'grails create-controller Entry' if you don't already have the EntryController created.
Now, we'll create two controller closures:
def search = {
render(view:'search')
}
and
def searchResults = {
def entryCriteria = Entry.createCriteria()
def results = entryCriteria.list {
if(params?.title) {
ilike{"title","%${params.title}%"
}
if(params?.day) {
eq("eventDate", params.day)
}
}
render(view:'searchResults', model:['results':results])
}
Finally, let's create a searchResults.gsp in grails-app/views/entry
<h1>Results</h1>
<g:each in=${results}>
${it}
</g:each>
So, now putting it all together:
- Run your app and go to localhost:8080/${appName}/entry/search, and that should bring up your search form.
- Enter a title and/or date, submit the form and that should send your form data into the searchResults closure in your controller
- The criteria search in the closure will find all the Entries that match your search criteria
- You render the search results view and pass it your matching entries, then on the gsp we loop through each of the results and display it.
As a disclaimer, I'm writing this code on the fly in the stack overflow text editor, so I won't be surprised if there are a couple syntax issues that I missed. Hopefully the explanation will be enough to get you on your way. Now let's talk about taking this basic example and polishing it up to a more respectable solution.
I would recommend you try to do the following after you get the hang of this basic example:
- Try building a command object to represent your search form, it will allow you to validate your user's input and is a more manageable approach than dealing directly with the request parameters like I did above.
- Build some unit tests for your command object and the controller flow; check that error messages are being populated for your various validations.
- Take the criteria search that we have defined in the searchResults closure, and move it out to a separate Grails Service, pass your command search object to the service to get your results. Then, you can re-use the search functionality if you need it else where in your application later.
- Build some unit tests for your service; mock out the Entry domain object and verify that your search functionality is working correctly.
If you're struggling to get the above solution to work, try to simplify it even further. Eliminate the date search and just search by title. Replace all the criteria logic in the searchResults action with:
def results = Entry.findByTitleILike(params?.title)
Start simple and improve and you'll get the hang of it in no time.
Good Luck!