CQ5 JCR query in multiple paths - aem

I need to have a JCR SQL query of this form :
select * from jcr:content where cq:template like '%myTemplate%' and ( jcr:path like '%path1%' or jcr:path like '%path2%')
But I get an exception saying that "incorrect use of property jcr:path" Is there a quick workaround for this ? The number of paths to search in may vary each time based on user selection.

I tried the following query in CQ's Query tool and it worked.
SELECT * FROM [cq:PageContent] WHERE [cq:PageContent].[cq:template] LIKE '%content%' AND ( isdescendantnode('/content/geometrixx/fr/') OR isdescendantnode('/content/geometrixx/en/'))
But ISDESCENDANTNODE requires an absolute path and i think relative ones wouldnt work.

As you've noticed, it's impossible to use multiple path comparisons in the JCR queries. You have a few options here:
Create a few queries, one per path.
Add some custom attribute to jcr:content node marking the pages you are interested in and use it instead of paths.
Iterate over path1 and path2 subtrees rather than query them.

Related

SQL2 query for accessing tags from assets

I want to know what is the SQL2 query for accessing child nodes of a parent node for full text search.
I have an image saved into assets named men-watches.png. I have created a tag named watches and assigned this tag to asset mentioned above.
I have done the same with other images too. Please help me with the SQL2 full text search query as I want to search this asset.
I tried
select * from [dam:Asset] as d where contains (d.[jcr:content/metadata/cq:tags] = ‘watches’)
But I am asked to write a full text search query in SQL2 using IN clause for tags ‘watches’ and writing it for not only checking tags, but also for checking for title or description too.
Not sure if I understood it correctly, but this example would search if one of provided tags are on some images.
SELECT * FROM [dam:Asset] AS asset WHERE (asset.[jcr:content/metadata/cq:tags] IN ('watches','something else')) AND ISDESCENDANTNODE(asset,'/content/dam')

Is it possible to search for a rendition of an image based on the dimensions via QueryBuilder API

What I would like to achieve is the following. Based on the specified dimension and an asset title I would like to find all the asset renditions which correspond to the search criteria.
Currently, I am using the QueryBuilder API as I am not working in Java but it seems impossible that in just one call to the AEM I manage to get the wanted rendition.
What would be the best way for searching the image renditions?
Looking forward to your ideas!
You can do with SQL2 JOIN query:
SELECT parent.* FROM [dam:AssetContent] AS parent INNER JOIN [nt:file] AS child ON ISDESCENDANTNODE(child,parent) WHERE ISDESCENDANTNODE(parent, '/content/dam') AND parent.[cq:name]='men_5.jpg' and name(child)='cq5dam.thumbnail.48.48.png'
parent looks for dam:AssetContent nodes with name=men_5.jpg and child looks for nt:file nodes under corresponding asset with nodename=cq5dam.thumbnail.48.48.png

AEM 6.2:: Search cq:Page and dam:Asset with a specific tag and ordered by jcr:created

I am trying to create PredicateGroup that would search all cq:Page and dam:Asset under respective paths. Further, these nodes are tagged with a specific tag and ordered by jcr:created.
Here is what I have written:
group.1_group.p.and=true
group.1_group.path=/content/some/path
group.1_group.type=cq:Page
group.1_group.1_property=#jcr:content/cq:tags
group.1_group.1_property.value=root:some/tag
group.p.or=true
group.2_group.p.and=true
group.2_group.path=/content/dam/some/path
group.2_group.type=dam:Asset
group.2_group.1_property=#jcr:content/metadata/cq:tags
group.2_group.1_property.value=root:some/tag
and it won't work on query debugger. I have also looked at this URL Search DAM assets and Cq pages using lastModified date | QueryBuilder and I don't know what I am doing wrong. Right now, I haven't added the condition for sorting because I was trying to go step by step.
Thanks in advance

jcr sql-2 get node name

i am working on aem 6.3 and would like to get page name
SELECT * FROM [cq:Page] WHERE ISDESCENDANTNODE("/content/Product/Silhouettes/Accessories/Bands/Headband")
If I need to retrieve name of the nodes using sql-2 , how do I achieve it?
You can specify column constraints like title, node name, etc this way -
SELECT nodeSet.name, nodeset.title
FROM [cq:Page] AS nodeSet
WHERE ISDESCENDANTNODE("/content/Product/Silhouettes/Accessories/Bands/Headband")
Note: the query tool in AEM(Tools -> Query) will not list the query results according to the columns you've mentioned, it will only list the node paths.
You can look at using /etc/importers/bulkeditor.html or AEM fiddle tool to visualize the query results based on column constraints.
If you want to achieve this programmatically, you can use the same query as you've mentioned in your question and use javax.jcr.query.* and javax.jcr.Node.* API's to retrieve just about any property from the query result. This article here should help you achieve this programatically.
Use the ResourceResolver API to execute and obtain the query results:
final Iterator<Resource> pagesIterator = resolver.findResources('<your_query_here>', javax.jcr.query.Query.JCR_SQL2);
while (pagesIterator.hasNext()) {
final Resource pageResource = pagesIterator.next();
LOG.info(pageResource.getName());
}
However, please note that if you are using any version higher then CQ 5.6, you should use instead the Page API.
In this case, the listChildren(Filter<Page> filter, boolean deep) method will do the job.
The PageFilter parameter may be used if you want to filter through pages of some kind. So if no extra criteria for your page finding algorithm, your may pass null or a new empty object.
The boolean parameter: if false it returns only direct child pages, and if true would list all the descendant pages of the given page.
Therefore, the equivalent solution of the SQL Query which would provide you the same end results would be:
Iterator<Page> rootPageIterator = rootPage.listChildren(null, true);

Search pages with a Tag in CQ5

I am working on a custom search component in CQ5. I need to search for 1 or more tags selected by user using checkboxes. I tried using an earlier query to search text (select * from cq:Pagecontent where...)
I tried using :
select * from cq:PageContent where cq:tags like '%mytag%'
but it is not working. There are 2 pages which have 'mytag' as tag.
Any suggestion on how to do it ?
The following query is working for me. I'm searching here for for the following tags marketing:interest/services and marketing:interest/product
//element(*,cq:PageContent)[#cq:tags='marketing:interest/services' or #cq:tags='marketing:interest/product']
At the moment I would still go for XPATH, because of the better performance then SQL2.
When searching for a tag I also would avoid wildcards as they are not necessary if you are searching for an exact tagname.
Wildcards can negatively influence the performance of your query.