微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何减少Java / groovy中的自定义API端点的输出

如何解决如何减少Java / groovy中的自定义API端点的输出

我正在努力改善对我们的Confluence-Server平台的自定义搜索。 我们有一个名为Scriptrunner的插件,它允许我们使用Groovy而不是Java来编写代码

我正在处理的代码是Search API端点,它目前可以正常工作,但是返回很多不必要的信息,甚至返回重复信息,因此我想以最有效的方式缩小搜索范围。

该平台具有我要用于实现的javadoc,链接https://docs.atlassian.com/ConfluenceServer/javadoc/7.8.1/com/atlassian/confluence/search/v2/SearchManager.html

我要实现以下部分

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.

但是我不明白如何正确生成Set<String> requestedFields

这是我的尝试:

import...

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,100);

Set<String> requestedFields = new HashSet<String>();
requestedFields.add("displayTitle");

def searchresult = searchManager.search(searchContent,requestedFields)

return searchresult.getAll()

如果我想使用其他方法

search(ISearch search)
Perform a search with a given criteria.

脚本运行正常,但是返回了很多我想删除的信息。

除了要实现的方法外,我还接受其他任何类型的建议,在这些建议中,我可以仅指定要输出的信息,以便可以安全地输出大小和处理能力。

P.S。 我已经尝试在他们的社区页面上详细询问相同的问题,但是由于我刚刚从Java / Groovy的工作中学到了知识,因此我可以在此方面使用一些开发人员帮助。

非常详细:https://community.atlassian.com/t5/Confluence-questions/Need-to-optimize-the-output-of-a-custom-Search-API-Endpoint-in/qaq-p/1515177

更新:

以下是作为API端点实现的工作代码

import com.atlassian.seraph.auth.DefaultAuthenticator
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator


import com.atlassian.confluence.search.service.ContentTypeEnum
import com.atlassian.confluence.search.v2.SearchManager
import com.atlassian.confluence.search.v2.searchfilter.SiteSearchPermissionsSearchFilter
import com.atlassian.confluence.search.v2.ContentSearch

import com.atlassian.confluence.search.v2.query.*
import com.atlassian.confluence.search.v2.sort.RelevanceSort
import com.atlassian.confluence.search.v2.SearchSort

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import org.codehaus.jackson.map.ObjectMapper

import java.util.HashSet

@BaseScript CustomEndpointDelegate delegate

testSearch(
    httpMethod: "GET",groups: ["access_group_1","access_group_2"]
) { MultivaluedMap queryParams,String body ->

    def searchManager = ComponentLocator.getComponent(SearchManager)

    // Query can be of any type noted here:
    //https://developer.atlassian.com/server/confluence/searching-using-the-v2-search-api/
    def paramQueryString = (queryParams.get(new String("q"))).get(0)
    def query = BooleanQuery.andQuery(new TextQuery(paramQueryString));
    def sort = new RelevanceSort();
    def searchFilter = SiteSearchPermissionsSearchFilter.getInstance();
    def searchContent = new ContentSearch(query,100);

    def searchresult = searchManager.search(searchContent)

    return Response.ok(new JsonBuilder([results: searchresult.getAll()]).toString()).build()
}

当我调用API

http://10.10.10.11:8080/rest/scriptrunner/latest/custom/testSearch?q=ArticleThatwillbedeleted

我收到以下JSON响应

{
    "results": [
        {
            "displayTitle": "ArticleThatwillbedeleted","handle": {
                "className": "com.atlassian.confluence.pages.Page","id": 359071873
            },"lastUpdateDescription": "","ownerTitle": null,"spaceName": "Employee Team Space","creatorUser": {
                "backingUser": {
                    "active": true,"lowerName": "vnikolov","directoryId": 142049281,"fullName": "Vasil Nikolov","emailAddress": "vnikolov@domain.com","email": "vnikolov@domain.com","name": "vnikolov","displayName": "Vasil Nikolov"
                },"key": {
                    "stringValue": "8a606c8c56a371040156a37301341285"
                },"name": "vnikolov"
            },"resultExcerpt": "this is the body of the article that will be delted.","ownerType": null,"lastModifier": "vnikolov","urlPath": "/display/WIT/ArticleThatwillbedeleted","resultExcerptWithHighlights": "this is the body of the article that will be delted.","explain": {
                "present": false,"empty": true
            },"lastModifierUser": {
                "backingUser": {
                    "active": true,"extraFields": {
                "content-version": "1"
            },"lastModificationDate": "2020-10-20T20:42:27+0000","type": "page","content": "   \nthis is the body of the article that will be delted.\n ","creationDate": "2020-10-20T20:41:46+0000","personalLabels": [],"status": "current","spaceKey": "WIT","contentVersion": 1,"creator": "vnikolov","displayTitleWithHighlights": "ArticleThatwillbedeleted","homePage": false,"sanitisedContent": "this is the body of the article that will be delted."
        }
    ]
}

我得到4倍的身体(resultExcerpt,resultExcerptWithHighlights,content,sanitisedContent) 我所需要的只是content,并尽可能将其切成一定的大小或字符长度。

当我尝试通过添加以下行并修改requestedFields

来实现searchresult
    def requestedFields = [ 'content','displaytitle' ] as Set
    def searchresult = searchManager.search(searchContent,requestedFields)

我得到的JSON响应是:

{
    "results": [
        {
            "resultExcerpt": "","resultExcerptWithHighlights": "","extraFields": {},"displayTitleWithHighlights": ""
        }
    ]
}

我注意到的另一件事是在工作示例中返回的类是: com.atlassian.confluence.search.v2.lucene.LucenesearchResult@1233a8a4 并在requestedFields尝试中,结果类为: com.atlassian.confluence.search.v2.ProjectedSearchResult@6c688cdd

我想找到一种方法来控制API的输出,不必一定是我要实现的requestedFields方法

解决方法

JsonBuilder呈现所有对象属性,而不仅是您从服务器请求的字段。

我看到的呈现请求字段的最简单方法:

def requestedFields = [ 'content','displaytitle' ] as Set
def searchresult = searchManager.search(searchContent,requestedFields)

def table = searchresult.getAll().collect{row-> 
    requestedFields.collectEntries{fldName->  
        [ fldName,row.getField(fldName) ] 
    }
}
    
def json = new groovy.json.JsonBuilder(table).toPrettyString()

Response.ok(json).build()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?