How do I search for a certain part of a Label?

In Confluence, I am trying to search all pages that have a certain text inside of labels.

For example,

Label: project-GM

Ideal search : GM or label : GM

I've so far tried using ScriptRunner and using there guide to do a Label Search Extractor but there isn't much information on the subject on how to custimze and fit it to my needs.  

Any recomdations please?

2 answers

This widget could not be displayed.
Stephen Deutsch Community Champion Jul 13, 2017

Hi Nadav,

This should work in a standard search:

labelText:/.*GM.*/

Great answer thanks! 

And how can I get all the documents/pages as a variable in-script?

 

This widget could not be displayed.

A few caveats:

I don't have scriptrunner installed, so I couldn't test it. I'm not an expert in Groovy, and I don't know so much about extractors. That said, this might work (adapted from the example on https://scriptrunner.adaptavist.com/latest/confluence/SearchExtractors.html):

import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.pages.Page
import org.apache.lucene.document.Field
import org.apache.lucene.document.StringField

if (searchable instanceof Page) {
Page page = (Page) searchable
String labelText = "gm"
String labels = page.getLabels().inject("") { acc, val -> acc + val.getName() + " " }
if (labels.contains(labelText)) {
document.add(new StringField("labelContains", labelText, Field.Store.YES))
}
}

 

then you would search for "labelContains : gm" (note that labels are always lowercase).

Awesome ! really helpful, last question is how do I do it for any kind of text so it can work on many sorts of labels (project-gm, project-bee.....), and in that way I can search whatever sub-text I want ( labelContains : XXXX) and it will work......

 

Unfortunately, I'm not sure about that one; by making this search extractor, you're creating an alias in the lucene index, so in order to match any value, you would have to create permutations for every possible value in order to match exactly (i.e. for project you would search for pr, pro, proj, proje, ro, roj, roje, ect.). If you are only going to use "project-xxx" to match, then something like this might work (as I said, I have no way of testing):

import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.pages.Page
import org.apache.lucene.document.Field
import org.apache.lucene.document.StringField

if (searchable instanceof Page) {
Page page = (Page) searchable
String projects = page.getLabels().findAll { it.getName().contains('project-') }.inject("") { acc, val -> acc + val.getName().replace("project-", "") + " " }.trim()
if (projects) {
document.add(new StringField("project", projects, Field.Store.YES))
}
}

then you should be able to search for "project : gm" or "project : bee" or whatever. 

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in Teamwork

What teamwork quotes inspire you?

Hey everyone! My name is Natalie and I'm an editor of the Atlassian Blog and I've got a question for you: What's your favorite quote about teamwork?  We've compiled a list here, along with...

192 views 18 7
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you