Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

Issue in custom search code

Avatar

Level 9

Hi All,

We have the below code 

1] Ideally according to the code, if if (resultListNode.size() > resultsPerPage) , where resultsPerPage = 10, then results should show up in "Show More".Issue is, suppose there are multiple duplicate values[say around 12, like xyz/jcr:content, xyz/jcr:content/parsys/bodycopy, xyz/jcr:content/parsys/page_heading and so on] returned  along with several other results[say around 10] after hitting the query.But in this scenario, the resultListNode.size will be less than resultsPerPage and show more will never be called. How should this situation be handled.

2] Any thoughts/code snippet on this will be helpful.

 

Code Snippet as below :

 

public final SearchResult getSQL2Result(ResourceResolver resourceResolver, String sql2Query,
            final Long offsetLimit, final int resultsPerPage)
    
int resultsLimit = resultsPerPage + 1;
        LOG.info("sql2query >> " + sql2Query + "offset is " + offsetLimit + "limit " + resultsLimit);
        Session session = null;
        List<Page> resultListNode = new ArrayList<Page>();
        final SearchResult searchResult = new SearchResult();
        try
        {
            session = (Session) resourceResolver.adaptTo(Session.class);
            javax.jcr.query.Query query = session.getWorkspace().getQueryManager()
                    .createQuery(sql2Query, javax.jcr.query.Query.JCR_SQL2);
            query.setLimit(resultsLimit);
            query.setOffset(offsetLimit);
            QueryResult results = query.execute();
            RowIterator iterator = results.getRows();
            LOG.info("iterator.getSize() "+ iterator.getSize()); 
            while (iterator.hasNext())
            {
                Row currentRow = iterator.nextRow();
                String nodePath = currentRow.getNode().getPath();
                // name defined in sql2 query as an alias
                if (nodePath.contains("/jcr:content"))
                {
                    nodePath = nodePath.substring(0, nodePath.indexOf("/jcr:content"));
                    LOG.info("nodePath "+nodePath);
                }
                Page pageResource = resourceResolver.getResource(nodePath).adaptTo(Page.class);
                LOG.debug("node page path " + nodePath + " score " + currentRow.getScore());
                // check for duplicate results if the q string parameter is available at two places
                // For example - if the search string is available as blocktitle & also in body copy component
                if (!resultListNode.contains(pageResource))
                {
                    resultListNode.add(pageResource);
                }
            }
            LOG.debug("size if the returned result " + resultListNode.size());
            searchResult.setShowMore(calculateHasMoreResults(resultListNode.size(), resultsPerPage));
            if (resultListNode.size() > resultsPerPage)
            {
                resultListNode = resultListNode.subList(0, resultsPerPage);
            }
        

5 Replies

Avatar

Level 10

I am not clear in your issue. You state:

lts[say around 10] after hitting the query.But in this scenario, the resultListNode.size will be less than resultsPerPage and show more will never be called

Are you asking what happens if resultListNode is less than resultsPerPage?

Avatar

Level 9

Hi Scott,

I mean the query passed into the function returns 12 results[all of them duplicates in an order] followed by 10 more. In this case, even though the search results  returned are close to 22 in total, resultListNode.size() will always show less than resultsPerPage and Show More is not called. 

Ideally,resultListNode.size() should be more than resultsPerPage and Show More function should be called.

Avatar

Level 10

Here is your code:

    if (resultListNode.size() > resultsPerPage)
            {
                resultListNode = resultListNode.subList(0, resultsPerPage);
            }

I am not seeing where you define Show More?

Avatar

Level 9

Hi Scott,

Just above the lien you mentioned, we have searchResult.setShowMore(calculateHasMoreResults(resultListNode.size(), resultsPerPage));

Had missed adding the below funtion:

private boolean calculateHasMoreResults(long size, final int resultsPerPage)
    {
        return size > resultsPerPage;
    }

Avatar

Level 9

Hi All,

Any thoughts on the above will be helpful.