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

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

import com.yahoo.language.Linguistics;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.prelude.Index;
import com.yahoo.prelude.SearchDefinition;
import com.yahoo.prelude.query.PhraseSegmentItem;
import com.yahoo.prelude.query.Substring;
import com.yahoo.prelude.query.WordAlternativesItem;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.search.Query;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.querytransform.NormalizingSearcher;
import com.yahoo.search.searchchain.Execution;

import org.junit.jupiter.api.Test;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bratseth
 */
public class NormalizingSearcherTestCase {

    private static final Linguistics linguistics = new SimpleLinguistics();

    @Test
    void testNoNormalizingNecssary() {
        Query query = new Query("/search?query=bilen&search=cluster1&restrict=type1");
        createExecution().search(query);
        assertEquals("WEAKAND(100) bilen", query.getModel().getQueryTree().getRoot().toString());
    }

    @Test
    void testAttributeQuery() {
        Query query = new Query("/search?query=attribute:" + enc("b\u00e9yonc\u00e8 b\u00e9yonc\u00e8") + "&search=cluster1&restrict=type1");
        createExecution().search(query);
        assertEquals("WEAKAND(100) attribute:b\u00e9yonc\u00e8 beyonce", query.getModel().getQueryTree().getRoot().toString());
    }

    @Test
    void testOneTermNormalizing() {
        Query query = new Query("/search?query=b\u00e9yonc\u00e8&search=cluster1&restrict=type1");
        createExecution().search(query);
        assertEquals("WEAKAND(100) beyonce", query.getModel().getQueryTree().getRoot().toString());
    }

    @Test
    void testOneTermNoNormalizingDifferentSearchDef() {
        Query query = new Query("/search?query=b\u00e9yonc\u00e8&search=cluster1&restrict=type2");
        createExecution().search(query);
        assertEquals("WEAKAND(100) béyoncè", query.getModel().getQueryTree().getRoot().toString());
    }

    @Test
    void testTwoTermQuery() throws UnsupportedEncodingException {
        Query query = new Query("/search?query=" + enc("b\u00e9yonc\u00e8 beyonc\u00e9") + "&search=cluster1&restrict=type1");
        createExecution().search(query);
        assertEquals("WEAKAND(100) beyonce beyonce", query.getModel().getQueryTree().getRoot().toString());
    }

    private String enc(String s) {
        return URLEncoder.encode(s, StandardCharsets.UTF_8);
    }

    @Test
    void testPhraseQuery() {
        Query query = new Query("/search?query=" + enc("\"b\u00e9yonc\u00e8 beyonc\u00e9\"") + "&search=cluster1&restrict=type1");
        query.getTrace().setLevel(2);
        createExecution().search(query);
        assertEquals("WEAKAND(100) \"beyonce beyonce\"", query.getModel().getQueryTree().getRoot().toString());
    }

    @Test
    void testLiteralBoost() {
        Query query = new Query("/search?query=nop&search=cluster1&restrict=type1");
        List<WordAlternativesItem.Alternative> terms = new ArrayList<>();
        Substring origin = new Substring(0, 5, "h\u00F4tels");
        terms.add(new WordAlternativesItem.Alternative("h\u00F4tels", 1.0d));
        terms.add(new WordAlternativesItem.Alternative("h\u00F4tel", 0.7d));
        query.getModel().getQueryTree().setRoot(new WordAlternativesItem("default", true, origin, terms));
        createExecution().search(query);
        WordAlternativesItem w = (WordAlternativesItem) query.getModel().getQueryTree().getRoot();
        assertEquals(4, w.getAlternatives().size());
        boolean foundHotel = false;
        for (WordAlternativesItem.Alternative a : w.getAlternatives()) {
            if ("hotel".equals(a.word)) {
                foundHotel = true;
                assertEquals(.7d * .7d, a.exactness, 1e-15);
            }
        }
        assertTrue(foundHotel, "Did not find the expected normalized form \"hotel\".");
    }


    @Test
    void testPhraseSegmentNormalization() {
        Query query = new Query("/search?query=&search=cluster1&restrict=type1");
        PhraseSegmentItem phraseSegment = new PhraseSegmentItem("default", false, false);
        phraseSegment.addItem(new WordItem("net"));
        query.getModel().getQueryTree().setRoot(phraseSegment);
        assertEquals("'net'", query.getModel().getQueryTree().getRoot().toString());
        createExecution().search(query);
        assertEquals("'net'", query.getModel().getQueryTree().getRoot().toString());
    }

    private Execution createExecution() {
        return new Execution(new NormalizingSearcher(linguistics),
                             Execution.Context.createContextStub(createIndexFacts(), linguistics));
    }

    private IndexFacts createIndexFacts() {
        Map<String, List<String>> clusters = new LinkedHashMap<>();
        clusters.put("cluster1", List.of("type1", "type2", "type3"));
        clusters.put("cluster2", List.of("type4", "type5"));
        Collection<SearchDefinition> searchDefs = List.of(
                createSearchDefinitionWithFields("type1", true),
                createSearchDefinitionWithFields("type2", false),
                new SearchDefinition("type3"),
                new SearchDefinition("type4"),
                new SearchDefinition("type5"));
        return new IndexFacts(new IndexModel(clusters, searchDefs));
    }

    private SearchDefinition createSearchDefinitionWithFields(String name, boolean normalize) {
        SearchDefinition type = new SearchDefinition(name);

        Index defaultIndex = new Index("default");
        defaultIndex.setNormalize(normalize);
        type.addIndex(defaultIndex);

        Index absoluteIndex = new Index("absolute");
        absoluteIndex.setNormalize(normalize);
        type.addIndex(absoluteIndex);

        Index normalizercheckIndex = new Index("normalizercheck");
        normalizercheckIndex.setNormalize(normalize);
        type.addIndex(normalizercheckIndex);

        Index attributeIndex = new Index("attribute");
        attributeIndex.setAttribute(true);
        type.addIndex(attributeIndex);

        return type;
    }

}