aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/searcher/test/PosSearcherTestCase.java
blob: 042e198edfcc7ebb56bdc2c7466578a2e8291230 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.searcher.test;

import com.yahoo.component.chain.Chain;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.prelude.searcher.PosSearcher;
import com.yahoo.search.Searcher;
import com.yahoo.search.rendering.RendererRegistry;
import com.yahoo.search.result.ErrorHit;
import com.yahoo.search.searchchain.Execution;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

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

/**
 * Tests for the PosSearcher
 *
 * @author Gunnar Gauslaa Bergem
 */
@SuppressWarnings("deprecation")
public class PosSearcherTestCase {

    private final PosSearcher searcher = new PosSearcher();

    /**
     * Tests basic lat/long input.
     */
    @Test
    void testBasics() {
        Query q = new Query();
        q.properties().set("pos.ll", "N0;E0");
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,0,0,450668,0,1,0,4294967295)", q.getRanking().getLocation().toString());

        q = new Query();
        q.properties().set("pos.ll", "N60;E30");
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,30000000,60000000,450668,0,1,0,2147483647)", q.getRanking().getLocation().toString());
    }

    /**
     * Tests basic bounding box input.
     */
    @Test
    void testBoundingBox() {
        Query q = new Query();
        q.properties().set("pos.bb", "n=51.9,s=50.2,e=0.34,w=-10.01");
        doSearch(searcher, q, 0, 10);
        assertEquals("[2,-10010000,50200000,340000,51900000]",
                q.getRanking().getLocation().toString());

        q = new Query();
        q.properties().set("pos.bb", "n=0,s=0,e=123.456789,w=-123.456789");
        doSearch(searcher, q, 0, 10);
        assertEquals("[2,-123456789,0,123456789,0]",
                q.getRanking().getLocation().toString());

        q = new Query();
        q.properties().set("pos.bb", "n=12.345678,s=-12.345678,e=0,w=0");
        doSearch(searcher, q, 0, 10);
        assertEquals("[2,0,-12345678,0,12345678]",
                q.getRanking().getLocation().toString());

        q = new Query();
        q.properties().set("pos.bb", "n=0.000001,s=-0.000001,e=0.000001,w=-0.000001");
        doSearch(searcher, q, 0, 10);
        assertEquals("[2,-1,-1,1,1]",
                q.getRanking().getLocation().toString());
    }

    /**
     * Tests basic bounding box input.
     */
    @Test
    void testBoundingBoxAndRadius() {
        Query q = new Query();
        q.properties().set("pos.bb", "n=60.111,s=59.999,e=30.111,w=29.999");
        q.properties().set("pos.ll", "N60;E30");
        doSearch(searcher, q, 0, 10);
        assertEquals(
                "[2,29999000,59999000,30111000,60111000]" +
                        "(2,30000000,60000000,450668,0,1,0,2147483647)",
                q.getRanking().getLocation().toString());
    }

    /**
     * Tests different ways of specifying the radius.
     */
    @Test
    void testRadiusUnits() {
        Query q = new Query();
        q.properties().set("pos.ll", "N0;E0");
        q.properties().set("pos.radius", "2km");
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,0,0,18026,0,1,0,4294967295)", q.getRanking().getLocation().toString());

        q = new Query();
        q.properties().set("pos.ll", "N0;E0");
        q.properties().set("pos.radius", "2000m");
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,0,0,18026,0,1,0,4294967295)", q.getRanking().getLocation().toString());

        q = new Query();
        q.properties().set("pos.ll", "N0;E0");
        q.properties().set("pos.radius", "20mi");
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,0,0,290112,0,1,0,4294967295)", q.getRanking().getLocation().toString());

        q = new Query();
        q.properties().set("pos.ll", "N0;E0");
        q.properties().set("pos.radius", "2km");
        q.properties().set("pos.units", "udeg");
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,0,0,18026,0,1,0,4294967295)", q.getRanking().getLocation().toString());

        q = new Query();
        q.properties().set("pos.ll", "N0;E0");
        q.properties().set("pos.radius", "-42");
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,0,0,-1,0,1,0,4294967295)", q.getRanking().getLocation().toString());
        assertEquals("(2,0,0,-1,0,1,0,4294967295)", q.getRanking().getLocation().backendString());
    }

    /**
     * Tests xy position (internal format).
     */
    @Test
    void testXY() {
        Query q = new Query();
        q.properties().set("pos.xy", "22500;22500");
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,22500,22500,450668,0,1,0)", q.getRanking().getLocation().toString());

        q = new Query();
        q.properties().set("pos.xy", "22500;22500");
        q.properties().set("pos.units", "unknown");
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,22500,22500,450668,0,1,0)", q.getRanking().getLocation().toString());
    }

    @Test
    void testNotOverridingOldStyleParameters() {
        PosSearcher searcher = new PosSearcher();
        Query q = new Query("?query=test&pos.ll=N10.15;E6.08&location=(2,-1100222,0,300,0,1,0,CalcLatLon)");
        q.setTraceLevel(1);
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,-1100222,0,300,0,1,0,4294967295)", q.getRanking().getLocation().toString());
    }

    /**
     * Tests input parameters that should report errors.
     */
    @Test
    void testInvalidInput() {
        PosSearcher searcher = new PosSearcher();
        Result result;

        Query q = new Query();
        q.properties().set("pos.ll", "NE74.14;E14.48");
        result = doSearch(searcher, q, 0, 10);
        assertEquals("Error in pos parameters: Unable to parse lat/long string 'NE74.14;E14.48': already set direction once, cannot add direction: E",
                ((ErrorHit) result.hits().get(0)).errors().iterator().next().getDetailedMessage());

        q = new Query();
        q.properties().set("pos.ll", "NE74.14;E14.48");
        q.properties().set("pos.xy", "82400, 72800");
        result = doSearch(searcher, q, 0, 10);
        assertEquals("Error in pos parameters: Cannot handle both lat/long and xy coords at the same time",
                ((ErrorHit) result.hits().get(0)).errors().iterator().next().getDetailedMessage());
    }

    @Test
    void testWrappingTheNorthPole() {
        Query q = new Query();
        q.properties().set("pos.ll", "N89.9985365158;E122.163600102");
        q.properties().set("pos.radius", "20mi");
        doSearch(searcher, q, 0, 10);
        assertEquals("(2,122163600,89998536,290112,0,1,0,109743)", q.getRanking().getLocation().toString());
    }

    private Result doSearch(Searcher searcher, Query query, int offset, int hits) {
        query.setOffset(offset);
        query.setHits(hits);
        return createExecution(searcher).search(query);
    }

    private Execution createExecution(Searcher searcher) {
        return new Execution(chainedAsSearchChain(searcher), Execution.Context.createContextStub());
    }

    private Chain<Searcher> chainedAsSearchChain(Searcher topOfChain) {
        List<Searcher> searchers = new ArrayList<>();
        searchers.add(topOfChain);
        return new Chain<>(searchers);
    }

}