<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Search Archives - Sourced Code</title>
	<atom:link href="https://sourcedcode.com/blog/category/aem/development/search/feed" rel="self" type="application/rss+xml" />
	<link>https://sourcedcode.com</link>
	<description>AEM Blog Made Just for You.</description>
	<lastBuildDate>Wed, 28 Feb 2024 02:32:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.8</generator>

<image>
	<url>https://sourcedcode.com/wp-content/uploads/2019/09/cropped-favicon-32x32.png</url>
	<title>Search Archives - Sourced Code</title>
	<link>https://sourcedcode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Generate Custom Oak Indexes with the Oak Index Definition Generator</title>
		<link>https://sourcedcode.com/blog/aem/generate-custom-oak-indexes-with-the-oak-index-definition-generator</link>
					<comments>https://sourcedcode.com/blog/aem/generate-custom-oak-indexes-with-the-oak-index-definition-generator#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Mon, 26 Feb 2024 02:28:19 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6568</guid>

					<description><![CDATA[<p>Indexing in Adobe Experience Manager is very important. It&#8217;s not just about making things faster; it totally changes how we handle and find data online. With the Oak Index Definition Generator, developers get a really handy tool to shape how searches work behind the scenes. This tool isn&#8217;t just about making indexes, it&#8217;s about making [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/generate-custom-oak-indexes-with-the-oak-index-definition-generator">Generate Custom Oak Indexes with the Oak Index Definition Generator</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Indexing in Adobe Experience Manager is very important. It&#8217;s not just about making things faster; it totally changes how we handle and find data online. With the Oak Index Definition Generator, developers get a really handy tool to shape how searches work behind the scenes. This tool isn&#8217;t just about making indexes, it&#8217;s about making them just right for each project&#8217;s specific needs. By using this tool, developers can create indexes that fit perfectly with what they&#8217;re trying to do, making searches faster and more efficient. This smart way of indexing helps AEM websites be faster and more flexible, keeping them ahead of the game in the digital world.</p>
<div class="mention-block">
    <strong>Quick Links</strong></p>
<ol>
<li><a href="#importance_of_indexing">Importance of Indexing</a></li>
<li><a href="#creating_custom_oak_indexes">Creating Custom Oak Indexes</a></li>
<li><a href="#conclusion">Conclusion</a></li>
</ol>
</div>
<hr class="spacer-large"/>
<h2 id="importance_of_indexing">Why Indexing is Important</h2>
<p>Indexing AEM is critical for optimizing performance and managing content efficiently. The Oak Index, a key feature of AEM, improves search query speed by organizing data for quicker retrieval. This not only boosts system performance but also supports customization for specific project requirements. Efficient indexing enables scalable and responsive AEM sites, ensuring they can handle growing content without compromising on speed. Understanding and utilizing the Oak Index effectively is essential for maintaining high-performance AEM projects that meet modern digital experience demands.</p>
<hr class="spacer-large"/>
<h3 id="creating_custom_oak_indexes">Using the Oak Index Definition Generator</h3>
<p><a href="https://sourcedcode.com/wp-content/uploads/2024/02/oak-index-generator-tool-screenshot.png"><img fetchpriority="high" decoding="async" src="https://sourcedcode.com/wp-content/uploads/2024/02/oak-index-generator-tool-screenshot.png" alt="Oak Index Definition Generator" width="1486" height="898" class="alignnone size-full wp-image-6575" srcset="https://sourcedcode.com/wp-content/uploads/2024/02/oak-index-generator-tool-screenshot.png 1486w, https://sourcedcode.com/wp-content/uploads/2024/02/oak-index-generator-tool-screenshot-300x181.png 300w, https://sourcedcode.com/wp-content/uploads/2024/02/oak-index-generator-tool-screenshot-1024x619.png 1024w, https://sourcedcode.com/wp-content/uploads/2024/02/oak-index-generator-tool-screenshot-768x464.png 768w, https://sourcedcode.com/wp-content/uploads/2024/02/oak-index-generator-tool-screenshot-600x363.png 600w" sizes="(max-width: 1486px) 100vw, 1486px" /></a></p>
<p>Creating custom Oak indexes with the Oak Index Definition Generator is a straightforward process:</p>
<ol>
<li>Open the Oak Index Definition Generator at <a href="http://oakutils.appspot.com/generate/index" rel="noopener" target="_blank">http://oakutils.appspot.com/generate/index</a>.</li>
<li>Input your query in the Queries field.</li>
<li>Click &#8220;Generate&#8221; to create your custom Oak index.</li>
<li>Copy the XML into your AEM project for further adjustments.</li>
</ol>
<hr class="spacer-large"/>
<h4 id="conclusion">Conclusion</h4>
<p>It&#8217;s essential for developers to familiarize themselves with the AEM index definition structure to maximize the effectiveness of their custom indexes. Adobe provides resources such as a cheat sheet on AEM index definition structure, offering guidance on how to properly configure indexes for optimal performance.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/generate-custom-oak-indexes-with-the-oak-index-definition-generator">Generate Custom Oak Indexes with the Oak Index Definition Generator</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/generate-custom-oak-indexes-with-the-oak-index-definition-generator/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AEM 6.5.5 Not Indexing Problem *Fixed*</title>
		<link>https://sourcedcode.com/blog/aem/aem-6-5-5-not-indexing-problem-fixed</link>
					<comments>https://sourcedcode.com/blog/aem/aem-6-5-5-not-indexing-problem-fixed#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 16 Dec 2023 20:00:01 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6376</guid>

					<description><![CDATA[<p>In the course of establishing a new multitenant website, we opted to re-use the search mechanism for both site1 and site2. However, following the deployment of our code to the cloud QA environment, a significant hurdle surfaced: site2&#8217;s search consistently failed to produce results. This revelation made us to invest considerable effort in a detailed [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-6-5-5-not-indexing-problem-fixed">AEM 6.5.5 Not Indexing Problem *Fixed*</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<section>
In the course of establishing a new multitenant website, we opted to re-use the search mechanism for both site1 and site2. However, following the deployment of our code to the cloud QA environment, a significant hurdle surfaced: site2&#8217;s search consistently failed to produce results. This revelation made us to invest considerable effort in a detailed debugging journey.</p>
<p>Follow this step-by-step guide below as we transparently outline each phase of our process to resolve this issue.</p>
<div class="mention-block">
Navigating the challenges posed by the AEM 6.5.5 indexing problem, we&#8217;ve acquired invaluable insights that we believe will prove highly beneficial to the broader community. It&#8217;s crucial to emphasize that the focus of this article is specifically on the on-premises AEM 6.5.5 solution, distinct from the AEMaaCS solution.
</div>
</section>
<hr class="spacer-large"/>
<section>
<h2>Step 1: Identifying the Issue with the Query Performance Tool</h2>
<p>Our initial step involved using the query performance tool provided by AEM 6.5.5: <span class="code-highlight-secodary">/libs/granite/operations/content/diagnosistools/queryPerformance.html</span>. This tool allowed us to pinpoint the queries in use. As we focused on the search entry for site2, we discovered that the Lucene index was incorrectly utilizing the index intended for site1. This realization prompted us to proceed to the next step.</p>
</section>
<hr class="spacer-large"/>
<section>
<h2>Step 2: Updating Lucene Index Properties</h2>
<p>In the second step, we accessed <span class="code-highlight-secodary">crx/de</span> and located the Lucene index entry under <span class="code-highlight-secodary">/oak:index/*</span>. After finding the node, we updated the node properties, setting <span class="code-highlight-secodary">resync = false</span>. Monitoring the indexing logs, we confirmed a successful reindex. Despite obtaining the exact query for site2&#8217;s request, using the explain query tool revealed that the results were not as expected. Undeterred, we proceeded to the next step.</p>
</section>
<hr class="spacer-large"/>
<section>
<h2>Step 3: Deleting and Remaking %_TARGET_FOLDER_%</h2>
<p>With the issue persisting, we decided to take more decisive action in the third step. We shut down AEM, deleted <span class="code-highlight-secodary">./crx-quickstart/repository/index/%_TARGET_FOLDER_%</span>, and restarted the environment. After waiting for 5-10 minutes, the logs indicated that the <span class="code-highlight-secodary">%_TARGET_FOLDER_%</span> had been remade. Unfortunately, using the explain query tool still showed unexpected results, prompting us to move on to the next approach.</p>
</section>
<hr class="spacer-large"/>
<section>
<h2>Step 4: Complete Index Folder Deletion</h2>
<p>For the fourth step, we took a more extensive approach by shutting down AEM and deleting the entire index folder: <span class="code-highlight-secodary">./crx-quickstart/repository/index/*</span>. After restarting and patiently waiting for 10-30 minutes, the logs confirmed the successful recreation of the index folder. However, the query results were still not meeting our expectations.</p>
</section>
<hr class="spacer-large"/>
<section>
<h2>Step 5: Unconventional Solution</h2>
<p>By the fifth step, conventional options exhausted, we attempted an unconventional approach. Authorizing into <span class="code-highlight-secodary">crx/de</span>, we forcefully deleted both site1 and site2&#8217;s <span class="code-highlight-secodary">oak:index</span> rules. Subsequently, we reinstalled the rules for both sites. The result? A surprising success, with site2&#8217;s search now functioning as expected.</p>
</section>
<hr class="spacer-large"/>
<section>
<h2>Conclusion</h2>
<p>Navigating the intricacies of AEM 6.5.5&#8217;s indexing system can be challenging. However, with a systematic approach and a willingness to explore unconventional solutions, it&#8217;s possible to overcome seemingly insurmountable issues. By sharing our step-by-step journey, we hope this article proves valuable to others facing similar challenges in the realm of AEM development.</p>
</section>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-6-5-5-not-indexing-problem-fixed">AEM 6.5.5 Not Indexing Problem *Fixed*</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-6-5-5-not-indexing-problem-fixed/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JCR SQL2 ISDESCENDANTNODE Multiple Paths Example</title>
		<link>https://sourcedcode.com/blog/aem/jcr-sql2-isdescendantnode-multiple-paths-example</link>
					<comments>https://sourcedcode.com/blog/aem/jcr-sql2-isdescendantnode-multiple-paths-example#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 18 Oct 2023 02:01:56 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6203</guid>

					<description><![CDATA[<p>While working with the JCR SQL2 Queries, you may encounter scenarios where content is distributed across various branches of the repository. Querying these branches efficiently is key to implement features to capture requirements. In this article, we will focus o how to collect and query for distributed across various branches of the repository. Using the [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/jcr-sql2-isdescendantnode-multiple-paths-example">JCR SQL2 ISDESCENDANTNODE Multiple Paths Example</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>While working with the JCR SQL2 Queries, you may encounter scenarios where content is distributed across various branches of the repository. Querying these branches efficiently is key to implement features to capture requirements. In this article, we will focus o how to collect and query for distributed across various branches of the repository. Using the ISDESCENDANTNODE feature of the JCR SQL Query proves it possible.</p>
<p><strong>Understanding ISDESCENDANTNODE</strong></p>
<p>ISDESCENDANTNODE is a condition in JCR SQL2 that checks if a node is a descendant of another, based on their paths. It is particularly beneficial for querying content hierarchies. Imagine you have content in different branches (content tree) of your AEM repository. You want to search across these branches simultaneously, without running separate queries. This is where ISDESCENDANTNODE shines.</p>
<p>By specifying multiple paths within the ISDESCENDANTNODE condition, you can search for content across these paths concurrently, streamlining your queries. This powerful feature simplifies and optimizes the content retrieval process in AEM.</p>
<hr class="spacer-large" />
<p><strong>Finding all cq:Pages under 1 path</strong><br />
with the jcr:content/jcr:title CONTAINS %terms%</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">SELECT <span style="color: #339933;">*</span> FROM <span style="color: #009900;">&#91;</span>cq<span style="color: #339933;">:</span>Page<span style="color: #009900;">&#93;</span><br />
WHERE ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/us/'</span><span style="color: #009900;">&#41;</span><br />
AND CONTAINS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>jcr<span style="color: #339933;">:</span>title<span style="color: #009900;">&#93;</span>, <span style="color: #0000ff;">'terms'</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<p><strong>Finding all cq:Pages under 2 paths</strong><br />
with the jcr:content/jcr:title CONTAINS %terms%</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">SELECT <span style="color: #339933;">*</span> FROM <span style="color: #009900;">&#91;</span>cq<span style="color: #339933;">:</span>Page<span style="color: #009900;">&#93;</span><br />
WHERE <span style="color: #009900;">&#40;</span>ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/us/'</span><span style="color: #009900;">&#41;</span> <br />
OR ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/ca/'</span><span style="color: #009900;">&#41;</span> <br />
AND CONTAINS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>jcr<span style="color: #339933;">:</span>title<span style="color: #009900;">&#93;</span>, <span style="color: #0000ff;">'terms'</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<p><strong>Finding all cq:Pages under 3 paths</strong><br />
with the jcr:content/jcr:title CONTAINS %terms%</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">SELECT <span style="color: #339933;">*</span> FROM <span style="color: #009900;">&#91;</span>cq<span style="color: #339933;">:</span>Page<span style="color: #009900;">&#93;</span><br />
WHERE <span style="color: #009900;">&#40;</span>ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/us/'</span><span style="color: #009900;">&#41;</span> <br />
OR ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/ca/'</span><span style="color: #009900;">&#41;</span> <br />
OR ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/pl/'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
AND CONTAINS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>jcr<span style="color: #339933;">:</span>title<span style="color: #009900;">&#93;</span>, <span style="color: #0000ff;">'terms'</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/jcr-sql2-isdescendantnode-multiple-paths-example">JCR SQL2 ISDESCENDANTNODE Multiple Paths Example</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/jcr-sql2-isdescendantnode-multiple-paths-example/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Optimizing &#038; Debugging Search Queries, Explain Query Tool AEM</title>
		<link>https://sourcedcode.com/blog/aem/optimizing-debugging-search-queries-explain-query-tool-aem</link>
					<comments>https://sourcedcode.com/blog/aem/optimizing-debugging-search-queries-explain-query-tool-aem#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 09 Sep 2023 03:29:56 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6092</guid>

					<description><![CDATA[<p>The &#8220;Explain Query Tool&#8221; tool assists developers in understanding, optimizing, and debugging search queries, which are crucial for the AEM&#8217;s performance and accurate content delivery. Understanding the Explain Query Tool The Explain Query Tool essentially breaks down and explains the steps and processes a query goes through in AEM. Think of it as a &#8216;debug [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/optimizing-debugging-search-queries-explain-query-tool-aem">Optimizing &#038; Debugging Search Queries, Explain Query Tool AEM</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The &#8220;Explain Query Tool&#8221; tool assists developers in understanding, optimizing, and debugging search queries, which are crucial for the AEM&#8217;s performance and accurate content delivery.</p>
<hr class="spacer-larger"/>
<h2>Understanding the Explain Query Tool</h2>
<p>The Explain Query Tool essentially breaks down and explains the steps and processes a query goes through in AEM. Think of it as a &#8216;debug mode&#8217; for your search queries. It provides insights into how the system interprets the query, the indexes it leverages, the estimated cost of the query, and other essential metrics.</p>
<hr class="spacer-larger"/>
<h2>Benefits of Using the Explain Query Tool:</h2>
<ul>
<li><strong>Performance Analysis</strong>: By revealing how the system interprets and executes the query, developers can identify bottlenecks or inefficiencies.</li>
<li><strong>Better Indexing</strong>: Developers can understand which indexes are being used and optimize or create new ones accordingly.</li>
<li><strong>Debugging</strong>: It becomes easier to pinpoint issues in complex queries when developers can visualize each step.</li>
</ul>
<hr class="spacer-larger"/>
<h2>Using Explain Query Tool with Different AEM Backend Search APIs</h2>
<h3>1. SQL2:</h3>
<p>SQL2 is the SQL-like query language used in AEM, and it&#8217;s one of the primary ways developers retrieve data from the JCR (Java Content Repository).</p>
<h4>How to Use:</h4>
<p>Navigate to the AEM Query Performance Tool, and select on the &#8220;Explain Query&#8221; tab. Choose the &#8220;Language&#8221; drop down to be SQL-JCR2, and input the JCR-SQL2 query that you are trying to test. Now click on explain, and you&#8217;ll see a breakdown of the query&#8217;s interpretation and execution.</p>
<p>AEM Query Performance Tool<br />
<a href="http://localhost:4502/libs/granite/operations/content/diagnosistools/queryPerformance.html" rel="noopener" target="_blank">http://localhost:4502/libs/granite/operations/content/diagnosistools/queryPerformance.html</a></p>
<hr class="spacer-large"/>
<h3>2. Query Builder API:</h3>
<p>Query Builder API allows developers to build search queries programmatically using a more intuitive map-based syntax. It abstracts some complexities of the JCR, offering a more accessible way to retrieve content.</p>
<h4>How to Use:</h4>
<p>Navigate to the AEM Query Performance Tool, and select on the &#8220;Explain Query&#8221; tab. Choose the &#8220;Language&#8221; drop down to be Query Builder, and input your Query Builder query that you are trying to test. Now click on explain, and you&#8217;ll see a breakdown of the query&#8217;s interpretation and execution.</p>
<div class="mention-block--gold">
<strong>Note</strong><br />
For Query Builder queries, only the generated XPath is included in the explanation and result counts. For an in-depth analysis (including filtering and facets extraction), use the <a href="http://localhost:4502/libs/cq/search/content/querydebug.html" rel="noopener" target="_blank">Query Builder Debugger tool</a>.
</div>
<p>AEM Query Performance Tool<br />
<a href="http://localhost:4502/libs/granite/operations/content/diagnosistools/queryPerformance.html" rel="noopener" target="_blank">http://localhost:4502/libs/granite/operations/content/diagnosistools/queryPerformance.html</a></p>
<hr class="spacer-large"/>
<h3>3. XPath:</h3>
<p>XPath is another way of querying the JCR in AEM. While it&#8217;s less commonly used than SQL2, it can be more expressive and powerful for certain hierarchical data structures.</p>
<h4>How to Use:</h4>
<p>Navigate to the AEM Query Performance Tool, and select on the &#8220;Explain Query&#8221; tab. Choose the &#8220;Language&#8221; drop down to be XPath, and input the XPath Query that you are trying to test. Now click on explain, and you&#8217;ll see a breakdown of the query&#8217;s interpretation and execution.</p>
<p>AEM Query Performance Tool<br />
<a href="http://localhost:4502/libs/granite/operations/content/diagnosistools/queryPerformance.html" rel="noopener" target="_blank">http://localhost:4502/libs/granite/operations/content/diagnosistools/queryPerformance.html</a></p>
<hr class="spacer-larger"/>
<h2>Other Options for the AEM Query Performance Tool</h2>
<ol>
<li><strong>Include Execution Time</strong>: Execution time will be reported after running the query. Please note that long running queries will report an explanation with a delay.</li>
<li><strong>Read first page of results</strong>: The first 20 results will be read after running the query.  Please note that long running queries will report an explanation with a delay, and the overall execution will timeout after 60 seconds.</li>
<li><strong>Include Node Count</strong>: The node count is calculated by iterating the full result set of the query. Please note that long running queries will report an explanation with a delay, and the overall execution will timeout after 60 seconds.</li>
</ol>
<p>AEM Query Performance Tool<br />
<a href="http://localhost:4502/libs/granite/operations/content/diagnosistools/queryPerformance.html" rel="noopener" target="_blank">http://localhost:4502/libs/granite/operations/content/diagnosistools/queryPerformance.html</a></p>
<hr class="spacer-larger"/>
<h2>Conclusion</h2>
<p>The Explain Query Tool in AEM is a powerful ally for developers, especially those heavily invested in ensuring the performance and accuracy of their search queries. By familiarizing oneself with this tool across the various backend search APIs, developers can optimize their AEM platform for both performance and maintainability, ensuring that the content delivery is swift and relevant.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/optimizing-debugging-search-queries-explain-query-tool-aem">Optimizing &#038; Debugging Search Queries, Explain Query Tool AEM</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/optimizing-debugging-search-queries-explain-query-tool-aem/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AEM JCR SQL2 Full Text Search Case Insensitive</title>
		<link>https://sourcedcode.com/blog/aem/aem-jcr-sql2-full-text-search-case-insensitive</link>
					<comments>https://sourcedcode.com/blog/aem/aem-jcr-sql2-full-text-search-case-insensitive#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Mon, 28 Aug 2023 06:06:51 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5999</guid>

					<description><![CDATA[<p>One of the key features of AEM is its support for searching and querying content stored in its Java Content Repository (JCR). In this article, we will explore how to perform a case-insensitive full-text search using the JCR_SQL2 query language in AEM and provide a practical example of achieving this through a servlet. Performing Case-Insensitive [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-jcr-sql2-full-text-search-case-insensitive">AEM JCR SQL2 Full Text Search Case Insensitive</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>One of the key features of AEM is its support for searching and querying content stored in its Java Content Repository (JCR). In this article, we will explore how to perform a case-insensitive full-text search using the JCR_SQL2 query language in AEM and provide a practical example of achieving this through a servlet.</p>
<hr class="spacer-large"/>
<h2>Performing Case-Insensitive Full Text Search</h2>
<p>A case-insensitive full-text search is a common requirement when you want to retrieve content regardless of the letter casing used in the search query. In AEM, the JCR_SQL2 query language is used to interact with the JCR repository. To perform a case-insensitive full-text search, you can utilize the<span class="code-highlight-secondary">LOWER()</span>function to convert both the search query and the content being searched to lowercase. This ensures that the search is not case-sensitive.</p>
<hr class="spacer-larger"/>
<h2>Example Query Code</h2>
<p>Consider a scenario where you want to search for assets that are descendants of a specific path and meet certain criteria, while also performing a case-insensitive search for specific keywords. Below is an example of a JCR_SQL2 query code block that achieves this:</p>
<div class="code-1200">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONArray</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONObject</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Session</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Node</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryResult</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.RowIterator</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=GET&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/mysearchservlet&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MySearchServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Retrieve the search query parameter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryParameter <span style="color: #339933;">=</span> request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;query&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Validate if the query parameter is not null or empty</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>queryParameter <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> queryParameter.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject errorResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span>, <span style="color: #0000ff;">&quot;Missing query parameter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>errorResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Retrieve the JCR session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Session session <span style="color: #339933;">=</span> request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Session.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Define your query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM [cq:Page] AS page &quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;WHERE ISDESCENDANTNODE(page, '/content/myproject')&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp;AND (&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;LOWER(page.[jcr:content/jcr:title]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;OR LOWER(page.[jcr:content/cq:template]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;OR LOWER(page.[jcr:content/jcr:description]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp;)&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;ORDER BY [jcr:score] DESC&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the QueryManager and create the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryManager queryManager <span style="color: #339933;">=</span> session.<span style="color: #006633;">getWorkspace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getQueryManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>queryString, Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Execute the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryResult result <span style="color: #339933;">=</span> query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Process the results and build JSON response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray resultsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RowIterator rows <span style="color: #339933;">=</span> result.<span style="color: #006633;">getRows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>rows.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node pageNode <span style="color: #339933;">=</span> rows.<span style="color: #006633;">nextRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getNode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject pageData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/jcr:title&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;template&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/cq:template&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;description&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/jcr:description&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsArray.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>pageData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Respond with JSON search results</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>resultsArray.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject errorResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span>, e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>errorResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Close the JCR session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.<span style="color: #006633;">logout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<hr class="spacer-large" />
<h2>Example Try Catch</h2>
<div class="mention-block">
When utilizing service users in Java, it&#8217;s essential to incorporate try-catch blocks for exception handling. Java introduced enhanced try-catch blocks, which allow you to catch multiple exceptions using a single catch block, in version 7. Before Java 7, try-catch blocks could only handle one exception type per catch block. With the improvements in Java 7, developers gained the ability to efficiently catch and manage various types of exceptions within a structured framework. These enhanced try-catch blocks serve as a powerful means of error management, preventing application crashes, facilitating user-friendly error messages, assisting in the debugging process, and bolstering overall code reliability.</p>
<p>Moreover, when working with service users, it&#8217;s important to remember that the JCR session should always be properly closed. Failing to close the session can lead to lingering threads and resource leaks. Incorporating try-with-resources or explicit session closure ensures that resources are released and system resources are utilized optimally. This practice contributes to maintaining the stability and efficiency of your application.
</p></div>
<div class="code-1200">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolverFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONArray</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONObject</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collections</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=GET&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/mysearchservlet&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MySearchServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @<span style="color: #003399;">Reference</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ResourceResolverFactory resolverFactory<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Retrieve the search query parameter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryParameter <span style="color: #339933;">=</span> request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;query&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Validate if the query parameter is not null or empty</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>queryParameter <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> queryParameter.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject errorResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span>, <span style="color: #0000ff;">&quot;Missing query parameter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>errorResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, Object<span style="color: #339933;">&gt;</span> authInfo <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">singletonMap</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResourceResolverFactory.<span style="color: #006633;">SUBSERVICE</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sourcedCodeSystemUser&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span>ResourceResolver resourceResolver <span style="color: #339933;">=</span> resolverFactory.<span style="color: #006633;">getServiceResourceResolver</span><span style="color: #009900;">&#40;</span>authInfo<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Define your query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM [cq:Page] AS page &quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;WHERE ISDESCENDANTNODE(page, '/content/myproject')&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp;AND (&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;LOWER(page.[jcr:content/jcr:title]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;OR LOWER(page.[jcr:content/cq:template]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;OR LOWER(page.[jcr:content/jcr:description]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp;)&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;ORDER BY [jcr:score] DESC&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the QueryManager and create the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryManager queryManager <span style="color: #339933;">=</span> resourceResolver.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>QueryManager.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>queryString, Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Execute the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryResult result <span style="color: #339933;">=</span> query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Process the results and build JSON response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray resultsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RowIterator rows <span style="color: #339933;">=</span> result.<span style="color: #006633;">getRows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>rows.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node pageNode <span style="color: #339933;">=</span> rows.<span style="color: #006633;">nextRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getNode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject pageData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/jcr:title&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;template&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/cq:template&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;description&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/jcr:description&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsArray.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>pageData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Respond with JSON search results</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>resultsArray.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject errorResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span>, <span style="color: #0000ff;">&quot;An error occurred while processing the request.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>errorResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p><strong>Curl Example:</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">curl <span style="color: #0000ff;">&quot;http://localhost:4502/bin/mysearchservlet?query=my_search_keyword&quot;</span></div></td></tr></tbody></table></div>
<p><strong>Output:</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Page Title 1&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;template&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;/conf/phonak/settings/wcm/templates/mytemplate&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;This is the description of Page 1&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Page Title 2&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;template&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;/conf/phonak/settings/wcm/templates/anothertemplate&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Description for Page 2&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#93;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<p>In this example, the<span class="code-highlight-secondary">LOWER()</span>function is applied to both the properties being searched and the search query<span class="code-highlight-secondary">fulltext</span> This ensures that the search is case-insensitive, allowing you to retrieve relevant assets regardless of letter casing.</p>
<p>Practical Example: Implementing the Search in a Servlet</p>
<p>To practically implement the above query within a servlet in AEM, you would typically perform the following steps:</p>
<li>Create a Java servlet that extends<span class="code-highlight-secondary">SlingAllMethodsServlet</span>or implements<span class="code-highlight-secondary">SlingServlet</span></li>
<li>In the servlet&#8217;s<span class="code-highlight-secondary">doGet</span>or<span class="code-highlight-secondary">doPost</span>method, create an instance of the<span class="code-highlight-secondary">QueryManager</span>to execute the JCR_SQL2 query.</li>
<li>Use the query string from the example above, replacing placeholders like<span class="code-highlight-secondary">productCFsRootPath</span>and<span class="code-highlight-secondary">fulltext</span>with actual values.</li>
<li>Process the query result, extract the relevant data, and respond to the client with the search results.</li>
<hr class="spacer-large"/>
<h2>Conclusion</h2>
<p>Performing a case-insensitive full-text search using the JCR_SQL2 query language in Adobe Experience Manager allows you to provide a seamless search experience for users regardless of their choice of letter casing. By utilizing the<span class="code-highlight-secondary">LOWER()</span>function, you can effectively eliminate case sensitivity from your search queries and retrieve the most relevant content. Incorporating this technique into your AEM projects can enhance user experience and ensure that content discovery is efficient and user-friendly.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-jcr-sql2-full-text-search-case-insensitive">AEM JCR SQL2 Full Text Search Case Insensitive</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-jcr-sql2-full-text-search-case-insensitive/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JUNIT 5: JCR SQL 2 Unit Tests w/ Sling Servlet</title>
		<link>https://sourcedcode.com/blog/aem/junit-5-jcr-sql-2-unit-tests-w-sling-servlet</link>
					<comments>https://sourcedcode.com/blog/aem/junit-5-jcr-sql-2-unit-tests-w-sling-servlet#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 09 Aug 2023 12:30:05 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT5]]></category>
		<category><![CDATA[Premium Content]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5855</guid>

					<description><![CDATA[<p>I&#8217;ve dedicated quite a chunk of my personal time to crafting this blog post, and yes, I&#8217;ve rigorously tested all useful test cases that you can learn from. Why, you ask? It&#8217;s all for you, my awesome readers! I want to ensure that you not only find value in this post but also gain useful [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-5-jcr-sql-2-unit-tests-w-sling-servlet">JUNIT 5: JCR SQL 2 Unit Tests w/ Sling Servlet</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I&#8217;ve dedicated quite a chunk of my personal time to crafting this blog post, and yes, I&#8217;ve rigorously tested all useful test cases that you can learn from. Why, you ask? It&#8217;s all for you, my awesome readers! I want to ensure that you not only find value in this post but also gain useful knowledge that you can easily share with your fellow devs.</p>
<p>I&#8217;m here to make it super simple for you. This JCR SQL 2 Unit Tests w/ Sling Servlet blog post will only cover the modern approach to unit testing using JUnit 5 and the io.wcm.testing.aem-mock.junit5 library. I&#8217;m excited to show you a live example of a Servlet utilizing the Java Search, JCR SQL 2 API. By the time we&#8217;re done, you&#8217;ll be equipped with the tools to write tests that not only work but also rock!</p>
<div class="mention-block">
<strong>Quick Links</strong></p>
<ol>
<li><a href="#what-to-test-for-when-testing-a-servlet-or-osgi-service-that-utilizes-the-java-jcr-sql-2-api">What to test for when testing a Servlet or OSGI Service that utilizes the Java, JCR SQL 2 API?</a>
<ol>
<li>a. <a href="#query-string">Query String</a></li>
<li>b. <a href="#results-of-the-query-nodes">Results of the Query, Nodes</a></li>
</ol>
</li>
<li><a href="#code-examples">Code Examples</a>
<ol>
<li>a. <a href="#get-paged-by-path-servlet-java">GetPagedByPathServlet.java</a></li>
<li>b. <a href="#get-paged-by-path-servlet-test-java">GetPagedByPathServletTest.java</a></li>
</ol>
</li>
</ol>
</div>
<div class="mention-block--gold">
<strong>Pre-requirement</strong><br />
&#8211; JUnit 5<br />
&#8211; io.wcm.testing.aem-mock.junit5 library.</p>
<p>Additionally, if you want to learn more about JCR SQL 2, <a href="/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet" target="_blank" rel="noopener">click here</a> to view the blog. Or if you want to explore and learn all other existing AEM Java Search APIs, <a href="/blog/aem/a-simple-guide-to-aem-search-apis-exploring-your-options-for-powerful-search-functionality" rel="noopener" target="_blank">click here</a>.
</div>
<hr class="spacer-larger"/>
<h2 id="what-to-test-for-when-testing-a-servlet-or-osgi-service-that-utilizes-the-java-jcr-sql-2-api">1. What to test for when testing a Servlet or OSGI Service that utilizes the Java, JCR SQL 2 API?</h2>
<div style="height:50px;"></div>
<h4 id="query-string">1a. Query String</h4>
<p>In my opinion, it&#8217;s crucial to focus on two main areas. First, you should thoroughly test the <strong>query string</strong> that gets executed. This query string is like the roadmap guiding your code&#8217;s behavior. Ensuring its accuracy is essential since it forms the backbone of your code&#8217;s functionality.</p>
<h4 id="results-of-the-query-nodes">2b. Results of the Query, Nodes</h4>
<p>The second area of emphasis lies in the <strong>results of the query</strong>, which are obtained through the nodeIterator. This is where the real action happens – think of it as the hub where your code processes data. Testing this part of your code involves ensuring that the data processing via the nodeIterator is in line with your intended modifications.</p>
<p>To sum up, when testing a Servlet or OSGi Service, your primary focus should revolve around two core elements: meticulously verifying the accuracy of the query string and confirming that the data processing through the nodeIterator meets your expectations. By honing in on these aspects, you&#8217;re building a solid foundation of trust in your code.</p>
<hr class="spacer-larger"/>
<h2 id="code-examples">2. Code Examples</h2>
<div style="height:50px;"></div>
<h3 id="get-paged-by-path-servlet-java">2a. GetPagedByPathServlet.java</h3>
<p>Here in this example, you&#8217;d see a AEM Sling Servlet being created, and within doGet you&#8217;d see us utilizing the JCR SQL2 API, which captures and transforms the results into a JSON object.</p>
<div class="code-1200">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.PrintWriter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Node</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.NodeIterator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.RepositoryException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Session</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryResult</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingSafeMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gson.JsonArray</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gson.JsonObject</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> javax.<span style="color: #006633;">servlet</span>.<span style="color: #006633;">Servlet</span>.<span style="color: #000000; font-weight: bold;">class</span>, property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/getpagedbypath&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GetPagedByPathServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingSafeMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the JCR session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session session <span style="color: #339933;">=</span> request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Session.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Query statement</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT page.* FROM [cq:Page] AS page &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;INNER JOIN [cq:PageContent] AS jcrContentNode ON ISCHILDNODE(jcrContentNode, page) &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;WHERE ISDESCENDANTNODE(page, '/content/we-retail') &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;AND jcrContentNode.[cq:lastModified] &lt;= CAST('2023-01-01T00:00:00.000+00:00' AS DATE)&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create the query object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryManager queryManager <span style="color: #339933;">=</span> session.<span style="color: #006633;">getWorkspace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getQueryManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>queryString, Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Execute the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryResult result <span style="color: #339933;">=</span> query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the nodes from the query result</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeIterator nodeIterator <span style="color: #339933;">=</span> result.<span style="color: #006633;">getNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Collect the paths of the first ten pages in a list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> pagePaths <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node pageNode <span style="color: #339933;">=</span> nodeIterator.<span style="color: #006633;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> pagePath <span style="color: #339933;">=</span> pageNode.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pagePaths.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>pagePath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create a JSON object to hold the results</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JsonObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonArray resultsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JsonArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Add the paths to the JSON array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> path <span style="color: #339933;">:</span> pagePaths<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsArray.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Add the JSON array to the response object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;results&quot;</span>, resultsArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Set the response content type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Write the JSON response to the output</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">PrintWriter</span> writer <span style="color: #339933;">=</span> response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>RepositoryException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exception</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>SlingHttpServletResponse.<span style="color: #006633;">SC_INTERNAL_SERVER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error executing query: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p><strong>JSON response</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;results&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page1&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page2&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page3&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page4&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page5&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page6&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page7&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page8&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page9&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page10&quot;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>Curl call</strong></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">curl -X GET http://localhost:4502/bin/getpagedbypath</div></td></tr></tbody></table></div>
<hr class="spacer-larger"/>
<h3 id="get-paged-by-path-servlet-test-java">2b. GetPagedByPathServletTest.java</h3>
<p>Here in this unit test examples. In my opinion, what is mostly effective unit tested for the JCR SQL 2 API, and to test the query string, and next, test the query result. This example does just it. I have tested the code, and it is working as expected.</p>
<div class="code-2000">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit5.AemContext</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit5.AemContextExtension</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.BeforeEach</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.Test</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.extension.ExtendWith</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.ArgumentCaptor</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.Captor</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.Mock</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.MockitoAnnotations</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.NodeIterator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.RepositoryException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Session</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Workspace</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryResult</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.PrintWriter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.StringWriter</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">Assertions</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #339933;">*;</span><br />
<br />
@ExtendWith<span style="color: #009900;">&#40;</span>AemContextExtension.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">class</span> GetPagedByPathServletTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext context <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> GetPagedByPathServlet underTest <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GetPagedByPathServlet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SlingHttpServletRequest request<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SlingHttpServletResponse response<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> QueryManager queryManager<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Query query<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> QueryResult queryResult<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Session session<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ResourceResolver resourceResolver<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Captor<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ArgumentCaptor<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> queryCaptor<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">PrintWriter</span> printWriter<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @BeforeEach<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; MockitoAnnotations.<span style="color: #006633;">openMocks</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>QueryManager.<span style="color: #000000; font-weight: bold;">class</span>, queryManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>Query.<span style="color: #000000; font-weight: bold;">class</span>, query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>QueryResult.<span style="color: #000000; font-weight: bold;">class</span>, queryResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>resourceResolver<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>resourceResolver.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Session.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>session<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Workspace workspace <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Workspace.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>session.<span style="color: #006633;">getWorkspace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>workspace<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>workspace.<span style="color: #006633;">getQueryManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>queryManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>anyString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, eq<span style="color: #009900;">&#40;</span>Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>queryResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>printWriter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* In this test, we will only focus on the query string that is executed. This is a particularly<br />
&nbsp; &nbsp; &nbsp;* important step, because we would like to ensure that our query string is correct before<br />
&nbsp; &nbsp; &nbsp;* query.execute() is called.<br />
&nbsp; &nbsp; &nbsp;* @throws Exception<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> testDoGet_executedQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NodeIterator nodeIterator <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>NodeIterator.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>queryResult.<span style="color: #006633;">getNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>nodeIterator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest.<span style="color: #006633;">doGet</span><span style="color: #009900;">&#40;</span>request, response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Verify that the correct query string was executed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Capture the argument passed to queryManager.createQuery</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>queryManager<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>queryCaptor.<span style="color: #006633;">capture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, eq<span style="color: #009900;">&#40;</span>Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Compare the captured query string with the expected query string</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> executedQueryString <span style="color: #339933;">=</span> queryCaptor.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> expectedQueryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT page.* FROM [cq:Page] AS page &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;INNER JOIN [cq:PageContent] AS jcrContentNode ON ISCHILDNODE(jcrContentNode, page) &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;WHERE ISDESCENDANTNODE(page, '/content/we-retail') &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;AND jcrContentNode.[cq:lastModified] &lt;= CAST('2023-01-01T00:00:00.000+00:00' AS DATE)&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expectedQueryString, executedQueryString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* In this test, we are actually going to mock the response from query.execute() and verify<br />
&nbsp; &nbsp; &nbsp;* that the results has been processed, and the output of the servlet is correct. Here you<br />
&nbsp; &nbsp; &nbsp;* can see us mocking the NodeIterator, and then mocking the next node calls.<br />
&nbsp; &nbsp; &nbsp;* @throws Exception<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> testDoGet_successfulJsonResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NodeIterator nodeIterator <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>NodeIterator.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span>, <span style="color: #000066; font-weight: bold;">true</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>mock<span style="color: #009900;">&#40;</span>javax.<span style="color: #006633;">jcr</span>.<span style="color: #006633;">Node</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/we-retail/page1&quot;</span>, <span style="color: #0000ff;">&quot;/content/we-retail/page2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>queryResult.<span style="color: #006633;">getNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>nodeIterator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest.<span style="color: #006633;">doGet</span><span style="color: #009900;">&#40;</span>request, response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> expectedOutput <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;{&quot;</span>results<span style="color: #0000ff;">&quot;:[&quot;</span><span style="color: #339933;">/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<span style="color: #339933;">/</span>page1<span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<span style="color: #339933;">/</span>page2<span style="color: #0000ff;">&quot;]}&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; printWriter.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>expectedOutput<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* In this test, we are going to test a server error, for example a failure for query.execute(),<br />
&nbsp; &nbsp; &nbsp;* and verify that the response status is set to 500, and the error message is printed.<br />
&nbsp; &nbsp; &nbsp;* @throws Exception<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> testDoGet_serverError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>anyString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, eq<span style="color: #009900;">&#40;</span>Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenThrow</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RepositoryException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test exception&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">StringWriter</span> stringWriter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">PrintWriter</span> printWriter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PrintWriter</span><span style="color: #009900;">&#40;</span>stringWriter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>printWriter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest.<span style="color: #006633;">doGet</span><span style="color: #009900;">&#40;</span>request, response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Verify that the response status is set to 500</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>SlingHttpServletResponse.<span style="color: #006633;">SC_INTERNAL_SERVER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Verify that the error message is printed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> expectedErrorMessage <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Error executing query: Test exception<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expectedErrorMessage, stringWriter.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-5-jcr-sql-2-unit-tests-w-sling-servlet">JUNIT 5: JCR SQL 2 Unit Tests w/ Sling Servlet</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/junit-5-jcr-sql-2-unit-tests-w-sling-servlet/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Search in AEM</title>
		<link>https://sourcedcode.com/blog/aem/search-in-aem</link>
					<comments>https://sourcedcode.com/blog/aem/search-in-aem#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Fri, 04 Aug 2023 05:32:45 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5676</guid>

					<description><![CDATA[<p>This comprehensive guide offers AEM developers like you a roadmap to implement powerful search functionality using AEM Search APIs. Unravel the potential of three core AEM Search APIs: XPath Search API, JCR SQL2 Search API, and Query Builder Search API. Each API brings unique features to the table, and we&#8217;ll guide you through their capabilities [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/search-in-aem">Search in AEM</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This comprehensive guide offers AEM developers like you a roadmap to implement powerful search functionality using AEM Search APIs.</p>
<p>Unravel the potential of three core AEM Search APIs: XPath Search API, JCR SQL2 Search API, and Query Builder Search API. Each API brings unique features to the table, and we&#8217;ll guide you through their capabilities to help you make the right choice for your projects.</p>
<p>To ensure a seamless learning experience, we present practical Java examples for each API. These real-world scenarios will solidify your understanding and enable you to apply the concepts with confidence.</p>
<p>Ready to supercharge your AEM development skills? Dive into the world of AEM Search APIs and embark on a journey to enhance your content search capabilities. Join us in &#8220;Search in AEM &#8211; A Simple Guide to AEM Search APIs: Exploring Your Options for Powerful Search Functionality.&#8221;</p>
<p>Read the full article here: <a href="https://sourcedcode.com/blog/aem/a-simple-guide-to-aem-search-apis-exploring-your-options-for-powerful-search-functionality">Search in AEM &#8211; A Simple Guide to AEM Search APIs: Exploring Your Options for Powerful Search Functionality</a>.</p>
<p>Unlock the true potential of AEM&#8217;s search capabilities and elevate your projects to new heights!</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/search-in-aem">Search in AEM</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/search-in-aem/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Query Builder API Exclude Pages or Paths</title>
		<link>https://sourcedcode.com/blog/aem/query-builder-api-exclude-pages-or-paths</link>
					<comments>https://sourcedcode.com/blog/aem/query-builder-api-exclude-pages-or-paths#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Fri, 04 Aug 2023 05:01:29 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5652</guid>

					<description><![CDATA[<p>This blog article focuses on searching cq:Page under a particular path, but exclude sub pages. In this article we will share you the Query Builder API&#8217;s Query, and share with you the Java code implementation of a servlet. At the end of this article, you should be able to search cq:Page nodes under a particular [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/query-builder-api-exclude-pages-or-paths">Query Builder API Exclude Pages or Paths</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This blog article focuses on searching cq:Page under a particular path, but exclude sub pages. In this article we will share you the Query Builder API&#8217;s Query, and share with you the Java code implementation of a servlet. At the end of this article, you should be able to search cq:Page nodes under a particular path, but exclude particular resources under some paths, under the sub tree.</p>
<hr class="spacer-larger"/>
<h3>Query Builder API&#8217;s Query:</h3>
<p>Find all cq:Page nodes under /content/my-site/us/en<br />
<strong>NOT</strong> cq:Page nodes under /content/my-site/us/en/services<br />
<strong>NOT</strong> cq:Page nodes under /content/my-site/us/en/products</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">path<span style="color: #339933;">=/</span>content<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>site<span style="color: #339933;">/</span>us<span style="color: #339933;">/</span>en<br />
type<span style="color: #339933;">=</span>cq<span style="color: #339933;">:</span>Page<br />
<br />
group.1_group.<span style="color: #006633;">p</span>.<span style="color: #006633;">not</span><span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">true</span><br />
group.1_group.<span style="color: #006633;">path</span><span style="color: #339933;">=/</span>content<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>site<span style="color: #339933;">/</span>us<span style="color: #339933;">/</span>en<span style="color: #339933;">/</span>services<br />
group.1_group.<span style="color: #006633;">path</span>.<span style="color: #006633;">self</span><span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">true</span><br />
<br />
group.2_group.<span style="color: #006633;">p</span>.<span style="color: #006633;">not</span><span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">true</span><br />
group.2_group.<span style="color: #006633;">path</span><span style="color: #339933;">=/</span>content<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>site<span style="color: #339933;">/</span>us<span style="color: #339933;">/</span>en<span style="color: #339933;">/</span>products<br />
group.2_group.<span style="color: #006633;">path</span>.<span style="color: #006633;">self</span><span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">true</span></div></td></tr></tbody></table></div>
<p><strong>XPath Output:</strong><br />
<a href="https://sourcedcode.com/wp-content/uploads/2023/08/x-path-query.png"><img decoding="async" width="2032" height="422" src="https://sourcedcode.com/wp-content/uploads/2023/08/x-path-query.png" alt="XPath Output" class="alignnone size-full wp-image-5662" srcset="https://sourcedcode.com/wp-content/uploads/2023/08/x-path-query.png 2032w, https://sourcedcode.com/wp-content/uploads/2023/08/x-path-query-300x62.png 300w, https://sourcedcode.com/wp-content/uploads/2023/08/x-path-query-1024x213.png 1024w, https://sourcedcode.com/wp-content/uploads/2023/08/x-path-query-768x159.png 768w, https://sourcedcode.com/wp-content/uploads/2023/08/x-path-query-1536x319.png 1536w, https://sourcedcode.com/wp-content/uploads/2023/08/x-path-query-600x125.png 600w" sizes="(max-width: 2032px) 100vw, 2032px" /></a></p>
<hr class="spacer-larger"/>
<h3>Query Builder API Servlet Example:</h3>
<div class="code-750">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.query.QueryBuilder</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; service <span style="color: #339933;">=</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span>,<br />
&nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/getPages&quot;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GetPagesServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> ITEMS_PER_PAGE <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> pageNumber <span style="color: #339933;">=</span> getPageNumber<span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pageNumber&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> contentPath <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/content/my-site/us/en&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Root path for the pages.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create the predicate using a Map</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;</span> predicateMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; predicateMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path&quot;</span>, contentPath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; predicateMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;type&quot;</span>, <span style="color: #0000ff;">&quot;cq:Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Add additional predicate groups if needed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; predicateMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;group.1_group.p.not&quot;</span>, <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; predicateMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;group.1_group.path&quot;</span>, contentPath <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/services&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; predicateMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;group.1_group.path.self&quot;</span>, <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; predicateMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;group.2_group.p.not&quot;</span>, <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; predicateMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;group.2_group.path&quot;</span>, contentPath <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/products&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; predicateMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;group.2_group.path.self&quot;</span>, <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> paths <span style="color: #339933;">=</span> getPathsForPage<span style="color: #009900;">&#40;</span>contentPath, pageNumber, predicateMap<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Convert the list of paths to JSON using Gson</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> jsonResponse <span style="color: #339933;">=</span> convertListToJson<span style="color: #009900;">&#40;</span>paths<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Set the JSON response in the HTTP response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonResponse<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> getPathsForPage<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> contentPath, <span style="color: #000066; font-weight: bold;">int</span> pageNumber, Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;</span> predicateMap<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Use QueryBuilder to retrieve pages based on the given predicates.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> paths <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> startIndex <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>pageNumber <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> ITEMS_PER_PAGE<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; QueryBuilder queryBuilder <span style="color: #339933;">=</span> getResourceResolver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>QueryBuilder.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>queryBuilder <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Set the limit and offset for pagination</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; predicateMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;p.limit&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ITEMS_PER_PAGE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; predicateMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;p.offset&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>startIndex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryBuilder.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>predicateMap<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterator<span style="color: #339933;">&lt;</span>Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, Object<span style="color: #339933;">&gt;&gt;</span> result <span style="color: #339933;">=</span> query.<span style="color: #006633;">getResult</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getHits</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Fetch the paths for the current page</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, Object<span style="color: #339933;">&gt;</span> hit <span style="color: #339933;">=</span> result.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> path <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> hit.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; paths.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> paths<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Rest of the methods remain the same as in the previous version.</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// getPageNumber, convertListToJson, etc.</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p><strong>Curl Command</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">curl <span style="color: #339933;">-</span>X GET <span style="color: #0000ff;">&quot;http://localhost:4502/bin/getPages?pageNumber=1&quot;</span></div></td></tr></tbody></table></div>
<p><strong>JSON Output</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;">&#91;</span><br />
&nbsp; <span style="color: #0000ff;">&quot;/content/my-site/us/en/item-1&quot;</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;/content/my-site/us/en/item-2&quot;</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;/content/my-site/us/en/item-3&quot;</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;/content/my-site/us/en/item-4&quot;</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;/content/my-site/us/en/item-5&quot;</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;/content/my-site/us/en/item-6&quot;</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;/content/my-site/us/en/item-7&quot;</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;/content/my-site/us/en/item-8&quot;</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;/content/my-site/us/en/item-9&quot;</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;/content/my-site/us/en/item-10&quot;</span><br />
<span style="color: #009900;">&#93;</span></div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/query-builder-api-exclude-pages-or-paths">Query Builder API Exclude Pages or Paths</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/query-builder-api-exclude-pages-or-paths/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Why Indexing in AEM is so important for Search</title>
		<link>https://sourcedcode.com/blog/aem/why-indexing-in-aem-is-so-important-for-search</link>
					<comments>https://sourcedcode.com/blog/aem/why-indexing-in-aem-is-so-important-for-search#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 29 Jul 2023 03:27:33 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5362</guid>

					<description><![CDATA[<p>As we are developers implement search, I would like to share the importance of indexing in AEM. I would like to express that this is a simple post, but enough information to get you to understand the importance of indexing. Continue and read the article below to see the significance &#038; Importance of Indexing in [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/why-indexing-in-aem-is-so-important-for-search">Why Indexing in AEM is so important for Search</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>As we are developers implement search, I would like to share the importance of indexing in AEM. I would like to express that this is a simple post, but enough information to get you to understand the importance of indexing. Continue and read the article below to see the significance &#038; Importance of Indexing in AEM.</p>
<ol>
<li><strong>Improved Search Performance:</strong> Indexing enables rapid content searches, even in large repositories, improving search speed and efficiency.</li>
<li><strong>Enhanced User Experience:</strong> Quick and relevant search results lead to a seamless and satisfying user experience.</li>
<li><strong>Efficient Content Retrieval:</strong> Indexing enables swift access to specific content items, streamlining content management.</li>
<li><strong>Scalability and Performance:</strong> Indexing supports AEM&#8217;s scalability while maintaining high performance levels.</li>
<li><strong>Complex Querying Support:</strong> Indexing efficiently handles complex search queries with multiple filters and conditions.</li>
</ol>
<hr class="spacer-larger" />
<h2>Best Practices for Indexing</h2>
<ul>
<li><strong>Regular Updates:</strong> Schedule frequent index updates to reflect the latest content changes.</li>
<li><strong>Monitor Index Health:</strong> Keep track of index size and performance metrics to identify issues.</li>
<li><strong>Use Index Filters:</strong> Optimize the index&#8217;s size and relevancy by defining specific filters.</li>
<li><strong>Configure Indexing Properties:</strong> Properly configure indexing settings based on content nature and usage patterns.</li>
</ul>
<hr class="spacer-larger" />
<h2>Conclusion</h2>
<p>Indexing is a critical component of AEM, ensuring efficient content retrieval and enhanced user experiences. By following indexing best practices, organizations can fully unlock the potential of AEM for seamless content management and delivery.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/why-indexing-in-aem-is-so-important-for-search">Why Indexing in AEM is so important for Search</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/why-indexing-in-aem-is-so-important-for-search/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AEM Effective Pagination with Query Builder API</title>
		<link>https://sourcedcode.com/blog/aem/aem-effective-pagination-with-query-builder-api</link>
					<comments>https://sourcedcode.com/blog/aem/aem-effective-pagination-with-query-builder-api#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 26 Jul 2023 03:23:47 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5359</guid>

					<description><![CDATA[<p>There are many ways to implement pagination for AEM content. Effectively choosing the right solution will indeed Bu decrease development time, and promote a performant experience to your end users. AEM has multiple search API&#8217;s like xPath, JCR SQL2, Node JCR, Query Builder, etc&#8230; But in my opinion, the Query Builder API would be the [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-effective-pagination-with-query-builder-api">AEM Effective Pagination with Query Builder API</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>There are many ways to implement pagination for AEM content. Effectively choosing the right solution will indeed Bu decrease development time, and promote a performant experience to your end users. AEM has multiple search API&#8217;s like xPath, JCR SQL2, Node JCR, Query Builder, etc&#8230; But in my opinion, the Query Builder API would be the most effective decision when building a solution with pagination. In AEM, the Query Builder API has emerged as the preferred solution for creating pagination experiences. In this article, we will explore why the Query Builder API stands out as the go-to choice for pagination in AEM.</p>
<div class="mention-block">
<strong>Quick Links</strong></p>
<ol>
<li><a href="#understanding-pagination-in-aem">1. Understanding Pagination in AEM</a></li>
<li><a href="#the-query-builder-api-an-overview">2. The Query Builder API: An Overview</a></li>
<li>
        <a href="#benefits-of-the-query-builder-api-for-pagination">3. Benefits of the Query Builder API for Pagination</a></p>
<ul>
<li><a href="#performance-optimization">3a. Performance Optimization</a></li>
<li><a href="#flexibility-and-customization">3b. Flexibility and Customization</a></li>
<li><a href="#support-for-dynamic-content">3c. Support for Dynamic Content</a></li>
<li><a href="#facilitating-search-functionality">3d. Facilitating Search Functionality</a></li>
</ul>
</li>
<li>
        <a href="#pagination-and-more-features-with-query-builder-api">4. Pagination and More Features with Query Builder API</a></p>
<ul>
<li><a href="#offset-and-limit">4a.Offset and Limit</a></li>
<li><a href="#sorting">4b.Sorting</a></li>
<li><a href="#search-filters">4c.Search Filters</a></li>
<li><a href="#query-aggregation">4d.Query Aggregation</a></li>
<li><a href="#content-pre-fetching">4e. Content Pre-fetching</a></li>
</ul>
</li>
<li><a href="#a-simple-servlet-for-querying-pages-in-java">A Simple Servlet for Querying Pages in Java</a></li>
<li><a href="#conclusion">Conclusion</a></li>
</ol>
</div>
<hr class="spacer-larger"/>
<h2 id="understanding-pagination-in-aem">1. Understanding Pagination in AEM</h2>
<p>What is pagination? In general, pagination is a process that divides large sets of content into smaller, more manageable sections, commonly referred to as &#8220;pages&#8221;; but however, with AEM, anything can be paginated. By doing so, users can navigate through content easily, without being presented with an exhaustive list all at once. In AEM, pagination is particularly crucial for content-heavy websites or applications, ensuring that visitors can access desired information quickly. You can probably imagine, when the page first loads, you can see the first 10 results, and clicking on &#8220;next&#8221;, you are able to see the next 10 results, and so on&#8230;</p>
<hr class="spacer-larger"/>
<h2 id="the-query-builder-api-an-overview">2. The Query Builder API: An Overview</h2>
<p>The Query Builder API is a powerful tool within AEM that allows developers to construct complex queries to retrieve content from the AEM repository. It is designed to provide a more flexible and efficient way of searching and fetching content than traditional JCR-SQL2 queries. The Query Builder API abstracts the underlying technology and provides an easy-to-use interface to interact with the AEM repository.</p>
<hr class="spacer-larger"/>
<h2 id="benefits-of-the-query-builder-api-for-pagination">3. Benefits of the Query Builder API for Pagination</h2>
<h3 id="performance-optimization">3a. Performance Optimization</h3>
<p>One of the primary reasons the Query Builder API is preferred for pagination in AEM is its ability to optimize performance. When dealing with large repositories, traditional queries can be resource-intensive and slow, impacting the page loading time and user experience. The Query Builder API, on the other hand, leverages optimized query execution, resulting in faster response times and enhanced page performance.</p>
<h3 id="flexibility-and-customization">3b. Flexibility and Customization</h3>
<p>The Query Builder API offers developers a wide range of parameters and options to fine-tune their queries. This level of flexibility enables developers to customize the pagination logic based on specific use cases. Whether it&#8217;s setting the number of items per page, defining sorting criteria, or filtering results, the Query Builder API empowers developers to tailor pagination experiences according to project requirements.</p>
<h3 id="support-for-dynamic-content">3c. Support for Dynamic Content</h3>
<p>In dynamic environments where content changes frequently, pagination needs to adapt to reflect these updates accurately. The Query Builder API can seamlessly handle dynamic content by providing real-time results based on the most recent data in the AEM repository. This ensures that the user always receives the latest content, maintaining an up-to-date and engaging user experience.</p>
<h3 id="facilitating-search-functionality">3d. Facilitating Search Functionality</h3>
<p>Pagination is often used in conjunction with search functionality to improve content discovery. The Query Builder API&#8217;s capability to perform complex searches across various content attributes makes it a natural fit for integrating search with pagination. By combining these features, users can easily find and navigate to the exact content they need.</p>
<hr class="spacer-larger"/>
<h2 id="pagination-and-more-features-with-query-builder-api">4. Pagination and More Features with Query Builder API</h2>
<h3 id="offset-and-limit">4a. Offset and Limit</h3>
<p>The Query Builder API allows developers to define the starting point (offset) and the maximum number of items (limit) to fetch for each page. This enables seamless navigation between pages, allowing users to access content beyond the first page of results.</p>
<h3 id="sorting">4b. Sorting</h3>
<p>To enhance user experience, pagination often goes hand in hand with sorting. The Query Builder API allows developers to specify sorting criteria based on content attributes. For example, you can sort content by date, popularity, or any other relevant property to ensure a logical order for the paginated results.</p>
<h3 id="search-filters">4c. Search Filters</h3>
<p>Incorporating search filters along with pagination empowers users to refine their search queries and find specific content efficiently. The Query Builder API enables developers to apply various filters and constraints to narrow down the results, creating a more personalized experience for users.</p>
<h3 id="query-aggregation">4d. Query Aggregation</h3>
<p>The Query Builder API also supports query aggregation, which enables developers to group results based on specific attributes. This feature is particularly useful when presenting aggregated data, such as categories or tags, in a paginated manner.</p>
<h3 id="content-pre-fetching">4e. Content Pre-fetching</h3>
<p>To further optimize user experience, the Query Builder API allows developers to implement content pre-fetching. By fetching additional content in the background, users experience faster loading times when navigating between pages, reducing any perceived delays.</p>
<hr class="spacer-larger"/>
<h2 id="a-simple-servlet-for-querying-pages-in-java">5. A Simple Servlet for Querying Pages in Java</h2>
<p>Below is an example of a simple Java servlet that uses the Query Builder API to query pages from the AEM repository for pagination purposes:</p>
<div class="code-750">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.propertytypes.ServiceRanking</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Reference</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.PredicateGroup</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.QueryBuilder</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.result.SearchResult</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span>,<br />
&nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=GET&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/findPagePaths&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.extensions=json&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
@ServiceRanking<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1001</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Adjust the service ranking as per your requirement</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PaginatedPagesServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @<span style="color: #003399;">Reference</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> QueryBuilder queryBuilder<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get request parameters for search and pagination</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> searchTerm <span style="color: #339933;">=</span> request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;q&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> currentPage <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> pageSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Number of items per page</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Calculate the offset to start retrieving items</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> offset <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>currentPage <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> pageSize<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Build the query map for the QueryBuilder API</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;</span> queryMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; queryMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path&quot;</span>, <span style="color: #0000ff;">&quot;/content&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Replace with your desired root path</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; queryMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;type&quot;</span>, <span style="color: #0000ff;">&quot;cq:Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; queryMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fulltext.relPath&quot;</span>, <span style="color: #0000ff;">&quot;jcr:content&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; queryMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fulltext&quot;</span>, searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Use the &quot;q&quot; parameter for search terms</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; queryMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;p.offset&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>offset<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; queryMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;p.limit&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>pageSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Execute the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryBuilder.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>PredicateGroup.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>queryMap<span style="color: #009900;">&#41;</span>, request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Session.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">SearchResult</span> result <span style="color: #339933;">=</span> query.<span style="color: #006633;">getResult</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the paths of the pages from the search result using GJSON</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JSONArray pathsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; result.<span style="color: #006633;">getHits</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000; font-weight: bold;">forEach</span><span style="color: #009900;">&#40;</span>hit <span style="color: #339933;">-&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> path <span style="color: #339933;">=</span> hit.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pathsArray.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Send the paginated content (paths of the pages) to the response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>pathsArray.<span style="color: #006633;">toJSONString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p>Curl</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"># Replace <span style="color: #0000ff;">&quot;http://your_server&quot;</span> with the base <span style="color: #003399;">URL</span> of your AEM instance.<br />
# Replace <span style="color: #0000ff;">&quot;/bin/findPagePaths?q=search_term&amp;page=1&quot;</span> with the path to your servlet,<br />
# the desired search term <span style="color: #009900;">&#40;</span>search_term<span style="color: #009900;">&#41;</span>, and the desired page number.<br />
<br />
# Search with <span style="color: #0000ff;">&quot;your_search_term&quot;</span> and get results <span style="color: #000000; font-weight: bold;">for</span> Page <span style="color: #cc66cc;">1</span><br />
curl <span style="color: #339933;">-</span>X GET <span style="color: #0000ff;">&quot;http://your_server/bin/findPagePaths?q=your_search_term&amp;page=1&quot;</span><br />
<br />
# Search with <span style="color: #0000ff;">&quot;another_search_term&quot;</span> and get results <span style="color: #000000; font-weight: bold;">for</span> Page <span style="color: #cc66cc;">2</span><br />
curl <span style="color: #339933;">-</span>X GET <span style="color: #0000ff;">&quot;http://your_server/bin/findPagePaths?q=another_search_term&amp;page=2&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-larger"/>
<h2 id="conclusion">5. Conclusion</h2>
<p>In conclusion, the Query Builder API in Adobe Experience Manager is a versatile and powerful tool that provides an ideal solution for implementing pagination experiences. With its performance optimization, flexibility, and support for dynamic content, the Query Builder API ensures that users can navigate through content-rich websites or applications seamlessly.</p>
<p>By incorporating features like offset and limit, sorting, search filters, query aggregation, and content pre-fetching, developers can create highly customizable and efficient pagination experiences for their AEM projects. Additionally, the provided Java servlet example demonstrates how the Query Builder API can be used to retrieve paginated content from the AEM repository. As AEM continues to evolve, leveraging the Query Builder API will remain essential for maintaining an excellent user experience and keeping users engaged with your content.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-effective-pagination-with-query-builder-api">AEM Effective Pagination with Query Builder API</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-effective-pagination-with-query-builder-api/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>A Simple Guide to AEM Search APIs: Exploring Your Options for Powerful Search Functionality</title>
		<link>https://sourcedcode.com/blog/aem/a-simple-guide-to-aem-search-apis-exploring-your-options-for-powerful-search-functionality</link>
					<comments>https://sourcedcode.com/blog/aem/a-simple-guide-to-aem-search-apis-exploring-your-options-for-powerful-search-functionality#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 01 Jul 2023 04:44:35 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5330</guid>

					<description><![CDATA[<p>As an AEM developer, the ability and skill to identify solutions and implementations to quickly and efficiently search through vast amounts of content is crucial. Understanding the all the AEM platform&#8217;s search API capabilities will ensure you are a effective developer, when implementing solutions for AEM search. In this article, we will explore the various [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/a-simple-guide-to-aem-search-apis-exploring-your-options-for-powerful-search-functionality">A Simple Guide to AEM Search APIs: Exploring Your Options for Powerful Search Functionality</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>As an AEM developer, the ability and skill to identify solutions and implementations to quickly and efficiently search through vast amounts of content is crucial. Understanding the all the AEM platform&#8217;s search API capabilities will ensure you are a effective developer, when implementing solutions for AEM search. In this article, we will explore the various options provided by AEM Search APIs, enabling you to make informed decisions while implementing search capabilities within your AEM projects. Throughout each search API topic, we will provide code examples as well, so you know exactly what to expect.</p>
<div class="mention-block">
<strong>Quick Links</strong></p>
<ol>
<li><a href="#xpath-search-api">XPath Search API</a></li>
<li><a href="#jcr-sql2-search-api">JCR SQL2 Search API</a></li>
<li><a href="#query-builder-search-api">Query Builder Search API</a></li>
</ol>
</div>
<hr class="spacer-larger"/>
<h2 id="xpath-search-api">1. XPath Search API</h2>
<p>XPath is a query language that allows you to navigate through the nodes and elements of an XML document. In the context of AEM, XPath queries can be used to search for content within the JCR (Java Content Repository) repository. While XPath is a powerful and flexible query language, it is essential to use it judiciously, as complex queries can have performance implications.</p>
<p><strong>Some Benefits and Features:</strong></p>
<ol>
<li>XPath Queries: XPath is a versatile query language that enables you to navigate through XML documents and locate specific nodes or elements within the content repository.</li>
<li>Flexibility: XPath queries offer high flexibility and allow you to search content based on various criteria, making it suitable for complex search requirements.</li>
<li>Node Structure: XPath queries rely on the hierarchical structure of nodes, making it well-suited for navigating and filtering content with a fixed structure.</li>
<li>Rich Expressions: With XPath, you can employ a wide range of expressions, such as text matching, attribute filtering, and range-based searches.<br />
Dynamic Content: XPath can handle dynamic content, but developers should be cautious with complex queries as they may impact performance.</li>
</ol>
<p><strong>JAVA Example:</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Node</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.NodeIterator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryResult</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Assuming you have access to the JCR Session object (session)</span><br />
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; QueryManager queryManager <span style="color: #339933;">=</span> session.<span style="color: #006633;">getWorkspace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getQueryManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span> xpathQuery <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/jcr:root/content//*[@jcr:contains(., 'searchKeyword')]&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>xpathQuery, Query.<span style="color: #006633;">XPATH</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; QueryResult result <span style="color: #339933;">=</span> query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; NodeIterator nodeIterator <span style="color: #339933;">=</span> result.<span style="color: #006633;">getNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Node node <span style="color: #339933;">=</span> nodeIterator.<span style="color: #006633;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Process the node that matches the search criteria</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>node.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exceptions</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-larger"/>
<h2 id="jcr-sql2-search-api">2. JCR SQL2 Search API</h2>
<p>JCR SQL2 is a structured query language designed specifically for the Java Content Repository. It provides an SQL-like syntax for querying content stored in the repository. JCR SQL2 queries are generally more straightforward to use compared to XPath and are often preferred for basic search requirements due to their familiarity to developers familiar with SQL.</p>
<p><strong>Some Benefits and Features:</strong></p>
<ol>
<li>SQL-Like Syntax: JCR SQL2 provides a structured query language with an SQL-like syntax, making it more familiar to developers experienced in working with relational databases.</li>
<li>Simplicity: JCR SQL2 queries are generally more straightforward to use compared to XPath, especially for developers already familiar with SQL.</li>
<li>Dynamic Queries: JCR SQL2 supports dynamic queries, allowing you to parameterize search criteria and adapt to changing requirements.</li>
<li>Performance Optimization: When used appropriately, JCR SQL2 can offer better performance for complex queries that involve multiple content nodes and properties.</li>
<li>Full-Text Search: JCR SQL2 supports full-text search using the CONTAINS function, enabling text-based searches on content.</li>
</ol>
<p><strong>JAVA Example:</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Node</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.NodeIterator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryResult</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Assuming you have access to the JCR Session object (session)</span><br />
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; QueryManager queryManager <span style="color: #339933;">=</span> session.<span style="color: #006633;">getWorkspace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getQueryManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span> sql2Query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM [nt:unstructured] AS node WHERE CONTAINS(node.*, 'searchKeyword')&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>sql2Query, Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; QueryResult result <span style="color: #339933;">=</span> query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; NodeIterator nodeIterator <span style="color: #339933;">=</span> result.<span style="color: #006633;">getNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Node node <span style="color: #339933;">=</span> nodeIterator.<span style="color: #006633;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Process the node that matches the search criteria</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>node.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exceptions</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-larger"/>
<h2 id="query-builder-search-api">3. Query Builder Search API</h2>
<p>The Query Builder API is a high-level API provided by AEM to build and execute queries against the repository. It acts as a wrapper around the underlying query languages (XPath or JCR SQL2) and provides a more intuitive and developer-friendly interface for constructing complex queries. The Query Builder API abstracts away the complexity of query languages and is recommended for most search scenarios in AEM.</p>
<p><strong>Some Benefits and Features:</strong></p>
<ol>
<li>High-Level API: The Query Builder API is a powerful and high-level abstraction for constructing and executing queries against the repository.</li>
<li>Developer-Friendly: The Query Builder API provides a more intuitive interface compared to raw query languages, making it easier for developers to build complex queries.</li>
<li>Abstraction of Complexity: The API abstracts away the underlying query language, making it more accessible to developers without extensive knowledge of XPath or JCR SQL2.</li>
<li>Faceted Search: The Query Builder API supports faceted search, enabling users to filter search results based on predefined categories or facets.</li>
<li>Spell Checker and Suggestions: The API includes built-in support for spell checking and real-time suggestions, improving the search experience for users.</li>
<li>Great for Pagination: The Query Builder API offers built-in pagination support, allowing you to retrieve search results in manageable chunks. This is particularly useful when dealing with large result sets, as it enhances performance and reduces memory consumption.</li>
</ol>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.PredicateGroup</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.QueryBuilder</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Assuming you have access to the ResourceResolver object (resourceResolver)</span><br />
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; QueryBuilder queryBuilder <span style="color: #339933;">=</span> resourceResolver.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>QueryBuilder.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span> searchKeyword <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;searchKeyword&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Building the predicate for the query</span><br />
&nbsp; &nbsp; PredicateGroup predicateGroup <span style="color: #339933;">=</span> PredicateGroup.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;group.1_property&quot;</span>, <span style="color: #0000ff;">&quot;jcr:content/@jcr:title&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;group.1_property.operation&quot;</span>, <span style="color: #0000ff;">&quot;fulltext&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;group.1_property.value&quot;</span>, searchKeyword<br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryBuilder.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>predicateGroup, resourceResolver.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Session.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; com.<span style="color: #006633;">day</span>.<span style="color: #006633;">cq</span>.<span style="color: #006633;">search</span>.<span style="color: #006633;">result</span>.<span style="color: #003399;">SearchResult</span> result <span style="color: #339933;">=</span> query.<span style="color: #006633;">getResult</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>com.<span style="color: #006633;">day</span>.<span style="color: #006633;">cq</span>.<span style="color: #006633;">search</span>.<span style="color: #006633;">result</span>.<span style="color: #006633;">Hit</span> hit <span style="color: #339933;">:</span> result.<span style="color: #006633;">getHits</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> path <span style="color: #339933;">=</span> hit.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Process the hit node that matches the search criteria</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exceptions</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/a-simple-guide-to-aem-search-apis-exploring-your-options-for-powerful-search-functionality">A Simple Guide to AEM Search APIs: Exploring Your Options for Powerful Search Functionality</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/a-simple-guide-to-aem-search-apis-exploring-your-options-for-powerful-search-functionality/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Find all AEM Pages with Specific Page Properties with Query Builder API</title>
		<link>https://sourcedcode.com/blog/aem/find-all-aem-pages-with-specific-page-properties-with-query-builder-api</link>
					<comments>https://sourcedcode.com/blog/aem/find-all-aem-pages-with-specific-page-properties-with-query-builder-api#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sun, 21 Aug 2022 16:23:16 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3707</guid>

					<description><![CDATA[<p>The Query Builder is a great tool that allows us to search for nodes in the JCR. We often as AEM developers use this tool to build a query for us to use in the backend to build query descriptions (predicates); the set of predicates produced will call the Predicate Evaluator which knows how to [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/find-all-aem-pages-with-specific-page-properties-with-query-builder-api">Find all AEM Pages with Specific Page Properties with Query Builder API</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The Query Builder is a great tool that allows us to search for nodes in the JCR. We often as AEM developers use this tool to build a query for us to use in the backend to build query descriptions (predicates); the set of predicates produced will call the Predicate Evaluator which knows how to handle that specific predicate for XPath, filtering, and facet extraction.</p>
<p>In this article, we will be sharing query examples to find all AEM pages with specific page page properties using the<strong> Query Builder Debugger</strong> tool.</p>
<hr class="spacer-large" />
<h2>1. Find all AEM pages with a single page property</h2>
<p>Find all pages under /content/we-retail,<br />
and has page properties set with cq:productMaster == &#8216;/var/commerce/products/we-retail/wo/pants/faba_running_pants&#8217;<br />
show max results.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">path<span style="color: #339933;">=/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<br />
type<span style="color: #339933;">=</span>cq<span style="color: #339933;">:</span>Page<br />
property<span style="color: #339933;">=</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>cq<span style="color: #339933;">:</span>productMaster<br />
property.<span style="color: #006633;">value</span><span style="color: #339933;">=</span> <br />
<span style="color: #339933;">/</span>var<span style="color: #339933;">/</span>commerce<span style="color: #339933;">/</span>products<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<span style="color: #339933;">/</span>wo<span style="color: #339933;">/</span>pants<span style="color: #339933;">/</span>faba_running_pants<br />
p.<span style="color: #006633;">limit</span><span style="color: #339933;">=-</span><span style="color: #cc66cc;">1</span></div></td></tr></tbody></table></div>
<p><a href="http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Atype%3Dcq%3APage%0D%0Aproperty%3Djcr%3Acontent%2Fcq%3AproductMaster%0D%0Aproperty.value%3D%09%0D%0A%2Fvar%2Fcommerce%2Fproducts%2Fwe-retail%2Fwo%2Fpants%2Ffaba_running_pants%0D%0Ap.limit%3D-1" rel="noopener" target="_blank">http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Atype%3Dcq%3APage%0D%0Aproperty%3Djcr%3Acontent%2Fcq%3AproductMaster%0D%0Aproperty.value%3D%09%0D%0A%2Fvar%2Fcommerce%2Fproducts%2Fwe-retail%2Fwo%2Fpants%2Ffaba_running_pants%0D%0Ap.limit%3D-1</a></p>
<h3>Example Code</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.Resource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolverFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Reference</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.QueryBuilder</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.result.SearchResult</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.result.Hit</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.Query</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONArray</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONObject</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collections</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; service <span style="color: #339933;">=</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span>,<br />
&nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/queryservlet&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=GET&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> QueryServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @<span style="color: #003399;">Reference</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ResourceResolverFactory resolverFactory<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @<span style="color: #003399;">Reference</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> QueryBuilder queryBuilder<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, Object<span style="color: #339933;">&gt;</span> authInfo <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">singletonMap</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResourceResolverFactory.<span style="color: #006633;">SUBSERVICE</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sourcedCodeSystemUser&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span>ResourceResolver resourceResolver <span style="color: #339933;">=</span> resolverFactory.<span style="color: #006633;">getServiceResourceResolver</span><span style="color: #009900;">&#40;</span>authInfo<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryPath <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/content/we-retail&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryType <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;cq:Page&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryProperty <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jcr:content/cq:productMaster&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryPropertyValue <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/var/commerce/products/we-retail/wo/pants/faba_running_pants&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;</span> queryParams <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path&quot;</span>, queryPath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;type&quot;</span>, queryType<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;property&quot;</span>, queryProperty<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;property.value&quot;</span>, queryPropertyValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryBuilder.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>PredicateGroup.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>queryParams<span style="color: #009900;">&#41;</span>, resourceResolver.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>javax.<span style="color: #006633;">jcr</span>.<span style="color: #006633;">Session</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">SearchResult</span> result <span style="color: #339933;">=</span> query.<span style="color: #006633;">getResult</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray resultsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Hit hit <span style="color: #339933;">:</span> result.<span style="color: #006633;">getHits</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Resource resource <span style="color: #339933;">=</span> hit.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject pageData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path&quot;</span>, resource.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsArray.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>pageData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;results&quot;</span>, resultsArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject errorResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span>, <span style="color: #0000ff;">&quot;An error occurred while processing the request.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>errorResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>Curl Example</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">curl <span style="color: #339933;">-</span>X GET http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//localhost:4502/bin/queryservlet</span></div></td></tr></tbody></table></div>
<p><strong>Output</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">&quot;results&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;path&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;/content/we-retail/en&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;path&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;/content/we-retail/de&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ... more paths</span><br />
&nbsp; <span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h2>2. Find all AEM pages with multiple page properties</h2>
<p>Find all pages under /content/we-retail,<br />
and has page properties set with cq:productMaster == &#8216;/var/commerce/products/we-retail/wo/pants/faba_running_pants&#8217;<br />
and has page properties set with cq:template == &#8216;/conf/we-retail/settings/wcm/templates/product-page&#8217;<br />
show max results.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">path<span style="color: #339933;">=/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<br />
type<span style="color: #339933;">=</span>cq<span style="color: #339933;">:</span>Page<br />
path<span style="color: #339933;">=/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<br />
type<span style="color: #339933;">=</span>cq<span style="color: #339933;">:</span>Page<br />
group.1_property<span style="color: #339933;">=</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>cq<span style="color: #339933;">:</span>productMaster<br />
group.1_property.<span style="color: #006633;">value</span><span style="color: #339933;">=</span> <br />
<span style="color: #339933;">/</span>var<span style="color: #339933;">/</span>commerce<span style="color: #339933;">/</span>products<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<span style="color: #339933;">/</span>wo<span style="color: #339933;">/</span>pants<span style="color: #339933;">/</span>faba_running_pants<br />
group.2_property<span style="color: #339933;">=</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>cq<span style="color: #339933;">:</span>template<br />
group.2_property.<span style="color: #006633;">value</span><span style="color: #339933;">=</span> <br />
<span style="color: #339933;">/</span>conf<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<span style="color: #339933;">/</span>settings<span style="color: #339933;">/</span>wcm<span style="color: #339933;">/</span>templates<span style="color: #339933;">/</span>product<span style="color: #339933;">-</span>page<br />
p.<span style="color: #006633;">limit</span><span style="color: #339933;">=-</span><span style="color: #cc66cc;">1</span></div></td></tr></tbody></table></div>
<p><a href="http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Atype%3Dcq%3APage%0D%0Agroup.1_property%3Djcr%3Acontent%2Fcq%3AproductMaster%0D%0Agroup.1_property.value%3D%09%0D%0A%2Fvar%2Fcommerce%2Fproducts%2Fwe-retail%2Fwo%2Fpants%2Ffaba_running_pants%0D%0Agroup.2_property%3Djcr%3Acontent%2Fcq%3Atemplate%0D%0Agroup.2_property.value%3D%09%0D%0A%2Fconf%2Fwe-retail%2Fsettings%2Fwcm%2Ftemplates%2Fproduct-page%0D%0Ap.limit%3D-1" rel="noopener" target="_blank">http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Atype%3Dcq%3APage%0D%0Agroup.1_property%3Djcr%3Acontent%2Fcq%3AproductMaster%0D%0Agroup.1_property.value%3D%09%0D%0A%2Fvar%2Fcommerce%2Fproducts%2Fwe-retail%2Fwo%2Fpants%2Ffaba_running_pants%0D%0Agroup.2_property%3Djcr%3Acontent%2Fcq%3Atemplate%0D%0Agroup.2_property.value%3D%09%0D%0A%2Fconf%2Fwe-retail%2Fsettings%2Fwcm%2Ftemplates%2Fproduct-page%0D%0Ap.limit%3D-1</a></p>
<h3>Example Code</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.Resource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolverFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Reference</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.QueryBuilder</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.result.SearchResult</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.result.Hit</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.search.Query</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONArray</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONObject</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collections</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; service <span style="color: #339933;">=</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span>,<br />
&nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/queryservlet&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=GET&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> QueryServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @<span style="color: #003399;">Reference</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ResourceResolverFactory resolverFactory<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @<span style="color: #003399;">Reference</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> QueryBuilder queryBuilder<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, Object<span style="color: #339933;">&gt;</span> authInfo <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">singletonMap</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResourceResolverFactory.<span style="color: #006633;">SUBSERVICE</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sourcedCodeSystemUser&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span>ResourceResolver resourceResolver <span style="color: #339933;">=</span> resolverFactory.<span style="color: #006633;">getServiceResourceResolver</span><span style="color: #009900;">&#40;</span>authInfo<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;</span> queryParams <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path&quot;</span>, <span style="color: #0000ff;">&quot;/content/we-retail&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;type&quot;</span>, <span style="color: #0000ff;">&quot;cq:Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;group.1_property&quot;</span>, <span style="color: #0000ff;">&quot;jcr:content/cq:productMaster&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;group.1_property.value&quot;</span>, <span style="color: #0000ff;">&quot;/var/commerce/products/we-retail/wo/pants/faba_running_pants&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;group.2_property&quot;</span>, <span style="color: #0000ff;">&quot;jcr:content/cq:template&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;group.2_property.value&quot;</span>, <span style="color: #0000ff;">&quot;/conf/we-retail/settings/wcm/templates/product-page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;p.limit&quot;</span>, <span style="color: #0000ff;">&quot;-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryBuilder.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>PredicateGroup.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>queryParams<span style="color: #009900;">&#41;</span>, resourceResolver.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>javax.<span style="color: #006633;">jcr</span>.<span style="color: #006633;">Session</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">SearchResult</span> result <span style="color: #339933;">=</span> query.<span style="color: #006633;">getResult</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray resultsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Hit hit <span style="color: #339933;">:</span> result.<span style="color: #006633;">getHits</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Resource resource <span style="color: #339933;">=</span> hit.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject pageData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path&quot;</span>, resource.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsArray.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>pageData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;results&quot;</span>, resultsArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject errorResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span>, <span style="color: #0000ff;">&quot;An error occurred while processing the request.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>errorResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="mention-block"><strong>For other ways to find AEM pages on AEM using the query builder</strong>, checkout this article -><br />
<a href="/blog/aem/find-all-aem-pages-with-tags-with-query-builder" rel="noopener" target="_blank">Find all AEM Pages with Tags with Query Builder API</a>
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/find-all-aem-pages-with-specific-page-properties-with-query-builder-api">Find all AEM Pages with Specific Page Properties with Query Builder API</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/find-all-aem-pages-with-specific-page-properties-with-query-builder-api/feed</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Find all AEM Pages with Tags with Query Builder API</title>
		<link>https://sourcedcode.com/blog/aem/find-all-aem-pages-with-tags-with-query-builder</link>
					<comments>https://sourcedcode.com/blog/aem/find-all-aem-pages-with-tags-with-query-builder#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Tue, 25 Jan 2022 23:31:17 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Short Post]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3414</guid>

					<description><![CDATA[<p>The Query Builder is a great tool that allows us to search for nodes in the JCR. We often as AEM developers use this tool to build a query for us to use in the backend to build query descriptions (predicates); the set of predicates produced will call the Predicate Evaluator which knows how to [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/find-all-aem-pages-with-tags-with-query-builder">Find all AEM Pages with Tags with Query Builder API</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The Query Builder is a great tool that allows us to search for nodes in the JCR. We often as AEM developers use this tool to build a query for us to use in the backend to build query descriptions (predicates); the set of predicates produced will call the Predicate Evaluator which knows how to handle that specific predicate for XPath, filtering, and facet extraction.</p>
<p>In this article, we will be sharing query examples to find all AEM pages with single-tag &#038;&#038; multi-tags using the<strong> Query Builder Debugger</strong> tool.</p>
<hr class="spacer-large" />
<h2>1. Find all AEM pages with a single cq:tag</h2>
<p>Find all pages under /content/we-retail,<br />
and has the tag of we-retail:apparel/pants,<br />
show max results.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">path<span style="color: #339933;">=/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<br />
type<span style="color: #339933;">=</span>cq<span style="color: #339933;">:</span>Page<br />
property<span style="color: #339933;">=</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>@cq<span style="color: #339933;">:</span>tags<br />
property.<span style="color: #006633;">value</span><span style="color: #339933;">=</span>we<span style="color: #339933;">-</span>retail<span style="color: #339933;">:</span>apparel<span style="color: #339933;">/</span>pants<br />
p.<span style="color: #006633;">limit</span><span style="color: #339933;">=-</span><span style="color: #cc66cc;">1</span></div></td></tr></tbody></table></div>
<p><a href="http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Atype%3Dcq%3APage%0D%0Aproperty%3Djcr%3Acontent%2F%40cq%3Atags%0D%0Aproperty.value%3Dwe-retail%3Aapparel%2Fpants%0D%0A%0D%0A" rel="noopener" target="_blank">http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Atype%3Dcq%3APage%0D%0Aproperty%3Djcr%3Acontent%2F%40cq%3Atags%0D%0Aproperty.value%3Dwe-retail%3Aapparel%2Fpants%0D%0A%0D%0A</a></p>
<hr class="spacer-large" />
<h2>1. Find all AEM pages with multiple cq:tags</h2>
<p>Find all pages under /content/we-retail, a<br />
and<br />
((has the tag of we-retail:apparel/pants) or<br />
(has the tag of we-retail:apparel/pants)),<br />
show max results.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">path<span style="color: #339933;">=/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<br />
type<span style="color: #339933;">=</span>cq<span style="color: #339933;">:</span>Page<br />
group.1_property<span style="color: #339933;">=</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>@cq<span style="color: #339933;">:</span>tags<br />
group.1_property.<span style="color: #006633;">value</span><span style="color: #339933;">=</span>we<span style="color: #339933;">-</span>retail<span style="color: #339933;">:</span>apparel<span style="color: #339933;">/</span>pants<br />
group.2_property<span style="color: #339933;">=</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>@cq<span style="color: #339933;">:</span>tags<br />
group.2_property.<span style="color: #006633;">value</span><span style="color: #339933;">=</span>we<span style="color: #339933;">-</span>retail<span style="color: #339933;">:</span>apparel<span style="color: #339933;">/</span>shirt<br />
group.<span style="color: #006633;">p</span>.<span style="color: #006633;">or</span><span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">true</span><br />
p.<span style="color: #006633;">limit</span><span style="color: #339933;">=-</span><span style="color: #cc66cc;">1</span></div></td></tr></tbody></table></div>
<p><a href="http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Atype%3Dcq%3APage%0D%0Agroup.1_property%3Djcr%3Acontent%2F%40cq%3Atags%0D%0Agroup.1_property.value%3Dwe-retail%3Aapparel%2Fpants%0D%0Agroup.2_property%3Djcr%3Acontent%2F%40cq%3Atags%0D%0Agroup.2_property.value%3Dwe-retail%3Aapparel%2Fshirt%0D%0Agroup.p.or%3Dtrue%0D%0Ap.limit%3D-1%0D%0A%0D%0A%0D%0A" rel="noopener" target="_blank">http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Atype%3Dcq%3APage%0D%0Agroup.1_property%3Djcr%3Acontent%2F%40cq%3Atags%0D%0Agroup.1_property.value%3Dwe-retail%3Aapparel%2Fpants%0D%0Agroup.2_property%3Djcr%3Acontent%2F%40cq%3Atags%0D%0Agroup.2_property.value%3Dwe-retail%3Aapparel%2Fshirt%0D%0Agroup.p.or%3Dtrue%0D%0Ap.limit%3D-1%0D%0A%0D%0A%0D%0A</a></p>
<hr class="spacer-large" />
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/find-all-aem-pages-with-tags-with-query-builder">Find all AEM Pages with Tags with Query Builder API</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/find-all-aem-pages-with-tags-with-query-builder/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Find AEM JCR Nodes with Full Text Search</title>
		<link>https://sourcedcode.com/blog/aem/find-aem-jcr-nodes-with-full-text-search</link>
					<comments>https://sourcedcode.com/blog/aem/find-aem-jcr-nodes-with-full-text-search#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 12 Jan 2022 02:07:03 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3108</guid>

					<description><![CDATA[<p>Every AEM developer knows that a JCR node consists of properties and values. Whenever a component is configured or whenever page properties are set, the value is saved in the JCR node as properties and values. There are times when we would like to search all the JCR node properties, to match the value with [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/find-aem-jcr-nodes-with-full-text-search">Find AEM JCR Nodes with Full Text Search</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Every AEM developer knows that a JCR node consists of properties and values. Whenever a component is configured or whenever page properties are set, the value is saved in the JCR node as properties and values. </p>
<p>There are times when we would like to search all the JCR node properties, to match the value with some kind of string. We can do this with fulltext search in AEM. This action is most useful when trying to find unused components in the code.</p>
<p>When performing a fulltext search, please be aware that you will be using Jackrabbit Oak, Apache Lucene. You will search via the Apache Lucene indexes, cached in the JCR. Update and Delete operations in the repository are followed by the Lucene reindexing.</p>
<p>In this article, we will cover 2 ways of performing a full-text search, with examples.</p>
<div class="mention-block">
<strong>Quick Links</strong></p>
<ol>
<li><a href="#fulltext-search-AEM_Query_Builder">AEM FullText Search with the Query Builder</a></li>
<li><a href="#fulltext-search-JCR_SQL2">AEM FullText Search with the JCR_SQL2</a></li>
</ol>
</div>
<hr class="spacer-larger"/>
<h3 id="fulltext-search-AEM_Query_Builder">1. AEM FullText Search with the Query Builder</h3>
<p><strong>Example 1: In this example we will find all [nt:base] JCR nodes matching any %pants%</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Search <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">:</span><br />
anything under <span style="color: #0000ff;">&quot;/content/we-retail&quot;</span><br />
and any.<span style="color: #006633;">properties</span> with values LIKE <span style="color: #0000ff;">&quot;%pants%&quot;</span><br />
and show me more than <span style="color: #cc66cc;">10</span> results.<br />
<br />
<span style="color: #339933;">-------------</span><br />
<br />
http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//localhost:4502/libs/cq/search/content/querydebug.html</span><br />
<br />
path<span style="color: #339933;">=/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<br />
fulltext<span style="color: #339933;">=</span>pants<br />
p.<span style="color: #006633;">limit</span><span style="color: #339933;">=-</span><span style="color: #cc66cc;">1</span></div></td></tr></tbody></table></div>
<p>Visit localhost: <a href="http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Afulltext%3Dpants%0D%0Ap.limit%3D-1" rel="noopener" target="_blank">http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Afulltext%3Dpants%0D%0Ap.limit%3D-1</a></p>
<hr class="spacer-large"/>
<p><strong>Example 2: In this example we will find all [cq:Page] JCR nodes matching any %weretail/components/structure/page%</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Search <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">:</span><br />
anything under <span style="color: #0000ff;">&quot;/content/we-retail&quot;</span><br />
and only <span style="color: #0000ff;">&quot;cq:Page&quot;</span> nodes<br />
and any.<span style="color: #006633;">properties</span> with values LIKE <span style="color: #0000ff;">&quot;%weretail/components/structure/page%&quot;</span><br />
and show me more than <span style="color: #cc66cc;">10</span> results.<br />
<br />
<span style="color: #339933;">-------------</span><br />
<br />
http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//localhost:4502/libs/cq/search/content/querydebug.html</span><br />
<br />
path<span style="color: #339933;">=/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<br />
type<span style="color: #339933;">=</span>cq<span style="color: #339933;">:</span>Page<br />
fulltext<span style="color: #339933;">=</span>weretail<span style="color: #339933;">/</span>components<span style="color: #339933;">/</span>structure<span style="color: #339933;">/</span>page<br />
p.<span style="color: #006633;">limit</span><span style="color: #339933;">=-</span><span style="color: #cc66cc;">1</span></div></td></tr></tbody></table></div>
<p>Visit localhost: <a href="http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Atype%3Dcq%3APage%0D%0Afulltext%3Dweretail%2Fcomponents%2Fstructure%2Fpage%0D%0Ap.limit%3D-1" rel="noopener" target="_blank">http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&#038;query=path%3D%2Fcontent%2Fwe-retail%0D%0Atype%3Dcq%3APage%0D%0Afulltext%3Dweretail%2Fcomponents%2Fstructure%2Fpage%0D%0Ap.limit%3D-1</a></p>
<hr class="spacer-larger"/>
<h3 id="fulltext-search-JCR_SQL2">2. AEM FullText Search with the JCR_SQL2</h3>
<div class="mention-block">
For a quick review on how to query using the JCR SQL console from CRX/DE, click on each step below.<br />
<a class="foobox" href="https://sourcedcode.com/wp-content/uploads/2022/01/1.how-to-aem-jcr-sql2.png" data-caption-desc="Step 1. Visit CRX/DE, from the CRX/DE main navigation, Tools &gt; Query." alt="Step 1. Visit CRX/DE, from the CRX/DE main navigation, Tools &gt; Query.">Step 1. Visit CRX/DE, from the CRX/DE main navigation, Tools &gt; Query.</a><br />
<a class="foobox" href="https://sourcedcode.com/wp-content/uploads/2022/01/2.how-to-aem-jcr-sql2.png" data-caption-desc="Step 2. From the Query Console, change the path to JCR_SQL2." alt="Step 2. From the Query Console, change the path to JCR_SQL2.">Step 2. From the Query Console, change the path to JCR_SQL2.</a><br />
<a class="foobox" href="https://sourcedcode.com/wp-content/uploads/2022/01/3.how-to-aem-jcr-sql2.png" data-caption-desc="Step 3. Insert your JCR_SQL2 query, and click on Execute. The results should be shown." alt="Step 3. Insert your JCR_SQL2 query, and click on Execute. The results should be shown.">Step 3. Insert your JCR_SQL2 query, and click on Execute. The results should be shown.</a>
</div>
<p class="spacer-large">
<p><strong>Example 1: In this example we will find all [nt:base] JCR nodes matching any %pants%</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Search <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">:</span><br />
anything under <span style="color: #0000ff;">&quot;/content/we-retail&quot;</span><br />
and any.<span style="color: #006633;">properties</span> with values LIKE <span style="color: #0000ff;">&quot;%pants%&quot;</span><br />
and show me more than <span style="color: #cc66cc;">10</span> results.<br />
<br />
<span style="color: #339933;">-------------</span><br />
<br />
http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//localhost:4502/crx/de/index.jsp</span><br />
<br />
SELECT <span style="color: #339933;">*</span> FROM <span style="color: #009900;">&#91;</span>nt<span style="color: #339933;">:</span>base<span style="color: #009900;">&#93;</span> AS nodes <br />
WHERE ISDESCENDANTNODE <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
AND CONTAINS<span style="color: #009900;">&#40;</span>nodes .<span style="color: #339933;">*</span>, <span style="color: #0000ff;">'pants'</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>Visit localhost: <a href="http://localhost:4502/crx/de/index.jsp" rel="noopener" target="_blank">http://localhost:4502/crx/de/index.jsp</a></p>
<hr class="spacer-large"/>
<p><strong>Example 2: In this example we will find all [cq:Page] JCR nodes matching any %weretail/components/structure/page%</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Search <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">:</span><br />
anything under <span style="color: #0000ff;">&quot;/content/we-retail&quot;</span><br />
and only <span style="color: #0000ff;">&quot;cq:Page&quot;</span> nodes<br />
and any.<span style="color: #006633;">properties</span> with values LIKE <span style="color: #0000ff;">&quot;%weretail/components/structure/page%&quot;</span><br />
and show me more than <span style="color: #cc66cc;">10</span> results.<br />
<br />
<span style="color: #339933;">-------------</span><br />
<br />
http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//localhost:4502/libs/cq/search/content/querydebug.html</span><br />
<br />
SELECT <span style="color: #339933;">*</span> FROM <span style="color: #009900;">&#91;</span>cq<span style="color: #339933;">:</span>Page<span style="color: #009900;">&#93;</span> AS nodes <br />
WHERE ISDESCENDANTNODE <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
AND CONTAINS<span style="color: #009900;">&#40;</span>nodes .<span style="color: #339933;">*</span>, <span style="color: #0000ff;">'weretail/components/structure/page'</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>Visit localhost: <a href="http://localhost:4502/crx/de/index.jsp" rel="noopener" target="_blank">http://localhost:4502/crx/de/index.jsp</a></p>
<hr class="spacer"/>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/find-aem-jcr-nodes-with-full-text-search">Find AEM JCR Nodes with Full Text Search</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/find-aem-jcr-nodes-with-full-text-search/feed</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>How to use AEM JCR SQL2 query strings to query for nodes in Java Content Repository</title>
		<link>https://sourcedcode.com/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet</link>
					<comments>https://sourcedcode.com/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Mon, 02 Apr 2018 19:31:06 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Quick Reference]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<category><![CDATA[Video Tutorial]]></category>
		<guid isPermaLink="false">http://sourcedcode.com/?p=82</guid>

					<description><![CDATA[<p>JCR-SQL2 (Java Content Repository &#8211; Structured Query Language 2) is domain specific language used to query JCR nodes held in the JCR repository. The syntax for JCR-SQL2 is fairly similar to SQL, so if you have some experience with SQL, this tutorial will be a walk in the park for you. The best way to [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet">How to use AEM JCR SQL2 query strings to query for nodes in Java Content Repository</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>JCR-SQL2 (Java Content Repository &#8211; Structured Query Language 2) is domain specific language used to query JCR nodes held in the JCR repository. The syntax for JCR-SQL2 is fairly similar to SQL, so if you have some experience with SQL, this tutorial will be a walk in the park for you.</p>
<p>The best way to learn the power of the JCR-SQL2 language is to try it out on CRX/DE Lite. Without future or do, below will be a lesson on JCR-SQL2. To locate CRX/DE Lite&#8217;s query tool, visit <a href="http://localhost:4502/crx/de/index.jsp" rel="noopener noreferrer" target="_blank">http://localhost:4502/crx/de/index.jsp</a>, then select on the tools drop down, and choose query.</p>
<div class="mention-block--gold">
<h3>2023 Added Section: SQL-JCR2 Snippets</h3>
<p>Examples can be found at the bottom of this tutorial, <a href="#jcr-sql2-code-snippets-cheat-sheet">JCR-SQL2 Code Snippets Cheat Sheet</a>, where you can find some examples for real life use cases. Head into your <a href="http://localhost:4502/crx/de/index.jsp" rel="noopener" target="_blank">crx-de > tools > query > type:JCR SQL2</a>, and try it our yourself.
</div>
<p><img decoding="async" src="http://sourcedcode.com/wp-content/uploads/2018/04/tools-query-tool-1024x662.png" alt="" width="1024" height="662" class="alignnone size-large wp-image-167" srcset="https://sourcedcode.com/wp-content/uploads/2018/04/tools-query-tool-1024x662.png 1024w, https://sourcedcode.com/wp-content/uploads/2018/04/tools-query-tool-300x194.png 300w, https://sourcedcode.com/wp-content/uploads/2018/04/tools-query-tool-768x496.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<hr />
<h2>JCR-SQL2 Syntax</h2>
<ol>
<li><a href="#select-statement">SELECT Statement</a></li>
<li><a href="#name-statement">NAME() Statement</a></li>
<li><a href="#isdescendantnode-statement">ISDESCENDANTNODE Statement</a></li>
<li><a href="#contains-operator">CONTAINS Statement</a></li>
<li><a href="#like-operator">LIKE Operator</a></li>
<li><a href="#is-not-null">IS NOT NULL Property</a></li>
<li><a href="#order-by-keyword">ORDER BY Keyword</a></li>
<li><a href="#cast-statement">CAST() Statement</a></li>
<li><a href="#jcr-sql2-code-snippets-cheat-sheet">JCR-SQL2 Code Snippets Cheat Sheet</a> (new 2023)</li>
<li><a href="#jcr-sql2-java-code-example-servlet">JCR SQL2 Java Code Example Servlet</a> (new 2023)</li>
</ol>
<p><iframe loading="lazy" width="560" height="315" src="https://www.youtube.com/embed/0sMMzJs1dpc" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<hr class="spacer-large" />
<h4 id="select-statement">1. The JCR-SQL2 SELECT Statement</h4>
<ul>
<li>The SELECT statement is used to select all JCR nodes that matches the JCR node&#8217;s primary type.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- nt:base is a primary type that represents every single node in the JCR.</span><br />
<span style="color: #808080; font-style: italic;">-- returns all nodes in the JCR.</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:Page] nodes</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [dam:Asset] nodes</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>dam:Asset<span style="color: #FF00FF;">&#93;</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="8658919803"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<hr class="spacer-large" />
<h4 id="name-statement">2. The JCR-SQL2 NAME() Statement</h4>
<ul>
<li>Selecting nodes with a specific node name.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all [cq:Page] nodes WHERE the node's name is equal to &quot;we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> NAME<span style="color: #FF00FF;">&#40;</span>nodes<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;we-retail&quot;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [dam:Asset] nodes WHERE the node's name is equal to &quot;we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>dam:Asset<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> NAME<span style="color: #FF00FF;">&#40;</span>nodes<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;we-retail&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h4 id="isdescendantnode-statement">3. The JCR-SQL2 ISDESCENDANTNODE Statement</h4>
<ul>
<li>Selecting nodes under a file path.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all nodes WHERE nodes exist under &quot;/content/we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">select</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:Page] nodes WHERE nodes exist under &quot;/content/we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [dam:Asset] nodes WHERE nodes exist under &quot;/content/dam/we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>dam:Asset<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>dam<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [nt:unstructured] nodes WHERE nodes exist under &quot;/content/we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:unstructured<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h4 id="contains-statement">4. The JCR-SQL2 CONTAINS Statement</h4>
<ul>
<li>Selecting nodes where the properties contain a value.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all nodes WHERE node's property jcr:title CONTAINS &quot;we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #00CC00;">CONTAINS</span><span style="color: #FF00FF;">&#40;</span>nodes.title<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:Page] nodes where node's property jcr:title CONTAINS &quot;we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #00CC00;">CONTAINS</span><span style="color: #FF00FF;">&#40;</span>nodes.title<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #2 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="2263738126"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<hr class="spacer-large" />
<h4 id="like-operator">5. The JCR-SQL2 LIKE Operator</h4>
<ul>
<li>The LIKE operator is used to search for a specified pattern in node properties.</li>
<li>There are two wildcards used in conjunction with the LIKE operator: % and _</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under &quot;/content&quot; </span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title starts with any values and ends with &quot;w&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">&quot;<span style="color: #008080; font-weight: bold;">%</span>w&quot;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under &quot;/content&quot; </span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title starts with &quot;w&quot; and ends with any values</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">&quot;w<span style="color: #008080; font-weight: bold;">%</span>&quot;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under &quot;/contentl&quot; </span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title matching &quot;w&quot; in any position</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">&quot;<span style="color: #008080; font-weight: bold;">%</span>w<span style="color: #008080; font-weight: bold;">%</span>&quot;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under &quot;/content&quot; </span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title start with any values and have &quot;w&quot; in the second position and ends with any values</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">&quot;<span style="color: #008080; font-weight: bold;">_</span>w<span style="color: #008080; font-weight: bold;">%</span>&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h4 id="is-not-null">6. The JCR-SQL2 IS NOT NULL Property</h4>
<ul>
<li>Used for validation of property&#8217;s value is not null.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all nodes WHERE nodes exist under &quot;/content&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title IS NOT NULL</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">IS NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under &quot;/content&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title IS NOT NULL</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">IS NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #3 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="3385248102"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<hr class="spacer-large" />
<h4 id="order-by-keyword">7. The JCR-SQL2 ORDER BY Keyword</h4>
<ul>
<li>The ORDER BY keyword is used to sort the result-set in ascending or descending order.</li>
<li>Returned values are sorted by default in ascending order.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all nodes WHERE nodes exist under the &quot;/content/we-retail&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- ORDER BY jcr:created ascending</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span> <br />
<span style="color: #990099; font-weight: bold;">ORDER BY</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:created<span style="color: #FF00FF;">&#93;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all nodes WHERE nodes exist under the &quot;/content/we-retail&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- ORDER BY node's property jcr:created descending</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #990099; font-weight: bold;">ORDER BY</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:created<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">DESC</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h4 id="cast-statement">8. The JCR-SQL2 CAST() Statement</h4>
<ul>
<li>Convert an expression from one data type to another.</li>
<li>Cast Types:
<ul>
<li>STRING</li>
<li>BINARY</li>
<li>DATE</li>
<li>LONG</li>
<li>DOUBLE</li>
<li>DECIMAL</li>
<li>BOOLEAN</li>
<li>NAME</li>
<li>PATH</li>
<li>REFERENCE</li>
<li>WEAKREFERENCE</li>
<li>URI</li>
</ul>
</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under the &quot;/content/we-retail&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- AND node's navRoot equals to &quot;true&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">as</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span> <br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>navRoot<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> CAST<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;true&quot;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">BOOLEAN</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under the &quot;/content/we-retail&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- AND node's creation date is greater than &quot;April 1st, 2018&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span> <br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:created<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">&gt;</span> CAST<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;2018-04-01T00:00:00.000Z&quot;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">DATE</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under the &quot;/content/we-retail&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- AND node's maxRating is less than &quot;5.0&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">as</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span> <br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>maxRating<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">&lt;</span> CAST<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;5.0&quot;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">DECIMAL</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<div class="mention-block">
<h3>Final Notes: When is the perfect time to use JCR-SQL2?</h3>
<ol>
<li>Build a images and documents search component, if your AEM site that allows consumers to search for images or documents.</li>
<li>Build a blog post search component, If your AEM site has a blog section.</li>
<li>Build a secure resources query API, If you are allowing 3rd party applications to query and find nodes in your AEM site.</li>
</ol>
<p>There&#8217;s many other things to utilize JCR-SQL2 to query nodes, but those are just some quick examples.</p>
</div>
<hr class="spacer-larger" />
<h4 id="jcr-sql2-code-snippets-cheat-sheet">9. JCR-SQL2 Code Snippets Cheat Sheet</h4>
<p>Use these code snippets below to rapidly build your AEM JCR SQL2 search queries, I will try my best to give you some really great examples:</p>
<h5>a. Find all [nt:unstructured] nodes, under the path &#8220;/content/we-retail&#8221;, where the node name = &#8220;root&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:unstructured<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> NAME<span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;root&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>b. Find all components ([nt:unstructured] nodes), under the path &#8220;/content/we-retail&#8221;, where component resourceType = &#8220;weretail/components/content/image&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:unstructured<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>sling:resourceType<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;weretail/components/content/image&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>b. Find all components ([nt:unstructured] nodes), under the path &#8220;/content/we-retail&#8221;, where component resourceType = &#8220;weretail/components/content/image&#8221; AND the fileReference property contains with &#8220;/content/dam/we-retail/en/experiences&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:unstructured<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>sling:resourceType<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;weretail/components/content/image&quot;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #00CC00;">CONTAINS</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span>fileReference<span style="color: #FF00FF;">&#93;</span><span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/dam/we-retail/en/experiences&quot;</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>c. Find all [nt:unstructured] nodes, under the path &#8220;/content/we-retail&#8221;,  where the property(&#8220;jcr:title&#8221;) contains &#8220;Featured products&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:unstructured<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #00CC00;">CONTAINS</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span><span style="color: #000033;">,</span> <span style="color: #008000;">&quot;Featured products&quot;</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>d. Find all pages (cq:Page nodes), under the path &#8220;/content/we-retail&#8221;,  where the cq:lastModified date is before or equals to &#8220;January 1st, 2000&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> page.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> page<br />
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> jcrContentNode <span style="color: #990099; font-weight: bold;">ON</span> ISCHILDNODE<span style="color: #FF00FF;">&#40;</span>jcrContentNode<span style="color: #000033;">,</span> page<span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>page<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> jcrContentNode.<span style="color: #FF00FF;">&#91;</span>cq:lastModified<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">&lt;=</span> CAST<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;2023-01-01T00:00:00.000+00:00&quot;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">DATE</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>e. Find all pages (cq:Page nodes), under the path &#8220;/content/we-retail&#8221;,  where the jcr:created date is before to &#8220;July 1st, 2022&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> page.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> page<br />
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> jcrContentNode <span style="color: #990099; font-weight: bold;">ON</span> ISCHILDNODE<span style="color: #FF00FF;">&#40;</span>jcrContentNode<span style="color: #000033;">,</span> page<span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>page<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> jcrContentNode.<span style="color: #FF00FF;">&#91;</span>jcr:created<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">&lt;</span> CAST<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;2022-07-01T00:00:00.000+00:00&quot;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">DATE</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>f. Find all [cq:PageContent] nodes, under the path &#8220;/content/we-retail&#8221;,  where the cq:template equals to &#8220;/conf/we-retail/settings/wcm/templates/hero-page&#8221;</h5>
<p>This AEM JCR SQ2 Query is an example for how to find all nodes with a specific template, results returned as cq:PageContent node.</p>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>cq:template<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;/conf/we-retail/settings/wcm/templates/hero-page&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>g. Find all pages (cq:Page nodes), under the path &#8220;/content/we-retail&#8221;,  where the cq:template equals to &#8220;/conf/we-retail/settings/wcm/templates/hero-page&#8221;</h5>
<p>This AEM JCR SQ2 Query is an example for how to find all nodes with a specific template, results returned as cq:Page node.</p>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> page.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> page<br />
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> jcrContentNode <span style="color: #990099; font-weight: bold;">ON</span> ISCHILDNODE<span style="color: #FF00FF;">&#40;</span>jcrContentNode<span style="color: #000033;">,</span> page<span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>page<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> jcrContentNode.<span style="color: #FF00FF;">&#91;</span>cq:template<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;/conf/we-retail/settings/wcm/templates/hero-page&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>h. Find all [cq:PageContent] nodes, under the path &#8220;/content/we-retail&#8221;,  where the cq:tags[] contains &#8220;we-retail:gender/men&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">This AEM JCR SQ2 Query <span style="color: #CC0099; font-weight: bold;">is</span> an example for how <span style="color: #990099; font-weight: bold;">to</span> find <span style="color: #990099; font-weight: bold;">all</span> nodes <span style="color: #990099; font-weight: bold;">with</span> a single cq:tags<span style="color: #FF00FF;">&#91;</span><span style="color: #FF00FF;">&#93;</span><span style="color: #000033;">,</span> results returned <span style="color: #990099; font-weight: bold;">as</span> cq:PageContent node.<br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>cq:tags<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;we-retail:gender/men&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>i. Find all [cq:PageContent] nodes, under the path &#8220;/content/we-retail&#8221;,  where the cq:tags[] contains &#8220;we-retail:gender/men&#8221; AND cq:tags[] contains &#8220;we-retail:activity/surfing&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">This AEM JCR SQ2 Query <span style="color: #CC0099; font-weight: bold;">is</span> an example for how <span style="color: #990099; font-weight: bold;">to</span> find <span style="color: #990099; font-weight: bold;">all</span> nodes <span style="color: #990099; font-weight: bold;">with</span> multiple cq:tags<span style="color: #FF00FF;">&#91;</span><span style="color: #FF00FF;">&#93;</span><span style="color: #000033;">,</span> results returned <span style="color: #990099; font-weight: bold;">as</span> cq:PageContent node.<br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>cq:tags<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;we-retail:gender/men&quot;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>cq:tags<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;we-retail:activity/surfing&quot;</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #4 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="2079905296"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #5 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="5183553115"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<style>
h5 {
    font-size: 16px;
    font-weight: 100;
}
</style>
<hr class="spacer-larger" />
<h4 id="jcr-sql2-java-code-example-servlet">9. JCR-SQL2 Java Code Example Servlet (2023)</h4>
<p>This is an example of how an AEM servlet would actually utilize the JCR-SQL2 Query with the examples above. It will output a JSON array of the page paths.</p>
<div class="code-1200">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.PrintWriter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Node</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.NodeIterator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.RepositoryException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Session</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryResult</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingSafeMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gson.JsonArray</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gson.JsonObject</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> javax.<span style="color: #006633;">servlet</span>.<span style="color: #006633;">Servlet</span>.<span style="color: #000000; font-weight: bold;">class</span>, property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/getpagedbypath&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GetPagedByPathServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingSafeMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the JCR session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session session <span style="color: #339933;">=</span> request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Session.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Query statement</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT page.* FROM [cq:Page] AS page &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;INNER JOIN [cq:PageContent] AS jcrContentNode ON ISCHILDNODE(jcrContentNode, page) &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;WHERE ISDESCENDANTNODE(page, '/content/we-retail') &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;AND jcrContentNode.[cq:lastModified] &lt;= CAST('2023-01-01T00:00:00.000+00:00' AS DATE)&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create the query object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryManager queryManager <span style="color: #339933;">=</span> session.<span style="color: #006633;">getWorkspace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getQueryManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>queryString, Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Execute the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryResult result <span style="color: #339933;">=</span> query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the nodes from the query result</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeIterator nodeIterator <span style="color: #339933;">=</span> result.<span style="color: #006633;">getNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Collect the paths of the first ten pages in a list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> pagePaths <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node pageNode <span style="color: #339933;">=</span> nodeIterator.<span style="color: #006633;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> pagePath <span style="color: #339933;">=</span> pageNode.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pagePaths.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>pagePath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create a JSON object to hold the results</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JsonObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonArray resultsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JsonArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Add the paths to the JSON array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> path <span style="color: #339933;">:</span> pagePaths<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsArray.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Add the JSON array to the response object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;results&quot;</span>, resultsArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Set the response content type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Write the JSON response to the output</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">PrintWriter</span> writer <span style="color: #339933;">=</span> response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>RepositoryException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exception</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>SlingHttpServletResponse.<span style="color: #006633;">SC_INTERNAL_SERVER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error executing query: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p><strong>JSON response</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;results&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page1&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page2&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page3&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page4&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page5&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page6&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page7&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page8&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page9&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page10&quot;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>Curl call</strong></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">curl -X GET http://localhost:4502/bin/getpagedbypath</div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet">How to use AEM JCR SQL2 query strings to query for nodes in Java Content Repository</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet/feed</wfw:commentRss>
			<slash:comments>26</slash:comments>
		
		
			</item>
	</channel>
</rss>
