AEM query builder: search multiple properties

こ雲淡風輕ζ 提交于 2020-07-10 08:53:26

问题


How can I list all the properties and respective values for more than one property under a give node.

For example, the below code, I could search for one property only. But I need to search for 10 different property(alttext, img, promos.. etc) and get respective values(if exist) for it.

 Map<String, String> map = new HashMap<String, String>();
                    map.put(TYPE_PREDICATE, "nt:base");
                    map.put(PATH_PREDICATE, printAttachmentJsonNodePath);
                    map.put("property", "fileReference");
                    map.put("p.excerpt", "true");
                    map.put(SEARCH_LIMIT_PREDICATE, "-1");

                    Query query = queryBuilder.createQuery(PredicateGroup.create(map),
                            resourceResolver.adaptTo(Session.class));
                    SearchResult result = query.getResult();

                    for (Hit hit : result.getHits()) {
                        String path = hit.getPath();
                        Resource resourceHit = resourceResolver.getResource(path);

                            Node node = resourceHit.adaptTo(Node.class);


                            String fileReference = node.getProperty("fileReference").getString();
                            System.out.println(fileReference);
                    }

回答1:


you can use numerical prefixes for multiple properties:

map.put("1_property", "jcr:content/cq:template");
map.put("1_property.value", "/apps/geometrixx/templates/homepage");
map.put("2_property", "jcr:content/jcr:title");
map.put("2_property.value", "English");

here is documentation




回答2:


If you have a lot of properties, or amount of properties is generated during runtime you could use Predicate.class

Your code:

Map<String, String> map = new HashMap<String, String>();
map.put(TYPE_PREDICATE, "nt:base");
map.put(PATH_PREDICATE, printAttachmentJsonNodePath);
map.put("property", "fileReference");
map.put("p.excerpt", "true");
map.put(SEARCH_LIMIT_PREDICATE, "-1");

Query query = queryBuilder.createQuery(PredicateGroup.create(map),
        resourceResolver.adaptTo(Session.class));
SearchResult result = query.getResult();

Will looks in the following way:

PredicateGroup rootGroup = new PredicateGroup();
rootGroup.add(new Predicate("type").set("type", "nt:base"));
rootGroup.add(new Predicate("path").set("path", printAttachmentJsonNodePath));
rootGroup.add(new Predicate("property").set(property, "fileReference"));
rootGroup.add(new Predicate("property").set(property, "property1").set("value", value1));
.....
rootGroup.add(new Predicate("property").set(property, "propertyN").set("value", valueN));
rootGroup.set(Predicate.PARAM_EXCERPT, Boolean.TRUE.toString());
rootGroup.set(Predicate.PARAM_LIMIT, "-1");

Query query = queryBuilder.createQuery(rootGroup);
query.setHitsPerPage(limit); // also you could put limit here

SearchResult result = query.getResult();
...........

In this case AND condition will be created betwean properties : @property1=value1 AND @property2=value2

If you want OR, create separate group and add it to the root group:

PredicateGroup properties = new PredicateGroup();
properties.add(new Predicate("property").set(property, "property1").set("value", value1));
.....
properties.add(new Predicate("property").set(property, "propertyN").set("value", valueN));
properties.setAllRequired(false);

rootGroup.add(properties);


来源:https://stackoverflow.com/questions/51505913/aem-query-builder-search-multiple-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!