Need to optimize the output of a custom Search API Endpoint in ScriptRunner for Confluence Server

Vasil Nikolov October 23, 2020

We are using a custom search query with ScriptRunner followed by this article :
https://developer.atlassian.com/server/confluence/searching-using-the-v2-search-api/

The problem we have is that the query returns a lot of information we dont need or its duplicate, like the Content of the page is displayed with 3 different properties (resultExcerpt, content, sanitisedContent).
Our goal is:
1. To make the SearchManager return only the fields/properties we specify, title and content.
2. Optionally, reduce the content length to about 500 characters.

Looking in to the SearchManager interface javadoc, I see there is a method to return only the fields requested 


search (ISearch search, Set<String> requestedFields)
Perform a search with a given criteria, the returns searchResults only have the fields requested in the projection filled out, no other fields are valid in the searchResult.

I could not figure out how I can make the proper

Set<String> requestedFields

I tried to make it work in many ways, but I always get:

com.atlassian.confluence.search.v2.ProjectedSearchResult

When I do .getClass I am getting the default

class com.atlassian.confluence.search.v2.DefaultSearchResults

Here is my pity attempt to make it work, from the Console

import com.atlassian.seraph.auth.DefaultAuthenticatorimport com.atlassian.confluence.user.UserAccessorimport com.atlassian.confluence.user.AuthenticatedUserThreadLocalimport com.atlassian.confluence.spaces.SpaceManagerimport com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.search.service.ContentTypeEnumimport com.atlassian.confluence.search.v2.SearchManagerimport com.atlassian.confluence.search.v2.searchfilter.SiteSearchPermissionsSearchFilterimport com.atlassian.confluence.search.v2.ContentSearchimport com.atlassian.confluence.search.v2.DefaultSearchResults
import com.atlassian.confluence.search.v2.query.*import com.atlassian.confluence.search.v2.sort.RelevanceSortimport com.atlassian.confluence.search.v2.SearchSort
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegateimport groovy.json.JsonBuilderimport groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMapimport javax.ws.rs.core.Responseimport org.codehaus.jackson.map.ObjectMapper
import java.util.HashSet


def searchManager = ComponentLocator.getComponent(SearchManager)
def paramQueryString = "ArticleThatWillBeDeleted"
def query = BooleanQuery.andQuery(new TextQuery(paramQueryString));def sort = new RelevanceSort();def searchFilter = SiteSearchPermissionsSearchFilter.getInstance();def searchContent = new ContentSearch(query, sort, searchFilter, 0100);
Set<String> requestedFields = new HashSet<String>();requestedFields.add("displayTitle");
def searchresult = searchManager.search(searchContent,requestedFields)
return searchresult.getAll()

Here is a sample of the output I am getting (when I use the JSON builder and call the API directly, because I dont know how to expand the return in the Console

{
"resultExcerpt": "",
"explain": {
"present": false,
"empty": true
},
"resultExcerptWithHighlights": "",
"extraFields": {

},
"displayTitleWithHighlights": ""
}

 

Without trying to use the requestedFields, the output looks something like this (I have only paste the first letters of the values)

displayTitle : A
handle : @
lastUpdateDescription :
ownerTitle :
spaceName : E
creatorUser : @
e
resultExcerpt : t
ownerType :
lastModifier : v
urlPath : /
resultExcerptWithHighlights : t
explain : @
lastModifierUser : @
e
extraFields : @
lastModificationDate : 2
type : p
content :
t

creationDate : 2
personalLabels : {
status : c
spaceKey : W
contentVersion : 1
creator : v
displayTitleWithHighlights : A
homePage : F
sanitisedContent : t

I allow the fact that I could have understand that all wrong, so I am open for suggestions to the problem I wanna solve.

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events