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

import com.yahoo.component.chain.Chain;
import com.yahoo.config.subscription.ConfigGetter;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchers.ValidateMatchPhaseSearcher;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.vespa.config.search.AttributesConfig;
import org.junit.jupiter.api.Test;

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

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

/**
 * @author baldersheim
 */
public class ValidateMatchPhaseSearcherTestCase {

    private final ValidateMatchPhaseSearcher searcher;

    @SuppressWarnings("deprecation")
    public ValidateMatchPhaseSearcherTestCase() {
        searcher = new ValidateMatchPhaseSearcher(
                ConfigGetter.getConfig(AttributesConfig.class,
                                       "raw:" +
                                                     "attribute[4]\n" +
                                                     "attribute[0].name                ok\n" +
                                                     "attribute[0].datatype            INT32\n" +
                                                     "attribute[0].collectiontype      SINGLE\n" +
                                                     "attribute[0].fastsearch          true\n" +
                                                     "attribute[1].name                not_fast\n" +
                                                     "attribute[1].datatype            INT32\n" +
                                                     "attribute[1].collectiontype      SINGLE\n" +
                                                     "attribute[1].fastsearch          false\n" +
                                                     "attribute[2].name                not_numeric\n" +
                                                     "attribute[2].datatype            STRING\n" +
                                                     "attribute[2].collectiontype      SINGLE\n" +
                                                     "attribute[2].fastsearch          true\n" +
                                                     "attribute[3].name                not_single\n" +
                                                     "attribute[3].datatype            INT32\n" +
                                                     "attribute[3].collectiontype      ARRAY\n" +
                                                     "attribute[3].fastsearch          true"
        ));
    }

    private static String getErrorMatch(String attribute) {
        return "4: Invalid query parameter: The attribute '" +
                attribute +
                "' is not available for match-phase. It must be a single value numeric attribute with fast-search.";
    }

    private static String getErrorDiversity(String attribute) {
        return "4: Invalid query parameter: The attribute '" +
                attribute +
                "' is not available for match-phase diversification. It must be a single value numeric or string attribute.";
    }

    @Test
    void testMatchPhaseAttribute() {
        assertEquals("", search(""));
        assertEquals("", match("ok"));
        assertEquals(getErrorMatch("not_numeric"), match("not_numeric"));
        assertEquals(getErrorMatch("not_single"), match("not_single"));
        assertEquals(getErrorMatch("not_fast"), match("not_fast"));
        assertEquals(getErrorMatch("not_found"), match("not_found"));
    }

    @Test
    void testDiversityAttribute() {
        assertEquals("", search(""));
        assertEquals("", diversify("ok"));
        assertEquals("", diversify("not_numeric"));
        assertEquals(getErrorDiversity("not_single"), diversify("not_single"));
        assertEquals("", diversify("not_fast"));
        assertEquals(getErrorDiversity("not_found"), diversify("not_found"));
    }

    private String match(String m) {
        return search("&ranking.matchPhase.attribute=" + m);
    }

    private String diversify(String m) {
        return search("&ranking.matchPhase.attribute=ok&ranking.matchPhase.diversity.attribute=" + m);
    }

    private String search(String m) {
        String q = "/?query=sddocname:test" + m;
        Result r = doSearch(searcher, new Query(q), 0, 10);
        if (r.hits().getError() != null) {
            return r.hits().getError().toString();
        }
        return "";
    }

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

}