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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,414,280
Community Members
 
Community Events
170
Community Groups

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

Edited

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

Atlassian Community Events