Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

AEM 6.1 Search Options pagination using Xpath

Avatar

Level 3

I am create javax.jcr.Query using xpath. The issue with this is that it doesn't have Pagination support in the QueryResult unlike SearchResult from cq package.

I wanted to be able to get dynamic search fields and search.

constructing xpath is easy and I found the search is faster as well. Can Some one help to get the Pagination support in QueryResult or

Convert javax.jcr.Query to cq Query?

My current Code:

            QueryManager qm=session.getWorkspace().getQueryManager();
            Query query = qm.createQuery(xpathQuery.toString(), Query.XPATH);
            QueryResult result = query.execute();

           for (NodeIterator iterator = result.getNodes(); iterator.hasNext();) {

 

}

Also can you please confirm my understanding about xpath search is faster compared to querybuilder?

 

 

Thanks for your support

1 Accepted Solution

Avatar

Correct answer by
Level 10

Look here in JCR API docs - it states xpath is depreciated:

http://www.day.com/specs/jsr170/javadocs/jcr-2.0/javax/jcr/query/Query.html

http://stackoverflow.com/questions/24903623/sql2-equivalent-for-this-xpath-query

QueryBuilder API supports pagination and built upon JCR api:

Implementing pagination

By default the Query Builder would also provide the number of hits. Depending on the result size this might take long time as determining the accurate count involves checking every result for access control. Mostly the total is used to implement pagination for the end user UI. As determining the exact count can be slow it is recommended to make use of the guessTotal feature to implement the pagination.

For example, the UI can adapt following approach:

  • Get and display the accurate count of the number of total hits (SearchResult.getTotalMatches() or total in the querybuilder.json response) are less than or equal to 100;
  • Set guessTotal to 100 while making the call to the Query Builder.
  • The response can have the following outcome:
    • total=43more=false - Indicates that total number of hits is 43. The UI can show up to ten results as part of the first page and provide pagination for the next three pages. You can also use this implementation to display a descriptive text like "43 results found".
    • total=100more=true - Indicates that the total number of hits is greater than 100 and the exact count is not known. The UI can show up to ten as part of the first page and provide pagination for the next ten pages. You can also use this to display a text like "more than 100 results found". As the user goes to the next pages calls made to the Query Builder would increase the limit of guessTotal and also of the offset and limitparameters.

https://docs.adobe.com/content/docs/en/aem/6-0/develop/search/querybuilder-api.html

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

Look here in JCR API docs - it states xpath is depreciated:

http://www.day.com/specs/jsr170/javadocs/jcr-2.0/javax/jcr/query/Query.html

http://stackoverflow.com/questions/24903623/sql2-equivalent-for-this-xpath-query

QueryBuilder API supports pagination and built upon JCR api:

Implementing pagination

By default the Query Builder would also provide the number of hits. Depending on the result size this might take long time as determining the accurate count involves checking every result for access control. Mostly the total is used to implement pagination for the end user UI. As determining the exact count can be slow it is recommended to make use of the guessTotal feature to implement the pagination.

For example, the UI can adapt following approach:

  • Get and display the accurate count of the number of total hits (SearchResult.getTotalMatches() or total in the querybuilder.json response) are less than or equal to 100;
  • Set guessTotal to 100 while making the call to the Query Builder.
  • The response can have the following outcome:
    • total=43more=false - Indicates that total number of hits is 43. The UI can show up to ten results as part of the first page and provide pagination for the next three pages. You can also use this implementation to display a descriptive text like "43 results found".
    • total=100more=true - Indicates that the total number of hits is greater than 100 and the exact count is not known. The UI can show up to ten as part of the first page and provide pagination for the next ten pages. You can also use this to display a text like "more than 100 results found". As the user goes to the next pages calls made to the Query Builder would increase the limit of guessTotal and also of the offset and limitparameters.

https://docs.adobe.com/content/docs/en/aem/6-0/develop/search/querybuilder-api.html

Avatar

Level 3

Thanks for your reply.

I dont see xpath search is deprecated in 6.1. It is easy to build query using xpath than QueryBuilder.

How do I add search attributes dynamically 

.

My xpath query is 

/jcr:root/content/dnb/home/lc//*[jcr:contains(@jcr:title,'market') or jcr:contains(@description,'market') or jcr:contains(@title,'market') or jcr:contains(@text,'market') or jcr:contains(@html,'market') and (jcr:contains(@cq:tags,'function:cmo') or jcr:contains(@cq:tags,'function:credit') )]

Pseudo Logic:

(title=market or description = market)

and

(tags=cmo or tag=credit)

Can you help me to construct this in Query builder?