AEM Query

AEM Query

Two ways about Query

SQL2:

String sql = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([" + searchPath + "]) and (CONTAINS('"

+ titleProperty + "', '" + keywords + "') or CONTAINS('"

+ titleDesc + "', '" + keywords + "'))";

private QueryResult searchResult(Session session, String sql) {

QueryResult result = null;

try {

QueryManager queryManager = session.getWorkspace().getQueryManager();

Query query = queryManager.createQuery(sql, Query.JCR_SQL2);

result = query.execute();

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

 

Query:

private JSONObject getGlobalSearch(SlingHttpServletRequest request, Session session, String searchPath,

String keywords, JSONObject object){

         if(keywords.equals("")){

         keywords = "*";

         }

         else{

         keywords = "*"+keywords+"*";

         }

         String fulltextSearchTerm = keywords;

         // create query description as hash map (simplest way, same as form post)

         Map<String, String> map = new HashMap<String, String>();

      // create query description as hash map (simplest way, same as form post)

//         path=/main/path/where

//         group.1_property=myProperty

//         group.1_property.operation=exists

//         group.1_property.value=true

//         2_group.p.or=true

//         2_group.p.not=true

//         2_group.3_path=/main/path/where/first/you/donot/want/to/search

//         2_group.4_path=/main/path/where/second/you/donot/want/to/search

                      

         

         map.put("path", "/content/studyconnect/us/en");

         map.put("type", "cq:page");

         map.put("group.p.or", "true"); // combine this group with OR

         map.put("group.1_fulltext", fulltextSearchTerm);

//         map.put("group.1_fulltext.relPath", "jcr:content");

         map.put("group.1_fulltext.operation", "like");

//         map.put("group.2_fulltext", fulltextSearchTerm);

//         map.put("group.2_fulltext.relPath", "jcr:content/@cq:tags");

//         map.put("2_group.p.or", "false");

//         map.put("2_group.p.not", "true");

         

//         map.put("2_group.3_property", "jcr:createdBy");

//         map.put("2_group.3_property.value", fulltextSearchTerm);

//         map.put("2_group.3_property.operation", "not");

//         2_group.3_path=/main/path/where/first/you/donot/want/to/search

//         2_group.4_path=/main/path/where/second/you/donot/want/to/search

         

         // can be done in map or with Query methods

//         map.put("p.offset", "0"); // same as query.setStart(0) below

         map.put("p.limit", "-1");

                           

         Query query = builder.createQuery(PredicateGroup.create(map), session);

 

//         query.setStart(0);

//         query.setHitsPerPage(-1);

 

         SearchResult result = query.getResult();

         if (result != null) {

         List<JSONObject> glossaries = new ArrayList<>();

         Iterator<Node> nodes  = result.getNodes();

         try{

         while (nodes.hasNext()) {

         Node node = nodes.next();

         JSONObject obj = new JSONObject();

         obj.put("path", node.getPath());

         glossaries.add(obj);

         }

         object.put("glossaries", glossaries);

         }catch(Exception e){

         e.printStackTrace();

         }

         }

         return object;

}

fulltext does not search with system properties, Example: jcr:createdBy

参考:

https://helpx.adobe.com/experience-manager/using/using-query-builder-servlet.html

https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/adobe/acs/samples/search/querybuilder/impl/SampleQueryBuilder.java

https://gist.github.com/floriankraft/8b3720464318cd5cd9e2

https://hashimkhan.in/2017/10/09/digest-the-query-builder-api-aem-search-tips/

转载于:https://my.oschina.net/slightScenery/blog/1923163

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值