aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/handler/SearchResponse.java
blob: 9b502a647abffba5edeb11339caab9c7568044f0 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.handler;

import com.yahoo.container.handler.Timing;
import com.yahoo.container.logging.HitCounts;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.result.ErrorHit;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.result.Hit;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Some leftover static methods.
 *
 * @author Steinar Knutsen
 */
public class SearchResponse {

    // Remove (the empty) summary feature field if not requested.
    static void removeEmptySummaryFeatureFields(Result result) {
        // TODO: Move to some searcher in Vespa backend search chains
        if ( ! result.hits().getQuery().getRanking().getListFeatures())
            for (Iterator<Hit> i = result.hits().unorderedIterator(); i.hasNext();)
                i.next().removeField(Hit.RANKFEATURES_FIELD);
    }

    static void trimHits(Result result) {
        if (result.getConcreteHitCount() > result.hits().getQuery().getHits()) {
            result.hits().trim(0, result.hits().getQuery().getHits());
        }
    }

    static Iterator<? extends ErrorMessage> getErrorIterator(ErrorHit h) {
        if (h == null) {
            return new ArrayList<ErrorMessage>(0).iterator();
        } else {
            return h.errorIterator();
        }
    }

    static boolean isSuccess(Result result) {
        if (result.hits().getErrorHit() == null) return true;
        for (Hit hit : result.hits()) {
            if (hit.isMeta()) continue;
            if ( ! hit.isFillable() || ! hit.getFilled().isEmpty()) return true;
        }
        return false;
    }

    public static Timing createTiming(Query query, Result result) {
        long summaryStartTime = result.getElapsedTime().firstFill();
        long queryStartTime = result.getElapsedTime().first();
        return new Timing(summaryStartTime,
                          0,
                          queryStartTime == Long.MAX_VALUE ? 0 : queryStartTime,
                          query.getTimeout());
    }

    public static HitCounts createHitCounts(Query query, Result result) {
        com.yahoo.container.handler.Coverage coverage = result.getCoverage(false);

        return (coverage != null)
                ? new HitCounts(result.getHitCount(), result.getConcreteHitCount(), result.getTotalHitCount(),
                                query.getHits(), query.getOffset(), coverage.toLoggingCoverage())
                : new HitCounts(result.getHitCount(), result.getConcreteHitCount(), result.getTotalHitCount(),
                                 query.getHits(), query.getOffset(), null);
    }

}