aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/query/parser/test/ParseTestCase.java
blob: f35ffcee0c65393ed6c4901713af449bd7b08609 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query.parser.test;

import com.yahoo.language.Language;
import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.SearchDefinition;
import com.yahoo.prelude.query.AndItem;
import com.yahoo.prelude.query.AndSegmentItem;
import com.yahoo.prelude.query.CompositeItem;
import com.yahoo.prelude.query.IntItem;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.NotItem;
import com.yahoo.prelude.query.NullItem;
import com.yahoo.prelude.query.OrItem;
import com.yahoo.prelude.query.PhraseItem;
import com.yahoo.prelude.query.PhraseSegmentItem;
import com.yahoo.prelude.query.PrefixItem;
import com.yahoo.prelude.query.RankItem;
import com.yahoo.prelude.query.SubstringItem;
import com.yahoo.prelude.query.SuffixItem;
import com.yahoo.prelude.query.TaggableItem;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.language.process.SpecialTokens;
import com.yahoo.prelude.query.parser.TestLinguistics;
import com.yahoo.search.Query;
import org.junit.jupiter.api.Test;

import java.util.Iterator;

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

/**
 * Tests query parsing.
 *
 * @author bratseth
 */
public class ParseTestCase {

    private final ParsingTester tester = new ParsingTester();

    @Test
    void testSimpleTermQuery() {
        tester.assertParsed("foobar", "foobar", Query.Type.ANY);
    }

    @Test
    void testTermWithIndexPrefix() {
        tester.assertParsed("url:foobar",
                "url:foobar",
                Query.Type.ANY);
    }

    @Test
    void testTermWithCatalogAndIndexPrefix() {
        tester.assertParsed("normal.title:foobar", "normal.title:foobar", Query.Type.ANY);
    }

    @Test
    void testMultipleTermsWithUTF8EncodingOred() {
        tester.assertParsed("OR l\u00e5gen delta M\u00dcNICH M\u00fcnchen",
                "l\u00e5gen delta M\u00dcNICH M\u00fcnchen",
                Query.Type.ANY);
    }

    @Test
    void testMultipleTermsWithMultiplePrefixes() {
        tester.assertParsed("RANK (+bar -normal.title:foo -baz) url:foobar",
                "url:foobar +bar -normal.title:foo -baz", Query.Type.ANY);
    }

    @Test
    void testSimpleQueryDefaultOr() {
        tester.assertParsed("OR foobar foo bar baz", "foobar foo bar baz", Query.Type.ANY);
    }

    @Test
    void testOrAndNot() {
        tester.assertParsed("RANK (+(AND baz bar) -xyzzy -foobaz) foobar foo",
                "foobar +baz foo -xyzzy -foobaz +bar", Query.Type.ANY);
    }

    @Test
    void testSimpleOrNestedAnd() {
        tester.assertParsed("RANK (OR foo bar baz) foobar xyzzy",
                "foobar +(foo bar baz) xyzzy", Query.Type.ANY);
    }

    @Test
    void testSimpleOrNestedNot() {
        tester.assertParsed("+(OR foobar xyzzy) -(AND foo bar baz)",
                "foobar -(foo bar baz) xyzzy", Query.Type.ANY);
    }

    @Test
    void testOrNotNestedAnd() {
        tester.assertParsed("RANK (+(AND baz (OR foo bar baz) bar) -xyzzy -foobaz) foobar foo",
                "foobar +baz foo -xyzzy +(foo bar baz) -foobaz +bar",
                Query.Type.ANY);
    }

    @Test
    void testOrAndNotNestedNot() {
        tester.assertParsed("RANK (+(AND baz bar) -xyzzy -(AND foo bar baz) -foobaz) foobar foo",
                "foobar +baz foo -xyzzy -(foo bar baz) -foobaz +bar",
                Query.Type.ANY);
    }

    @Test
    void testOrMultipleNestedAnd() {
        tester.assertParsed("RANK (AND (OR fo ba foba) (OR foz baraz)) foobar foo bar baz",
                "foobar +(fo ba foba) foo bar +(foz baraz) baz",
                Query.Type.ANY);
    }

    @Test
    void testOrMultipleNestedNot() {
        tester.assertParsed("+(OR foobar foo bar baz) -(AND fo ba foba) -(AND foz baraz)",
                "foobar -(fo ba foba) foo bar -(foz baraz) baz",
                Query.Type.ANY);
    }

    @Test
    void testOrAndNotMultipleNestedAnd() {
        tester.assertParsed("RANK (+(AND baz (OR foo bar baz) (OR foz bazaz) bar) -xyzzy -foobaz) foobar foo",
                "foobar +baz foo -xyzzy +(foo bar baz) -foobaz +(foz bazaz) +bar",
                Query.Type.ANY);
    }

    @Test
    void testOrAndNotMultipleNestedNot() {
        tester.assertParsed("RANK (+(AND baz bar) -xyzzy -(AND foo bar baz) -foobaz -(AND foz bazaz)) foobar foo",
                "foobar +baz foo -xyzzy -(foo bar baz) -foobaz -(foz bazaz) +bar",
                Query.Type.ANY);
    }

    @Test
    void testOrMultipleNestedAndNot() {
        tester.assertParsed("RANK (+(AND (OR ffoooo bbaarr) (OR oof rab raboof)) -(AND fo ba foba) -(AND foz baraz)) foobar foo bar baz",
                "foobar -(fo ba foba) foo +(ffoooo bbaarr) bar +(oof rab raboof) -(foz baraz) baz",
                Query.Type.ANY);
    }

    @Test
    void testOrAndNotMultipleNestedAndNot() {
        tester.assertParsed("RANK (+(AND (OR ffoooo bbaarr) (OR oof rab raboof) baz xyxyzzy) -(AND fo ba foba) -foo -bar -(AND foz baraz)) foobar",
                "foobar -(fo ba foba) -foo +(ffoooo bbaarr) -bar +(oof rab raboof) -(foz baraz) +baz +xyxyzzy",
                Query.Type.ANY);
    }

    @Test
    void testExplicitPhrase() {
        Item root = tester.assertParsed("\"foo bar foobar\"", "\"foo bar foobar\"", Query.Type.ANY);
        assertTrue(root instanceof PhraseItem);
        assertTrue(((PhraseItem) root).isExplicit());
    }

    @Test
    void testPhraseWithIndex() {
        tester.assertParsed("normal.title:\"foo bar foobar\"",
                "normal.title:\"foo bar foobar\"", Query.Type.ANY);
    }

    @Test
    void testPhrasesAndTerms() {
        tester.assertParsed("OR \"foo bar foobar\" xyzzy \"baz gaz faz\"",
                "\"foo bar foobar\" xyzzy \"baz gaz faz\"", Query.Type.ANY);
    }

    @Test
    void testPhrasesAndTermsWithOperators() {
        tester.assertParsed("RANK (+(AND \"baz gaz faz\" bazar) -\"foo bar foobar\") foofoo xyzzy",
                "foofoo -\"foo bar foobar\" xyzzy +\"baz gaz faz\" +bazar",
                Query.Type.ANY);
    }

    @Test
    void testSimpleTermQueryDefaultAnd() {
        tester.assertParsed("foobar", "foobar", Query.Type.ALL);
    }

    @Test
    void testTermWithCatalogAndIndexPrefixDefaultAnd() {
        tester.assertParsed("normal.title:foobar", "normal.title:foobar", Query.Type.ALL);
    }

    @Test
    void testMultipleTermsWithMultiplePrefixesDefaultAnd() {
        tester.assertParsed("+(AND url:foobar bar) -normal.title:foo -baz",
                "url:foobar +bar -normal.title:foo -baz",
                Query.Type.ALL);
    }

    @Test
    void testSimpleQueryDefaultAnd() {
        tester.assertParsed("AND foobar foo bar baz", "foobar foo bar baz", Query.Type.ALL);
    }

    @Test
    void testNotDefaultAnd() {
        tester.assertParsed("+(AND foobar (OR foo bar baz) xyzzy) -(AND foz baraz bazar)",
                "foobar +(foo bar baz) xyzzy -(foz baraz bazar)",
                Query.Type.ALL);
    }

    @Test
    void testSimpleTermQueryDefaultPhrase() {
        tester.assertParsed("foobar",
                "foobar",
                Query.Type.PHRASE);
    }

    @Test
    void testSimpleQueryDefaultPhrase() {
        Item root = tester.assertParsed("\"foobar foo bar baz\"",
                "foobar foo bar baz",
                Query.Type.PHRASE);
        assertTrue(root instanceof PhraseItem);
        assertFalse(((PhraseItem) root).isExplicit());
    }

    @Test
    void testMultipleTermsWithMultiplePrefixesDefaultPhrase() {
        tester.assertParsed("\"url foobar bar normal title foo baz\"",
                "url:foobar +bar -normal.title:foo -baz",
                Query.Type.PHRASE);
    }

    @Test
    void testOdd1() {
        tester.assertParsed("AND window print error", "+window.print() +error",
                Query.Type.ALL);
    }

    @Test
    void testOdd2() {
        tester.assertParsed("normal.title:kaboom", "normal.title:\"kaboom\"",
                Query.Type.ALL);
    }

    @Test
    void testOdd2Uppercase() {
        tester.assertParsed("normal.title:KABOOM", "NORMAL.TITLE:\"KABOOM\"", Query.Type.ALL);
    }

    @Test
    void testOdd3() {
        tester.assertParsed("AND foo (OR size.all:[200;300] date.all:512)",
                "foo +(size.all:[200;300] date.all:512)", Query.Type.ALL);
    }

    @Test
    void testNullQuery() {
        tester.assertParsed(null, null, Query.Type.ALL);
    }

    @Test
    void testEmptyQuery() {
        tester.assertParsed(null, "", Query.Type.ALL);
    }

    @Test
    void testNotOnly() {
        tester.assertParsed(null, "-foobar", Query.Type.ALL);
    }

    @Test
    void testMultipleNotsOnlt() {
        tester.assertParsed(null, "-foo -bar -foobar", Query.Type.ALL);
    }

    @Test
    void testOnlyNotComposite() {
        tester.assertParsed(null, "-(foo bar baz)", Query.Type.ALL);
    }

    @Test
    void testNestedCompositesDefaultOr() {
        tester.assertParsed("RANK (OR foobar bar baz) foo xyzzy",
                "foo +(foobar +(bar baz)) xyzzy", Query.Type.ANY);
    }

    @Test
    void testNestedCompositesDefaultAnd() {
        tester.assertParsed("AND foo (OR foobar bar baz) xyzzy",
                "foo +(foobar +(bar baz)) xyzzy", Query.Type.ALL);
    }

    @Test
    void testNestedCompositesPhraseDefault() {
        tester.assertParsed("\"foo foobar bar baz xyzzy\"",
                "foo +(foobar +(bar baz)) xyzzy", Query.Type.PHRASE);
    }

    @Test
    void testNumeric() {
        tester.assertParsed("34", "34", Query.Type.ANY);
    }

    @Test
    void testGreaterNumeric() {
        tester.assertParsed("<454", "<454", Query.Type.ANY);
    }

    @Test
    void testGreaterNumericWithIndex() {
        IntItem item = (IntItem) tester.assertParsed("score:<454", "score:<454", Query.Type.ANY);
        assertEquals("score", item.getIndexName());
        assertEquals(454L, item.getToLimit().number());
        assertFalse(item.getToLimit().isInclusive());
    }

    @Test
    void testSmallerNumeric() {
        tester.assertParsed(">454", ">454", Query.Type.ANY);
    }

    @Test
    void testSmallerNumericWithIndex() {
        IntItem item = (IntItem) tester.assertParsed("score:>454", "score:>454", Query.Type.ANY);
        assertEquals("score", item.getIndexName());
        assertEquals(454L, item.getFromLimit().number());
        assertFalse(item.getFromLimit().isInclusive());
    }

    @Test
    void testFullRange() {
        tester.assertParsed("[34;454]", "[34;454]", Query.Type.ANY);
    }

    @Test
    void testFullRangeLimit() {
        tester.assertParsed("[34;454;7]", "[34;454;7]", Query.Type.ANY);
        tester.assertParsed("[34;454;-7]", "[34;454;-7]", Query.Type.ANY);
    }

    @Test
    void testLowOpenRange() {
        tester.assertParsed("[;454]", "[;454]", Query.Type.ANY);
    }

    @Test
    void testHiOpenRange() {
        tester.assertParsed("[34;]", "[34;]", Query.Type.ANY);
    }

    @Test
    void testNumericWithIndex() {
        tester.assertParsed("document.size:[34;454]", "document.size:[34;454]", Query.Type.ANY);
    }

    @Test
    void testMultipleNumeric() {
        tester.assertParsed("OR [34;454] <34", "[34;454] <34", Query.Type.ANY);
    }

