Skip to content

Commit 7ef7cdb

Browse files
committed
Attempt at running tests against the real Lucene StandardQueryParser. Note that there are still some differences.
1 parent 7d5bffd commit 7ef7cdb

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

test_java_queryparser.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// jrunscript -cp "/usr/local/Cellar/elasticsearch/1.7.2/libexec/lucene-queryparser-4.10.4.jar:/usr/local/Cellar/elasticsearch/1.7.2/libexec/lucene-core-4.10.4.jar" -f test_java_queryparser.js
2+
3+
function openJsonFile(path) {
4+
var data = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(path));
5+
var str = new java.lang.String(data, 'utf-8');
6+
return JSON.parse(str);
7+
}
8+
9+
qp = new org.apache.lucene.queryparser.flexible.standard.StandardQueryParser()
10+
qp.setAllowLeadingWildcard(true);
11+
12+
var examples = openJsonFile('test_java_queryparser_examples.json')
13+
14+
var failures = 0;
15+
var results = {};
16+
for (var query in examples) {
17+
var expect = examples[query];
18+
var result;
19+
try {
20+
result = qp.parse(query, '_all').toString();
21+
} catch (exc) {
22+
result = null;
23+
}
24+
results[query] = result;
25+
if (result !== expect) {
26+
print('FAIL ' + JSON.stringify(query) + ' GOT ' + JSON.stringify(result) + ' EXPECTED ' + JSON.stringify(expect));
27+
failures++;
28+
}
29+
}
30+
31+
print('Ran ' + Object.keys(examples).length + ' tests, ' + failures + ' failed.');
32+
33+
if (failures) {
34+
print(JSON.stringify(results, null, 4));
35+
throw 'failed';
36+
}

