aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/grouping/vespa/HitConverter.java
blob: 388f528b535c56ebb21fa792dc91dc8f494249fe (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.vespa;

import com.yahoo.fs4.QueryPacketData;
import com.yahoo.prelude.fastsearch.DocsumDefinitionSet;
import com.yahoo.prelude.fastsearch.FastHit;
import com.yahoo.prelude.fastsearch.GroupingListHit;
import com.yahoo.search.Query;
import com.yahoo.search.Searcher;
import com.yahoo.search.result.Hit;
import com.yahoo.searchlib.aggregation.FS4Hit;
import com.yahoo.searchlib.aggregation.VdsHit;

/**
 * Implementation of the {@link ResultBuilder.HitConverter} interface for {@link GroupingExecutor}.
 *
 * @author Simon Thoresen
 */
class HitConverter implements ResultBuilder.HitConverter {

    private final Searcher searcher;
    private final Query query;

    /**
     * Creates a new instance of this class.
     *
     * @param searcher The searcher that owns this converter.
     * @param query    The query that returned the hits.
     */
    public HitConverter(Searcher searcher, Query query) {
        this.searcher = searcher;
        this.query = query;
    }

    @Override
    public com.yahoo.search.result.Hit toSearchHit(String summaryClass, com.yahoo.searchlib.aggregation.Hit hit) {
        if (hit instanceof FS4Hit) {
            return convertFs4Hit(summaryClass, (FS4Hit)hit);
        } else if (hit instanceof VdsHit) {
            return convertVdsHit(summaryClass, (VdsHit)hit);
        } else {
            throw new UnsupportedOperationException("Hit type '" + hit.getClass().getName() + "' not supported.");
        }
    }

    private Hit convertFs4Hit(String summaryClass, FS4Hit groupHit) {
        FastHit hit = new FastHit();
        hit.setRelevance(groupHit.getRank());
        hit.setGlobalId(groupHit.getGlobalId());
        hit.setPartId(groupHit.getPath(), 0);
        hit.setDistributionKey(groupHit.getDistributionKey());
        hit.setFillable();
        hit.setSearcherSpecificMetaData(searcher, summaryClass);

        Hit ctxHit = (Hit)groupHit.getContext();
        if (ctxHit == null) {
            throw new NullPointerException("Hit has no context.");
        }
        hit.setSource(ctxHit.getSource());
        hit.setSourceNumber(ctxHit.getSourceNumber());
        hit.setQuery(ctxHit.getQuery());

        if (ctxHit instanceof GroupingListHit) {
            // in a live system the ctxHit can only by GroupingListHit, but because the code used Hit prior to version
            // 5.10 we need to check to avoid breaking existing unit tests -- both internally and with customers
            QueryPacketData queryPacketData = ((GroupingListHit)ctxHit).getQueryPacketData();
            if (queryPacketData != null) {
                hit.setQueryPacketData(queryPacketData);
            }
        }
        return hit;
    }

    private Hit convertVdsHit(String summaryClass, VdsHit grpHit) {
        FastHit ret = new FastHit();
        ret.setRelevance(grpHit.getRank());
        if (grpHit.getSummary().getData().length > 0) {
            GroupingListHit ctxHit = (GroupingListHit)grpHit.getContext();
            if (ctxHit == null) {
                throw new NullPointerException("Hit has no context.");
            }
            DocsumDefinitionSet defs = ctxHit.getDocsumDefinitionSet();
            defs.lazyDecode(summaryClass, grpHit.getSummary().getData(), ret);
            ret.setFilled(summaryClass);
            ret.setFilled(query.getPresentation().getSummary());
        }
        return ret;
    }
}