summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/semantics/test/SemanticSearcherTestCase.java
blob: a3779351a6d20390b78d325ac37113d6badcb344 (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.semantics.test;

import com.google.common.util.concurrent.MoreExecutors;
import com.yahoo.component.chain.Chain;
import com.yahoo.language.Linguistics;
import com.yahoo.language.simple.SimpleLinguistics;
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.rendering.RendererRegistry;
import com.yahoo.search.searchchain.Execution;

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

/**
 * Tests semantic searching
 *
 * @author bratseth
 */
@SuppressWarnings("deprecation")
public class SemanticSearcherTestCase extends RuleBaseAbstractTestCase {

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

    public void testSingleShopping() {
        assertSemantics("brand:sony",
                               "sony");
    }

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

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

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

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

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

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

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

    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");
    }

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

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

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

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

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

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

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

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

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

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

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

    public void testPluralReplaceBecomesSingular() {
        assertSemantics("AND from:paris to:texas","pariss to texass");
    }

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

    //This test is order dependent. Fix it!!
    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);
    }

    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) {
        Execution.Context context = new Execution.Context(null, null, null, new RendererRegistry(MoreExecutors.directExecutor()), new SimpleLinguistics());
        return new Execution(chainedAsSearchChain(searcher), context);
    }

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

}