Search code examples
repositorycontainshippocms

Hippo CMS search within linked beans


I have a hippo bean that contains a list of hippobeans as linkedBeans.

  @HippoEssentialsGenerated(internalName = "example:comment")
public List<HippoBean> getComment() {
    return getLinkedBeans("comment", HippoBean.class);
}

Now I want to create a query to retrieve the comments like this:

hstQuery = HstQueryBuilder.create(scope)
            .ofTypes(Article.class)
            .where(constraint("example:comment").contains("good job"))
            .build();

The idea is to retrieve all the articles such that the comments associated to them contain the string "good job". So far this query does not return anything. Is it possible to look inside the array of HippoBeans associated as LinkedBeans to another HippoBean? And if so how can this be accomplished? I have seen examples that allow searching for text in a single property, like this:

 HstQuery hstQuery = HstQueryBuilder.create(scope)
            .ofTypes(BaseDocument.class)
            .where(constraint("title").contains("Hello World"))
            .limit(pageSize)
            .offset(pageSize * (pageNum - 1))
            .orderByDescending("mynamespace:date")
            .build();

in this case the query returns only those documents such that their title contains the string "Hello World"


Solution

  • This will return all the comment documents that contain a link to the article document:

    ContentBeanUtils.createIncomingBeansQuery(article,scope,"example:comment/@hippo:docbase,Comment.class,false)
    

    The first argument is the specific instance of the article class that has been linked by several comment documents.

    For a more detailed explanation see: https://www.onehippo.org/library/concepts/search/search-all-hippodocument-beans-that-have-a-link-to-a-hippodocument-you-have.html