aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/querytransform/test/QueryCombinatorTestCase.java
blob: 995c1cc7a722785cacb4718979f005a37a0b196f (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
166
167
168
169
170
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.querytransform.test;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.yahoo.component.ComponentId;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.query.AndItem;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.search.Query;
import com.yahoo.search.Searcher;
import com.yahoo.search.querytransform.QueryCombinator;
import com.yahoo.search.searchchain.Execution;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
 * Unit testing of the searcher com.yahoo.search.querytransform.QueryCombinator.
 *
 * @author Steinar Knutsen
 */
public class QueryCombinatorTestCase {

    Searcher searcher;

    @Before
    @SuppressWarnings("deprecation")
    public void setUp() throws Exception {
        searcher = new QueryCombinator(new ComponentId("combinationTest"));
    }

    @Test
    public void testStraightForwardSearch() {
        Query q = new Query("?query=a&query.juhu=b");
        Execution e = new Execution(searcher, Execution.Context.createContextStub(new IndexFacts()));
        e.search(q);
        assertEquals("AND a b", q.getModel().getQueryTree().toString());
        q = new Query("?query=a&query.juhu=b&defidx.juhu=juhu.22[gnuff]");
        e = new Execution(searcher,  Execution.Context.createContextStub(new IndexFacts()));
        e.search(q);
        assertEquals("AND a juhu.22[gnuff]:b", q.getModel().getQueryTree().toString());
        q = new Query("?query=a&query.juhu=");
        e = new Execution(searcher,  Execution.Context.createContextStub(new IndexFacts()));
        e.search(q);
        assertEquals("a", q.getModel().getQueryTree().toString());
        q = new Query("?query=a+c&query.juhu=b");
        e = new Execution(searcher,  Execution.Context.createContextStub(new IndexFacts()));
        e.search(q);
        assertEquals("AND a c b", q.getModel().getQueryTree().toString());
    }

    @Test
    public void testNoBaseQuery() {
        Query q = new Query("?query.juhu=b");
        Execution e = new Execution(searcher,  Execution.Context.createContextStub(new IndexFacts()));
        e.search(q);
        assertEquals("b", q.getModel().getQueryTree().toString());
    }

    @Test
    public void testDefaultIndexWithoutQuery() {
        Query q = new Query("?defidx.juhu=b");
        Execution e = new Execution(searcher, Execution.Context.createContextStub(new IndexFacts()));
        e.search(q);
        assertEquals("NULL", q.getModel().getQueryTree().toString());
        q = new Query("?query=a&defidx.juhu=b");
        e = new Execution(searcher,  Execution.Context.createContextStub(new IndexFacts()));
        e.search(q);
        assertEquals("a", q.getModel().getQueryTree().toString());
    }

    private static class StringPair {

        public final String index;
        public final String value;

        StringPair(String index, String value) {
            super();
            this.index = index;
            this.value = value;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((index == null) ? 0 : index.hashCode());
            result = prime * result + ((value == null) ? 0 : value.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            final StringPair other = (StringPair) obj;
            if (index == null) {
                if (other.index != null)
                    return false;
            } else if (!index.equals(other.index))
                return false;
            if (value == null) {
                if (other.value != null)
                    return false;
            } else if (!value.equals(other.value))
                return false;
            return true;
        }

    }

    @Test
    public void testMultiPart() {
        Query q = new Query("?query=a&query.juhu=b&query.nalle=c");
        Execution e = new Execution(searcher,  Execution.Context.createContextStub(new IndexFacts()));
        Set<String> items = new HashSet<>();
        items.add("a");
        items.add("b");
        items.add("c");
        e.search(q);
        // OK, the problem here is we have no way of knowing whether nalle or
        // juhu was added first, since we have passed through HashMap instances
        // inside the implementation

        AndItem root = (AndItem) q.getModel().getQueryTree().getRoot();
        Iterator<?> iterator = root.getItemIterator();
        while (iterator.hasNext()) {
            WordItem word = (WordItem) iterator.next();
            if (items.contains(word.stringValue())) {
                items.remove(word.stringValue());
            } else {
                assertFalse("Got unexpected item in query tree: " + word.stringValue(), true);
            }
        }
        assertEquals("Not all expected items found in query.", 0, items.size());

        Set<StringPair> nastierItems = new HashSet<>();
        nastierItems.add(new StringPair("", "a"));
        nastierItems.add(new StringPair("juhu.22[gnuff]", "b"));
        nastierItems.add(new StringPair("gnuff[8].name(\"tralala\")", "c"));
        q = new Query("?query=a&query.juhu=b&defidx.juhu=juhu.22[gnuff]&query.nalle=c&defidx.nalle=gnuff[8].name(%22tralala%22)");
        e = new Execution(searcher,  Execution.Context.createContextStub(new IndexFacts()));
        e.search(q);
        root = (AndItem) q.getModel().getQueryTree().getRoot();
        iterator = root.getItemIterator();
        while (iterator.hasNext()) {
            WordItem word = (WordItem) iterator.next();
            StringPair asPair = new StringPair(word.getIndexName(), word.stringValue());
            if (nastierItems.contains(asPair)) {
                nastierItems.remove(asPair);
            } else {
                assertFalse("Got unexpected item in query tree: ("
                        + word.getIndexName() + ", " + word.stringValue() + ")",
                        true);
            }
        }
        assertEquals("Not all expected items found in query.", 0, nastierItems.size());
    }

}