AEM查询构建器:搜索多个属性

时间:2018-07-24 19:07:06

标签: aem cq5 query-builder aem-6

如何在给定节点下列出一个以上属性的所有属性和相应值。

例如,下面的代码,我只能搜索一个属性。但是我需要搜索10个不同的属性(alttext,img,promos等),并为其获取相应的值(如果存在)。

 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);
                    }

2 个答案:

答案 0 :(得分:3)

您可以为多个属性使用数字前缀:

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");

这是documentation

答案 1 :(得分:0)

如果您有很多属性,或者在运行时生成了大量属性,则可以使用Predicate.class

您的代码:

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();

将以以下方式显示:

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();
...........

在这种情况下,AND条件将在以下属性之间创建:@property1=value1 AND @property2=value2

如果要进行OR,请创建单独的组并将其添加到根组:

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);