How to execute CQL query in Confluence ScriptRunner?

zaharovvv_suek_ru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 25, 2019

I have a CQL(Confluence Query Language) query:

код=P

and this query gives me two results:

CQL - confluence query language.png 

How is it possible to execute this code in Confluence ScriptRunner? I mean execute cql query  in Confluence Script Runner like it can be done in Jira ScriptRunner.

 

I've tried to use Jira ScriptRunner code, but Jira code is not working in Confluence Script Runner:

String JqlQuery = "issueType = Epic AND project = HH AND 'Epic Name' = 'epic name'"
def parseResult = searchService.parseQuery(curUser, JqlQuery)
def searchResult = searchService.search(curUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())

 

3 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Rinaldi James Michael March 12, 2023

I've gone through quite a few discussions on this and have made two groovy scripts for two CQL query use cases. These can easily be modified as per requirements. 

I know this post was from 2019, but I still think this is very useful. Hi from 2023 👋

 

1. Get values from the title column of a CQL query's results. 

https://github.com/Rinaldi-James-Michael/Atlassian-Public/blob/main/Confluence/Fetch%20Data%20from%20a%20CQL%20query's%20result%20-%20Confluence.groovy

 

2. Add groups to Space permissions of the Spaces returned by a CQL query (this only works if the query contains type=space)

https://github.com/Rinaldi-James-Michael/Atlassian-Public/blob/main/Confluence/Add%20groups%20to%20Spaces%20based%20on%20the%20results%20of%20a%20CQL%20query%20-%20Confluence.groovy

0 votes
Mark de Bont September 1, 2021

Do I understand correctly that these types of queries can not be executed in Cloud?

0 votes
Roland Holban (Adaptavist)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 23, 2020

You can use the `CQLSearchUtils` class for this.

import com.onresolve.scriptrunner.canned.confluence.utils.CQLSearchUtils
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def cqlSearchUtils = ScriptRunnerImpl.scriptRunner.getBean(CQLSearchUtils)
def cql = "space = FOOBAR"
def results = cqlSearchUtils.searchForContent(cql)
Karunakar Chikoti February 25, 2021

I tried the same, but getting error message:

groovy.lang.MissingMethodException: No signature of method: com.onresolve.scriptrunner.canned.confluence.utils.CQLSearchUtils.searchForContent() is applicable for argument types: (String) values: [space = AP]
at Script96.run(Script96.groovy:8)

 

Here is the script we tried on the scriptrunner console:

import com.onresolve.scriptrunner.canned.confluence.utils.CQLSearchUtils
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def cqlSearchUtils = ScriptRunnerImpl.scriptRunner.getBean(CQLSearchUtils)
def cqlQuery = 'space = AP' // some CQL query
def pages = cqlSearchUtils.searchForContent(cqlQuery)

Any suggestions?

 

Thanks,

Rafał Żydek June 11, 2021

import com.onresolve.scriptrunner.canned.confluence.utils.CQLSearchUtils
import com.onresolve.scriptrunner.canned.confluence.utils.CQLSearch
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def cqlSearchUtils = ScriptRunnerImpl.scriptRunner.getBean(CQLSearchUtils)
def cqlQuery = 'space = KNOW' // some CQL query
def cqlSearch = new CQLSearch()

def pages = cqlSearchUtils.searchForPages(CQLSearch.fromQuery(cqlQuery))

pages.each{ page ->
log.warn page
}

TAGS
AUG Leaders

Atlassian Community Events