aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/federation/vespa/test/QueryMarshallerTestCase.java
blob: 022177bc42b657e96e3aa4f1246c8cb317514ed0 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.federation.vespa.test;

import com.yahoo.language.Linguistics;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.query.*;
import com.yahoo.search.Query;
import com.yahoo.search.federation.vespa.QueryMarshaller;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.test.QueryTestCase;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class QueryMarshallerTestCase {

    private static final Linguistics linguistics = new SimpleLinguistics();

    @Test
    public void testCommonCommonCase() {
        AndItem root = new AndItem();
        addThreeWords(root);
        assertEquals("a AND b AND c", new QueryMarshaller().marshal(root));
    }

    @Test
    public void testPhrase() {
        PhraseItem root = new PhraseItem();
        root.setIndexName("habla");
        addThreeWords(root);
        assertEquals("habla:\"a b c\"", new QueryMarshaller().marshal(root));
    }

    @Test
    public void testPhraseDefaultIndex() {
        PhraseItem root = new PhraseItem();
        addThreeWords(root);
        assertEquals("\"a b c\"", new QueryMarshaller().marshal(root));
    }

    @Test
    public void testLittleMoreComplex() {
        AndItem root = new AndItem();
        addThreeWords(root);
        OrItem ambig = new OrItem();
        root.addItem(ambig);
        addThreeWords(ambig);
        AndItem but = new AndItem();
        addThreeWords(but);
        ambig.addItem(but);
        assertEquals("a AND b AND c AND ( a OR b OR c OR ( a AND b AND c ) )",
                     new QueryMarshaller().marshal(root));
    }

    @Test
    public void testRank() {
        RankItem root = new RankItem();
        addThreeWords(root);
        assertEquals("a RANK b RANK c", new QueryMarshaller().marshal(root));
    }

    @Test
    public void testNear() {
        NearItem near = new NearItem(3);
        addThreeWords(near);
        assertEquals("a NEAR(3) b NEAR(3) c", new QueryMarshaller().marshal(near));
    }

    @Test
    public void testONear() {
        ONearItem oNear = new ONearItem(3);
        addThreeWords(oNear);
        assertEquals("a ONEAR(3) b ONEAR(3) c", new QueryMarshaller().marshal(oNear));
    }

    private void addThreeWords(CompositeItem root) {
        root.addItem(new WordItem("a"));
        root.addItem(new WordItem("b"));
        root.addItem(new WordItem("c"));
    }

    @Test
    public void testNegativeGroupedTerms() {
        testQueryString(new QueryMarshaller(), "a -(b c) -(d e)",
                        "a ANDNOT ( b AND c ) ANDNOT ( d AND e )");
    }

    @Test
    public void testPositiveGroupedTerms() {
        testQueryString(new QueryMarshaller(), "a (b c)", "a AND ( b OR c )");
    }

    @Test
    public void testInt() {
        testQueryString(new QueryMarshaller(), "yahoo 123", "yahoo AND 123");
    }

    @Test
    public void testCJKOneWord() {
        testQueryString(new QueryMarshaller(), "天龍人");
    }

    @Test
    public void testTwoWords() {
        testQueryString(new QueryMarshaller(), "John Smith", "John AND Smith", null, new SimpleLinguistics());
    }

    @Test
    public void testTwoWordsInPhrase() {
        testQueryString(new QueryMarshaller(), "\"John Smith\"", "\"John Smith\"", null, new SimpleLinguistics());
    }

    @Test
    public void testCJKTwoSentences() {
        testQueryString(new QueryMarshaller(), "是不是這樣的夜晚 你才會這樣地想起我", "是不是這樣的夜晚 AND 你才會這樣地想起我");
    }

    @Test
    public void testCJKTwoSentencesWithLanguage() {
        testQueryString(new QueryMarshaller(), "助妳好孕 生1胎北市發2萬", "助妳好孕 AND 生1胎北市發2萬", "zh-Hant");
    }

    @Test
    public void testCJKTwoSentencesInPhrase() {
        QueryMarshaller marshaller = new QueryMarshaller();
        testQueryString(marshaller, "\"助妳好孕 生1胎北市發2萬\"", "\"助妳好孕 生1胎北市發2萬\"", "zh-Hant");
        testQueryString(marshaller, "\"是不是這樣的夜晚 你才會這樣地想起我\"", "\"是不是這樣的夜晚 你才會這樣地想起我\"");
    }

    @Test
    public void testCJKMultipleSentences() {
        testQueryString(new QueryMarshaller(), "염부장님과 함께했던 좋은 추억들은", "염부장님과 AND 함께했던 AND 좋은 AND 추억들은");
    }

    @Test
    public void testIndexRestriction() {
        /** ticket 3707606, comment #29 */
        testQueryString(new QueryMarshaller(), "site:nytimes.com", "site:\"nytimes com\"");
    }

    private void testQueryString(QueryMarshaller marshaller, String uq) {
        testQueryString(marshaller, uq, uq, null);
    }

    private void testQueryString(QueryMarshaller marshaller, String uq, String mq) {
        testQueryString(marshaller, uq, mq, null);
    }

    private void testQueryString(QueryMarshaller marshaller, String uq, String mq, String lang) {
        testQueryString(marshaller, uq, mq, lang, linguistics);
    }

    private void testQueryString(QueryMarshaller marshaller, String uq, String mq, String lang, Linguistics linguistics) {
        Query query = new Query("/?query=" + QueryTestCase.httpEncode(uq) + ((lang != null) ? "&language=" + lang : ""));
        query.getModel().setExecution(new Execution(new Execution.Context(null, new IndexFacts(), null, null, linguistics)));
        assertEquals(mq, marshaller.marshal(query.getModel().getQueryTree().getRoot()));
    }

}