{"id":3382,"date":"2014-03-23T13:50:23","date_gmt":"2014-03-23T13:50:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/cq5-java-querybuilder-api-collection-of-common-programming-errors\/"},"modified":"2014-03-23T13:50:23","modified_gmt":"2014-03-23T13:50:23","slug":"cq5-java-querybuilder-api-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/cq5-java-querybuilder-api-collection-of-common-programming-errors\/","title":{"rendered":"CQ5 Java QueryBuilder API-Collection of common programming errors"},"content":{"rendered":"<p>I have seen in adobe API documentation QueryBuilder Service which can be called by URLs for many query options, this returns JSON based responses. Very well. I wan to use this in java API. I have found some examples and tried in eclipse but some thing i dont know that how to get the Service &#8220;sling&#8221; as in given code below: OR in other words how to make a QueryBuilder Java object.<\/p>\n<p>Any specific JAR i needed or i need to install Apache SLING on my PC i am not sure any one who can share this will be helpful as i am new to CQ5 and has no idea.<\/p>\n<p>The example code is as below:<\/p>\n<pre><code>        Repository repository = JcrUtils.getRepository(SERVER);\n        SimpleCredentials credentials = new SimpleCredentials(USERNAME, PASSWORD.toCharArray());\n        Session session = repository.login(credentials);\n        System.out.println(\"got session: \" + session);\n\n        \/*HERE SLING IS THE PROBLEM HOW TO GET SLING *\/  \n        QueryBuilder qbuilder = sling.getService(QueryBuilder.class);\n        String fulltextSearchTerm = \"Geometrixx\";\n\n        \/\/ create query description as hash map (simplest way, same as form\n        \/\/ post)\n        Map map = new HashMap();\n\n        \/\/ create query description as hash map (simplest way, same as form\n        \/\/ post)\n        map.put(\"path\", \"\/content\");\n        map.put(\"type\", \"cq:Page\");\n        map.put(\"group.p.or\", \"true\"); \/\/ combine this group with OR\n        map.put(\"group.1_fulltext\", fulltextSearchTerm);\n        map.put(\"group.1_fulltext.relPath\", \"jcr:content\");\n        map.put(\"group.2_fulltext\", fulltextSearchTerm);\n        map.put(\"group.2_fulltext.relPath\", \"jcr:content\/@cq:tags\");\n\n        \/\/ can be done in map or with Query methods\n        map.put(\"p.offset\", \"0\"); \/\/ same as query.setStart(0) below\n        map.put(\"p.limit\", \"20\"); \/\/ same as query.setHitsPerPage(20) below\n\n        Query query = qbuilder.createQuery(PredicateGroup.create(map), session);\n        query.setStart(0);\n        query.setHitsPerPage(20);\n\n        SearchResult result = query.getResult();\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have seen in adobe API documentation QueryBuilder Service which can be called by URLs for many query options, this returns JSON based responses. Very well. I wan to use this in java API. I have found some examples and tried in eclipse but some thing i dont know that how to get the Service [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3382","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3382"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3382\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}