aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/semantics/test/SemanticSearcherTestCase.java
blob: bee65db43477fe01fafa299bca809093c96bcb67 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.semantics.test;

import com.yahoo.component.chain.Chain;
import com.yahoo.prelude.query.WeightedSetItem;
import com.yahoo.search.Query;
import com.yahoo.prelude.query.NullItem;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

/**
 * Tests semantic searching
 *
 * @author bratseth
 */
public class SemanticSearcherTestCase extends RuleBaseAbstractTestCase {

    public SemanticSearcherTestCase() {
        super("rules.sr");
    }

    @Test
    public void testSingleShopping() {
        assertSemantics("brand:sony",
                               "sony");
        assertSemantics("brand:sony!150",
                        "sony!150");
    }

    @Test
    public void testCombinedShopping() {
        assertSemantics("AND brand:sony category:camera",
                        "sony camera");
    }

    @Test
    public void testPhrasedShopping() {
        assertSemantics("AND brand:sony category:\"digital camera\"",
                        "sony digital camera");
    }

    @Test
    public void testSimpleLocal() {
        assertSemantics("AND listing:restaurant place:geary",
                        "restaurant in geary");
    }

    @Test
    public void testLocal() {
        assertSemantics("AND listing:restaurant place:\"geary street san francisco\"",
                        "restaurant in geary street san francisco");
    }

    @Test
    public void testLiteralReplacing() {
        assertSemantics("AND lord of rings", "lotr");
    }

    @Test
    public void testAddingAnd() {
        assertSemantics("AND bar foobar:bar",
                        "bar");
    }

    @Test
    public void testAddingRank() {
        assertSemantics("RANK word foobar:word",
                        "word");
    }

    @Test
    public void testFilterIsIgnored() {
        assertSemantics("RANK word |a |word |b foobar:word",
                        "word&filter=a word b");
        assertSemantics("RANK a |word |b",
                        "a&filter=word b");
    }

    @Test
    public void testAddingNegative() {
        assertSemantics("+java -coffee",
                        "java");
    }

    @Test
    public void testAddingNegativePluralToSingular() {
        assertSemantics("+javas -coffee",
                        "javas");
    }

    @Test
    public void testCombined() {
        assertSemantics("AND bar listing:restaurant place:\"geary street san francisco\" foobar:bar",
                        "bar restaurant in geary street san francisco");
    }

    @Test
    public void testStopWord() {
        assertSemantics("strokes","the strokes");
    }

    @Test
    public void testStopWords1() {
        assertSemantics("strokes","be the strokes");
    }

    @Test
    public void testStopWords2() {
        assertSemantics("strokes","the strokes be");
    }

    @Test
    public void testDontRemoveEverything() {
        assertSemantics("the","the the the");
    }

    @Test
    public void testMoreStopWordRemoval() {
        assertSemantics("hamlet","hamlet to be or not to be");
    }

    @Test
    public void testTypeChange() {
        assertSemantics("RANK default:typechange doors","typechange doors");
    }

    @Test
    public void testTypeChangeWithSingularToPluralButNonReplaceWillNotSingularify() {
        assertSemantics("RANK default:typechange door","typechange door");
    }

    @Test
    public void testExplicitContext() {
        assertSemantics("AND from:paris to:texas","paris to texas");
    }

    @Test
    public void testOrProduction() {
        assertSemantics("OR something somethingelse", "something");
    }

    // This test is order dependent. Fix it!!
    @Test
    public void testWeightedSetItem() {
        Query q = new Query();
        WeightedSetItem weightedSet = new WeightedSetItem("fieldName");
        weightedSet.addToken("a", 1);
        weightedSet.addToken("b", 2);
        q.getModel().getQueryTree().setRoot(weightedSet);
        assertSemantics("WEIGHTEDSET fieldName{[1]:\"a\",[2]:\"b\"}", q);
    }

    @Test
    public void testNullQuery() {
        Query query = new Query(""); // Causes a query containing a NullItem
        doSearch(searcher, query, 0, 10);
        assertEquals(NullItem.class, query.getModel().getQueryTree().getRoot().getClass()); // Still a NullItem
    }

    private Result doSearch(Searcher searcher, Query query, int offset, int hits) {
        query.setOffset(offset);
        query.setHits(hits);
        return createExecution(searcher).search(query);
    }

    private Execution createExecution(Searcher searcher) {
        return new Execution(chainedAsSearchChain(searcher), Execution.Context.createContextStub());
    }

    private Chain<Searcher> chainedAsSearchChain(Searcher topOfChain) {
        List<Searcher> searchers = new ArrayList<>();
        searchers.add(topOfChain);
        return new Chain<>(searchers);
    }

}