Mohammad Sunny
Mohammad Sunny

Reputation: 371

How should I customize my search result in vespa?

I have a search result like whcih is given below :

{
"root": {
    "id": "toplevel",
    "relevance": 1,
    "fields": {
        "totalCount": 20
    },
    "coverage": {
        "coverage": 100,
        "documents": 20,
        "full": true,
        "nodes": 1,
        "results": 1,
        "resultsFull": 1
    },
    "children": [{
            "id": "group:string:Jones",
            "relevance": 9870,
            "value": "Jones",
            "fields": {
                "sum(price)": 39816
            }
        },
        {
            "id": "group:string:Brown",
            "relevance": 8000,
            "value": "Brown",
            "fields": {
                "sum(price)": 20537
            }
        }
    ]
}

}

I do not want fields and coverage in my search results. How can I achieve this?. And also I want to change status according to error with error message. How can I do this? Please help.

Upvotes: 4

Views: 242

Answers (2)

Jon
Jon

Reputation: 2339

Response payload: You can create your own renderer to control the returned format: https://docs.vespa.ai/documentation/result-rendering.html

HTTP status code: The rules for determining the status code to return are:

  • If the Result contains no errors (Result.hits().getError()==null): 200 OK is returned.

  • If the Result contains errors and no regular hits: If the error code of any ErrorMessage in the Result (Result.hits().getErrorHit().errorIterator()) is a "WEB SERVICE ERROR CODE", the first of those is returned. Otherwise, if it is a "HTTP COMPATIBLE ERROR CODE", the mapping of it is returned. Otherwise 500 INTERNAL_SERVER_ERROR is returned.

  • If the Result contains errors and also contains valid hits: The same as above, but 200 OK is returned by default instead of 500.

WEB SERVICE ERROR CODES:

200, 301, 302, 307, 400, 401, 403, 404, 405, 406, 408, 428, 429, 431, 500, 501, 502, 511

HTTP COMPATIBLE ERROR CODES:

com.yahoo.container.protect.Error.BAD_REQUEST -> Http code 400
com.yahoo.container.protect.Error.UNAUTHORIZED -> Http code 401
com.yahoo.container.protect.Error.FORBIDDEN -> Http code 403
com.yahoo.container.protect.Error.NOT_FOUND -> Http code 404
com.yahoo.container.protect.Error.INTERNAL_SERVER_ERROR -> Http code 500
com.yahoo.container.protect.Error.INSUFFICIENT_STORAGE -> Http code 507

With this information, you can write a Searcher component (https://docs.vespa.ai/documentation/searcher-development.html) which sets an ErrorMessage in the Result corresponding to the HTTP status you want.

For a real world example see e.g the rate limiting Searcher bundled in Vespa: https://github.com/vespa-engine/vespa/blob/master/container-search/src/main/java/com/yahoo/search/searchers/RateLimitingSearcher.java#L133

Upvotes: 3

Lester Solbakken
Lester Solbakken

Reputation: 266

To customize results you can use a result renderer. Please take a look at https://docs.vespa.ai/documentation/result-rendering.html which should be complete with examples.

Upvotes: 1

Related Questions