    @Test
    void testMultipleIntegerWithIndex() {
        tester.assertParsed("OR document.size:[34;454] date:>1234567890",
                "document.size:[34;454] date:>1234567890", Query.Type.ANY);
    }

    @Test
    void testMixedNumericAndOtherTerms() {
        tester.assertParsed("RANK (AND document.size:<1024 xyzzy) foo date:>123456890",
                "foo +document.size:<1024 +xyzzy date:>123456890",
                Query.Type.ANY);
    }

    @Test
    void testEmptyPhrase() {
        tester.assertParsed("\"to be or not\"", "\"\"to be or not", Query.Type.ANY);
    }

    @Test
    void testItemPhraseEmptyPhrase() {
        tester.assertParsed("RANK to \"or not to be\"", "+to\"or not to be\"\"\"", Query.Type.ANY);
    }

    @Test
    void testSimpleQuery() {
        tester.assertParsed("OR if am \"f g 4 2\" maybe", "if am \"  f g 4 2\"\" maybe", Query.Type.ANY);
    }

    @Test
    void testExcessivePluses() {
        tester.assertParsed("+(AND other is nothing) -test",
                "++other +++++is ++++++nothing -test", Query.Type.ANY);
    }

    @Test
    void testMinusAndPluses() {
        tester.assertParsed(null, "--test+-if", Query.Type.ANY);
    }

    @Test
    void testPlusesAndMinuses() {
        tester.assertParsed("AND a b c d d", "a+b+c+d--d", Query.Type.ANY);
    }

    @Test
    void testNumbers() {
        tester.assertParsed("AND 123 2132odfd 934032 32423", "123+2132odfd.934032,,32423", Query.Type.ANY);
    }

    @Test
    void testOtherSignsInQuote() {
        tester.assertParsed("AND 0032 4 320 24329043", "0032+4\\320.24329043", Query.Type.ANY);
    }

    @Test
    void testGribberish() {
        tester.assertParsed("1349832840234l3040roer\u00e6lf12",
                ",1349832840234l3040roer\u00e6lf12",
                Query.Type.ANY);
    }

    @Test
    void testUrl() {
        tester.assertParsed("AND www:www www:hotelaiguablava www:com",
                "+www:www.hotelaiguablava:com",
                Query.Type.ANY);
    }

    @Test
    void testUrlGribberish() {
        tester.assertParsed("OR (AND 3 16) fast.type:lycosoffensive",
                "[ 3:16 fast.type:lycosoffensive",
                Query.Type.ANY);
    }

    @Test
    void testBracedWordAny() {
        tester.assertParsed("foo", "(foo)", Query.Type.ANY);
    }

    @Test
    void testBracedWordAll() {
        tester.assertParsed("foo", "(foo)", Query.Type.ALL);

    }

    @Test
    void testBracedWords() {
        tester.assertParsed("OR (OR foo bar) (OR xyzzy foobar)",
                "(foo bar) (xyzzy foobar)", Query.Type.ANY);
    }

    @Test
    void testNullAdvanced() {
        tester.assertParsed(null, null, Query.Type.ADVANCED);
    }

    @Test
    void testEmptyAdvanced() {
        tester.assertParsed(null, "", Query.Type.ADVANCED);
    }

    @Test
    void testSimpleAdvanced() {
        tester.assertParsed("foobar", "foobar", Query.Type.ADVANCED);
    }

    @Test
    void testPrefixAdvanced() {
        tester.assertParsed("url:foobar", "url:foobar", Query.Type.ADVANCED);
    }

    @Test
    void testPrefixWithDotAdvanced() {
        tester.assertParsed("normal.title:foobar", "normal.title:foobar", Query.Type.ADVANCED);
    }

    @Test
    void testUTF8Advanced() {
        tester.assertParsed("m\u00fcnchen", "m\u00fcnchen", Query.Type.ADVANCED);
    }

    @Test
    void testSimplePhraseAdvanced() {
        tester.assertParsed("\"foo bar foobar\"", "\"foo bar foobar\"", Query.Type.ADVANCED);
    }

    @Test
    void testSimplePhraseWithIndexAdvanced() {
        tester.assertParsed("normal.title:\"foo bar foobar\"",
                "normal.title:\"foo bar foobar\"",
                Query.Type.ADVANCED);
    }

    @Test
    void testMultiplePhrasesAdvanced() {
        tester.assertParsed("AND \"foo bar foobar\" \"baz gaz faz\"",
                "\"foo bar foobar\" and \"baz gaz faz\"",
                Query.Type.ADVANCED);
    }

    @Test
    void testNumberAdvanced() {
        tester.assertParsed("34", "34", Query.Type.ADVANCED);
    }

    @Test
    void testLargerNumberAdvanced() {
        tester.assertParsed("<454", "<454", Query.Type.ADVANCED);
    }

    @Test
    void testLesserNumberAdvanced() {
        tester.assertParsed(">454", ">454", Query.Type.ADVANCED);
    }

    @Test
    void testRangeAdvanced() {
        tester.assertParsed("[34;454]", "[34;454]", Query.Type.ADVANCED);
    }

    @Test
    void testLowOpenRangeAdvanced() {
        tester.assertParsed("[;454]", "[;454]", Query.Type.ADVANCED);
    }

    @Test
    void testHighOpenRangeAdvanced() {
        tester.assertParsed("[34;]", "[34;]", Query.Type.ADVANCED);
    }

    @Test
    void testIdexedRangeAdvanced() {
        tester.assertParsed("document.size:[34;454]", "document.size:[34;454]", Query.Type.ADVANCED);
    }

    @Test
    void testSimpleAndAdvanced() {
        tester.assertParsed("AND foo bar", "foo and bar", Query.Type.ADVANCED);
    }

    @Test
    void testSimpleOrAdvanced() {
        tester.assertParsed("OR foo bar", "foo or bar", Query.Type.ADVANCED);
    }

    @Test
    void testSimpleAndNotAdvanced() {
        tester.assertParsed("+foo -bar", "foo andnot bar", Query.Type.ADVANCED);
    }

    @Test
    void testSimpleRankAdvanced() {
        tester.assertParsed("RANK foo bar", "foo rank bar", Query.Type.ADVANCED);
    }

    @Test
    void testMultipleAndAdvanced() {
        tester.assertParsed("AND foo bar foobar", "foo and bar and foobar", Query.Type.ADVANCED);
    }

    @Test
    void testMultipleOrAdvanced() {
        tester.assertParsed("OR foo bar foobar", "foo or bar or foobar", Query.Type.ADVANCED);
    }

    @Test
    void testMultipleAndnotAdvanced() {
        tester.assertParsed("+foo -bar -foobar", "foo andnot bar andnot foobar", Query.Type.ADVANCED);
    }

    @Test
    void testMultipleRankAdvanced() {
        tester.assertParsed("RANK foo bar foobar", "foo rank bar rank foobar", Query.Type.ADVANCED);
    }

    @Test
    void testMixedAdvanced() {
        tester.assertParsed("OR (AND foo bar) foobar", "foo and bar or foobar", Query.Type.ADVANCED);
    }

    @Test
    void testNestedAdvanced() {
        tester.assertParsed("AND foo (OR bar foobar)", "foo and (bar or foobar)", Query.Type.ADVANCED);
    }

    @Test
    void testMultipleNestedAdvanced() {
        tester.assertParsed("+(AND foo xyzzy) -(OR bar foobar)",
                "(foo and xyzzy) andnot (bar or foobar)", Query.Type.ADVANCED);
    }

    @Test
    void testDoubleNestedAdvanced() {
        tester.assertParsed("AND foo (OR bar (OR xyzzy foobar))",
                "foo and (bar or (xyzzy or foobar))", Query.Type.ADVANCED);
    }

    @Test
    void testDeeplyAdvanced() {
        tester.assertParsed(
                "AND foo (OR bar (OR (AND (AND baz (+(OR bazar zyxxy) -fozbaz)) (OR boz bozor) xyzzy) foobar))",
                "foo and (bar or ((baz and ((bazar or zyxxy) andnot fozbaz)) and (boz or bozor) and xyzzy or foobar))",
                Query.Type.ADVANCED);
    }

    @Test
    void testDeeplyAdvancedUppercase() {
        tester.assertParsed(
                "AND FOO (OR BAR (OR (AND (AND BAZ (+(OR BAZAR ZYXXY) -FOZBAZ)) (OR BOZ BOZOR) XYZZY) FOOBAR))",
                "FOO AND (BAR OR ((BAZ AND ((BAZAR OR ZYXXY) ANDNOT FOZBAZ)) AND (BOZ OR BOZOR) AND XYZZY OR FOOBAR))",
                Query.Type.ADVANCED);
    }

    @Test
    void testAbortedIntegerRange() {
        tester.assertParsed("AND audio.audall:744 audio.audall:ph",
                "+audio.audall:[744 +audio.audall:ph", Query.Type.ANY);
    }

    @Test
    void testJunk() {
        tester.assertParsed("+l -fast.type:offensive",
                ",;'/.<l:>? -fast.type:offensive", Query.Type.ALL);
    }

    @Test
    void testOneTermPhraseWithIndex() {
        tester.assertParsed("normal.title:foo", "normal.title:\"foo\"", Query.Type.ANY);
    }

    @Test
    void testOneTermPhraseWithIndexAdvanced() {
        tester.assertParsed("normal.title:foo", "normal.title:\"foo\"", Query.Type.ADVANCED);
    }

    @Test
    void testIncorrect1Advanced() {
        tester.assertParsed("\"to be or not\"", "\"\"to be or not", Query.Type.ADVANCED);
    }

    @Test
    void testIncorrect2Advanced() {
        tester.assertParsed("AND to \"or not to be\"", "+to\"or not to be\"\"\"", Query.Type.ADVANCED);
    }

    @Test
    void testIncorrect3Advanced() {
        tester.assertParsed("AND if am \"f g 4 2\" maybe",
                "if am \"  f g 4 2\"\" maybe", Query.Type.ADVANCED);
    }

    @Test
    void testIncorrect4Advanced() {
        tester.assertParsed("AND other is nothing test",
                "++other +++++is ++++++nothing -test", Query.Type.ADVANCED);
    }

    @Test
    void testImplicitPhrase1Advanced() {
        tester.assertParsed("AND test if", "--test+-if", Query.Type.ADVANCED);
    }

    @Test
    void testImplicitPhrase2Advanced() {
        tester.assertParsed("AND a b c d d", "a+b+c+d--d", Query.Type.ADVANCED);
    }

    @Test
    void testImplicitPhrase3Advanced() {
        tester.assertParsed("AND 123 2132odfd 934032 32423",
                "123+2132odfd.934032,,32423", Query.Type.ADVANCED);
    }

    @Test
    void testImplicitPhrase4Advanced() {
        tester.assertParsed("AND 0032 4 320 24329043", "0032+4\\320.24329043", Query.Type.ADVANCED);
    }

    @Test
    void testUtf8Advanced() {
        tester.assertParsed("1349832840234l3040roer\u00e6lf12",
                ",1349832840234l3040roer\u00e6lf12", Query.Type.ADVANCED);
    }

    @Test
    void testOperatorSearchAdvanced() {
        tester.assertParsed("RANK (OR (AND and and) or andnot) rank",
                "and and and or or or andnot rank rank", Query.Type.ADVANCED);
    }

    @Test
    void testIncorrectParenthesisAdvanced() {
        tester.assertParsed("AND foo bar", "foo and bar )", Query.Type.ADVANCED);
    }

    @Test
    void testOpeningParenthesisOnlyAdvanced() {
        tester.assertParsed("AND foo (OR bar (AND foobar xyzzy))",
                "(foo and (bar or (foobar and xyzzy", Query.Type.ADVANCED);
    }

    @Test
    void testSimpleWeight() {
        tester.assertParsed("foo!150", "foo!", Query.Type.ANY);
    }

    @Test
    void testMultipleWeight() {
        tester.assertParsed("foo!250", "foo!!!", Query.Type.ANY);
    }

    @Test
    void testExplicitWeight() {
        tester.assertParsed("foo!200", "foo!200", Query.Type.ANY);
    }

    @Test
    void testExplicitZeroWeight() {
        tester.assertParsed("foo!0", "foo!0", Query.Type.ANY);
    }

    @Test
    void testSimplePhraseWeight() {
        tester.assertParsed("\"foo bar\"!150", "\"foo bar\"!", Query.Type.ANY);
    }

    @Test
    void testSingleHyphen() {
        tester.assertParsed("AND a b", "a-b", Query.Type.ALL);
    }

    @Test
    void testUserCase() {
        tester.assertParsed("\"a a\"", "\"a- a-*\"", Query.Type.ALL);
    }

    @Test
    void testMultiplePhraseWeight() {
        tester.assertParsed("\"foo bar\"!250", "\"foo bar\"!!!", Query.Type.ANY);
    }

