aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query/EquivItem.java
blob: d6b576951f934723c6979e800abd5013169c1cfa (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query;

import com.yahoo.protect.Validator;

import java.util.Collection;

/**
 * An Item where each child is an <i>alternative</i> which can be matched.
 * Produces the same recall as Or, but differs in that the relevance of a match
 * does not increase if more than one children is matched: With Equiv, matching one child perfectly is a perfect match.
 * <p>
 * This can only have Word, WordAlternatives, Exact, Int or Phrase children.
 *
 * @author havardpe
 */
public class EquivItem extends CompositeTaggableItem {

    /** Makes an EQUIV item with no children */
    public EquivItem() {}

    /**
     * Creates an EQUIV with the given item as child. The new EQUIV will take connectivity,
     * significance and weight from the given item.
     *
     * @param item will be modified and added as a child
     */
    public EquivItem(Item item) {
        addItem(item);

        // steal other item's connectivity:
        if (item.connectedItem != null) {
            setConnectivity(item.connectedItem, item.connectivity);
            item.connectedItem = null;
            item.connectivity = 0.0;
        }
        TaggableItem back = (TaggableItem)item.connectedBacklink;
        if (back != null) {
            back.setConnectivity(this, back.getConnectivity());
            item.connectedBacklink = null;
        }

        // steal other item's significance:
        if (item.explicitSignificance) {
            setSignificance(item.significance);
        }

        // steal other item's weight:
        setWeight(item.getWeight());

        // we have now stolen all the other item's unique id needs:
        item.setHasUniqueID(false);
    }

    /**
     * Creates an EQUIV with the given item and a set of alternate words as children.
     * The new EQUIV will take connectivity, significance and weight from the given item.
     *
     * @param item will be modified and added as a child
     * @param words set of words to create WordItems from
     */
    public EquivItem(Item item, Collection<String> words) {
        this(item);
        String idx = ((IndexedItem)item).getIndexName();
        for (String word : words) {
            WordItem witem = new WordItem(word, idx);
            addItem(witem);
        }
    }

    @Override
    public ItemType getItemType() {
        return ItemType.EQUIV;
    }

    @Override
    public String getName() {
        return "EQUIV";
    }

    @Override
    protected void adding(Item item) {
        super.adding(item);
        Validator.ensure("Could not add an item of type " + item.getItemType() +
                         ": Equiv can only have word, wordAlternatives, int, exact, or phrase as children",
                         acceptsChildrenOfType(item.getItemType()));
    }

    @Override
    public boolean acceptsItemsOfType(ItemType itemType) {
        return acceptsChildrenOfType(itemType);
    }

    /** Returns true if this accepts child items of the given type */
    public static boolean acceptsChildrenOfType(ItemType itemType) {
        return itemType == ItemType.WORD ||
               itemType == ItemType.WORD_ALTERNATIVES ||
               itemType == ItemType.INT ||
               itemType == ItemType.EXACT ||
               itemType == ItemType.PHRASE;
    }

}