aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/query/rewrite/test/QueryRewriteSearcherTestCase.java
blob: 5e3f3d0d4b9b6dac3f6445b859e2597d914cfc0f (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
// 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.io.File;
import java.util.*;

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

/**
 * Generic Test Cases for QueryRewriteSearcher
 *
 * @author Karen Lee
 */
public class QueryRewriteSearcherTestCase {

    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 FAKE_FSA_CONFIG_PATH = "file:src/test/java/com/yahoo/search/query/rewrite/test/" +
                                                "test_rewriter_fake_fsa.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 FAKE_FSA_PATH = "src/test/java/com/yahoo/search/query/rewrite/test/" +
                                         "test_name_rewriter.cfg";
    private final String NAME_REWRITER_NAME = NameRewriter.REWRITER_NAME;
    private final String MISSPELL_REWRITER_NAME = MisspellRewriter.REWRITER_NAME;

    /**
     * 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 a chain of two rewriters
        ArrayList<Searcher> searchers = new ArrayList<>();
        searchers.add(misspellRewriter);
        searchers.add(nameRewriter);

        Execution execution = QueryRewriteSearcherTestUtils.createExecutionObj(searchers);
        utils = new QueryRewriteSearcherTestUtils(execution);
    }

    /**
     * Invalid FSA config path
     * Query will be passed to next rewriter
     */
    @Test
    void testInvalidFSAConfigPath() {
        // Instantiate Name Rewriter with fake FSA path
        RewritesConfig config = QueryRewriteSearcherTestUtils.createConfigObj(FAKE_FSA_CONFIG_PATH);
        HashMap<String, File> fileList = new HashMap<>();
        fileList.put(NameRewriter.NAME_ENTITY_EXPAND_DICT, new File(FAKE_FSA_PATH));
        NameRewriter nameRewriterWithFakePath = new NameRewriter(config, fileList);

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

        // Create a chain of two rewriters
        ArrayList<Searcher> searchers = new ArrayList<>();
        searchers.add(misspellRewriter);
        searchers.add(nameRewriterWithFakePath);

        Execution execution = QueryRewriteSearcherTestUtils.createExecutionObj(searchers);
        QueryRewriteSearcherTestUtils utilsWithFakePath = new QueryRewriteSearcherTestUtils(execution);

        utilsWithFakePath.assertRewrittenQuery("?query=will smith&type=all&" +
                NAME_REWRITER_NAME + "." +
                RewriterConstants.REWRITES_AS_UNIT_EQUIV + "=true",
                "query 'AND will smith'");
    }

    /**
     * IntentModel is null and rewriter throws exception
     * It should skip to the next rewriter
     */
    @Test
    void testExceptionInRewriter() {
        utils.assertRewrittenQuery("?query=will smith&type=all&" +
                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 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\"'");
    }

    /**
     * Two rewrites in chain
     * Query will be rewritten twice
     */
    @Test
    void testTwoRewritersInChain() {
        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&" +
                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);
    }

}