    @Test
    void testExplicitPhraseWeight() {
        tester.assertParsed("\"foo bar\"!200", "\"foo bar\"!200", Query.Type.ANY);
    }

    @Test
    void testUrlSubmodeHyphen() {
        assertTrue(ParsingTester.createIndexFacts().newSession(new Query()).getIndex("url.all").isUriIndex());
        tester.assertParsed("url.all:\"www-microsoft com\"", "url.all:www-microsoft.com", Query.Type.ANY);
    }

    @Test
    void testUrlSubmodeUnderscore() {
        tester.assertParsed("url.all:\"www_microsoft com\"", "url.all:www_microsoft.com", Query.Type.ANY);
    }

    @Test
    void testUrlSubmode() {
        tester.assertParsed("host.all:\"www-microsoft com $\"", "host.all:www-microsoft.com", Query.Type.ANY);
    }

    @Test
    void testExplicitHostNameAnchoringHost() {
        tester.assertParsed("host.all:\"^ www-microsoft com $\"", "host.all:^www-microsoft.com$", Query.Type.ANY);
    }

    @Test
    void testExplicitHostNameAnchoringSite() {
        tester.assertParsed("site:\"^ www-microsoft com $\"", "site:^www-microsoft.com$", Query.Type.ANY);
    }

    @Test
    void testExplicitHostNameAnchoring() {
        tester.assertParsed("host.all:\"^ http www krangaz-central com index html $\"",
                "host.all:^http://www.krangaz-central.com/index.html$",
                Query.Type.ANY);
    }

    @Test
    void testExplicitHostAnchoringRemoval() {
        tester.assertParsed("host.all:\"www-microsoft com\"",
                "host.all:www-microsoft.com*", Query.Type.ANY);

    }

    @Test
    void testQuery1Any() {
        tester.assertParsed("RANK (AND fast \"search engine\") kernel",
                "+fast +\"search engine\" kernel", Query.Type.ANY);

    }

    @Test
    void testQuery1Advanced() {
        tester.assertParsed("RANK (AND fast \"search engine\") kernel",
                "+fast and \"search engine\" rank kernel", Query.Type.ADVANCED);

    }

    @Test
    void testQuery2Any() {
        tester.assertParsed("+(OR title:car bmw) -z3", "title:car bmw -z3",
                Query.Type.ANY);

    }

    @Test
    void testQuery2Advanced() {
        tester.assertParsed("+(OR title:car bmw) -z3", "title:car or bmw andnot z3", Query.Type.ADVANCED);
    }

    @Test
    void testQuery3All() {
        tester.assertParsed("+(AND FAST search domain:no pagedepth:0) -title:phrase",
                "FAST search -title:phrase domain:no Pagedepth:0",
                Query.Type.ALL);
    }

    @Test
    void testQuery4Advanced() {
        tester.assertParsed("AND (+(AND FAST search) -title:phrase) domain:no pagedepth:0",
                "FAST and search andnot title:phrase and domain:no and Pagedepth:0",
                Query.Type.ADVANCED);
    }

    @Test
    void testQuery5Any() {
        tester.assertParsed("AND alltheweb fast search", "+alltheweb +fast +search", Query.Type.ANY);
    }

    @Test
    void testQuery6Any() {
        tester.assertParsed("RANK (+(AND query language) -sql) search", "+query +language -sql search", Query.Type.ANY);
    }

    @Test
    void testQuery7Any() {
        tester.assertParsed(
                "+(AND alltheweb (OR search engine)) -(OR excite altavista)",
                "(alltheweb and (search or engine)) andnot (excite or altavista)",
                Query.Type.ADVANCED);
    }

    @Test
    void testQuery8Advanced() {
        tester.assertParsed(
                "RANK (AND \"search engines\" \"query processing\") \"fast search\"",
                "(\"search engines\" and \"query processing\") rank \"fast search\"",
                Query.Type.ADVANCED);
    }

    @Test
    void testPStrangeAdvanced() {
        tester.assertParsed("AND AND r.s:jnl", "( AND +r.s:jnl) ", Query.Type.ADVANCED);
    }

    @Test
    void testEmptyNestAdvanced() {
        tester.assertParsed(null, "() ", Query.Type.ADVANCED);
    }

    @Test
    void testNestedBeginningAdvanced() {
        tester.assertParsed("AND (OR a b) c", "(a or b) and c", Query.Type.ADVANCED);
    }

    @Test
    void testNestedPositiveAny() {
        tester.assertParsed("AND (OR a b) c", "+(a b) +c", Query.Type.ANY);
    }

    @Test
    void testParseAdvancedQuery() {
        tester.assertParsed("AND joplin remediation r.s:jnl",
                "(joplin and + and remediation and +r.s:jnl)",
                Query.Type.ADVANCED);
    }

    @Test
    void testSimpleDotPhraseAny() {
        tester.assertParsed("OR a (AND b c) d", "a b.c d", Query.Type.ANY);
    }

    @Test
    void testSimpleHyphenPhraseAny() {
        tester.assertParsed("OR a (AND b c) d", "a b-c d", Query.Type.ANY);
    }

    @Test
    void testAnotherSimpleDotPhraseAny() {
        tester.assertParsed("OR (AND a b) c d", "a.b c d", Query.Type.ANY);
    }

    @Test
    void testYetAnotherSimpleDotPhraseAny() {
        tester.assertParsed("OR a b (AND c d)", "a b c.d", Query.Type.ANY);
    }

    @Test
    void testVariousSeparatorsPhraseAny() {
        tester.assertParsed("AND a b c d", "a-b.c%d", Query.Type.ANY);
    }

    @Test
    void testDoublyMarkedPhraseAny() {
        tester.assertParsed("OR a \"b c\" d", "a \"b.c\" d", Query.Type.ANY);
    }

    @Test
    void testPartlyDoublyMarkedPhraseAny() {
        tester.assertParsed("OR a \"b c d\"", "a \"b.c d\"", Query.Type.ANY);
    }

    @Test
    void testIndexedDottedPhraseAny() {
        tester.assertParsed("OR a (AND url:b url:c) d", "a url:b.c d", Query.Type.ANY);
    }

    @Test
    void testIndexedPlusedPhraseAny() {
        tester.assertParsed("OR a (AND normal.title:b normal.title:c) d", "a normal.title:b+c d", Query.Type.ANY);
    }

    @Test
    void testNestedNotAny() {
        tester.assertParsed(
                "RANK (+(OR normal.title:foobar (AND url:www url:pvv url:org)) -foo) a",
                "a +(normal.title:foobar url:www.pvv.org) -foo", Query.Type.ANY);
    }

    @Test
    void testDottedPhraseAdvanced() {
        tester.assertParsed("OR a (AND b c)", "a or b.c", Query.Type.ADVANCED);
    }

    @Test
    void testHyphenPhraseAdvanced() {
        tester.assertParsed("OR (AND a (AND b c)) d", "a and b-c or d", Query.Type.ADVANCED);
    }

    @Test
    void testAnotherDottedPhraseAdvanced() {
        tester.assertParsed("OR (AND a b) c", "a.b or c", Query.Type.ADVANCED);
    }

    @Test
    void testNottedDottedPhraseAdvanced() {
        tester.assertParsed("+a -(AND c d)", "a andnot c.d", Query.Type.ADVANCED);
    }

    @Test
    void testVariousSeparatorsPhraseAdvanced() {
        tester.assertParsed("AND a b c d", "a-b.c%d", Query.Type.ADVANCED);
    }

    @Test
    void testDoublyPhrasedAdvanced() {
        tester.assertParsed("OR (AND a \"b c\") d", "a and \"b.c\" or d", Query.Type.ADVANCED);
    }

    @Test
    void testPartlyDoublyPhrasedAdvanced() {
        tester.assertParsed("OR a \"b c d\"", "a or \"b.c d\"", Query.Type.ADVANCED);
    }

    @Test
    void testNestedDottedPhraseAdvanced() {
        tester.assertParsed("AND a (OR url:\"b c\" d)", "a and(url:\"b.c\" or d)", Query.Type.ADVANCED);
    }

    @Test
    void testNestedPlussedPhraseAdvanced() {
        tester.assertParsed("AND (OR a (AND normal.title:b normal.title:c)) d",
                "a or normal.title:b+c and d", Query.Type.ADVANCED);
    }

    @Test
    void testNottedNestedDottedPhraseAdvanced() {
        tester.assertParsed(
                "+(AND a (OR normal.title:foobar (AND url:www url:pvv url:org))) -foo",
                "a and (normal.title:foobar or url:www.pvv.org) andnot foo",
                Query.Type.ADVANCED);
    }

    @Test
    void testPlusedThenQuotedPhraseAny() {
        tester.assertParsed("\"a b c\"", "a+\"b c\"", Query.Type.ANY);
    }

    @Test
    void testPlusedTwiceThenQuotedPhraseAny() {
        tester.assertParsed("AND a b c d", "a+b+\"c d\"", Query.Type.ANY);
    }

    @Test
    void testPlusedThenQuotedPhraseAdvanced() {
        tester.assertParsed("\"a b c\"", "a+\"b c\"", Query.Type.ADVANCED);
    }

    @Test
    void testPhrasesInBraces() {
        tester.assertParsed("AND url.domain:microsoft url.domain:com",
                "+(url.domain:microsoft.com)", Query.Type.ALL);
    }

    @Test
    void testDoublyPhrasedPhrasesInBraces() {
        tester.assertParsed("url.domain:\"microsoft com\"",
                "+(url.domain:\"microsoft.com\")", Query.Type.ALL);
    }

    @Test
    void testSinglePrefixTerm() {
        Item root = tester.assertParsed("prefix*", "prefix*", Query.Type.ANY);
        assertTrue(root instanceof PrefixItem);
    }

    @Test
    void testSingleSubstringTerm() {
        Item root = tester.assertParsed("*substring*", "*substring*", Query.Type.ANY);
        assertTrue(root instanceof SubstringItem);
    }

    @Test
    void testSingleSuffixTerm() {
        Item root = tester.assertParsed("*suffix", "*suffix", Query.Type.ANY);
        assertTrue(root instanceof SuffixItem);
    }

    @Test
    void testPrefixAndWordTerms() {
        Item root = tester.assertParsed("OR foo prefix* bar", "foo prefix* bar", Query.Type.ANY);
        assertTrue(((OrItem) root).getItem(1) instanceof PrefixItem);
    }

    @Test
    void testSubstringAndWordTerms() {
        Item root = tester.assertParsed("OR foo *substring* bar", "foo *substring* bar", Query.Type.ANY);
        assertTrue(((OrItem) root).getItem(1) instanceof SubstringItem);
    }

    @Test
    void testSuffixAndWordTerms() {
        Item root = tester.assertParsed("OR foo *suffix bar", "foo *suffix bar", Query.Type.ANY);
        assertTrue(((OrItem) root).getItem(1) instanceof SuffixItem);
    }

    @Test
    void testPhraseNotPrefix() {
        tester.assertParsed("OR foo (AND prefix bar)", "foo prefix*bar", Query.Type.ANY);
    }

    @Test
    void testPhraseNotSubstring() {
        tester.assertParsed("OR foo (AND substring bar)", "foo *substring*bar", Query.Type.ANY);
    }

    @Test
    void testPhraseNotSuffix() {
        tester.assertParsed("OR (AND foo suffix) bar", "foo*suffix bar", Query.Type.ANY);
    }

    @Test
    void testIndexedPrefix() {
        Item root = tester.assertParsed("foo.bar:prefix*", "foo.bar:prefix*", Query.Type.ANY);
        assertTrue(root instanceof PrefixItem);
    }

    @Test
    void testIndexedSubstring() {
        Item root = tester.assertParsed("foo.bar:*substring*", "foo.bar:*substring*", Query.Type.ANY);
        assertTrue(root instanceof SubstringItem);
    }

    @Test
    void testIndexedSuffix() {
        Item root = tester.assertParsed("foo.bar:*suffix", "foo.bar:*suffix", Query.Type.ANY);
        assertTrue(root instanceof SuffixItem);
    }

    @Test
    void testIndexedPhraseNotPrefix() {
        tester.assertParsed("AND foo.bar:prefix foo.bar:xyzzy", "foo.bar:prefix*xyzzy", Query.Type.ANY);
    }

    @Test
    void testIndexedPhraseNotSubstring() {
        tester.assertParsed("AND foo.bar:substring foo.bar:xyzzy", "foo.bar:*substring*xyzzy", Query.Type.ANY);
    }

    @Test
    void testIndexedPhraseNotSuffix() {
        tester.assertParsed("AND foo.bar:xyzzy foo.bar:suffix", "foo.bar:xyzzy*suffix", Query.Type.ANY);
    }

    @Test
    void testPrefixWithWeight() {
        Item root = tester.assertParsed("prefix*!200", "prefix*!200", Query.Type.ANY);
        assertTrue(root instanceof PrefixItem);
    }

