aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/test/QueryWithFilterTestCase.java
blob: 3920a95bd982f5b50506ac59592597a8d9f48fea (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.test;

import com.yahoo.language.Language;
import com.yahoo.language.Linguistics;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.SearchDefinition;
import com.yahoo.search.Query;
import com.yahoo.search.searchchain.Execution;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author bratseth
 */
public class QueryWithFilterTestCase {

    /** Tests that default-index is not applied to ALL filters */
    @Test
    void testRankFilter() {
        Query q = newQueryFromEncoded("?query=trump" +
                                      "&model.type=all" +
                                      "&model.defaultIndex=text" +
                                      "&filter=filterattribute%3Afrontpage_US_en-US");
        assertEquals("RANK text:trump |filterattribute:frontpage_US_en-US",
                     q.getModel().getQueryTree().toString());
    }

    /** Tests that default-index is not applied to NOT filters */
    @Test
    void testAndFilter() {
        Query q = newQueryFromEncoded("?query=trump" +
                                      "&model.type=all" +
                                      "&model.defaultIndex=text" +
                                      "&filter=%2B%28filterattribute%3Afrontpage_US_en-US%29");
        assertEquals("AND text:trump |filterattribute:frontpage_US_en-US",
                     q.getModel().getQueryTree().toString());
    }

    /** Tests that default-index is not applied to NOT filters */
    @Test
    void testAndFilterWithoutExplicitIndex() {
        Query q = newQueryFromEncoded("?query=trump" +
                                      "&model.type=all" +
                                      "&model.defaultIndex=text" +
                                      "&filter=%2B%28filterTerm%29");
        assertEquals("AND text:trump |text:filterTerm",
                     q.getModel().getQueryTree().toString());
    }

    private Query newQueryFromEncoded(String queryString) {
        return newQueryFromEncoded(queryString, null, new SimpleLinguistics());
    }

    private Query newQueryFromEncoded(String encodedQueryString, Language language, Linguistics linguistics) {
        Query query = new Query(encodedQueryString);
        query.getModel().setExecution(new Execution(Execution.Context.createContextStub(createIndexFacts(),
                                                                                        linguistics)));
        query.getModel().setLanguage(language);
        return query;
    }

    private IndexFacts createIndexFacts() {
        SearchDefinition sd = new SearchDefinition("test");
        sd.addIndex(new Index("test"));
        sd.addIndex(attribute("filterattribute"));
        return new IndexFacts(new IndexModel(sd));
    }

    private Index attribute(String name) {
        Index index = new Index(name);
        index.setExact(true, null);
        return index;
    }

}