How to query linked documents by property

I have a custom component which should output all NewsDocument (documenttype) which have specific Category (linked documenttype). I will pass category_id as a parameter.

I found the following query constraint in documentation but it only works if Category is a Compound type. In my case Category is Document Type and linked to NewsDocument.

public void doBeforeRender() {
// …
HstQuery newsQuery = HstQueryBuilder.create(scope)
.ofTypes(NewsDocument.class)
.where(constraint(“mysite:category/@mysite:category_id”).contains(‘sport’))
.build();
HstQueryResult result = newsQuery.execute();
// …
}

If your category is a linked document, then it will be represented in your document as a uuid in a property of a subnode (or possibly as a direct property). Your query won’t work because you are looking for a property that doesn’t exist there. It’s not possible to do this with the query builder, and I’m not sure if you can even express it in xpath or sql (but it may be). In any case, I think this would turn out to be difficult to write and maintain even if it is possible and expensive to execute.

You should look into the taxonomy plugin.