    @Test
    void testSubstringWithWeight() {
        Item root = tester.assertParsed("*substring*!200", "*substring*!200", Query.Type.ANY);
        assertTrue(root instanceof SubstringItem);
    }

    @Test
    void testSuffixWithWeight() {
        Item root = tester.assertParsed("*suffix!200", "*suffix!200", Query.Type.ANY);
        assertTrue(root instanceof SuffixItem);
    }

    /** Non existing index → and **/
    @Test
    void testNonIndexPhraseNotPrefix() {
        tester.assertParsed("AND void prefix", "void:prefix*", Query.Type.ANY);
    }

    @Test
    void testNonIndexPhraseNotSubstring() {
        tester.assertParsed("AND void substring", "void:*substring*", Query.Type.ANY);
    }

    @Test
    void testNonIndexPhraseNotSuffix() {
        tester.assertParsed("AND void suffix", "void:*suffix", Query.Type.ANY);
    }

    /** Explicit phrase → remove '*' **/
    @Test
    void testExplicitPhraseNotPrefix() {
        tester.assertParsed("\"prefix bar\"", "\"prefix* bar\"", Query.Type.ANY);
    }

    @Test
    void testExplicitPhraseNotSubstring() {
        tester.assertParsed("\"substring bar\"", "\"*substring* bar\"", Query.Type.ANY);
    }

    @Test
    void testExplicitPhraseNotSuffix() {
        tester.assertParsed("\"suffix bar\"", "\"*suffix bar\"", Query.Type.ANY);
    }

    /** Extra star is ignored */
    @Test
    void testPrefixExtraStar() {
        Item root = tester.assertParsed("prefix*", "prefix**", Query.Type.ANY);
        assertTrue(root instanceof PrefixItem);
    }

    @Test
    void testSubstringExtraStar() {
        Item root = tester.assertParsed("*substring*", "**substring**", Query.Type.ANY);
        assertTrue(root instanceof SubstringItem);
    }

    @Test
    void testSuffixExtraStar() {
        Item root = tester.assertParsed("*suffix", "**suffix", Query.Type.ANY);
        assertTrue(root instanceof SuffixItem);
    }

    @Test
    void testPrefixExtraSpace() {
        Item root = tester.assertParsed("prefix", "prefix *", Query.Type.ANY);
        assertTrue(root instanceof WordItem);
    }

    @Test
    void testSubstringExtraSpace() {
        Item root = tester.assertParsed("*substring*", "* substring*", Query.Type.ANY);
        assertTrue(root instanceof SubstringItem);
    }

    @Test
    void testSubstringExtraSpace2() {
        Item root = tester.assertParsed("*substring", "* substring *", Query.Type.ANY);
        assertTrue(root instanceof SuffixItem);
    }

    @Test
    void testSuffixExtraSpace() {
        Item root = tester.assertParsed("*suffix", "* suffix", Query.Type.ANY);
        assertTrue(root instanceof SuffixItem);
    }

    /** Extra spaces with index **/
    @Test
    void testIndexPrefixExtraSpace() {
        tester.assertParsed("AND foo prefix", "foo:prefix *", Query.Type.ANY);
    }

    @Test
    void testIndexSubstringExtraSpace() {
        Item root = tester.assertParsed("OR foo substring*", "foo:* substring*", Query.Type.ANY);
        assertTrue(((OrItem) root).getItem(0) instanceof WordItem);
        assertTrue(((OrItem) root).getItem(1) instanceof PrefixItem);
    }

    @Test
    void testIndexSubstringExtraSpace2() {
        Item root = tester.assertParsed("OR foo substring", "foo:* substring *", Query.Type.ANY);
        assertTrue(((OrItem) root).getItem(0) instanceof WordItem);
        assertTrue(((OrItem) root).getItem(1) instanceof WordItem);
    }

    @Test
    void testIndexSuffixExtraSpace() {
        Item root = tester.assertParsed("OR foo suffix", "foo:* suffix", Query.Type.ANY);
        assertTrue(((OrItem) root).getItem(0) instanceof WordItem);
        assertTrue(((OrItem) root).getItem(1) instanceof WordItem);
    }

    /** Various tests for prefix, substring, and suffix terms **/
    @Test
    void testTermsWithStarsAndSpaces() {
        tester.assertParsed("OR foo *bar", "foo * bar", Query.Type.ANY);
    }

    @Test
    void testTermsWithStarsAndSpaces2() {
        tester.assertParsed("OR foo *bar *baz", "foo * * bar * * baz", Query.Type.ANY);
    }

    @Test
    void testTermsWithStarsAndPlussAndMinus() {
        tester.assertParsed("+(AND *bar baz*) -*foo*", "+*bar -*foo* +baz*", Query.Type.ANY);
    }

    @Test
    void testTermsWithStarsAndPlussAndMinus2() {
        tester.assertParsed("OR *bar *foo baz", "+ * bar - * foo * + baz *", Query.Type.ANY);
    }

    @Test
    void testTermsWithStarsAndExclamation() {
        tester.assertParsed("OR foo* 200 *bar* 200 *baz 200", "foo* !200 *bar* !200 *baz !200", Query.Type.ANY);
    }

    @Test
    void testTermsWithStarsAndExclamation2() {
        tester.assertParsed("OR foo 200 *bar 200", "foo *!200 *bar *!200", Query.Type.ANY);
    }

    @Test
    void testTermsWithStarsAndParenthesis() {
        tester.assertParsed("RANK *baz *bar* foo*", "(foo*) (*bar*) (*baz)", Query.Type.ANY);
    }

    @Test
    void testTermsWithStarsAndParenthesis2() {
        tester.assertParsed("RANK baz bar foo", "(foo)* *(bar)* *(baz)", Query.Type.ANY);
    }


    @Test
    void testSimpleAndFilter() {
        tester.assertParsed("AND bar |foo", "bar", "+foo", Query.Type.ANY);
    }

    @Test
    void testSimpleRankFilter() {
        tester.assertParsed("RANK bar |foo", "bar", "foo", Query.Type.ANY);
    }

    @Test
    void testSimpleNotFilter() {
        tester.assertParsed("+bar -|foo", "bar", "-foo", Query.Type.ANY);
    }

    @Test
    void testSimpleCompoundFilter1() {
        tester.assertParsed("RANK (AND bar |foo1) |foo2", "bar", "+foo1 foo2",
                Query.Type.ANY);
    }

    @Test
    void testSimpleCompoundFilter2() {
        tester.assertParsed("+(AND bar |foo1) -|foo3", "bar", "+foo1 -foo3",
                Query.Type.ANY);
    }

    @Test
    void testSimpleCompoundFilter3() {
        tester.assertParsed("RANK (+bar -|foo3) |foo2", "bar", "foo2 -foo3",
                Query.Type.ANY);
    }

    @Test
    void testSimpleCompoundFilter4() {
        tester.assertParsed("RANK (+(AND bar |foo1) -|foo3) |foo2", "bar",
                "+foo1 foo2 -foo3", Query.Type.ANY);
    }

    @Test
    void testAndFilterEmptyQuery() {
        tester.assertParsed("|foo", "", "+foo", Query.Type.ANY);
    }

    @Test
    void testRankFilterEmptyQuery() {
        tester.assertParsed("|foo", "", "foo", Query.Type.ANY);
    }

    @Test
    void testNotFilterEmptyQuery() {
        tester.assertParsed(null, "", "-foo", Query.Type.ANY);
    }

    @Test
    void testCompoundFilter1EmptyQuery() {
        tester.assertParsed("RANK |foo1 |foo2", "", "+foo1 foo2", Query.Type.ANY);
    }

    @Test
    void testCompoundFilter2EmptyQuery() {
        tester.assertParsed("+|foo1 -|foo3", "", "+foo1 -foo3", Query.Type.ANY);
    }

    @Test
    void testCompoundFilter3EmptyQuery() {
        tester.assertParsed("+|foo2 -|foo3", "", "foo2 -foo3", Query.Type.ANY);
    }

    @Test
    void testCompoundFilter4EmptyQuery() {
        tester.assertParsed("RANK (+|foo1 -|foo3) |foo2", "", "+foo1 foo2 -foo3",
                Query.Type.ANY);
    }

    @Test
    void testMultitermAndFilter() {
        tester.assertParsed("AND bar |foo |foz", "bar", "+foo +foz", Query.Type.ANY);
    }

    @Test
    void testMultitermRankFilter() {
        tester.assertParsed("RANK bar |foo |foz", "bar", "foo foz", Query.Type.ANY);
    }

    @Test
    void testMultitermNotFilter() {
        tester.assertParsed("+bar -|foo -|foz", "bar", "-foo -foz", Query.Type.ANY);
    }

    @Test
    void testMultitermCompoundFilter1() {
        tester.assertParsed("RANK (AND bar |foo1 |foz1) |foo2 |foz2", "bar",
                "+foo1 +foz1 foo2 foz2", Query.Type.ANY);
    }

    @Test
    void testMultitermCompoundFilter2() {
        tester.assertParsed("+(AND bar |foo1 |foz1) -|foo3 -|foz3", "bar",
                "+foo1 +foz1 -foo3 -foz3", Query.Type.ANY);
    }

    @Test
    void testMultitermCompoundFilter3() {
        tester.assertParsed("RANK (+bar -|foo3 -|foz3) |foo2 |foz2", "bar",
                "foo2 foz2 -foo3 -foz3", Query.Type.ANY);
    }

    @Test
    void testMultitermCompoundFilter4() {
        tester.assertParsed("RANK (+(AND bar |foo1 |foz1) -|foo3 -|foz3) |foo2 |foz2",
                "bar", "+foo1 +foz1 foo2 foz2 -foo3 -foz3", Query.Type.ANY);
    }

    @Test
    void testMultitermAndFilterEmptyQuery() {
        tester.assertParsed("AND |foo |foz", "", "+foo +foz", Query.Type.ANY);
    }

    @Test
    void testMultitermRankFilterEmptyQuery() {
        tester.assertParsed("OR |foo |foz", "", "foo foz", Query.Type.ANY);
    }

    @Test
    void testMultitermNotFilterEmptyQuery() {
        tester.assertParsed(null, "", "-foo -foz", Query.Type.ANY);
    }

    @Test
    void testMultitermCompoundFilter1EmptyQuery() {
        tester.assertParsed("RANK (AND |foo1 |foz1) |foo2 |foz2", "",
                "+foo1 +foz1 foo2 foz2", Query.Type.ANY);
    }

    @Test
    void testMultitermCompoundFilter2EmptyQuery() {
        tester.assertParsed("+(AND |foo1 |foz1) -|foo3 -|foz3", "",
                "+foo1 +foz1 -foo3 -foz3", Query.Type.ANY);
    }

    @Test
    void testMultitermCompoundFilter3EmptyQuery() {
        tester.assertParsed("+(OR |foo2 |foz2) -|foo3 -|foz3", "",
                "foo2 foz2 -foo3 -foz3", Query.Type.ANY);
    }

    @Test
    void testMultitermCompoundFilter4EmptyQuery() {
        tester.assertParsed("RANK (+(AND |foo1 |foz1) -|foo3 -|foz3) |foo2 |foz2", "",
                "+foo1 +foz1 foo2 foz2 -foo3 -foz3", Query.Type.ANY);
    }

    @Test
    void testMultipleDifferentPhraseSeparators() {
        tester.assertParsed("AND foo bar", "foo.-.bar", Query.Type.ANY);
    }

    @Test
    void testNoisyFilter() {
        tester.assertParsed("RANK (+(AND foobar kanoo) -|foo) |bar", "foobar and kanoo",
                "-foo ;+;bar", Query.Type.ADVANCED);
    }

    @Test
    void testReallyNoisyQuery1() {
        tester.assertParsed("AND word another", "&word\"()/&#)(/&another!\"", Query.Type.ALL);
    }

    @Test
    void testReallyNoisyQuery2() {
        tester.assertParsed("AND \u03bc\u03bc hei", "&&&`\u00b5\u00b5=@hei", Query.Type.ALL);
    }

    @Test
    void testReallyNoisyQuery3() {
        tester.assertParsed("AND hei hallo du der", "hei-hallo;du;der", Query.Type.ALL);
    }

    @Test
    void testNumberParsing() {
        Item root = tester.parseQuery("normal:400", null, Language.UNKNOWN, Query.Type.ANY, TestLinguistics.INSTANCE);
        assertEquals(root.getCode(), 5);
    }

    @Test
    void testRangeParsing() {
        Item root = tester.parseQuery("normal:[5;400]", null, Language.UNKNOWN, Query.Type.ANY, TestLinguistics.INSTANCE);
        assertEquals(root.toString(), "normal:[5;400]");
        assertEquals(root.getCode(), 5);
    }

    @Test
    void testNumberAsPrefix() {
        Item root = tester.assertParsed("89*", "89*", Query.Type.ANY);
        assertTrue(root instanceof PrefixItem);
    }

