Expand my Community achievements bar.

SOLVED

Using multiple PredicateGroups in QueryBuilder

Avatar

Level 7

Is it possible to use multiple PredicateGroups within QueryBuilder?

e.g.

map.put("path", "/content");
map.put("type", "cq:Page");
map.put("group.p.or", "true");
map.put("group.1_fulltext", fulltextSearchTerm);
map.put("group.1_fulltext.relPath", "jcr:content");
map.put("group.2_fulltext", fulltextSearchTerm);
map.put("group.2_fulltext.relPath", "jcr:content/@cq:tags");
map.put("group.p.or", "true");
map.put("group.3_path", "/content/geometrixx/en/company/management");
map.put("group.4_path", "/content/geometrixx/en/company/bod");
...
1 Accepted Solution

Avatar

Correct answer by
Level 3

Hi Darren,

I believe your requirement should be solved by using multiple predicate groups like below..
 

PredicateGroup predicateGroup1 = null; Map<String, String> map = new HashMap<String, String>(); PredicateGroup predicateGroup2 = null; Map<String, String> map2 = new HashMap<String, String>(); map1.put("path", "/content"); map1.put("type", "cq:Page"); map1.put("group.p.or", "true"); map1.put("group.1_fulltext", fulltextSearchTerm); map1.put("group.1_fulltext.relPath", "jcr:content"); map1.put("group.2_fulltext", fulltextSearchTerm); map1.put("group.2_fulltext.relPath", "jcr:content/@cq:tags"); map2.put("group.p.or", "true"); map2.put( "group.3_path" , "/content/geometrixx/en/company/management" ); map2.put( "group.4_path" , "/content/geometrixx/en/company/bod" ); ... predicateGroup1 = PredicateGroup.create(map); predicateGroup2 = PredicateGroup.create(map2); predicateGroup1.add(predicateGroup2); ... queryBuilder.createQuery(predicateGroup1, session);

 

Hope it helps.

Regards

Vatsal

View solution in original post

4 Replies

Avatar

Correct answer by
Level 3

Hi Darren,

I believe your requirement should be solved by using multiple predicate groups like below..
 

PredicateGroup predicateGroup1 = null; Map<String, String> map = new HashMap<String, String>(); PredicateGroup predicateGroup2 = null; Map<String, String> map2 = new HashMap<String, String>(); map1.put("path", "/content"); map1.put("type", "cq:Page"); map1.put("group.p.or", "true"); map1.put("group.1_fulltext", fulltextSearchTerm); map1.put("group.1_fulltext.relPath", "jcr:content"); map1.put("group.2_fulltext", fulltextSearchTerm); map1.put("group.2_fulltext.relPath", "jcr:content/@cq:tags"); map2.put("group.p.or", "true"); map2.put( "group.3_path" , "/content/geometrixx/en/company/management" ); map2.put( "group.4_path" , "/content/geometrixx/en/company/bod" ); ... predicateGroup1 = PredicateGroup.create(map); predicateGroup2 = PredicateGroup.create(map2); predicateGroup1.add(predicateGroup2); ... queryBuilder.createQuery(predicateGroup1, session);

 

Hope it helps.

Regards

Vatsal

Avatar

Level 7

VatsalGupta wrote...

Hi Darren,

I believe your requirement should be solved by using multiple predicate groups like below..
 

  1. PredicateGroup predicateGroup1 = null;
  2. Map<String, String> map = new HashMap<String, String>();
  3. PredicateGroup predicateGroup2 = null;
  4. Map<String, String> map2 = new HashMap<String, String>();
  5.  
  6. map1.put("path", "/content");
  7. map1.put("type", "cq:Page");
  8. map1.put("group.p.or", "true");
  9. map1.put("group.1_fulltext", fulltextSearchTerm);
  10. map1.put("group.1_fulltext.relPath", "jcr:content");
  11. map1.put("group.2_fulltext", fulltextSearchTerm);
  12. map1.put("group.2_fulltext.relPath", "jcr:content/@cq:tags");
  13.  
  14. map2.put("group.p.or", "true");
  15. map2.put( "group.3_path" , "/content/geometrixx/en/company/management" );
  16. map2.put( "group.4_path" , "/content/geometrixx/en/company/bod" );
  17. ...
  18.  
  19. predicateGroup1 = PredicateGroup.create(map);
  20. predicateGroup2 = PredicateGroup.create(map2);
  21.  
  22. predicateGroup1.add(predicateGroup2);
  23. ...
  24. queryBuilder.createQuery(predicateGroup1, session);

 

Hope it helps.

Regards

Vatsal

 

Thanks Vatsal. I would not have thought you could nest PredicateGroups.

Avatar

Level 3

Your welcome.

In fact if you would want to do a bit more complex query e.g applying a NOT operation on a Predicate group, that can also be done -

predicateGroup1 = PredicateGroup.create(map); predicateGroup2 = PredicateGroup.create(map2); predicateGroup2.setNegated(true); predicateGroup1.add(predicateGroup2);

You can check more operations here -

http://dev.day.com/docs/en/cq/current/javadoc/com/day/cq/search/PredicateGroup.html

Regards

Vatsal

Avatar

Community Advisor

Is there any way, we can implement partial text search instead of fulltextsearch?