aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/engine/ReferencedMatches.java
blob: b7083414e85911858e834dad4576c92bc704da67 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.semantics.engine;

import java.util.Iterator;
import java.util.List;

import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.PhraseItem;

/**
 * The matches referenced by a particular context name in a rule evaluation
 *
 * @author bratseth
 */
public class ReferencedMatches {

    private final String contextName;

    private final List<Match> matches = new java.util.ArrayList<>(1);

    public ReferencedMatches(String contextName) {
        this.contextName = contextName;
    }

    public void addMatch(Match match) {
        matches.add(match);
    }

    public String getContextName() { return contextName; }

    public Iterator<Match> matchIterator() {
        return matches.iterator();
    }

    /**
     * Returns the item to insert from these referenced matches, or null if none
     *
     * @param label the label of the matches
     */
    public Item toItem(String label) {
        if (matches.size() == 0) return null;
        if (matches.size() == 1) return matches.get(0).toItem(label);

        PhraseItem phrase = new PhraseItem(); // TODO: Somehow allow AND items instead here
        phrase.setIndexName(label);
        for (Iterator<Match> i = matches.iterator(); i.hasNext(); ) {
            phrase.addItem(i.next().toItem(label));
        }
        return phrase;
    }

}