    @Test
    void testNumberAsSubstring() {
        Item root = tester.assertParsed("*89*", "*89*", Query.Type.ANY);
        assertTrue(root instanceof SubstringItem);
    }

    @Test
    void testNumberAsSuffix() {
        Item root = tester.assertParsed("*89", "*89", Query.Type.ANY);
        assertTrue(root instanceof SuffixItem);
    }

    @Test
    void testTheStupidSymbolsWhichAreNowWordCharactersInUnicode() {
        tester.assertParsed("AND yz a", "yz\u00A8\u00AA\u00AF", Query.Type.ANY);
    }

    @Test
    void testTWoWords() {
        tester.assertParsed("\"hei h\u00e5\"", "\"hei h\u00e5\"", Query.Type.ANY);
    }

    @Test
    void testLoneStar() {
        assertTrue(tester.parseQuery("*", null, Language.UNKNOWN, Query.Type.ANY, TestLinguistics.INSTANCE) instanceof NullItem);
    }

    @Test
    void testLoneStarWithFilter() {
        tester.assertParsed("|a", "*", "+a", Query.Type.ANY);
    }

    @Test
    void testImplicitPhrasingWithIndex() {
        tester.assertParsed("AND a:b a:c", "a:/b/c", Query.Type.ANY);
    }

    @Test
    void testSingleNoisyTermWithIndex() {
        tester.assertParsed("a:b", "a:/b", Query.Type.ANY);
    }

    @Test
    void testSingleNoisyPhraseWithIndex() {
        tester.assertParsed("AND mail:yahoo mail:com", "mail:@yahoo.com", Query.Type.ANY);
    }

    @Test
    void testPhraseWithWeightAndIndex() {
        tester.assertParsed("to:\"a b\"!150", "to:\"a b\"!150", Query.Type.ANY);
    }

    @Test
    void testTermWithWeightAndIndex() {
        tester.assertParsed("to:a!150", "to:a!150", Query.Type.ANY);
    }

    @Test
    void testPhrasingWithIndexAndQuerySyntax() {
        tester.assertParsed("to:\"a b c\"", "to:\"a (b c)\"", Query.Type.ANY);
    }

    @Test
    void testPhrasingWithIndexAndHalfBrokenQuerySyntax() {
        tester.assertParsed("to:\"a b c\"", "to:\"a +b c)\"", Query.Type.ANY);
    }

    @Test
    void testURLHostQueryOneTerm1() {
        tester.assertParsed("site:\"com $\"", "site:com", Query.Type.ANY);
    }

    @Test
    void testURLHostQueryOneTerm2() {
        tester.assertParsed("site:com", "site:com*", Query.Type.ANY);
    }

    @Test
    void testURLHostQueryOneTerm3() {
        tester.assertParsed("site:\"com $\"", "site:.com", Query.Type.ANY);
    }

    @Test
    void testURLHostQueryOneTerm4() {
        tester.assertParsed("site:\"^ com $\"", "site:^com", Query.Type.ANY);
    }

    @Test
    void testURLHostQueryOneTerm5() {
        tester.assertParsed("site:\"^ com\"", "site:^com*", Query.Type.ANY);
    }

    @Test
    void testFullURLQuery() {
        tester.assertParsed(
                "url.all:\"http shopping yahoo-inc com 1080 this is a path shop d hab id 1804905709 frag1\"",
                "url.all:http://shopping.yahoo-inc.com:1080/this/is/a/path/shop?d=hab&id=1804905709#frag1",
                Query.Type.ANY);
    }

    @Test
    void testURLQueryHyphen() {
        tester.assertParsed(
                "url.all:\"http news bbc co uk go rss test - sport1 hi tennis 4112866 stm\"",
                "url.all:http://news.bbc.co.uk/go/rss/test/-/sport1/hi/tennis/4112866.stm",
                Query.Type.ANY);
    }

    @Test
    void testURLQueryUnderScoreNumber() {
        tester.assertParsed(
                "url.all:\"ap 20050621 45_ap_on_re_la_am_ca aruba_missing_teen_5\"",
                "url.all:/ap/20050621/45_ap_on_re_la_am_ca/aruba_missing_teen_5",
                Query.Type.ANY);
    }

    @Test
    void testOtherComplexUrls() {
        tester.assertParsed(
                "url.all:\"http redir folha com br redir online dinheiro rss091 http www1 folha uol com br folha dinheiro ult91u96593 shtml\"",
                "url.all:http://redir.folha.com.br/redir/online/dinheiro/rss091/*http://www1.folha.uol.com.br/folha/dinheiro/ult91u96593.shtml",
                Query.Type.ALL);
        tester.assertParsed(
                "url.all:\"http economista com mx online4 nsf all 6FC11CB53F8A305B0625702700709029 OpenDocument\"",
                "url.all:http://economista.com.mx/online4.nsf/(all)/6FC11CB53F8A305B0625702700709029?OpenDocument",
                Query.Type.ALL);
        tester.assertParsed(
                "url.all:\"http www tierradelfuego info index php s AR13xbyxg espectaculos programa ARc7hzxb\"",
                "url.all:http://www.tierradelfuego.info/index.php?s=AR13xbyxg$$espectaculos/programa$ARc7hzxb",
                Query.Type.ALL);
        tester.assertParsed(
                "url.all:\"http www newsadvance com servlet Satellite pagename LNA MGArticle IMD_BasicArticle c MGArticle cid 1031782787014 path mgnetwork diversions\"",
                "url.all:http://www.newsadvance.com/servlet/Satellite?pagename=LNA/MGArticle/IMD_BasicArticle&c=MGArticle&cid=1031782787014&path=!mgnetwork!diversions",
                Query.Type.ALL);
        tester.assertParsed(
                "AND ull:http ull:www ull:neue ull:oz ull:de ull:information ull:pub ull:Boulevard ull:index ull:html ull:file ull:a ull:3 ull:s ull:4 ull:file s:\"37 iptc bdt 20050607 294 dpa 9001170 txt\" s:\"3 dir\" s:\"26 opt DPA parsed boulevard\" s:\"7 bereich\" s:\"9 Boulevard\"",
                "ull:http://www.neue-oz.de/information/pub_Boulevard/index.html?file=a:3:{s:4:\"file\";s:37:\"iptc-bdt-20050607-294-dpa_9001170.txt\";s:3:\"dir\";s:26:\"/opt/DPA/parsed/boulevard/\";s:7:\"bereich\";s:9:\"Boulevard\";}",
                Query.Type.ALL);
    }

    @Test
    void testTooGreedyUrlParsing() {
        tester.assertParsed("AND site:\"nypost com $\" about", "site:nypost.com about",
                Query.Type.ALL);
    }

    @Test
    void testTooGreedyUrlParsing2() {
        tester.assertParsed("AND site:\"nypost com $\" about foo",
                "site:nypost.com about foo", Query.Type.ALL);
    }

    @Test
    void testSimplerDurbin() {
        tester.assertParsed("+(OR language:en \"Durbin said\" a) -newstype:rssexclude",
                "( a (\"Durbin said\" ) -newstype:rssexclude (language:en )",
                Query.Type.ALL);
    }

    @Test
    void testSimplerDurbin2() {
        tester.assertParsed("+(AND \"Durbin said\" language:en) -newstype:rssexclude",
                "( , (\"Durbin said\" ) -newstype:rssexclude (language:en )",
                Query.Type.ALL);
    }

    @Test
    void testDurbin() {
        tester.assertParsed(
                "AND \"Richard Durbin\" Welfare (+(OR language:en \"Durbin said\" \"Durbin says\" \"Durbin added\" \"Durbin agreed\" \"Durbin questioned\" date:>1109664000) -newstype:rssexclude)",
                "(\"Richard Durbin\" ) \"Welfare\" ((\"Durbin said\" \"Durbin says\" \"Durbin added\" \"Durbin agreed\" \"Durbin questioned\" )  -newstype:rssexclude date:>1109664000 (language:en )",
                Query.Type.ALL);
    }

    @Test
    void testTooLongQueryTerms() {
        tester.assertParsed("AND 545558598787gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggcfffffffffffffffffffffffffffffffffffffffffffccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccclllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyytttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrreeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqlkjhcxxdfffxdzzaqwwsxedcrfvtgbyhnujmikkiloolpppof filter ew 545558598787gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggcfffffffffffffffffffffffffffffffffffffffffffccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccclllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyytttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrreeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqlkjhcxxdfffxdzzaqwwsxedcrfvtgbyhnujmikkiloolpppof 2b 2f 545558598787gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggcfffffffffffffffffffffffffffffffffffffffffffccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccclllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyytttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrreeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqlkjhcxxdfffxdzzaqwwsxedcrfvtgbyhnujmikkiloolpppof",
                "+/545558598787gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggcfffffffffffffffffffffffffffffffffffffffffffccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccclllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyytttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrreeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqlkjhcxxdfffxdzzaqwwsxedcrfvtgbyhnujmikkiloolpppof&filter=ew:545558598787gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggcfffffffffffffffffffffffffffffffffffffffffffccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccclllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyytttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrreeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqlkjhcxxdfffxdzzaqwwsxedcrfvtgbyhnujmikkiloolpppof!1000 =.2b..2f.545558598787gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggcfffffffffffffffffffffffffffffffffffffffffffccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccclllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyytttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrreeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqlkjhcxxdfffxdzzaqwwsxedcrfvtgbyhnujmikkiloolpppof",
                Query.Type.ALL);
    }

    @Test
    void testNonSpecialTokenParsing() {
        ParsingTester customTester = new ParsingTester(SpecialTokens.empty());
        customTester.assertParsed("OR c or c with (AND tcp ip)", "c# or c++ with tcp/ip", Query.Type.ANY);
    }

    @Test
    void testNonIndexWithColons1() {
        tester.assertParsed("OR this is (AND notan iindex)", "this is notan:iindex", Query.Type.ANY);
    }

    @Test
    void testNonIndexWithColons2() {
        tester.assertParsed("OR this is (AND notan iindex either)", "this is notan:iindex:either", Query.Type.ANY);
    }

    @Test
    void testIndexThenUnderscoreTermBecomesIndex() {
        tester.assertParsed("AND name:batch name:article", "name:batch_article", Query.Type.ANY);
    }

    @Test
    void testFakeCJKSegmenting() {
        // "first" "second" and "third" are segments in the test language
        Item item = tester.parseQuery("name:firstsecondthird", null, Language.CHINESE_SIMPLIFIED, Query.Type.ANY, TestLinguistics.INSTANCE);

        assertTrue(item instanceof AndSegmentItem);
        AndSegmentItem segment = (AndSegmentItem) item;

        assertEquals(3, segment.getItemCount());
        assertEquals("name:first", segment.getItem(0).toString());
        assertEquals("name:second", segment.getItem(1).toString());
        assertEquals("name:third", segment.getItem(2).toString());

        assertEquals("name", ((WordItem) segment.getItem(0)).getIndexName());
        assertEquals("name", ((WordItem) segment.getItem(1)).getIndexName());
        assertEquals("name", ((WordItem) segment.getItem(2)).getIndexName());
    }

    @Test
    void testFakeCJKSegmentingOfPhrase() {
        // "first" "second" and "third" are segments in the test language
        Item item = tester.parseQuery("name:\"firstsecondthird\"", null, Language.CHINESE_SIMPLIFIED, Query.Type.ANY, TestLinguistics.INSTANCE);

        assertTrue(item instanceof PhraseSegmentItem);
        PhraseSegmentItem segment = (PhraseSegmentItem) item;

        assertEquals(3, segment.getItemCount());
        assertEquals("name:first", segment.getItem(0).toString());
        assertEquals("name:second", segment.getItem(1).toString());
        assertEquals("name:third", segment.getItem(2).toString());

        assertEquals("name", ((WordItem) segment.getItem(0)).getIndexName());
        assertEquals("name", ((WordItem) segment.getItem(1)).getIndexName());
        assertEquals("name", ((WordItem) segment.getItem(2)).getIndexName());
    }

    @Test
    void testAndItemAndImplicitPhrase() {
        tester.assertParsed("AND \u00d8 \u00d8 \u00d8 \u00d9",
                "\u00d8\u00b9\u00d8\u00b1\u00d8\u00a8\u00d9", "",
                Query.Type.ALL, Language.CHINESE_SIMPLIFIED);
    }

