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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.yql;
import com.yahoo.component.chain.Chain;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.searchchain.Execution;
import org.apache.http.client.utils.URIBuilder;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
/**
* Tests YQL expressions where a list of terms are supplied by indirection
*
* @author bratseth
*/
public class TermListTestCase {
@Test
public void testTermListInWeightedSet() {
URIBuilder builder = searchUri();
builder.setParameter("myTerms", "{'1':1, '2':1, 3:1}");
builder.setParameter("yql", "select * from sources * where weightedSet(user_id, @myTerms)");
Query query = searchAndAssertNoErrors(builder);
assertEquals("select * from sources * where weightedSet(user_id, {\"1\": 1, \"2\": 1, \"3\": 1})",
query.yqlRepresentation());
}
@Test
public void testTermListInWand() {
URIBuilder builder = searchUri();
builder.setParameter("myTerms", "{'1':1, 2:1, '3':1}");
builder.setParameter("yql", "select * from sources * where wand(user_id, @myTerms)");
Query query = searchAndAssertNoErrors(builder);
assertEquals("select * from sources * where wand(user_id, {\"1\": 1, \"2\": 1, \"3\": 1})",
query.yqlRepresentation());
}
@Test
public void testTermListInDotProduct() {
URIBuilder builder = searchUri();
builder.setParameter("myTerms", "{'1':1, '2':1, '3':1}");
builder.setParameter("yql", "select * from sources * where dotProduct(user_id, @myTerms)");
Query query = searchAndAssertNoErrors(builder);
assertEquals("select * from sources * where dotProduct(user_id, {\"1\": 1, \"2\": 1, \"3\": 1})",
query.yqlRepresentation());
}
private Query searchAndAssertNoErrors(URIBuilder builder) {
Query query = new Query(builder.toString());
var searchChain = new Chain<>(new MinimalQueryInserter());
var context = Execution.Context.createContextStub();
var execution = new Execution(searchChain, context);
Result r = execution.search(query);
var exception = exceptionOf(r);
assertNull(exception == null ? "No error":
exception.getMessage() + "\n" + Arrays.toString(exception.getStackTrace()),
r.hits().getError());
return query;
}
private Throwable exceptionOf(Result r) {
if (r.hits().getError() == null) return null;
if (r.hits().getError().getCause() == null) return null;
return r.hits().getError().getCause();
}
private URIBuilder searchUri() {
URIBuilder builder = new URIBuilder();
builder.setPath("search/");
return builder;
}
}
|