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/