    @Test
    void testAvoidMultiLevelAndForLongCJKQueries() {
        Item root = tester.parseQuery(
                "\u30d7\u30ed\u91ce\u7403\u962a\u795e\u306e\u672c\u62e0\u5730\u3001\u7532\u5b50\u5712\u7403\u5834\uff08\u5175\u5eab\u770c\u897f\u5bae\u5e02\uff09\u306f\uff11\u65e5\u3001\uff11\uff19\uff12\uff14\u5e74\u30d7\u30ed\u91ce\u7403\u962a\u795e\u306e\u672c\u62e0\u5730\u3001\u7532\u5b50\u5712\u7403\u5834\uff08\u5175\u5eab\u770c\u897f\u5bae\u5e02\uff09\u306f\uff11\u65e5\u3001\uff11\uff19\uff12\uff14\u5e74\u30d7\u30ed\u91ce\u7403\u962a\u795e\u306e\u672c\u62e0\u5730\u3001\u7532\u5b50\u5712\u7403\u5834\uff08\u5175\u5eab\u770c\u897f\u5bae\u5e02\uff09\u306f\uff11\u65e5\u3001\uff11\uff19\uff12\uff14\u5e74\u30d7\u30ed\u91ce\u7403\u962a\u795e\u306e\u672c\u62e0\u5730\u3001\u7532\u5b50\u5712\u7403\u5834\uff08\u5175\u5eab\u770c\u897f\u5bae\u5e02\uff09\u306f\uff11\u65e5\u3001\uff11\uff19\uff12\uff14\u5e74\u30d7\u30ed\u91ce\u7403\u962a\u795e\u306e\u672c\u62e0\u5730\u3001\u7532\u5b50\u5712\u7403\u5834\uff08\u5175\u5eab\u770c\u897f\u5bae\u5e02\uff09\u306f\uff11\u65e5\u3001\uff11\uff19\uff12\uff14\u5e74\u30d7\u30ed\u91ce\u7403\u962a\u795e\u306e\u672c\u62e0\u5730\u3001\u7532\u5b50\u5712\u7403\u5834\uff08\u5175\u5eab\u770c\u897f\u5bae\u5e02\uff09\u306f\uff11\u65e5\u3001\uff11\uff19\uff12\uff14\u5e74",
                "", Language.UNKNOWN, Query.Type.ALL, TestLinguistics.INSTANCE);

        assertTrue(4 > stackDepth(0, root), "Query tree too deep when parsing CJK queries.");
    }

    private int stackDepth(int i, Item root) {
        if (root instanceof CompositeItem) {
            int maxDepth = i;

            for (Iterator<Item> j = ((CompositeItem) root).getItemIterator(); j.hasNext();) {
                int newDepth = stackDepth(i + 1, j.next());

                maxDepth = java.lang.Math.max(maxDepth, newDepth);
            }
            return maxDepth;
        } else {
            return i;
        }
    }

    @Test
    void testFakeCJKSegmentingOfMultiplePhrases() {
        Item item = tester.parseQuery("name:firstsecond.s", null, Language.CHINESE_SIMPLIFIED, Query.Type.ANY, TestLinguistics.INSTANCE);
        assertEquals("AND (SAND name:first name:second) name:s", item.toString());
    }

    @Test
    void testOrFilter() {
        tester.assertParsed("AND d (OR |a |b)", "d", "+(a b)", Query.Type.ALL);
    }

    @Test
    void testOrFilterWithTypeAdv() {
        tester.assertParsed("AND d (OR |a |b)", "d", "+(a b)", Query.Type.ADVANCED);
    }

    @Test
    void testPhraseFilter() {
        tester.assertParsed("AND d |\"a b\"", "d", "+\"a b\"", Query.Type.ALL);
    }

    @Test
    void testMinusAndFilter() {
        tester.assertParsed("+d -(AND |a |b)", "d", "-(a b)", Query.Type.ALL);
    }

    @Test
    void testOrAndSomeTermsFilter() {
        tester.assertParsed("RANK d |c |a |b |e", "d", "c (a b) e", Query.Type.ALL);
    }

    // This is an ugly parse tree, but it's at least reasonable
    @Test
    void testOrAndSomeTermsFilterAndAnAnd() {
        AndItem root = (AndItem) tester.assertParsed("AND (RANK d |c |a |b |e) (OR |e |f)", "d", "c (a b) e +(e f)", Query.Type.ALL);
        assertFalse(root.isFilter()); // AND
        assertFalse(root.getItem(0).isFilter()); // RANK
        assertFalse(((RankItem) root.getItem(0)).getItem(0).isFilter()); // d
        assertTrue(((RankItem) root.getItem(0)).getItem(1).isFilter()); // c
        assertTrue(((RankItem) root.getItem(0)).getItem(2).isFilter()); // a
        assertTrue(((RankItem) root.getItem(0)).getItem(3).isFilter()); // b
        assertTrue(((RankItem) root.getItem(0)).getItem(4).isFilter()); // e
        assertFalse(root.getItem(1).isFilter()); // OR
        assertTrue(((OrItem) root.getItem(1)).getItem(0).isFilter()); // e
        assertTrue(((OrItem) root.getItem(1)).getItem(1).isFilter()); // f
    }

    @Test
    void testUrlNotConsumingBrace() {
        tester.assertParsed("AND A (OR url.all:B url.all:C) D E", "A (url.all:B url.all:C) D E", Query.Type.ALL);
    }

    // Really a syntax error on part of the user, but it's part of
    // the logic where balanced braces are allowed in URLs
    @Test
    void testUrlNotConsumingBrace2() {
        tester.assertParsed("AND A (OR url.all:B url.all:C) D E",
                "A (url.all:B url.all:C)D E", Query.Type.ALL);
    }

    @Test
    void testSiteNotConsumingBrace() {
        tester.assertParsed("AND A (OR site:\"B $\" site:\"C $\") D E",
                "A (site:B site:C) D E", Query.Type.ALL);
    }

    @Test
    void testCommaOnlyLeadsToImplicitPhrasing() {
        tester.assertParsed("AND A B C", "A,B,C", Query.Type.ALL);
    }

    @Test
    void testBangDoesNotBindAcrossSpace() {
        tester.assertParsed("word", "word !", Query.Type.ALL);
    }

    @Test
    void testLotsOfPlusMinus() {
        tester.assertParsed("OR word term", "word - + - + - term", Query.Type.ANY);
    }

    @Test
    void testLotsOfMinusPlus() {
        tester.assertParsed("OR word term", "word - + - + term", Query.Type.ANY);
    }

    @Test
    void testMinusDoesNotBindAcrossSpace() {
        tester.assertParsed("OR word term", "word - term", Query.Type.ANY);
    }

    @Test
    void testPlusDoesNotBindAcrossSpace() {
        tester.assertParsed("OR word term", "word + term", Query.Type.ANY);
    }

    @Test
    void testMinusDoesNotBindAcrossSpaceAllQuery() {
        tester.assertParsed("AND word term", "word - term", Query.Type.ALL);
    }

    @Test
    void testPlusDoesNotBindAcrossSpaceAllQuery() {
        tester.assertParsed("AND word term", "word + term", Query.Type.ALL);
    }

    @Test
    void testNoSpaceInIndexPrefix() {
        tester.assertParsed("AND url domain:s url.domain:b",
                "url. domain:s url.domain:b", Query.Type.ALL);
    }

    @Test
    void testNestedParensAndLittleElse() {
        tester.assertParsed("OR a b c d", "((a b) (c d))", Query.Type.ALL);
    }

    // This is simply to control it doesn't crash
    @Test
    void testNestedParensAndLittleElseMoreBroken() {
        tester.assertParsed("AND (OR a b) (OR c d)", "(a b) +(c d))", Query.Type.ALL);
    }

    @Test
    void testNestedUnbalancedParensAndLittleElseMoreBroken() {
        tester.assertParsed("OR a b c d", "((a b) +(c d)", Query.Type.ALL);
    }

    @Test
    void testUnbalancedParens() {
        tester.assertParsed("AND a b (OR c d)", "a b) +(c d))", Query.Type.ALL);
    }

    @Test
    void testUnbalancedStartingParens() {
        tester.assertParsed("OR a b c d", "((a b) +(c d", Query.Type.ALL);
    }

    @Test
    void testJPMobileExceptionQuery() {
        tester.assertParsed("OR concat and (AND make string) 1 47 or",
                "(concat \"and\" (make-string 1 47) \"or\")", Query.Type.ALL);
    }

    @Test
    void testColonDoesNotBindAcrossSpace() {
        tester.assertParsed("b", "a: b", Query.Type.ALL);
        tester.assertParsed("AND a b", "a : b", Query.Type.ALL);
        tester.assertParsed("AND a b", "a :b", Query.Type.ALL);
        tester.assertParsed("AND a b", "a.:b", Query.Type.ALL);
        tester.assertParsed("a:b", "a:b", Query.Type.ALL);
    }

    @Test
    void testGermanUriDecompounding() {
        tester.assertParsed("url.all:\"kommunikationsfehler de\"",
                "url.all:kommunikationsfehler.de", "", Query.Type.ALL, Language.GERMAN);
    }

    // Check the parser doesn't fail on these horrible query strings
    @Test
    void testTicket443882() {
        tester.assertParsed(
                "AND australian LOTTERY (+(OR language:en IN AFFILIATION WITH THE UK NATIONAL LOTTERY date:>1125475200) -newstype:rssexclude)",
                "australian LOTTERY (IN AFFILIATION WITH THE UK NATIONAL LOTTERY -newstype:rssexclude date:>1125475200 (language:en )",
                Query.Type.ALL);
        tester.assertParsed(
                "AND AND consulting (+(OR language:en albuquerque \"new mexico\" date:>1125475200) -newstype:rssexclude)",
                ") AND (consulting) ((albuquerque \"new mexico\" ) -newstype:rssexclude date:>1125475200 (language:en )",
                Query.Type.ALL);
        tester.assertParsed(
                "AND the church of Jesus Christ of latter Day Saints (+(OR language:en Mormon temples date:>1125475200) -newstype:rssexclude)",
                "the church of Jesus Christ of latter Day Saints (Mormon temples -newstype:rssexclude date:>1125475200 (language:en )",
                Query.Type.ALL);
    }

    @Test
    void testParensInQuotes() {
        tester.assertParsed("AND ringtone (OR a:\"Delivery SMAF large max 150kB 063\" a:\"RealMusic Delivery\")",
                "ringtone AND (a:\"Delivery SMAF large max.150kB (063)\" OR a:\"RealMusic Delivery\" )",
                Query.Type.ADVANCED);
        tester.assertParsed("AND ringtone AND (OR a:\"Delivery SMAF large max 150kB 063\" OR a:\"RealMusic Delivery\")",
                "ringtone AND (a:\"Delivery SMAF large max.150kB (063)\" OR a:\"RealMusic Delivery\" )",
                Query.Type.ALL);
        // The last one here is a little weird, but it's not a problem, so let it pass for now...
        tester.assertParsed("OR (OR ringtone AND) (OR a:\"Delivery SMAF large max 150kB 063\" OR a:\"RealMusic Delivery\")",
                "ringtone AND (a:\"Delivery SMAF large max.150kB (063)\" OR a:\"RealMusic Delivery\" )",
                Query.Type.ANY);
    }

    @Test
    void testMixedCaseIndexNames() {
        tester.assertParsed("AND mixedCase:a mixedCase:b notAnIndex c mixedCase:d",
                "mixedcase:a MIXEDCASE:b notAnIndex:c mixedCase:d",
                Query.Type.ALL);
    }

    /** CJK special tokens should be recognized also on non-boundaries */
    @Test
    void testChineseSpecialTokens() {
        tester.assertParsed("AND cat tcp/ip zu foo dotnet bar dotnet dotnet c# c++ bar dotnet dotnet wiz",
                "cattcp/ipzu foo.netbar.net.netC#c++bar.net.netwiz", "", Query.Type.ALL, Language.CHINESE_SIMPLIFIED);
    }

    /**
     * If a cjk special token replace is multi-segment, that token should perhaps be segmented
     * but right now it is not
     */
    @Test
    void testChineseSpecialTokensWithMultiSegmentReplace() {
        // special-token-fs is a special token, to be replaced by firstsecond, first and second are segments in test
        tester.assertParsed("AND tcp/ip firstsecond dotnet firstsecond (SAND first second)", "tcp/ipspecial-token-fs.net special-token-fs firstsecond",
                "", Query.Type.ALL, Language.CHINESE_SIMPLIFIED, TestLinguistics.INSTANCE);
    }

    @Test
    void testSpaceAndTermWeights() {
        tester.assertParsed("AND yahoo!360 yahoo 360 yahoo!150 360 yahoo 360 yahoo yahoo!150 yahoo!200",
                "yahoo!360 yahoo !360 yahoo! 360 yahoo ! 360 yahoo !!! yahoo! ! yahoo!!", Query.Type.ALL);
    }

    @Test
    void testNumbersAndNot() {
        tester.assertParsed("AND a -12", "a -12", Query.Type.ALL);
    }

    @Test
    void testNumbersAndDoubleNot() {
        tester.assertParsed("+a --12", "a --12", Query.Type.ALL);
    }

