0

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

例如,下面的代码,我只能搜索一个属性。但我需要搜索 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);
                    }
4

2 回答 2

4

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

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

这是文档

于 2018-07-25T02:35:43.390 回答
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();

Will 是这样看的:

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);
于 2018-07-30T08:08:28.413 回答