aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/query/rewrite/test/NameRewriterTestCase.java
blob: 98a93a572e96633d80e957c0981beb0e66b98846 (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
181
182
183
// Copyright Yahoo. 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.searchchain.*;
import com.yahoo.search.query.rewrite.*;
import com.yahoo.search.query.rewrite.rewriters.*;
import com.yahoo.search.query.rewrite.RewritesConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
 * Test Cases for NameRewriter
 *
 * @author Karen Lee
 */
public class NameRewriterTestCase {

    private QueryRewriteSearcherTestUtils utils;
    private final String 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 REWRITER_NAME = NameRewriter.REWRITER_NAME;

    /**
     * Load the NameRewriterSearcher and prepare the
     * execution object
     */
    @BeforeEach
    public void setUp() {
        RewritesConfig config = QueryRewriteSearcherTestUtils.createConfigObj(CONFIG_PATH);
        HashMap<String, File> fileList = new HashMap<>();
        fileList.put(NameRewriter.NAME_ENTITY_EXPAND_DICT, new File(NAME_ENTITY_EXPAND_DICT_PATH));
        NameRewriter searcher = new NameRewriter(config, fileList);

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

    /**
     * RewritesAsEquiv and OriginalAsUnit are on
     */
    @Test
    void testRewritesAsEquivAndOriginalAsUnit() {
        utils.assertRewrittenQuery("?query=will smith&type=all&" +
                REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_EQUIV + "=true&" +
                REWRITER_NAME + "." + RewriterConstants.ORIGINAL_AS_UNIT + "=true",
                "query 'OR \"will smith\" (AND will smith movies) " +
                        "(AND will smith news) (AND will smith imdb) " +
                        "(AND will smith lyrics) (AND will smith dead) " +
                        "(AND will smith nfl) (AND will smith new movie hancock) " +
                        "(AND will smith biography)'");
    }

    /**
     * RewritesAsEquiv is on
     */
    @Test
    void testRewritesAsEquiv() {
        utils.assertRewrittenQuery("?query=will smith&type=all&" +
                REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_EQUIV + "=true&",
                "query 'OR (AND will smith) (AND will smith movies) " +
                        "(AND will smith news) (AND will smith imdb) " +
                        "(AND will smith lyrics) (AND will smith dead) " +
                        "(AND will smith nfl) (AND will smith new movie hancock) " +
                        "(AND will smith biography)'");
    }

    /**
     * Complex query with more than two levels for RewritesAsEquiv is on case
     * Should not rewrite
     */
    @Test
    void testComplextQueryRewritesAsEquiv() {
        utils.assertRewrittenQuery("?query=((will smith) OR (willl smith)) AND (tom cruise)&type=adv&" +
                REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_EQUIV + "=true&",
                "query 'AND (OR (AND will smith) (AND willl smith)) (AND tom cruise)'");
    }

    /**
     * Single word query for RewritesAsEquiv and OriginalAsUnit on case
     */
    @Test
    void testSingleWordForRewritesAsEquivAndOriginalAsUnit() {
        utils.assertRewrittenQuery("?query=obama&type=all&" +
                REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_EQUIV + "=true&" +
                REWRITER_NAME + "." + RewriterConstants.ORIGINAL_AS_UNIT + "=true",
                "query 'OR obama (AND obama \"nobel peace prize\") " +
                        "(AND obama wiki) (AND obama nobel prize) " +
                        "(AND obama nobel peace prize) (AND obama wears mom jeans) " +
                        "(AND obama sucks) (AND obama news) (AND malia obama) " +
                        "(AND obama speech) (AND obama nobel) (AND obama wikipedia) " +
                        "(AND barack obama biography) (AND obama snl) " +
                        "(AND obama peace prize) (AND michelle obama) (AND barack obama)'");
    }

    /**
     * RewritesAsUnitEquiv and OriginalAsUnitEquiv are on
     */
    @Test
    void testRewritesAsUnitEquivAndOriginalAsUnitEquiv() {
        utils.assertRewrittenQuery("?query=will smith&type=all&" +
                REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_UNIT_EQUIV +
                "=true&" +
                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\"'");
    }

    /**
     * Single word query for RewritesAsUnitEquiv and OriginalAsUnitEquiv on case
     */
    @Test
    void testSingleWordForRewritesAsUnitEquivAndOriginalAsUnitEquiv() {
        utils.assertRewrittenQuery("?query=obama&type=all&" +
                REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_UNIT_EQUIV +
                "=true&" +
                REWRITER_NAME + "." + RewriterConstants.ORIGINAL_AS_UNIT_EQUIV +
                "=true",
                "query 'OR obama \"obama nobel peace prize\" \"obama wiki\" \"obama nobel prize\" \"obama nobel peace prize\" \"obama wears mom jeans\" \"obama sucks\" \"obama news\" \"malia obama\" \"obama speech\" \"obama nobel\" \"obama wikipedia\" \"barack obama biography\" \"obama snl\" \"obama peace prize\" \"michelle obama\" \"barack obama\"'");
    }

    /**
     * Boosting only query (n/a as rewrite in FSA)
     * for RewritesAsEquiv and OriginalAsUnit on case
     */
    @Test
    void testBoostingQueryForRewritesAsEquivAndOriginalAsUnit() {
        utils.assertRewrittenQuery("?query=angelina jolie&type=all&" +
                REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_EQUIV + "=true&" +
                REWRITER_NAME + "." + RewriterConstants.ORIGINAL_AS_UNIT + "=true",
                "query '\"angelina jolie\"'");
    }

    /**
     * No match in FSA for the query
     * RewritesAsEquiv and OriginalAsUnit on case
     */
    @Test
    void testFSANoMatchForRewritesAsEquivAndOriginalAsUnit() {
        utils.assertRewrittenQuery("?query=tom cruise&type=all&" +
                REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_EQUIV + "=true&" +
                REWRITER_NAME + "." + RewriterConstants.ORIGINAL_AS_UNIT + "=true",
                "query 'AND tom cruise'");
    }

    /**
     * RewritesAsUnitEquiv is on
     */
    @Test
    void testRewritesAsUnitEquiv() {
        utils.assertRewrittenQuery("?query=will smith&type=all&" +
                REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_UNIT_EQUIV +
                "=true",
                "query 'OR (AND 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\"'");
    }

    /**
     * RewritesAsUnitEquiv is on and MaxRewrites is set to 2
     */
    @Test
    void testRewritesAsUnitEquivAndMaxRewrites() {
        utils.assertRewrittenQuery("?query=will smith&type=all&" +
                REWRITER_NAME + "." + RewriterConstants.REWRITES_AS_UNIT_EQUIV +
                "=true&" +
                REWRITER_NAME + "." + RewriterConstants.MAX_REWRITES + "=2",
                "query 'OR (AND will smith) \"will smith movies\" " +
                        "\"will smith news\"'");
    }

}