Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Confluence Server REST API Search with ScriptRunner

Hi,

I have a problem with searching pages in Confluence. I executing my request from Jira by scriptrunner:

private checkIfPageExist(String pageName, confluenceLink)
{
def status = false;
log.error "checkIfPageExist pageName: "+pageName
try{

if(pageName.contains(" "))
{
pageName = pageName.replace(" ","%20")
}

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()
authenticatedRequestFactory
.createRequest(Request.MethodType.GET, "/rest/api/content/search?cql=title=%22${pageName}%22")
.addHeader("Content-Type", "application/json")
.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
if(response.statusCode != HttpURLConnection.HTTP_OK) {
throw new Exception(response.getResponseBodyAsString())
}
else {
log.error "response: "+response.getResponseBodyAsString()
def resp = new JsonSlurper().parseText(response.responseBodyAsString)
if(resp.results.title[0].toString().toLowerCase() == pageName.toLowerCase())
{
status = true;
}
}
}
})
}
catch(Exception ex)
{
log.error "Error: ${ex}"
}
finally
{
return status
}
}

 

I have created page in Confluence with name "Test". In my problematic case this function trying to find page with name "test". Function returns false because search engine does not recognize the size of characters.
But another function in my code is resposible for creating page and creating fail because Confluence returns information that page with the same name exist.

How can I provide recognizing size of characters in searching via API in Confluence?

1 answer

Hey, @Paweł Jakimiak ! So, I think the problem is caused because your are searching for titles using the EQUALS operator, which is case sensitive. If you want to do a case insensitive search, you should use the CONTAINS operator.

So, instead of

"/rest/api/content/search?cql=title=%22${pageName}%22"

use a URL like

"/rest/api/content/search?cql=title~%22${pageName}%22"

That will find pages regardless of case.

That said, your check on this line:

if(resp.results.title[0].toString().toLowerCase() == pageName.toLowerCase())

may need a little work if you use the CONTAINS (~) operator. That will only check that the first result in your search results matches your pageName variable. That may not be the case if you use the CONTAINS operator, since that will find more results. You might want something more like:

if (resp.results.any { it.toString().toLowerCase() == pageName.toLowerCase() })

to see if any of the pages returned have the same name, regardless of case, as using the CONTAINS operator is likely to yield many more results than EQUALS.

For more information, check out the Operators reference in the CQL docs.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Jira Cloud Performance Improvements

Hello everyone, I am a product manager in the Jira Cloud team focused on making sure our customers have a delightful experience using our products. Towards that goal, one of the areas which is extr...

175 views 2 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you