Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

AEM Query Question: Find pages with string pattern; print the pages and the match

Avatar

Level 5

I can find pages that contain a string pattern:

 

type=cq:Page
fulltext=*tion
group.1_path=/content/site/en
orderby.sort=desc
p.limit=10

This seems to print the pages that have the pattern *tion within:

    {
      "path": "/content/site/foo",
      "excerpt": "some text here",
      "name": "page name",
      "title": "page title",
      "lastModified": "timestamp",
      "created": "timestamp"
    },

 

I want a result that also include the matches themselves, either individually or as multi-values like this:

 

    {
      "path": "/content/site/foo",
      "match0": "https://contruction.com",  
      "match1": "tionesta",  
      "match2": "bastion",     
      "excerpt": "some text here",
      "name": "page name",
      "title": "page title",
      "lastModified": "timestamp",
      "created": "timestamp"
    },

or:

    {
      "path": "/content/site/foo",
      "match0": "https://contruction.com, tionesta, bastion",  
      "excerpt": "some text here",
      "name": "page name",
      "title": "page title",
      "lastModified": "timestamp",
      "created": "timestamp"
    },    

 Thanks for any info!

8 Replies

Hi @santhosh_kumark, Thanks for the reply. I have seen the helpful page you linked to. Is the snippet you posted something that will print the string match(es) and the page path they occur in?

Is there a word for what I want, like "facet" or something like that? Perhaps I can search better (or ask a better question) if I know the term that means: also print the match.

I see the sample code iterates over a resultset but can't tell if it prints the matched strings as well as the page paths. If it does, what part of the code is responsible for printing the page paths and string match(es)?

It looks like the example you suggest is Java. I was hoping for a query using querybuilder syntax. If it's not possible to do using querybuilder syntax, how would I go about using the snippet? Can I put it in a JSP page using crxde? Do I need to import any additional classes, etc.?

Thanks again!

Avatar

Community Advisor

Hi @this-that-the-otter you can use the below predicates. yes it will give the matching result along with its path.
path=/content
type=cq:Page
group.p.or=true
group.1_fulltext=https://contruction.com
group.1_fulltext.relPath=jcr:content
group.2_fulltext=tionesta
group.2_fulltext.relPath=jcr:content
group.3_fulltext=bastion
group.3_fulltext.relPath=jcr:content
p.offset=0
p.limit=20

http://localhost:4502/libs/cq/search/content/querydebug.html?_charset_=UTF-8&isURL=on&query=path%3D%...

 

Hi @santhosh_kumark I want to search the sub string "tion"  and find matches that contain it.

I want to print the page path and the matched string(s).

For example, searching for "tion" - I want to find any string that contains "tion" and the page on which it occurs. I want to print the string match and the page.

Construction, Tionesta, and Bastion are examples of what my query might find. I do not have these in mind ahead of the query.

If a page contains several strings that contain "tion" or just one, I want to print them or it, along with the page path - for example:

If https://www.adobe.com/about-adobe.html was one of the pages in my results, I want the json record of the result for this page to contain:

path: /about-adobe.html

match: Information, Solutions, education, Relations, applications (or each match on a separate line).

Thanks!

Avatar

Community Advisor

Hello @this-that-the-otter 

 

Please refer to https://github.com/paulrohrbeck/aem-links/blob/master/querybuilder_cheatsheet.md

 

Query would be:

path=/content/wknd/language-masters/en
type=cq:Page
fulltext=*tion*
p.limit=-1
p.facets=true

 

We would need Java code to extract the Facets

Map<String, Facet> facets = result.getFacets();
for (String key : facets.keySet()) {
    Facet facet = facets.get(key);
    if (facet.getContain**bleep**()) {
        for (Bucket bucket : facet.getBuckets()) {
            long count = bucket.getCount();
            Map<String, String> params = bucket.getPredicate().getParameters();
            for (String k : params.keySet()) {
                out.println("<br>k:"+k);
            }
        }
    }
}

 


Aanchal Sikka

Hi @aanchal-sikka 

Thanks for the reply. Can you explain how this works? I mentioned the term "facet" as an example but don't really know if I need them for my desired result set. Can you explain what a facet is and how your example query and java code work? How would I use the example query and code together to get the information I want?

It would be great to see how this works against sample content so I  may see if it works as I hope (see above /about-adobe.html example).

Avatar

Administrator

@this-that-the-otter Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Hi @kautuk_sahni, I have replied to @aanchal-sikka.

It would be great if there were encouragement/requirement for answers to be:

- explained

- provide working example

- demonstrate example working (i.e. show the result against sample data using explained and working example)

e.g. like stackoverflow, etc.