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

import com.yahoo.component.chain.Chain;
import com.yahoo.language.Language;
import com.yahoo.language.Linguistics;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexFactsFactory;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.NullItem;
import com.yahoo.prelude.query.parser.TestLinguistics;
import com.yahoo.prelude.querytransform.CJKSearcher;
import com.yahoo.search.Query;
import com.yahoo.search.Searcher;
import com.yahoo.search.query.parser.Parsable;
import com.yahoo.search.query.parser.Parser;
import com.yahoo.search.query.parser.ParserEnvironment;
import com.yahoo.search.query.parser.ParserFactory;
import com.yahoo.search.searchchain.Execution;

import org.junit.jupiter.api.Test;

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

/**
 * @author Steinar Knutsen
 */
public class CJKSearcherTestCase {

    private final IndexFacts indexFacts = IndexFactsFactory.newInstance("file:src/test/java/com/yahoo/prelude/" +
                                                                        "querytransform/test/cjk-index-info.cfg", null);

    @Test
    void testTermWeight() {
        assertTransformed("efg!10", "SAND e!10 fg!10",
                Query.Type.ALL, Language.CHINESE_SIMPLIFIED, Language.CHINESE_TRADITIONAL, TestLinguistics.INSTANCE);
    }

    /**
     * Overlapping tokens splits some sequences of "bcd" into "bc" "cd" instead of e.g. "b",
     * "cd". This improves recall in some cases. Vespa
     * must combine overlapping tokens as PHRASE, not AND to avoid a too high recall because of the token overlap.
     */
    @Test
    void testCjkQueryWithOverlappingTokens() {
        // The test language segmenter will segment "bcd" into the overlapping tokens "bc" "cd"
        assertTransformed("bcd", "SAND bc cd", Query.Type.ALL, Language.CHINESE_SIMPLIFIED, Language.CHINESE_TRADITIONAL,
                TestLinguistics.INSTANCE);

        // While "efg" will be segmented into one of the standard options, "e" "fg"
        assertTransformed("efg", "SAND e fg", Query.Type.ALL, Language.CHINESE_SIMPLIFIED, Language.CHINESE_TRADITIONAL,
                TestLinguistics.INSTANCE);
    }

    private void assertTransformed(String queryString, String expected, Query.Type mode, Language actualLanguage,
                                   Language queryLanguage, Linguistics linguistics) {
        Parser parser = ParserFactory.newInstance(mode, new ParserEnvironment()
                .setIndexFacts(indexFacts)
                .setLinguistics(linguistics));
        Item root = parser.parse(new Parsable().setQuery(queryString).setLanguage(actualLanguage)).getRoot();
        assertFalse(root instanceof NullItem);

        Query query = new Query("?language=" + queryLanguage.languageCode());
        query.getModel().getQueryTree().setRoot(root);

        new Execution(new Chain<Searcher>(new CJKSearcher()),
                      Execution.Context.createContextStub(indexFacts, linguistics)).search(query);
        assertEquals(expected, query.getModel().getQueryTree().getRoot().toString());
    }

}