aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/query/rewrite/test/SearchChainDispatcherSearcherTestCase.java
blob: 6325625aec9913c00af564bcc67f6a51021edb0e (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 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 java.util.*;
import java.io.File;

import com.yahoo.search.*;
import com.yahoo.search.searchchain.*;
import com.yahoo.search.query.rewrite.*;
import com.yahoo.search.query.rewrite.rewriters.*;
import com.yahoo.search.searchchain.SearchChainRegistry;
import com.yahoo.search.query.rewrite.RewritesConfig;
import com.yahoo.search.intent.model.*;
import com.yahoo.component.chain.Chain;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
 * Test Cases for SearchChainDispatcherSearcher
 *
 * @author Karen Lee
 */
public class SearchChainDispatcherSearcherTestCase {

    private QueryRewriteSearcherTestUtils utils;
    private final String NAME_REWRITER_CONFIG_PATH = "file:src/test/java/com/yahoo/search/query/rewrite/test/" +
                                                     "test_name_rewriter.cfg";
    private final String NAME_ENTITY_EXPAND_DICT_PATH = "src/test/java/com/yahoo/search/query/rewrite/test/" +
                                                        "name_rewriter_entity.fsa";
    private final String NAME_REWRITER_NAME = NameRewriter.REWRITER_NAME;
    private final String MISSPELL_REWRITER_NAME = MisspellRewriter.REWRITER_NAME;
    private final String US_MARKET_SEARCH_CHAIN = "us_qrw";

    /**
     * Load the QueryRewriteSearcher and prepare the
     * execution object
     */
    @BeforeEach
    public void setUp() {
        // Instantiate Name Rewriter
        RewritesConfig config = QueryRewriteSearcherTestUtils.createConfigObj(NAME_REWRITER_CONFIG_PATH);
        HashMap<String, File> fileList = new HashMap<>();
        fileList.put(NameRewriter.NAME_ENTITY_EXPAND_DICT, new File(NAME_ENTITY_EXPAND_DICT_PATH));
        NameRewriter nameRewriter = new NameRewriter(config, fileList);

        // Instantiate Misspell Rewriter
        MisspellRewriter misspellRewriter = new MisspellRewriter();

        // Create market search chain of two rewriters
        ArrayList<Searcher> searchers = new ArrayList<>();
        searchers.add(misspellRewriter);
        searchers.add(nameRewriter);
        Chain<Searcher> marketSearchChain = new Chain<>(US_MARKET_SEARCH_CHAIN, searchers);

        // Add market search chain to the registry
        SearchChainRegistry registry = new SearchChainRegistry();
        registry.register(marketSearchChain);

        // Instantiate Search Chain Dispatcher Searcher
        SearchChainDispatcherSearcher searchChainDispatcher = new SearchChainDispatcherSearcher();

        // Create a chain containing only the dispatcher
        Chain<Searcher> mainSearchChain = new Chain<>(searchChainDispatcher);
        Execution execution = new Execution(mainSearchChain, Execution.Context.createContextStub(registry));
        utils = new QueryRewriteSearcherTestUtils(execution);
    }

    /**
     * Execute the market chain
     * Query will be rewritten twice
     */
    @Test
    void testMarketChain() {
        IntentModel intentModel = new IntentModel(
                utils.createInterpretation("wills smith", 0.9,
                        true, false),
                utils.createInterpretation("will smith", 1.0,
                        false, true));

        utils.assertRewrittenQuery("?query=willl+smith&type=all&QRWChain=" + US_MARKET_SEARCH_CHAIN + "&" +
                MISSPELL_REWRITER_NAME + "." + RewriterConstants.QSS_RW + "=true&" +
                MISSPELL_REWRITER_NAME + "." + RewriterConstants.QSS_SUGG + "=true&" +
                NAME_REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_UNIT_EQUIV +
                "=true&" +
                NAME_REWRITER_NAME + "." + RewriterConstants.ORIGINAL_AS_UNIT_EQUIV + "=true",
                "query 'OR (AND willl smith) (AND will smith) " +
                        "\"will smith\" \"will smith movies\" " +
                        "\"will smith news\" \"will smith imdb\" " +
                        "\"will smith lyrics\" \"will smith dead\" " +
                        "\"will smith nfl\" \"will smith new movie hancock\" " +
                        "\"will smith biography\"'",
                intentModel);
    }

    /**
     * Market chain is not valid
     * Query will be passed to next rewriter
     */
    @Test
    void testInvalidMarketChain() {
        utils.assertRewrittenQuery("?query=will smith&type=all&QRWChain=abc&" +
                MISSPELL_REWRITER_NAME + "." + RewriterConstants.QSS_RW + "=true&" +
                MISSPELL_REWRITER_NAME + "." + RewriterConstants.QSS_SUGG + "=true&" +
                NAME_REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_UNIT_EQUIV +
                "=true",
                "query 'AND will smith'");
    }

    /**
     * Empty market chain value
     * Query will be passed to next rewriter
     */
    @Test
    void testEmptyMarketChain() {
        utils.assertRewrittenQuery("?query=will smith&type=all&QRWChain=&" +
                MISSPELL_REWRITER_NAME + "." + RewriterConstants.QSS_RW + "=true&" +
                MISSPELL_REWRITER_NAME + "." + RewriterConstants.QSS_SUGG + "=true&" +
                NAME_REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_UNIT_EQUIV +
                "=true",
                "query 'AND will smith'");
    }

    /**
     * Searchers down the chain after SearchChainDispatcher
     * should be executed
     */
    @Test
    void testChainContinuation() {
        // Instantiate Name Rewriter
        RewritesConfig config = QueryRewriteSearcherTestUtils.createConfigObj(NAME_REWRITER_CONFIG_PATH);
        HashMap<String, File> fileList = new HashMap<>();
        fileList.put(NameRewriter.NAME_ENTITY_EXPAND_DICT, new File(NAME_ENTITY_EXPAND_DICT_PATH));
        NameRewriter nameRewriter = new NameRewriter(config, fileList);

        // Instantiate Misspell Rewriter
        MisspellRewriter misspellRewriter = new MisspellRewriter();

        // Create market search chain of only misspell rewriter
        Chain<Searcher> marketSearchChain = new Chain<>(US_MARKET_SEARCH_CHAIN, misspellRewriter);

        // Add market search chain to the registry
        SearchChainRegistry registry = new SearchChainRegistry();
        registry.register(marketSearchChain);

        // Instantiate Search Chain Dispatcher Searcher
        SearchChainDispatcherSearcher searchChainDispatcher = new SearchChainDispatcherSearcher();

        // Create a chain containing the dispatcher and the name rewriter
        ArrayList<Searcher> searchers = new ArrayList<>();
        searchers.add(searchChainDispatcher);
        searchers.add(nameRewriter);

        // Create a chain containing only the dispatcher
        Chain<Searcher> mainSearchChain = new Chain<>(searchers);
        Execution execution = new Execution(mainSearchChain, Execution.Context.createContextStub(registry));
        new QueryRewriteSearcherTestUtils(execution);

        IntentModel intentModel = new IntentModel(
                utils.createInterpretation("wills smith", 0.9,
                        true, false),
                utils.createInterpretation("will smith", 1.0,
                        false, true));

        utils.assertRewrittenQuery("?query=willl+smith&type=all&QRWChain=" + US_MARKET_SEARCH_CHAIN + "&" +
                MISSPELL_REWRITER_NAME + "." + RewriterConstants.QSS_RW + "=true&" +
                MISSPELL_REWRITER_NAME + "." + RewriterConstants.QSS_SUGG + "=true&" +
                NAME_REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_UNIT_EQUIV +
                "=true&" +
                NAME_REWRITER_NAME + "." + RewriterConstants.ORIGINAL_AS_UNIT_EQUIV + "=true",
                "query 'OR (AND willl smith) (AND will smith) " +
                        "\"will smith\" \"will smith movies\" " +
                        "\"will smith news\" \"will smith imdb\" " +
                        "\"will smith lyrics\" \"will smith dead\" " +
                        "\"will smith nfl\" \"will smith new movie hancock\" " +
                        "\"will smith biography\"'",
                intentModel);
    }

}