aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/searcher/QueryValidatingSearcher.java
blob: 4e604dcd22600627ee0e282f539cecab1ab9f3c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.searcher;

import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;

/**
 * Ensures hits is 1000 or less and offset is 1000 or less.
 *
 * @author  <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class QueryValidatingSearcher extends Searcher {

    public Result search(Query query, Execution execution) {
        if (query.getHits() > 1000) {
            Result result = new Result(query);
            ErrorMessage error
                = ErrorMessage.createInvalidQueryParameter("Too many hits (more than 1000) requested.");
            result.hits().addError(error);
            return result;
        }
        if (query.getOffset() > 1000) {
            Result result = new Result(query);
            ErrorMessage error
                = ErrorMessage.createInvalidQueryParameter("Offset too high (above 1000).");
            result.hits().addError(error);
            return result;
        }
        return execution.search(query);
    }

}