aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/querytransform/BooleanSearcherTestCase.java
blob: 247c96f06767df0201eb66bc4a4eceb300506af3 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.querytransform;

import com.yahoo.component.chain.Chain;
import com.yahoo.prelude.query.PredicateQueryItem;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Collection;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * Test BooleanSearcher
 */
public class BooleanSearcherTestCase {

    private Execution exec;

    private Execution buildExec() {
        return new Execution(new Chain<Searcher>(new BooleanSearcher()),
                Execution.Context.createContextStub());
    }

    @BeforeEach
    public void setUp() throws Exception {
        exec = buildExec();
    }

    @Test
    void requireThatAttributeMapToSingleFeature() {
        PredicateQueryItem item = buildPredicateQueryItem("{gender:female}", null);
        assertEquals(1, item.getFeatures().size());
        assertEquals(0, item.getRangeFeatures().size());
        assertEquals("gender", item.getFeatures().iterator().next().getKey());
        assertEquals("female", item.getFeatures().iterator().next().getValue());
        assertEquals("PREDICATE_QUERY_ITEM gender=female", item.toString());
    }

    @Test
    void requireThatAttributeListMapToMultipleFeatures() {
        PredicateQueryItem item = buildPredicateQueryItem("{gender:[female,male]}", null);
        assertEquals(2, item.getFeatures().size());
        assertEquals(0, item.getRangeFeatures().size());
        assertEquals("PREDICATE_QUERY_ITEM gender=female, gender=male", item.toString());
    }

    @Test
    void requireThatRangeAttributesMapToRangeTerm() {
        PredicateQueryItem item = buildPredicateQueryItem(null, "{age:25}");
        assertEquals(0, item.getFeatures().size());
        assertEquals(1, item.getRangeFeatures().size());
        assertEquals("PREDICATE_QUERY_ITEM age:25", item.toString());

        item = buildPredicateQueryItem(null, "{age:25:0x43, height:170:[2,3,4]}");
        assertEquals(0, item.getFeatures().size());
        assertEquals(2, item.getRangeFeatures().size());
    }

    @Test
    void requireThatQueryWithoutBooleanPropertiesIsUnchanged() {
        Query q = new Query("");
        q.getModel().getQueryTree().setRoot(new WordItem("foo", "otherfield"));
        Result r = exec.search(q);

        WordItem root = (WordItem) r.getQuery().getModel().getQueryTree().getRoot();
        assertEquals("foo", root.getWord());
    }

    @Test
    void requireThatBooleanSearcherCanBuildPredicateQueryItem() {
        PredicateQueryItem root = buildPredicateQueryItem("{gender:female}", "{age:23:[2, 3, 5]}");

        Collection<PredicateQueryItem.Entry> features = root.getFeatures();
        assertEquals(1, features.size());
        PredicateQueryItem.Entry entry = (PredicateQueryItem.Entry) features.toArray()[0];
        assertEquals("gender", entry.getKey());
        assertEquals("female", entry.getValue());
        assertEquals(-1L, entry.getSubQueryBitmap());

        Collection<PredicateQueryItem.RangeEntry> rangeFeatures = root.getRangeFeatures();
        assertEquals(1, rangeFeatures.size());
        PredicateQueryItem.RangeEntry rangeEntry = (PredicateQueryItem.RangeEntry) rangeFeatures.toArray()[0];
        assertEquals("age", rangeEntry.getKey());
        assertEquals(23L, rangeEntry.getValue());
        assertEquals(44L, rangeEntry.getSubQueryBitmap());
    }

    @Test
    void requireThatKeysAndValuesCanContainSpaces() {
        PredicateQueryItem item = buildPredicateQueryItem("{'My Key':'My Value'}", null);
        assertEquals(1, item.getFeatures().size());
        assertEquals(0, item.getRangeFeatures().size());
        assertEquals("My Key", item.getFeatures().iterator().next().getKey());
        assertEquals("'My Value'", item.getFeatures().iterator().next().getValue());
        assertEquals("PREDICATE_QUERY_ITEM My Key='My Value'", item.toString());
    }

    private PredicateQueryItem buildPredicateQueryItem(String attributes, String rangeAttributes) {
        Query q = buildQuery("predicate", attributes, rangeAttributes);
        Result r = exec.search(q);
        return (PredicateQueryItem)r.getQuery().getModel().getQueryTree().getRoot();
    }

    private Query buildQuery(String field, String attributes, String rangeAttributes) {
        Query q = new Query("");
        q.properties().set("boolean.field", field);
        if (attributes != null) {
            q.properties().set("boolean.attributes", attributes);
        }
        if (rangeAttributes != null) {
            q.properties().set("boolean.rangeAttributes", rangeAttributes);
        }
        return q;
    }

}