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

Nadav Dahan July 13, 2017

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

1 vote
Stephen Deutsch
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 13, 2017

Hi Nadav,

This should work in a standard search:

labelText:/.*GM.*/

Nadav Dahan July 13, 2017

Great answer thanks! 

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

 

0 votes
Stephen Deutsch
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 13, 2017

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).

Nadav Dahan July 13, 2017

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......

 

Stephen Deutsch
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 13, 2017

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
TAGS
AUG Leaders

Atlassian Community Events