aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/querytransform/test/IndexCombinatorTestCase.java
blob: cba2e7da6471ea14481132adfe414dcf3ba38286 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.querytransform.test;

import com.yahoo.config.subscription.ConfigGetter;
import com.yahoo.container.QrSearchersConfig;
import com.yahoo.search.config.IndexInfoConfig;
import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.search.Query;
import com.yahoo.prelude.querytransform.IndexCombinatorSearcher;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.test.QueryTestCase;
import junit.framework.TestCase;

/**
 * Control query transformations when doing index name expansion in QRS.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class IndexCombinatorTestCase extends TestCase {

    private Searcher transformer;
    private IndexFacts f;

    public IndexCombinatorTestCase(String arg0) {
        super(arg0);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        transformer = new IndexCombinatorSearcher();
        f = new IndexFacts();
        f.addIndex("one", "z");
        Index i = new Index("default");
        i.addCommand("match-group a i");
        f.addIndex("one", i);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testDoNothing() {
        Result r = search("?query=z:y");
        assertEquals("z:y", r.getQuery().getModel().getQueryTree().getRoot().toString());
    }

    private Result search(String query) {
        return new Execution(transformer, Execution.Context.createContextStub(f)).search(new Query(QueryTestCase.httpEncode(query)));
    }

    public void testBasic() {
        Result r = search("?query=y");
        assertEquals("OR a:y i:y", r.getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testBasicPair() {
        Result r = search("?query=x y");
        assertEquals(
                "OR (AND a:x a:y) (AND a:x i:y) (AND i:x a:y) (AND i:x i:y)", r
                        .getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testBasicTriplet() {
        Result r = search("?query=x y z");
        assertEquals("AND (OR a:x i:x) (OR a:y i:y) (OR a:z i:z)", r.getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testBasicMixedSinglet() {
        Result r = search("?query=x z:q");
        assertEquals("OR (AND a:x z:q) (AND i:x z:q)", r.getQuery().getModel().getQueryTree().getRoot()
                .toString());
    }

    public void testBasicMixedPair() {
        Result r = search("?query=x y z:q");
        assertEquals(
                "OR (AND a:x a:y z:q) (AND a:x i:y z:q) (AND i:x a:y z:q) (AND i:x i:y z:q)",
                r.getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testBasicMixedTriplet() {
        Result r = search("?query=x y z:q r");
        assertEquals("AND (OR a:x i:x) (OR a:y i:y) z:q (OR a:r i:r)", r
                .getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testBasicOr() {
        Result r = search("?query=x y&type=any");
        assertEquals("OR a:y i:y a:x i:x", r.getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testBasicPhrase() {
        Result r = search("?query=\"x y\"");
        assertEquals("OR a:x y i:x y", r.getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testPhraseAndTerm() {
        Result r = search("?query=\"x y\" z");
        assertEquals(
                "OR (AND a:x y a:z) (AND a:x y i:z) (AND i:x y a:z) (AND i:x y i:z)",
                r.getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testBasicNot() {
        Result r = search("?query=+x -y");
        assertEquals("+(OR a:x i:x) -(OR a:y i:y)", r.getQuery().getModel().getQueryTree().getRoot()
                .toString());
    }

    public void testLessBasicNot() {
        Result r = search("?query=a and b andnot c&type=adv");
        assertEquals(
                "+(OR (AND a:a a:b) (AND a:a i:b) (AND i:a a:b) (AND i:a i:b)) -(OR a:c i:c)",
                r.getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testLongerAndInPositive() {
        Result r = search("?query=a and b and c andnot d&type=adv");
        assertEquals(
                "+(AND (OR a:a i:a) (OR a:b i:b) (OR a:c i:c)) -(OR a:d i:d)", r
                        .getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testTreeInNegativeBranch() {
        Result r = search("?query=a andnot (b and c)&type=adv");
        assertEquals("+(OR a:a i:a) -(AND (OR a:b i:b) (OR a:c i:c))", r
                .getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testSomeTerms() {
        Result r = search("?query=a b -c +d g.h \"abc def\" z:q");
        assertEquals(
                "+(AND (OR a:a i:a) (OR a:b i:b) (OR a:d i:d) (OR a:g h i:g h) (OR a:abc def i:abc def) z:q) -(OR a:c i:c)",
                r.getQuery().getModel().getQueryTree().getRoot().toString());
    }

    public void testMixedIndicesAndAttributes() {
        String indexInfoConfigID = "file:src/test/java/com/yahoo/prelude/querytransform/test/indexcombinator.cfg";
        ConfigGetter<IndexInfoConfig> getter = new ConfigGetter<>(IndexInfoConfig.class);
        IndexInfoConfig config = getter.getConfig(indexInfoConfigID);
        IndexFacts facts = new IndexFacts(new IndexModel(config, (QrSearchersConfig)null));

        Result r = new Execution(transformer, Execution.Context.createContextStub(facts)).search(new Query(QueryTestCase.httpEncode("?query=\"a b\"")));
        assertEquals("OR default:\"a b\" attribute1:a b attribute2:a b", r
                .getQuery().getModel().getQueryTree().getRoot().toString());
        r = new Execution(transformer, Execution.Context.createContextStub(facts)).search(new Query(QueryTestCase.httpEncode("?query=\"a b\" \"c d\"")));
        assertEquals(
                "OR (AND default:\"a b\" default:\"c d\")"
                + " (AND default:\"a b\" attribute1:c d)"
                + " (AND default:\"a b\" attribute2:c d)"
                + " (AND attribute1:a b default:\"c d\")"
                + " (AND attribute1:a b attribute1:c d)"
                + " (AND attribute1:a b attribute2:c d)"
                + " (AND attribute2:a b default:\"c d\")"
                + " (AND attribute2:a b attribute1:c d)"
                + " (AND attribute2:a b attribute2:c d)",
                r.getQuery().getModel().getQueryTree().getRoot().toString());
    }
}