aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/grouping/result/Group.java
blob: 6bc3d6245f2e9c744ce5aee6235b9bb77e8a3424 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.result;

import com.yahoo.search.result.Hit;
import com.yahoo.search.result.HitGroup;
import com.yahoo.search.result.Relevance;

/**
 * A single group in the grouping result model. A group may contain any number of results (stored
 * as fields, use {@link #getField(String)} to access), {@link GroupList} and {@link HitList}. Use the {@link
 * com.yahoo.search.grouping.GroupingRequest#getResultGroup(com.yahoo.search.Result)} to retrieve an instance of this.
 *
 * @author Simon Thoresen Hult
 */
public class Group extends HitGroup {

    private final GroupId groupId;

    /**
     * Creates a new instance of this class.
     *
     * @param groupId the id to assign to this group
     * @param rel     the relevance of this group
     */
    public Group(GroupId groupId, Relevance rel) {
        super(groupId.toString(), rel);
        setMeta(false);
        setAuxiliary(true);
        this.groupId = groupId;
    }

    /** Returns the id of this group. This is a model of the otherwise flattened {@link #getId() hit id}. */
    public GroupId getGroupId() {
        return groupId;
    }

    /**
     * Returns the {@link HitList} with the given label. The label is the one given to the {@link
     * com.yahoo.search.grouping.request.EachOperation} that generated the list. This method returns null if no such
     * list was found.
     *
     * @param label the label of the list to return
     * @return the requested list, or null
     */
    public HitList getHitList(String label) {
        for (Hit hit : this) {
            if (!(hit instanceof HitList)) {
                continue;
            }
            HitList lst = (HitList)hit;
            if (!label.equals(lst.getLabel())) {
                continue;
            }
            return lst;
        }
        return null;
    }

    /**
     * Returns the {@link GroupList} with the given label. The label is the one given to the {@link
     * com.yahoo.search.grouping.request.EachOperation} that generated the list. This method returns null if no such
     * list was found.
     *
     * @param label the label of the list to return
     * @return the requested list, or null
     */
    public GroupList getGroupList(String label) {
        for (Hit hit : this) {
            if (!(hit instanceof GroupList)) {
                continue;
            }
            GroupList lst = (GroupList)hit;
            if (!label.equals(lst.getLabel())) {
                continue;
            }
            return lst;
        }
        return null;
    }

}