blob: 498d6ca29802e8fdbdc38495736df4f026886442 (
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
|
// 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.search.Query;
import com.yahoo.prelude.query.AndItem;
import com.yahoo.prelude.query.CompositeItem;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.PhraseItem;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.prelude.querytransform.CollapsePhraseSearcher;
import com.yahoo.search.searchchain.Execution;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import static org.junit.Assert.assertEquals;
/**
* Check CollapsePhraseSearcher works and only is triggered when it
* should.
*
* @author Steinar Knutsen
*/
public class CollapsePhraseSearcherTestCase {
@Test
public void testSimplePositive() {
PhraseItem root = new PhraseItem();
root.addItem(new WordItem("abc"));
assertEquals("abc", transformQuery(root));
}
@Test
public void testPositive1() {
AndItem root = new AndItem();
root.addItem(new WordItem("a"));
PhraseItem embedded = new PhraseItem();
embedded.addItem(new WordItem("bcd"));
root.addItem(embedded);
root.addItem(new WordItem("e"));
assertEquals("AND a bcd e",
transformQuery(root));
}
@Test
public void testPositive2() {
AndItem root = new AndItem();
root.addItem(new WordItem("a"));
CompositeItem embedded = new AndItem();
embedded.addItem(new WordItem("bcd"));
CompositeItem phrase = new PhraseItem();
phrase.addItem(new WordItem("def"));
embedded.addItem(phrase);
root.addItem(embedded);
root.addItem(new WordItem("e"));
assertEquals("AND a (AND bcd def) e",
transformQuery(root));
}
@Test
public void testNoTerms() {
assertEquals("NULL", transformQuery("?query=" + enc("\"\"")));
}
@Test
public void testNegative1() {
assertEquals("WEAKAND(100) \"abc def\"", transformQuery("?query=" + enc("\"abc def\"")));
}
@Test
public void testNegative2() {
assertEquals("WEAKAND(100) a \"abc def\" b", transformQuery("?query=" + enc("a \"abc def\" b")));
}
private String enc(String s) {
try {
return URLEncoder.encode(s, "utf-8");
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
@Test
public void testNegative3() {
AndItem root = new AndItem();
root.addItem(new WordItem("a"));
CompositeItem embedded = new AndItem();
embedded.addItem(new WordItem("bcd"));
CompositeItem phrase = new PhraseItem();
phrase.addItem(new WordItem("def"));
phrase.addItem(new WordItem("ghi"));
embedded.addItem(phrase);
root.addItem(embedded);
root.addItem(new WordItem("e"));
assertEquals("AND a (AND bcd \"def ghi\") e",
transformQuery(root));
}
private String transformQuery(String rawQuery) {
CollapsePhraseSearcher searcher = new CollapsePhraseSearcher();
Query query = new Query(rawQuery);
new Execution(searcher, Execution.Context.createContextStub()).search(query);
Item newRoot = query.getModel().getQueryTree().getRoot();
if (newRoot != null)
return newRoot.toString();
else
return null;
}
private String transformQuery(Item queryTree) {
CollapsePhraseSearcher searcher = new CollapsePhraseSearcher();
Query query = new Query();
query.getModel().getQueryTree().setRoot(queryTree);
new Execution(searcher, Execution.Context.createContextStub()).search(query);
Item newRoot = query.getModel().getQueryTree().getRoot();
if (newRoot != null)
return newRoot.toString();
else
return null;
}
}
|