    @Test
    void testNegativeNumberWithIndex() {
        tester.assertParsed("normal:-12", "normal:-12", Query.Type.ALL);
    }

    @Test
    void testNegativeTermPositiveNumberWithIndex() {
        tester.assertParsed("+a -normal:12", "a -normal:12", Query.Type.ALL);
    }

    @Test
    void testNegativeTermNegativeNumberWithIndex() {
        tester.assertParsed("+a -normal:-12", "a -normal:-12", Query.Type.ALL);
    }

    @Test
    void testNegativeTermPositiveNumberInParentheses() {
        tester.assertParsed("+a -12", "a -(12)", Query.Type.ALL);
        tester.assertParsed("+a -(AND 12 15)", "a -(12 15)", Query.Type.ALL);
        tester.assertParsed("+a -12 -15", "a -(12) -(15)", Query.Type.ALL);
    }

    @Test
    void testSingleNegativeNumberLikeTerm() {
        tester.assertParsed("-12", "-12", Query.Type.ALL);
    }

    @Test
    void testNegativeLessThan() {
        tester.assertParsed("normal:<-3", "normal:<-3", Query.Type.ALL);
        tester.assertParsed("<-3", "<-3", Query.Type.ALL);
    }

    @Test
    void testNegativeBiggerThan() {
        tester.assertParsed("normal:>-3", "normal:>-3", Query.Type.ALL);
        tester.assertParsed(">-3", ">-3", Query.Type.ALL);
    }

    @Test
    void testNegativesInRanges() {
        tester.assertParsed("normal:[-4;9]", "normal:[-4;9]", Query.Type.ALL);
        tester.assertParsed("[-4;9]", "[-4;9]", Query.Type.ALL);
        tester.assertParsed("normal:[-4;-9]", "normal:[-4;-9]", Query.Type.ALL);
        tester.assertParsed("[-4;-9]", "[-4;-9]", Query.Type.ALL);
    }

    @Test
    void testDecimal() {
        Item root = tester.assertParsed("2.2", "2.2", Query.Type.ALL);
        assertTrue(root instanceof IntItem);
        tester.assertParsed("normal:2.2", "normal:2.2", Query.Type.ALL);
    }

    @Test
    void testVersionNumbers() {
        tester.assertParsed("AND 1 0 9", "1.0.9", Query.Type.ALL);
    }

    @Test
    void testDecimalNumbersAndNot() {
        tester.assertParsed("AND a -12.2", "a -12.2", Query.Type.ALL);
    }

    @Test
    void testDecimalNumbersAndDoubleNot() {
        tester.assertParsed("+a --12.2", "a --12.2", Query.Type.ALL);
    }

    @Test
    void testNegativeDecimalNumberWithIndex() {
        tester.assertParsed("normal:-12.2", "normal:-12.2", Query.Type.ALL);
    }

    @Test
    void testSingleNegativeDecimalNumberLikeTerm() {
        tester.assertParsed("-12.2", "-12.2", Query.Type.ALL);
    }

    @Test
    void testNegativeDecimalLessThan() {
        tester.assertParsed("normal:<-3.14", "normal:<-3.14", Query.Type.ALL);
        tester.assertParsed("<-3.14", "<-3.14", Query.Type.ALL);
    }

    @Test
    void testNegativeDecimalBiggerThan() {
        tester.assertParsed("normal:>-3.14", "normal:>-3.14", Query.Type.ALL);
        tester.assertParsed(">-3.14", ">-3.14", Query.Type.ALL);
    }

    @Test
    void testNegativesDecimalInRanges() {
        tester.assertParsed("normal:[-4.16;9.2]", "normal:[-4.16;9.2]", Query.Type.ALL);
        tester.assertParsed("[-4.16;9.2]", "[-4.16;9.2]", Query.Type.ALL);
        tester.assertParsed("normal:[-4.16;-9.2]", "normal:[-4.16;-9.2]", Query.Type.ALL);
        tester.assertParsed("[-4.16;-9.2]", "[-4.16;-9.2]", Query.Type.ALL);
    }

    @Test
    void testRangesAndNoise() {
        tester.assertParsed("[2;3]", "[2;3]]", Query.Type.ALL);
    }

    @Test
    void testIndexNoise() {
        tester.assertParsed("AND normal:a notanindex", "normal:a normal: notanindex:", Query.Type.ALL);
        tester.assertParsed(null, "normal:", Query.Type.ALL);
        tester.assertParsed(null, "normal:!", Query.Type.ALL);
        tester.assertParsed(null, "normal::", Query.Type.ALL);
        tester.assertParsed(null, "normal:_", Query.Type.ALL);
        tester.assertParsed(null, "normal:", Query.Type.ANY);
        tester.assertParsed(null, "normal:", Query.Type.ADVANCED);
    }

    @Test
    void testIndexNoiseAndExplicitPhrases() {
        tester.assertParsed("normal:\"a b\"", "normal:\" a b\"", Query.Type.ALL);
        tester.assertParsed("normal:\"a b\"", "normal:\"a b\"", Query.Type.ALL);
    }

    @Test
    void testExactMatchParsing1() {
        SearchDefinition sd = new SearchDefinition("testsd");

        Index index1 = new Index("testexact1");
        index1.setExact(true, null);
        sd.addIndex(index1);

        Index index2 = new Index("testexact2");
        index2.setExact(true, "()/aa*::*&");
        sd.addIndex(index2);

        IndexFacts indexFacts = new IndexFacts(new IndexModel(sd));
        ParsingTester customTester = new ParsingTester(indexFacts);

        customTester.assertParsed("testexact1:/,%&#", "testexact1:/,%&#", Query.Type.ALL);
        customTester.assertParsed("testexact2:/,%&#!!", "testexact2:/,%&#!!()/aa*::*&", Query.Type.ALL);
        customTester.assertParsed("AND word1 (OR testexact1:word2 testexact1:word3)", "word1 AND (testexact1:word2 OR testexact1:word3 )", Query.Type.ADVANCED);
        customTester.assertParsed("AND word (OR testexact1:AND testexact1:OR)", "word AND (testexact1: AND OR testexact1: OR )", Query.Type.ADVANCED);
    }

    /** Testing terminators containing control characters in conjunction with those control characters */
    @Test
    void testExactMatchParsing2() {
        SearchDefinition sd = new SearchDefinition("testsd");

        Index index1 = new Index("testexact1");
        index1.setExact(true, "*!*");
        sd.addIndex(index1);

        IndexFacts indexFacts = new IndexFacts(new IndexModel(sd));
        ParsingTester customTester = new ParsingTester(indexFacts);

        customTester.assertParsed("testexact1:_-_*!200", "testexact1:_-_*!**!!", Query.Type.ALL);
    }

    /** Testing terminators containing control characters in conjunction with those control characters */
    @Test
    void testExactMatchParsing3() {
        SearchDefinition sd = new SearchDefinition("testsd");

        Index index1 = new Index("testexact1");
        index1.setExact(true, "*");
        sd.addIndex(index1);

        IndexFacts indexFacts = new IndexFacts(new IndexModel(sd));
        ParsingTester customTester = new ParsingTester(indexFacts);

        customTester.assertParsed("testexact1:_-_*!200", "testexact1:_-_**!!", Query.Type.ALL);
    }

    // bug 1393139
    @Test
    void testMinimalBritneyFilter() {
        tester.assertParsed("RANK (+a -|c) b", "a RANK b", "-c", Query.Type.ADVANCED);
    }

    @Test
    void testBritneyFilter() {
        tester.assertParsed("RANK (+(AND performernameall:britney performernameall:spears songnameall:toxic |SongConsumable:1 |country:us |collapsedrecord:1 |doctype:song) -|collapsecount:0) (AND metadata:britney metadata:spears metadata:toxic)",
                "((performernameall:britney AND performernameall:spears AND songnameall:toxic) RANK (metadata:britney AND metadata:spears AND metadata:toxic))",
                "+SongConsumable:1 +country:us +collapsedrecord:1  -collapsecount:0  +doctype:song",
                Query.Type.ADVANCED);

        tester.assertParsed("AND (+(AND (RANK (OR (AND performernameall:britney performernameall:spears songnameall:toxic) (AND performernameall:britney performernameall:spears songnameall:toxic)) (AND metadata:britney metadata:spears metadata:toxic)) |SongConsumable:1 |country:us |collapsedrecord:1) -|collapsecount:0) |doctype:song",
                "(((performernameall:britney AND performernameall:spears AND songnameall:toxic) OR (performernameall:britney AND performernameall:spears AND songnameall:toxic)) RANK (metadata:britney AND metadata:spears AND metadata:toxic)))",
                "+SongConsumable:1 +country:us +collapsedrecord:1  -collapsecount:0  +doctype:song",
                Query.Type.ADVANCED);
    }

    // bug 1412840
    @Test
    void testGreedyPhrases() {
        tester.assertParsed("AND title:why title:\"1 2\"", "title:\"why\" title:\"&\" title:\"1/2\"", Query.Type.ALL);
    }

    // bug 1509347
    @Test
    void testUnderscoreInFieldNames() {
        tester.assertParsed("AND title:why score_under:what score_under:>5000",
                "title:why score_under:what score_under:>5000",
                Query.Type.ALL);
    }

    // bug 1509347
    @Test
    void testLeadingUnderscoreInFieldNames() {
        tester.assertParsed("AND title:why _under_score_:what _under_score_:>5000",
                "title:why _under_score_:what _under_score_:>5000",
                Query.Type.ALL);
    }

    // Re-add if underscore should be a word character
    // @Test
    // public void testUnderscoreAsWordCharacter() {
    //     tester.assertParsed("AND _a b_ a__b \"_a_b_c _d_e_f\"",
    //          "_a b_ a__b \"_a_b_c _d_e_f\"",
    //          Query.Type.ALL);
    // }

    // Re-add if underscore should be a word character
    // @Test
    // public void testUnderscoreAsWordWithIndexName() {
    //     tester.assertParsed("AND title:_a title:a title:_a_",
    //          "title:_a title:a title:_a_",
    //          Query.Type.ALL);
    // }

    // bug 524918
    @Test
    void testAdvancedSyntaxParensAndQuotes() {
        tester.assertParsed("OR a (AND \"b c d\" e)",
                "a OR (\"b (c) d\" AND e)",
                Query.Type.ADVANCED);
    }

    // This test is here instead of in the query parser because
    // this needs to become series of tests where the tokenizer
    // and parser will step on each other's toes.
    @Test
    void testStarFirstInAttributes() {
        tester.assertParsed("exactindex:*test",
                "exactindex:*test",
                Query.Type.ALL);

    }

    @Test
    void testOneWordWebParsing() {
        tester.assertParsed("a", "a", Query.Type.WEB);
    }

    @Test
    void testTwoWordWebParsing() {
        tester.assertParsed("AND a b", "a b", Query.Type.WEB);
    }

    @Test
    void testPlusWordWebParsing1() {
        Item root = tester.assertParsed("AND a b", "+a b", Query.Type.WEB);
        assertTrue(((AndItem) root).getItem(0).isProtected());
        assertFalse(((AndItem) root).getItem(1).isProtected());
    }

    @Test
    void testPlusWordWebParsing2() {
        Item root = tester.assertParsed("AND a b", "+a +b", Query.Type.WEB);
        assertTrue(((AndItem) root).getItem(0).isProtected());
        assertTrue(((AndItem) root).getItem(1).isProtected());
    }

    @Test
    void testNegativeWordsParsing1() {
        Item root = tester.assertParsed("+a -b", "a -b", Query.Type.WEB);
        assertFalse(((NotItem) root).getItem(0).isProtected());
        assertTrue(((NotItem) root).getItem(1).isProtected());
    }

    @Test
    void testNegativeWordsParsing2() {
        Item root = tester.assertParsed("+a -b", "+a -b", Query.Type.WEB);
        assertTrue(((NotItem) root).getItem(0).isProtected());
        assertTrue(((NotItem) root).getItem(1).isProtected());
    }

    @Test
    void testNegativeWordsParsing3() {
        tester.assertParsed("+a -b", "-b a", Query.Type.WEB);
    }

    @Test
    void testNegativeWordsParsing4() {
        tester.assertParsed("+(AND a b) -c -d", "a b -c -d", Query.Type.WEB);
    }

    @Test
    void testNegativeWordsParsing5() {
        tester.assertParsed("+(AND a \"b c\" d) -e -f", "a -e \"b c\" d -f", Query.Type.WEB);
    }

    @Test
    void testPhraseWebParsing() {
        tester.assertParsed("\"a b\"", "\"a b\"", Query.Type.WEB);
    }

    @Test
    void testPhraseAndExtraTermWebParsing() {
        tester.assertParsed("AND \"a b\" c", "\"a b\" c", Query.Type.WEB);
    }

    @Test
    void testNotOrWebParsing() {
        tester.assertParsed("AND a or b", "a or b", Query.Type.WEB);
    }