test_java_queryparser_examples.json

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
{
2+
"9999": "_all:9999",
3+
"\" this \"": "_all: this ",
4+
"\"*te*t\"": "_all:*te*t",
5+
"\"*te*t*\"": "_all:*te*t*",
6+
"\"+() AND that\"": "_all:+() AND that",
7+
"\"?te*t?\"": "_all:?te*t?",
8+
"\"a \\\"b c\\\" d\"": "_all:a \"b c\" d",
9+
"\"a \\+b c d\"": "_all:a +b c d",
10+
"\"func(*) AND that\"": "_all:func(*) AND that",
11+
"\"jakarta apache\"": "_all:jakarta apache",
12+
"\"jakarta apache\"^10": "_all:jakarta apache^10.0",
13+
"\"jakarta apache\"^10~": null,
14+
"\"jakarta apache\"^10~0.6": null,
15+
"\"jakarta apache\"~10": "_all:jakarta apache",
16+
"\"jakarta apache\"~10^": null,
17+
"\"jakarta apache\"~10^0.6": "_all:jakarta apache^0.6",
18+
"\"te*?t\"": "_all:te*?t",
19+
"\"te*t\"": "_all:te*t",
20+
"\"te??t\"": "_all:te??t",
21+
"\"te?t\"": "_all:te?t",
22+
"\"test*\"": "_all:test*",
23+
"\"text\"": "_all:text",
24+
"\"this \"": "_all:this ",
25+
"\"this \" ": "_all:this ",
26+
"\"this\"": "_all:this",
27+
"*": "_all:*",
28+
"*:*": "*:*",
29+
"*t": "_all:*t",
30+
"*t*": "_all:*t*",
31+
"*t*\\a": "_all:*t*a",
32+
"*t*a": "_all:*t*a",
33+
"*t*a*": "_all:*t*a*",
34+
"*t*a\\*": "_all:*t*a*",
35+
"*t*a\\?": "_all:*t*a?",
36+
"*t?": "_all:*t?",
37+
"*t?a": "_all:*t?a",
38+
"*t\\*a": "_all:*t*a",
39+
"*te*t": "_all:*te*t",
40+
"*te*t*": "_all:*te*t*",
41+
"+field:": null,
42+
"+field: {this TO that}": "field:{this TO that}",
43+
"+field:(-one +two three)": "-field:one +field:two field:three",
44+
"+field:[ this TO that ]": "field:[this TO that]",
45+
"+field:[this TO that]": "field:[this TO that]",
46+
"+field:{this TO that}": "field:{this TO that}",
47+
"+m:(a b c)": "m:a m:b m:c",
48+
"+value": "_all:value",
49+
"-field:(-one +two three)": "-field:one +field:two field:three",
50+
"-m:(a b NEAR c d AND e)": "m:a m:b m:NEAR m:c +m:d +m:e",
51+
"-value": "_all:value",
52+
"0.9999": "_all:0.9999",
53+
"00000000.9999": "_all:00000000.9999",
54+
"9999.1": "_all:9999.1",
55+
"?": "_all:?",
56+
"?t": "_all:?t",
57+
"?t*": "_all:?t*",
58+
"?t?": "_all:?t?",
59+
"?te*t?": "_all:?te*t?",
60+
"[\"#$%^&\" TO \"&*()\"]": "_all:[#$%^& TO &*()]",
61+
"[\"this\" TO \"that*\"]": "_all:[this TO that*]",
62+
"[* 20030101]^0.5~": null,
63+
"[* TO 20030101]": "_all:[* TO 20030101]",
64+
"[* TO this]": "_all:[* TO this]",
65+
"[* this]": "_all:[* TO this]",
66+
"[20020101 *]^0.5~": null,
67+
"[20020101 TO *]^0.5": "_all:[20020101 TO *]^0.5",
68+
"[20020101 TO 20030101]": "_all:[20020101 TO 20030101]",
69+
"[20020101 TO 20030101]^0.5": "_all:[20020101 TO 20030101]^0.5",
70+
"[20020101 TO 20030101]^0.5~": null,
71+
"[]": null,
72+
"[this TO *]": "_all:[this TO *]",
73+
"[this TO that]": "_all:[this TO that]",
74+
"[this that]": "_all:[this TO that]",
75+
"[this]": null,
76+
"\\(1\\+1\\)\\:2": "_all:(1+1):2",
77+
"\\*t": "_all:*t",
78+
"a\\\\\\+b": "_all:a\\+b",
79+
"a\\u0062c": "_all:abc",
80+
"escape:(\\+\\-\\&\\&\\|\\|\\!\\(\\)\\{\\}\\[\\]\\^\"\\~\\*\\?\\:\\)": null,
81+
"field: (one)": "field:one",
82+
"field:( one )": "field:one",
83+
"field:(one +two -three)": "field:one +field:two -field:three",
84+
"field:(one two three)": "field:one field:two field:three",
85+
"field:(one)": "field:one",
86+
"foo:*": "foo:*",
87+
"m:(+a b c)": "+m:a m:b m:c",
88+
"m:(-a +b c)^0.6": "(-m:a +m:b m:c)^0.6",
89+
"m:(a b NEAR c d AND e)": "m:a m:b m:NEAR m:c +m:d +m:e",
90+
"m:(a b NEAR c)": "m:a m:b m:NEAR m:c",
91+
"m:(a b c OR d NOT e)": "m:a m:b m:c m:d -m:e",
92+
"m:(a b c OR d)": "m:a m:b m:c m:d",
93+
"m:(a b c or d)": "m:a m:b m:c m:or m:d",
94+
"m:(a b c)": "m:a m:b m:c",
95+
"roam^": null,
96+
"roam^0.8": "_all:roam^0.8",
97+
"roam^0.899999999": "_all:roam^0.9",
98+
"roam^0.899999999~0.5": "_all:roam~2^0.9",
99+
"roam^0.8~": "_all:roam~2^0.8",
100+
"roam^8": "_all:roam^8.0",
101+
"roam^~": null,
102+
"roam~": "_all:roam~2",
103+
"roam~0.8": "_all:roam~0",
104+
"roam~0.899999999": "_all:roam~0",
105+
"roam~0.899999999^0.5": "_all:roam~0^0.5",
106+
"roam~0.8^": null,
107+
"roam~8": "_all:roam~2",
108+
"roam~^": null,
109+
"t*a": "_all:t*a",
110+
"t*a?": "_all:t*a?",
111+
"t*a?a": "_all:t*a?a",
112+
"te*?t": "_all:te*?t",
113+
"te*t": "_all:te*t",
114+
"te??t": "_all:te??t",
115+
"te?t": "_all:te?t",
116+
"test*": "_all:test*",
117+
"th\\*is": "_all:th*is",
118+
"this": "_all:this",
119+
"this0.9": "_all:this0.9",
120+
"this999": "_all:this999",
121+
"this^ 5": null,
122+
"this^5~ 9": "_all:this~2^5.0 _all:9",
123+
"title:(+return +\"pink panther\")": "+title:return +title:pink panther",
124+
"title:[20020101 TO 20030101]": "title:[20020101 TO 20030101]",
125+
"title:[20020101 TO 20030101]^0.5": "title:[20020101 TO 20030101]^0.5",
126+
"title:[20020101 TO 20030101]^0.5~": null,
127+
"title:{20020101 TO 20030101}": "title:{20020101 TO 20030101}",
128+
"title:{20020101 TO 20030101}^0.5": "title:{20020101 TO 20030101}^0.5",
129+
"title:{20020101 TO 20030101}^0.5~": null,
130+
"title:{Aida TO Carmen}": "title:{aida TO carmen}",
131+
"{\"#$%^&\" TO \"&*()\"}": "_all:{#$%^& TO &*()}",
132+
"{\"this\" TO \"that*\"}": "_all:{this TO that*}",
133+
"{* 20030101}^0.5~": null,
134+
"{* TO 20030101}^0.5": "_all:{* TO 20030101}^0.5",
135+
"{* TO this}": "_all:{* TO this}",
136+
"{* this}": "_all:{* TO this}",
137+
"{20020101 *}^0.5~": null,
138+
"{20020101 TO *}": "_all:{20020101 TO *}",
139+
"{20020101 TO 20030101}": "_all:{20020101 TO 20030101}",
140+
"{20020101 TO 20030101}^0.5": "_all:{20020101 TO 20030101}^0.5",
141+
"{20020101 TO 20030101}^0.5~": null,
142+
"{this TO *}": "_all:{this TO *}",
143+
"{this TO that}": "_all:{this TO that}",
144+
"{this that}": "_all:{this TO that}",
145+
"{}": null,
146+
"\"jakarta apache\" AND \"Apache Lucene\"": "+_all:jakarta apache +_all:Apache Lucene",
147+
"\"jakarta apache\" NOT \"Apache Lucene\"": "_all:jakarta apache -_all:Apache Lucene",
148+
"\"jakarta apache\" OR jakarta": "_all:jakarta apache _all:jakarta",
149+
"\"jakarta apache\"^4 \"Apache Lucene\"": "_all:jakarta apache^4.0 _all:Apache Lucene",
150+
"(jakarta OR apache) AND website": "+(_all:jakarta _all:apache) +_all:website",
151+
"(this) ((((((that))))))": "_all:this _all:that",
152+
"(this) (that)": "_all:this _all:that",
153+
"+jakarta lucene": "+_all:jakarta _all:lucene",
154+
"jakarta^4 apache": "_all:jakarta^4.0 _all:apache",
155+
"this ((((+(that)))))": "_all:this _all:that",
156+
"this ((that))": "_all:this _all:that",
157+
"this (+(((+(that))))": null,
158+
"this (+(((+(that)))))": "_all:this _all:that",
159+
"this (+(that)^7)": "_all:this _all:that^7.0",
160+
"this (++(((+(that)))))": null,
161+
"this (that)": "_all:this _all:that",
162+
"this +((((+(that)))))": "_all:this +_all:that",
163+
"this +(+((((that)))))": "_all:this +_all:that",
164+
"this +(that thus)^7": "_all:this +((_all:that _all:thus)^7.0)",
165+
"this +(that)": "_all:this +_all:that",
166+
"title:\"X x\" AND text:go title:\"x y\" AND A": "+title:X x +text:go +title:x y +_all:A",
167+
"title:X Y Z": "title:X _all:Y _all:Z"
168+
}

0 commit comments

Comments
 (0)