views:

316

answers:

1

Hi everyone (my first post!),

I have an HQL question (in Groovy/Grails) I was hoping someone could help me with.

I have a simple Asset object with a one-to-many Tags collection.

class Asset { Set tags static hasMany = [tags:Tag] }

class Tag { String name }

What I'm trying to do in HQL:

A user passes in some tags in params.tags (ex: groovy grails rocks) and wants to return Asset(s) that have those tags, and only those exact tags.

Here's my HQL that returns Assets if one or more of the tags are present in an Assets tags:

SELECT DISTINCT a FROM Asset a LEFT JOIN a.tags t WHERE t.name IN (:tags)

assetList = Asset.executeQuery( hql, [tags:tokenizedTagListFromParams]

The above code works perfect, but its really like an OR. If any of the tag(s) are found, it will return that Asset.

I only want to return Assets that have those exact same tags (in number as well).

Every time a new tag is created, I new Tag(name:xxx).save() so I can get the Tag instances and unique ID's for each tag that was asked for.

I also tried converting the passed in tags to a list of Tag instances with Tag.findByName(t1) for each tag, and also a list of (unique) Tag Id's into the HQL above with no luck.

I would appreciate any help/advice.

Thank you for your time, Burt

+1  A: 

It's taking too much brain effort to work this one out. Have you considered using the Searchable plugin instead? You could set up Tag as a "component" of Asset and then do a search like "+tag:groovy +tag:grails +tag:rocks". That would get you the results you're after (and probably quicker).

Peter Ledbrook
Hi Peter!This is an excellent suggestion. It just so happens that my project is using the searchable plugin!'tags' are a searchable 'component' defined in the Asset class.I have a page with a search box, and i tried just searching for those tags: groovy grails rocks and i get Asset hits. Excellent.But when I tried the syntax you mentioned, I didn't get any hits at all! Is there some special quotations I need or configuration? I checked the query parser syntax; it should work.I'm just wondering why I don't any results at all.Thank you for the excellent suggestion!Burt Prior
BurtP
The `tag:...` syntax works if your searchable class has a `tag` property. Perhaps a `tags:...` prefix would work?I know that you can index tag names against a field name like `tag`, but I can't remember how to configure it in Searchable. You could try: static searchable = { tags [name: "tag"] }in `Asset`.
Peter Ledbrook