    @Test
    void testNotOrALLParsing() {
        tester.assertParsed("AND a OR b", "a OR b", Query.Type.ALL);
    }

    @Test
    void testOrParsing1() {
        tester.assertParsed("OR a b", "a OR b", Query.Type.WEB);
    }

    @Test
    void testOrParsing2() {
        tester.assertParsed("OR a b c", "a OR b OR c", Query.Type.WEB);
    }

    @Test
    void testOrParsing3() {
        tester.assertParsed("OR a (AND b c) \"d e\"", "a OR b c OR \"d e\"", Query.Type.WEB);
    }

    @Test
    void testOrParsing4() {
        tester.assertParsed("OR (AND or1 a) or2", "or1 a OR or2", Query.Type.WEB);
    }

    @Test
    void testOrCornerCase1() {
        tester.assertParsed("AND OR a", "OR a", Query.Type.WEB);
    }

    @Test
    void testOrCornerCase2() {
        tester.assertParsed("AND OR a", "OR a OR", Query.Type.WEB); // Don't care
    }

    @Test
    void testOrCornerCase3() {
        tester.assertParsed("AND OR a", "OR a OR OR", Query.Type.WEB); // Don't care
    }

    @Test
    void testOrCornerCase4() {
        tester.assertParsed("+(OR (AND a b) (AND d c) (AND g h)) -e -f", "a b OR d c -e -f OR g h", Query.Type.WEB);
    }

    @Test
    void testOdd1Web() {
        tester.assertParsed("AND window print error", "+window.print() +error", Query.Type.WEB);
    }

    @Test
    void testNotOnlyWeb() {
        tester.assertParsed(null, "-foobar", Query.Type.WEB);
    }

    @Test
    void testMultipleNotsOnltWeb() {
        tester.assertParsed(null, "-foo -bar -foobar", Query.Type.WEB);
    }

    @Test
    void testOnlyNotCompositeWeb() {
        tester.assertParsed(null, "-(foo bar baz)", Query.Type.WEB);
    }

    @Test
    void testSingleNegativeNumberLikeTermWeb() {
        tester.assertParsed("-12", "-12", Query.Type.WEB);
    }

    @Test
    void testSingleNegativeDecimalNumberLikeTermWeb() {
        tester.assertParsed("-12.2", "-12.2", Query.Type.WEB);
    }

    @Test
    void testDefaultWebIndices() {
        tester.assertParsed("AND notanindex b", "notanindex:b", Query.Type.WEB);
        tester.assertParsed("site:\"b $\"", "site:b", Query.Type.WEB);
        tester.assertParsed("hostname:b", "hostname:b", Query.Type.WEB);
        tester.assertParsed("link:b", "link:b", Query.Type.WEB);
        tester.assertParsed("url:b", "url:b", Query.Type.WEB);
        tester.assertParsed("inurl:b", "inurl:b", Query.Type.WEB);
        tester.assertParsed("intitle:b", "intitle:b", Query.Type.WEB);
    }

    @Test
    void testHTMLWeb() {
        tester.assertParsed("AND h2 Title h2", "<h2>Title</h2>", Query.Type.WEB);
    }

    /**
     * Shortcut terms are represented as any other terms, but can be rewritten downstream.
     * The information about added bangs is available from the origin as shown (do not use the weight to find this)
     */
    @Test
    void testShortcutsWeb() {
        tester.assertParsed("AND map new york", "map new york", Query.Type.WEB);

        AndItem root = (AndItem) tester.assertParsed("AND map!150 new york", "map! new york", Query.Type.WEB);
        assertEquals('!', ((WordItem) root.getItem(0)).getOrigin().charAfter(0));

        root = (AndItem) tester.assertParsed("AND barack obama news!150", "barack obama news!", Query.Type.WEB);
        assertEquals('!', ((WordItem) root.getItem(2)).getOrigin().charAfter(0));
    }

    @Test
    void testZipCodeShortcutWeb() {
        tester.assertParsed("12345", "12345", Query.Type.WEB);
        IntItem root = (IntItem) tester.assertParsed("00012!150", "00012!", Query.Type.WEB);
        assertEquals('!', root.getOrigin().charAfter(0));
    }

    @Test
    void testDouble() {
        Item number = tester.assertParsed("123456789.987654321", "123456789.987654321", Query.Type.ALL);
        assertTrue(number instanceof IntItem);
    }

    @Test
    void testLong() {
        Item number = tester.assertParsed("3000000000000", "3000000000000", Query.Type.ALL);
        assertTrue(number instanceof IntItem);
    }

    @Test
    void testNear1() {
        tester.assertParsed("NEAR(2) new york", "new NEAR york", Query.Type.ADVANCED);
    }

    @Test
    void testNear2() {
        tester.assertParsed("ONEAR(2) new york", "new ONEAR york", Query.Type.ADVANCED);
    }

    @Test
    void testNear3() {
        tester.assertParsed("NEAR(3) new york", "new NEAR(3) york", Query.Type.ADVANCED);
    }

    @Test
    void testNear4() {
        tester.assertParsed("ONEAR(3) new york", "new ONEAR(3) york", Query.Type.ADVANCED);
    }

    @Test
    void testNear5() {
        tester.assertParsed("NEAR(3) map new york", "map NEAR(3) new NEAR(3) york", Query.Type.ADVANCED);
    }

    @Test
    void testNear6() {
        tester.assertParsed("ONEAR(3) map new york", "map ONEAR(3) new ONEAR(3) york", Query.Type.ADVANCED);
    }

    @Test
    void testNear7() {
        tester.assertParsed("NEAR(4) (NEAR(3) map new) york", "map NEAR(3) new NEAR(4) york", Query.Type.ADVANCED);
    }

    @Test
    void testNear8() {
        tester.assertParsed("ONEAR(4) (ONEAR(3) map new) york", "map ONEAR(3) new ONEAR(4) york", Query.Type.ADVANCED);
    }

    @Test
    void testNearPrefix() {
        tester.assertParsed("NEAR(2) a b*", "a NEAR b*", Query.Type.ADVANCED);
    }

    @Test
    void testNestedBracesAndPhrases() {
        String userQuery = "(\"Secondary Curriculum\" (\"Key Stage 3\" OR KS3) (\"Key Stage 4\" OR KS4)) ";
        tester.assertParsed(
                "OR \"Secondary Curriculum\" \"Key Stage 3\" OR KS3 \"Key Stage 4\" OR KS4",
                userQuery, Query.Type.ALL);
        userQuery = "(\"Grande distribution\" (\"developpement durable\" OR \"commerce equitable\"))";
        tester.assertParsed(
                "OR \"Grande distribution\" \"developpement durable\" OR \"commerce equitable\"",
                userQuery, Query.Type.ALL);
        userQuery = "(\"road tunnel\" (\"tunnel management\" OR AID OR \"traffic systems\" OR supervision OR "
                + "\"decision aid system\") (Spie OR Telegra OR Telvent OR Steria)) ";
        tester.assertParsed(
                "OR \"road tunnel\" \"tunnel management\" OR AID OR \"traffic systems\" OR supervision OR \"decision aid system\" Spie OR Telegra OR Telvent OR Steria",
                userQuery, Query.Type.ALL);
    }

    @Test
    void testYetAnotherCycleQuery() {
        tester.assertParsed("+(OR (+d -f) b) -c",
                "( b -c  ( d -f )",
                Query.Type.ALL);
    }

    @Test
    void testSimpleEquivAdvanced() {
        tester.assertParsed("EQUIV foo bar baz", "foo equiv bar equiv baz", Query.Type.ADVANCED);
    }

    @Test
    void testEquivWordIntPhraseAdvanced() {
        tester.assertParsed("EQUIV foo 5 \"a b\"", "foo equiv 5 equiv \"a b\"", Query.Type.ADVANCED);
    }

    @Test
    void testEquivRejectCompositeAdvanced() {
        try {
            tester.assertParsed("this should not parse", "foo equiv (a or b)", Query.Type.ADVANCED);
        } catch (Exception e) {
            // Success
        }
    }

    @Test
    void testSimpleWandAdvanced() {
        tester.assertParsed("WEAKAND(100) foo bar baz", "foo wand bar wand baz", Query.Type.ADVANCED);
    }

    @Test
    void testSimpleWandAdvancedWithNonDefaultN() {
        tester.assertParsed("WEAKAND(32) foo bar baz", "foo weakand(32) bar weakand(32) baz", Query.Type.ADVANCED);
    }

    @Test
    void testSimpleWandAdvancedWithNonDefaultNAndWeights() {
        tester.assertParsed("WEAKAND(32) foo!32 bar!64 baz", "foo!32 weakand(32) bar!64 weakand(32) baz", Query.Type.ADVANCED);
    }

    @Test
    void testTwoRanges() {
        tester.assertParsed("AND score:[1.25;2.18] age:[25;30]", "score:[1.25;2.18] AND age:[25;30]", Query.Type.ADVANCED);
    }

    @Test
    void testTooLargeTermWeights() {
        // This behavior is a bit silly:
        tester.assertParsed("AND a 12345678901234567890", "a!12345678901234567890", Query.Type.ALL);
        // but in light of
        tester.assertParsed("AND a!150 b", "a!b", Query.Type.ALL);
        // which was the behavior when adding handling of too large term
        // weights, it is at least consistent. It should probably be implicit
        // phrases instead.
    }

    @Test
    void testAndSegmenting() {
        Item root = tester.parseQuery("a'b&c'd", Language.ENGLISH, Query.Type.ALL);
        assertTrue(root instanceof AndItem);
        AndItem top = (AndItem) root;
        assertTrue(top.getItem(0) instanceof AndSegmentItem);
        assertTrue(top.getItem(1) instanceof AndSegmentItem);
        AndSegmentItem seg1 = (AndSegmentItem) top.getItem(0);
        AndSegmentItem seg2 = (AndSegmentItem) top.getItem(1);
        Item t1 = seg1.getItem(0);
        Item t2 = seg1.getItem(1);
        Item t3 = seg2.getItem(0);
        Item t4 = seg2.getItem(1);
        assertTrue(((TaggableItem) t2).hasUniqueID());
        assertTrue(((TaggableItem) t3).hasUniqueID());
        assertEquals(((TaggableItem) t1).getConnectedItem(), t2);
        assertEquals(((TaggableItem) t2).getConnectedItem(), t3);
        assertEquals(((TaggableItem) t3).getConnectedItem(), t4);
    }

    @Test
    void testSiteAndSegmentPhrases() {
        tester.assertParsed("host.all:\"www abc com x y-z $\"",
                "host.all:www.abc.com/x'y-z", "",
                Query.Type.ALL, Language.ENGLISH);
    }

    @Test
    void testSiteAndSegmentPhrasesFollowedByText() {
        tester.assertParsed("AND host.all:\"www abc com x y-z $\" (SAND a b)",
                "host.all:www.abc.com/x'y-z a'b", "",
                Query.Type.ALL, Language.ENGLISH);
    }

    @Test
    void testIntItemFollowedByDot() {
        tester.assertParsed("AND Campion Ste 3 When To Her Lute Corinna Sings", "Campion Ste: 3. When To Her Lute Corinna Sings", Query.Type.ALL);
    }

    @Test
    void testNotIntItemIfPrecededByHyphen() {
        tester.assertParsed("AND Horny Horny '98 Radio Edit", "Horny [Horny '98 Radio Edit]]", Query.Type.ALL);
    }

    @Test
    void testNonAsciiNumber() {
        tester.assertParsed("AND title:199 title:119 title:201 title:149", "title:199.119.201.149", Query.Type.ALL);
    }

    @Test
    void testNoGrammar1() {
        tester.assertParsed("WEAKAND(100) foobar", "foobar", Query.Type.TOKENIZE);
    }

    @Test
    void testNoGrammar2() {
        tester.assertParsed("WEAKAND(100) foobar", "-foobar", Query.Type.TOKENIZE);
    }

    @Test
    void testNoGrammar3() {
        tester.assertParsed("WEAKAND(100) foo bar", "foo -bar", Query.Type.TOKENIZE);
    }

    @Test
    void testNoGrammar4() {
        tester.assertParsed("WEAKAND(100) foo bar baz one two 37", "foo -(bar baz \"one two\" 37)", Query.Type.TOKENIZE);
    }

    @Test
    void testEmojis() {
        String emoji1 = "\uD83D\uDD2A"; // 🔪
        String emoji2 = "\uD83D\uDE00"; // 😀

        tester.assertParsed(emoji1, emoji1, Query.Type.ANY);
        tester.assertParsed(emoji2, emoji2, Query.Type.ANY);
        tester.assertParsed("AND " + emoji1 + " " + emoji2, emoji1 + emoji2, Query.Type.ANY);
        tester.assertParsed("AND " + emoji1 + " foo " + emoji2, emoji1 + "foo" + emoji2, Query.Type.ANY);
    }

}