aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/query/rewrite/test/QueryRewriteSearcherTestUtils.java
blob: 3e18b39587e9f2baaa17f4d65b9059bbbbd2214f (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.rewrite.test;

import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.intent.model.IntentModel;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.test.QueryTestCase;
import com.yahoo.search.query.rewrite.RewritesConfig;
import com.yahoo.text.interpretation.Modification;
import com.yahoo.text.interpretation.Interpretation;
import com.yahoo.text.interpretation.Annotations;
import com.yahoo.config.subscription.ConfigGetter;
import com.yahoo.component.chain.Chain;

import java.util.List;

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

/**
 * Test utilities for QueryRewriteSearcher
 *
 * @author Karen Lee
 */
public class QueryRewriteSearcherTestUtils {

    private Execution execution;

    /**
     * Constructor for this class
     * Load the QueryRewriteSearcher and prepare the
     * execution object
     */
    public QueryRewriteSearcherTestUtils(Execution execution) {
        this.execution = execution;
    }

    /**
     * Create config object based on config path
     *
     * @param configPath path for the searcher config
     */
    @SuppressWarnings("deprecation")
    public static RewritesConfig createConfigObj(String configPath) {
        return new ConfigGetter<>(RewritesConfig.class).getConfig(configPath);
    }

    /**
     * Create execution object based on searcher
     *
     * @param searcher searcher to be added to the search chain
     */
    public static Execution createExecutionObj(Searcher searcher) {
        return new Execution(new Chain<>(searcher), Execution.Context.createContextStub());
    }

    /**
     * Create execution object based on searchers
     *
     * @param searchers list of searchers to be added to the search chain
     */
    public static Execution createExecutionObj(List<Searcher> searchers) {
        return new Execution(new Chain<>(searchers), Execution.Context.createContextStub());
    }

    /**
     * Compare the rewritten query returned after executing
     * the origQuery against the provided finalQuery
     *
     * @param origQuery query to be passed to Query object e.g. "?query=will%20smith"
     * @param finalQuery expected final query from result.getQuery() e.g. "query 'AND will smith'"
     */
    public void assertRewrittenQuery(String origQuery, String finalQuery) {
        Query query = new Query(QueryTestCase.httpEncode(origQuery));
        Result result = execution.search(query);
        assertEquals(finalQuery, result.getQuery().toString());
    }

    /**
     * Set the provided intent model
     * Compare the rewritten query returned after executing
     * the origQuery against the provided finalQuery
     *
     * @param origQuery query to be passed to Query object e.g. "?query=will%20smith"
     * @param finalQuery expected final query from result.getQuery() e.g. "query 'AND will smith'"
     * @param intentModel IntentModel to be added to the Query
     */
    public void assertRewrittenQuery(String origQuery, String finalQuery, IntentModel intentModel) {
        Query query = new Query(origQuery);
        intentModel.setTo(query);
        Result result = execution.search(query);
        assertEquals(finalQuery, result.getQuery().toString());
    }

    /**
     * Create a new interpretation with modification that
     * contains the passed in query and score
     *
     * @param spellRewrite query to be used as modification
     * @param score score to be used as modification score
     * @param isQSSRW whether the modification is qss_rw
     * @param isQSSSugg whether the modification is qss_sugg
     * @return newly created interpretation with modification
     */
    public Interpretation createInterpretation(String spellRewrite, double score, boolean isQSSRW, boolean isQSSSugg) {
        Modification modification = new Modification(spellRewrite);
        Annotations annotation = modification.getAnnotation();
        annotation.put("score", score);
        if(isQSSRW)
            annotation.put("qss_rw", true);
        if(isQSSSugg)
            annotation.put("qss_sugg", true);
        Interpretation interpretation = new Interpretation(modification);
        return interpretation;
    }

}