results.each
, the results
appears empty.Hi @Alex,
I ran into the same issue recently when trying to report on recent comments. Two things are at play here:
def results = cqlSearchUtils.searchForPages(CQLSearch.fromQuery(cqlQuery)).toList()
log.warn("Find pages: ${results.size()}")
results.each { page ->
log.warn("Page: ${page.title}, ID: ${page.id}")
}
This way the result set is realised once, and you can safely get its size and iterate over it.
import com.atlassian.confluence.api.model.Expansion
import com.atlassian.confluence.api.model.content.Content
import com.atlassian.confluence.api.model.pagination.SimplePageRequest
import com.atlassian.confluence.api.model.search.SearchContext
import com.atlassian.confluence.api.service.search.CQLSearchService
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
def cqlSearchService = ScriptRunnerImpl.getOsgiService(CQLSearchService)
def cqlQuery = 'space = INFO and type = comment and created >= now("-7d")'
def pageRequest = new SimplePageRequest(0, 50)
def searchResult = cqlSearchService.searchContent(
cqlQuery,
SearchContext.builder().build(),
pageRequest,
Expansion.combine("container") // get parent page data
)
searchResult.each { Content comment ->
def parentPage = comment.container
log.warn("Comment ${comment.id} created ${comment.history.createdDate} on page ${parentPage?.title} (${parentPage?.id})")
}
This script searches for comments created in the last seven days and then logs the ID and creation date of each comment along with the title and ID of its parent page.
With these adjustments, you should see your results logged correctly.
— Mia Tamm from Simpleasyty
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.