aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/yql/VespaSerializer.java
blob: a354006aa9bf7cc80d83ddc16975639eaddb652e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.yql;

import static com.yahoo.search.yql.YqlParser.ACCENT_DROP;
import static com.yahoo.search.yql.YqlParser.ALTERNATIVES;
import static com.yahoo.search.yql.YqlParser.AND_SEGMENTING;
import static com.yahoo.search.yql.YqlParser.BOUNDS;
import static com.yahoo.search.yql.YqlParser.BOUNDS_LEFT_OPEN;
import static com.yahoo.search.yql.YqlParser.BOUNDS_OPEN;
import static com.yahoo.search.yql.YqlParser.BOUNDS_RIGHT_OPEN;
import static com.yahoo.search.yql.YqlParser.CONNECTION_ID;
import static com.yahoo.search.yql.YqlParser.CONNECTION_WEIGHT;
import static com.yahoo.search.yql.YqlParser.CONNECTIVITY;
import static com.yahoo.search.yql.YqlParser.DISTANCE;
import static com.yahoo.search.yql.YqlParser.DOT_PRODUCT;
import static com.yahoo.search.yql.YqlParser.END_ANCHOR;
import static com.yahoo.search.yql.YqlParser.EQUIV;
import static com.yahoo.search.yql.YqlParser.FILTER;
import static com.yahoo.search.yql.YqlParser.FUZZY;
import static com.yahoo.search.yql.YqlParser.GEO_LOCATION;
import static com.yahoo.search.yql.YqlParser.HIT_LIMIT;
import static com.yahoo.search.yql.YqlParser.IMPLICIT_TRANSFORMS;
import static com.yahoo.search.yql.YqlParser.LABEL;
import static com.yahoo.search.yql.YqlParser.MAX_EDIT_DISTANCE;
import static com.yahoo.search.yql.YqlParser.NEAR;
import static com.yahoo.search.yql.YqlParser.NEAREST_NEIGHBOR;
import static com.yahoo.search.yql.YqlParser.NORMALIZE_CASE;
import static com.yahoo.search.yql.YqlParser.ONEAR;
import static com.yahoo.search.yql.YqlParser.ORIGIN;
import static com.yahoo.search.yql.YqlParser.ORIGIN_LENGTH;
import static com.yahoo.search.yql.YqlParser.ORIGIN_OFFSET;
import static com.yahoo.search.yql.YqlParser.ORIGIN_ORIGINAL;
import static com.yahoo.search.yql.YqlParser.PHRASE;
import static com.yahoo.search.yql.YqlParser.PREFIX;
import static com.yahoo.search.yql.YqlParser.PREFIX_LENGTH;
import static com.yahoo.search.yql.YqlParser.RANGE;
import static com.yahoo.search.yql.YqlParser.RANK;
import static com.yahoo.search.yql.YqlParser.RANKED;
import static com.yahoo.search.yql.YqlParser.SAME_ELEMENT;
import static com.yahoo.search.yql.YqlParser.SCORE_THRESHOLD;
import static com.yahoo.search.yql.YqlParser.SIGNIFICANCE;
import static com.yahoo.search.yql.YqlParser.START_ANCHOR;
import static com.yahoo.search.yql.YqlParser.STEM;
import static com.yahoo.search.yql.YqlParser.SUBSTRING;
import static com.yahoo.search.yql.YqlParser.SUFFIX;
import static com.yahoo.search.yql.YqlParser.TARGET_NUM_HITS;
import static com.yahoo.search.yql.YqlParser.THRESHOLD_BOOST_FACTOR;
import static com.yahoo.search.yql.YqlParser.UNIQUE_ID;
import static com.yahoo.search.yql.YqlParser.URI;
import static com.yahoo.search.yql.YqlParser.USE_POSITION_DATA;
import static com.yahoo.search.yql.YqlParser.WAND;
import static com.yahoo.search.yql.YqlParser.WEAK_AND;
import static com.yahoo.search.yql.YqlParser.WEIGHT;
import static com.yahoo.search.yql.YqlParser.WEIGHTED_SET;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.collect.ImmutableMap;
import com.yahoo.prelude.query.AndItem;
import com.yahoo.prelude.query.AndSegmentItem;
import com.yahoo.prelude.query.BoolItem;
import com.yahoo.prelude.query.DotProductItem;
import com.yahoo.prelude.query.EquivItem;
import com.yahoo.prelude.query.FalseItem;
import com.yahoo.prelude.query.FuzzyItem;
import com.yahoo.prelude.query.ExactStringItem;
import com.yahoo.prelude.query.IndexedItem;
import com.yahoo.prelude.query.IntItem;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.GeoLocationItem;
import com.yahoo.prelude.query.MarkerWordItem;
import com.yahoo.prelude.query.NearItem;
import com.yahoo.prelude.query.NearestNeighborItem;
import com.yahoo.prelude.query.NotItem;
import com.yahoo.prelude.query.NullItem;
import com.yahoo.prelude.query.NumericInItem;
import com.yahoo.prelude.query.ONearItem;
import com.yahoo.prelude.query.OrItem;
import com.yahoo.prelude.query.PhraseItem;
import com.yahoo.prelude.query.PhraseSegmentItem;
import com.yahoo.prelude.query.PredicateQueryItem;
import com.yahoo.prelude.query.PrefixItem;
import com.yahoo.prelude.query.RangeItem;
import com.yahoo.prelude.query.RankItem;
import com.yahoo.prelude.query.RegExpItem;
import com.yahoo.prelude.query.SameElementItem;
import com.yahoo.prelude.query.SegmentingRule;
import com.yahoo.prelude.query.StringInItem;
import com.yahoo.prelude.query.Substring;
import com.yahoo.prelude.query.SubstringItem;
import com.yahoo.prelude.query.SuffixItem;
import com.yahoo.prelude.query.TaggableItem;
import com.yahoo.prelude.query.ToolBox;
import com.yahoo.prelude.query.ToolBox.QueryVisitor;
import com.yahoo.prelude.query.TrueItem;
import com.yahoo.prelude.query.UriItem;
import com.yahoo.prelude.query.WandItem;
import com.yahoo.prelude.query.WeakAndItem;
import com.yahoo.prelude.query.WeightedSetItem;
import com.yahoo.prelude.query.WordAlternativesItem;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.search.Query;
import com.yahoo.search.grouping.Continuation;
import com.yahoo.search.grouping.GroupingRequest;
import com.yahoo.search.query.QueryTree;

/**
 * Serialize Vespa query trees to YQL+ strings.
 *
 * @author Steinar Knutsen
 */
public class VespaSerializer {

    // TODO: Refactor, too much copy/paste
    private static abstract class Serializer<ITEM extends Item> {

        abstract void onExit(StringBuilder destination, ITEM item);

        String separator(Deque<SerializerWrapper> state) {
            throw new UnsupportedOperationException("Having several items for this query operator serializer, "
                                                    + this.getClass().getSimpleName() + ", not yet implemented.");
        }

        abstract boolean serialize(StringBuilder destination, ITEM item);

    }

    private static class AndSegmentSerializer extends Serializer<AndSegmentItem> {

        private static void serializeWords(StringBuilder destination, AndSegmentItem segment) {
            for (int i = 0; i < segment.getItemCount(); ++i) {
                if (i > 0) {
                    destination.append(", ");
                }
                Item current = segment.getItem(i);
                if (current instanceof WordItem) {
                    destination.append('"');
                    escape(((WordItem) current).getIndexedString(), destination).append('"');
                } else {
                    throw new IllegalArgumentException("Serializing of " + current.getClass().getSimpleName()
                                                       + " in segment AND expressions not implemented, please report this as a bug.");
                }
            }
        }

        @Override
        void onExit(StringBuilder destination, AndSegmentItem item) { }

        @Override
        boolean serialize(StringBuilder destination, AndSegmentItem item) {
            return serialize(destination, item, true);
        }

        static boolean serialize(StringBuilder destination, AndSegmentItem item, boolean includeField) {
            Substring origin = item.getOrigin();
            String image;
            int offset;
            int length;

            if (origin == null) {
                image = item.getRawWord();
                offset = 0;
                length = image.length();
            } else {
                image = origin.getSuperstring();
                offset = origin.start;
                length = origin.end - origin.start;
            }

            if (includeField) {
                destination.append(normalizeIndexName(item.getIndexName())).append(" contains ");
            }
            destination.append("({");
            serializeOrigin(destination, image, offset, length);
            destination.append(", ").append(AND_SEGMENTING).append(": true");
            destination.append("}");
            destination.append(PHRASE).append('(');
            serializeWords(destination, item);
            destination.append("))");
            return false;
        }
    }

    private static class AndSerializer extends Serializer<AndItem> {

        @Override
        void onExit(StringBuilder destination, AndItem item) {
            destination.append(')');
        }

        @Override
        String separator(Deque<SerializerWrapper> state) {
            return " AND ";
        }

        @Override
        boolean serialize(StringBuilder destination, AndItem item) {
            destination.append("(");
            return true;
        }
    }

    private static class DotProductSerializer extends Serializer<WeightedSetItem> {

        @Override
        void onExit(StringBuilder destination, WeightedSetItem item) { }

        @Override
        boolean serialize(StringBuilder destination, WeightedSetItem item) {
            serializeWeightedSetContents(destination, DOT_PRODUCT, item);
            return false;
        }

    }

    private static class EquivSerializer extends Serializer<EquivItem> {

        @Override
        void onExit(StringBuilder destination, EquivItem item) { }

        @Override
        boolean serialize(StringBuilder destination, EquivItem item) {
            String annotations = leafAnnotations(item);
            destination.append(getIndexName(item.getItem(0))).append(" contains ");
            if (annotations.length() > 0) {
                destination.append("({").append(annotations).append("}");
            }
            destination.append(EQUIV).append('(');
            int initLen = destination.length();
            for (Iterator<Item> i = item.getItemIterator(); i.hasNext();) {
                Item x = i.next();
                if (destination.length() > initLen) {
                    destination.append(", ");
                }
                if (x instanceof PhraseItem) {
                    PhraseSerializer.serialize(destination, (PhraseItem)x, false);
                } else {
                    destination.append('"');
                    escape(((IndexedItem) x).getIndexedString(), destination);
                    destination.append('"');
                }
            }
            if (annotations.length() > 0) {
                destination.append(')');
            }
            destination.append(')');
            return false;
        }

    }

    private static class NearSerializer extends Serializer<NearItem> {

        @Override
        void onExit(StringBuilder destination, NearItem item) { }

        @Override
        boolean serialize(StringBuilder destination, NearItem item) {
            String annotations = nearAnnotations(item);

            destination.append(getIndexName(item.getItem(0))).append(" contains ");
            if (annotations.length() > 0) {
                destination.append('(').append(annotations);
            }
            destination.append(NEAR).append('(');
            int initLen = destination.length();
            for (ListIterator<Item> i = item.getItemIterator(); i.hasNext();) {
                WordItem close = (WordItem) i.next();
                if (destination.length() > initLen) {
                    destination.append(", ");
                }
                destination.append('"');
                escape(close.getIndexedString(), destination).append('"');
            }
            destination.append(')');
            if (annotations.length() > 0) {
                destination.append(')');
            }
            return false;
        }

        static String nearAnnotations(NearItem n) {
            if (n.getDistance() != NearItem.defaultDistance) {
                return "{" + DISTANCE + ": " + n.getDistance() + "}";
            } else {
                return "";
            }
        }

    }

    private static class UriSerializer extends Serializer<UriItem> {

        @Override
        void onExit(StringBuilder destination, UriItem item) { }

        @Override
        boolean serialize(StringBuilder destination, UriItem uriItem) {
            String annotations = uriAnnotations(uriItem);

            destination.append(uriItem.getIndexName()).append(" contains ");
            if (annotations.length() > 0)
                destination.append('(').append(annotations);
            destination.append(URI).append("(\"");
            destination.append(uriItem.getArgumentString());
            destination.append("\")");
            if (annotations.length() > 0)
                destination.append(')');
            return false;
        }

        static String uriAnnotations(UriItem item) {
            if (item.hasStartAnchor() == item.isStartAnchorDefault() &&
                item.hasEndAnchor() == item.isEndAnchorDefault())
                return "";

            StringBuilder b = new StringBuilder();
            b.append("{");
            if (item.hasStartAnchor() != item.isStartAnchorDefault()) {
                b.append(START_ANCHOR + ": " + item.hasStartAnchor());
            }
            if (item.hasEndAnchor() != item.isEndAnchorDefault()) {
                if (b.length() > 2)
                    b.append(", ");
                b.append(END_ANCHOR + ": " + item.hasEndAnchor());
            }
            b.append("}");
            return b.toString();
        }

    }

    private static class NotSerializer extends Serializer<NotItem> {

        @Override
        void onExit(StringBuilder destination, NotItem item) {
            destination.append(')');
        }

        @Override
        String separator(Deque<SerializerWrapper> state) {
            if (state.peekFirst().subItems == 1) {
                return ") AND !(";
            } else {
                return " OR ";
            }
        }

        @Override
        boolean serialize(StringBuilder destination, NotItem item) {
            destination.append("(");
            return true;
        }
    }

    private static class NullSerializer extends Serializer<NullItem> {

        @Override
        void onExit(StringBuilder destination, NullItem item) { }

        @Override
        boolean serialize(StringBuilder destination, NullItem item) {
            throw new NullItemException("NullItem encountered in query tree. This is usually a symptom of an invalid " +
                                        "query or an error in a query transformer.");
        }

    }

    private static class NumberSerializer extends Serializer<IntItem> {

        @Override
        void onExit(StringBuilder destination, IntItem item) { }

        @Override
        boolean serialize(StringBuilder destination, IntItem intItem) {
            if (intItem.getFromLimit().number().equals(intItem.getToLimit().number())) {
                destination.append(normalizeIndexName(intItem.getIndexName())).append(" = ");
                annotatedNumberImage(intItem, intItem.getFromLimit().number().toString(), destination);
            } else if (intItem.getFromLimit().isInfinite()) {
                destination.append(normalizeIndexName(intItem.getIndexName()));
                destination.append(intItem.getToLimit().isInclusive() ? " <= " : " < ");
                annotatedNumberImage(intItem, intItem.getToLimit().number().toString(), destination);
            } else if (intItem.getToLimit().isInfinite()) {
                destination.append(normalizeIndexName(intItem.getIndexName()));
                destination.append(intItem.getFromLimit().isInclusive() ? " >= " : " > ");
                annotatedNumberImage(intItem, intItem.getFromLimit().number().toString(), destination);
            } else {
                serializeAsRange(destination, intItem);
            }
            return false;
        }

        private void serializeAsRange(StringBuilder destination, IntItem intItem) {
            String annotations = leafAnnotations(intItem);
            boolean leftOpen = !intItem.getFromLimit().isInclusive();
            boolean rightOpen = !intItem.getToLimit().isInclusive();
            String boundsAnnotation = "";
            int initLen;

            if (leftOpen && rightOpen) {
                boundsAnnotation = BOUNDS + ": " + "\"" + BOUNDS_OPEN + "\"";
            } else if (leftOpen) {
                boundsAnnotation = BOUNDS + ": " + "\"" + BOUNDS_LEFT_OPEN + "\"";
            } else if (rightOpen) {
                boundsAnnotation = BOUNDS + ": " + "\"" + BOUNDS_RIGHT_OPEN + "\"";
            }
            if (annotations.length() > 0 || boundsAnnotation.length() > 0) {
                destination.append("({");
            }
            initLen = destination.length();
            if (annotations.length() > 0) {
                destination.append(annotations);
            }
            comma(destination, initLen);
            if (boundsAnnotation.length() > 0) {
                destination.append(boundsAnnotation);
            }
            if (initLen != annotations.length()) {
                destination.append("}");
            }
            destination.append(RANGE).append('(')
                    .append(normalizeIndexName(intItem.getIndexName()))
                    .append(", ").append(intItem.getFromLimit().number())
                    .append(", ").append(intItem.getToLimit().number())
                    .append(")");
            if (annotations.length() > 0 || boundsAnnotation.length() > 0) {
                destination.append(")");
            }
        }

        private void annotatedNumberImage(IntItem item, String rawNumber, StringBuilder image) {
            String annotations = leafAnnotations(item);

            if (annotations.length() > 0) {
                image.append("({").append(annotations).append("}");
            }
            if ('-' == rawNumber.charAt(0)) {
                image.append('(');
            }
            image.append(rawNumber);
            appendLongIfNecessary(rawNumber, image);
            if ('-' == rawNumber.charAt(0)) {
                image.append(')');
            }
            if (annotations.length() > 0) {
                image.append(')');
            }
        }

        private void appendLongIfNecessary(String rawNumber, StringBuilder image) {
            // floating point
            if (rawNumber.indexOf('.') >= 0) {
                return;
            }
            try {
                long l = Long.parseLong(rawNumber);
                if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
                    image.append('L');
                }
            } catch (NumberFormatException e) {
                // somebody has managed to init an IntItem containing noise, just give up
            }
        }
    }

    private static class BoolSerializer extends Serializer<BoolItem> {

        @Override
        void onExit(StringBuilder destination, BoolItem item) { }

        @Override
        boolean serialize(StringBuilder destination, BoolItem item) {
            destination.append(normalizeIndexName(item.getIndexName())).append(" = ");
            destination.append(item.stringValue());
            return false;
        }

    }

    private static class TrueSerializer extends Serializer<TrueItem> {
        @Override
        void onExit(StringBuilder destination, TrueItem item) { }
        @Override
        boolean serialize(StringBuilder destination, TrueItem item) {
            destination.append("true");
            return false;
        }
    }

    private static class FalseSerializer extends Serializer<FalseItem> {
        @Override
        void onExit(StringBuilder destination, FalseItem item) { }
        @Override
        boolean serialize(StringBuilder destination, FalseItem item) {
            destination.append("false");
            return false;
        }
    }

    private static class RegExpSerializer extends Serializer<RegExpItem> {

        @Override
        void onExit(StringBuilder destination, RegExpItem item) { }

        @Override
        boolean serialize(StringBuilder destination, RegExpItem regexp) {
            String annotations = leafAnnotations(regexp);
            destination.append(normalizeIndexName(regexp.getIndexName())).append(" matches ");
            annotatedTerm(destination, regexp, annotations);
            return false;
        }
    }

    private static class FuzzySerializer extends Serializer<FuzzyItem> {

        @Override
        void onExit(StringBuilder destination, FuzzyItem item) { }

        @Override
        boolean serialize(StringBuilder destination, FuzzyItem fuzzy) {
            String annotations = fuzzyAnnotations(fuzzy);

            destination.append(normalizeIndexName(fuzzy.getIndexName())).append(" contains ");

            if (annotations.length() > 0) {
                destination.append('(').append(annotations);
            }

            destination.append(FUZZY).append('(');
            destination.append('"');
            escape(fuzzy.getIndexedString(), destination).append('"');
            destination.append(')');

            if (annotations.length() > 0) {
                destination.append(')');
            }
            return false;
        }

        static String fuzzyAnnotations(FuzzyItem fuzzyItem) {
            boolean isMaxEditDistanceSet = fuzzyItem.getMaxEditDistance() != FuzzyItem.DEFAULT_MAX_EDIT_DISTANCE;
            boolean isPrefixLengthSet = fuzzyItem.getPrefixLength() != FuzzyItem.DEFAULT_PREFIX_LENGTH;
            boolean isPrefixMatch = fuzzyItem.isPrefixMatch();
            boolean anyAnnotationSet = isMaxEditDistanceSet || isPrefixLengthSet || isPrefixMatch;

            if (!anyAnnotationSet) {
                return "";
            }

            StringBuilder builder = new StringBuilder();
            builder.append("{");
            if (isMaxEditDistanceSet) {
                builder.append(MAX_EDIT_DISTANCE + ":").append(fuzzyItem.getMaxEditDistance());
                if (isPrefixLengthSet || isPrefixMatch) {
                    builder.append(",");
                }
            }
            if (isPrefixLengthSet) {
                builder.append(PREFIX_LENGTH + ":").append(fuzzyItem.getPrefixLength());
                if (isPrefixMatch) {
                    builder.append(",");
                }
            }
            if (isPrefixMatch) {
                builder.append(PREFIX).append(':').append(fuzzyItem.isPrefixMatch());
            }
            builder.append("}");
            return builder.toString();
        }
    }

    private static class ONearSerializer extends Serializer<ONearItem> {

        @Override
        void onExit(StringBuilder destination, ONearItem item) {
        }

        @Override
        boolean serialize(StringBuilder destination, ONearItem item) {
            String annotations = NearSerializer.nearAnnotations(item);

            destination.append(getIndexName(item.getItem(0))).append(" contains ");
            if (annotations.length() > 0) {
                destination.append('(').append(annotations);
            }
            destination.append(ONEAR).append('(');
            int initLen = destination.length();
            for (ListIterator<Item> i = item.getItemIterator(); i.hasNext();) {
                WordItem close = (WordItem) i.next();
                if (destination.length() > initLen) {
                    destination.append(", ");
                }
                destination.append('"');
                escape(close.getIndexedString(), destination).append('"');
            }
            destination.append(')');
            if (annotations.length() > 0) {
                destination.append(')');
            }
            return false;
        }

    }

    private static class OrSerializer extends Serializer<OrItem> {

        @Override
        void onExit(StringBuilder destination, OrItem item) {
            destination.append(')');
        }

        @Override
        String separator(Deque<SerializerWrapper> state) {
            return " OR ";
        }

        @Override
        boolean serialize(StringBuilder destination, OrItem item) {
            destination.append("(");
            return true;
        }
    }

    private static class PhraseSegmentSerializer extends Serializer<PhraseSegmentItem> {

        private static void serializeWords(StringBuilder destination, PhraseSegmentItem segment) {
            for (int i = 0; i < segment.getItemCount(); ++i) {
                if (i > 0) {
                    destination.append(", ");
                }
                Item current = segment.getItem(i);
                if (current instanceof WordItem) {
                    destination.append('"');
                    escape(((WordItem) current).getIndexedString(), destination).append('"');
                } else {
                    throw new IllegalArgumentException("Serializing of " + current.getClass().getSimpleName()
                                                       + " in phrases not implemented, please report this as a bug.");
                }
            }
        }

        @Override
        void onExit(StringBuilder destination, PhraseSegmentItem item) { }

        @Override
        boolean serialize(StringBuilder destination, PhraseSegmentItem item) {
            return serialize(destination, item, true);
        }

        static boolean serialize(StringBuilder destination, Item item, boolean includeField) {
            PhraseSegmentItem phrase = (PhraseSegmentItem) item;
            Substring origin = phrase.getOrigin();
            String image;
            int offset;
            int length;

            if (includeField) {
                destination.append(normalizeIndexName(phrase.getIndexName())).append(" contains ");
            }
            if (origin == null) {
                image = phrase.getRawWord();
                offset = 0;
                length = image.length();
            } else {
                image = origin.getSuperstring();
                offset = origin.start;
                length = origin.end - origin.start;
            }

            destination.append("({");
            serializeOrigin(destination, image, offset, length);
            String annotations = leafAnnotations(phrase);
            if (annotations.length() > 0) {
                destination.append(", ").append(annotations);
            }
            if (phrase.getSegmentingRule() == SegmentingRule.BOOLEAN_AND) {
                destination.append(", ").append('"').append(AND_SEGMENTING).append("\": true");
            }
            destination.append("}");
            destination.append(PHRASE).append('(');
            serializeWords(destination, phrase);
            destination.append("))");
            return false;
        }
    }

    private static class PhraseSerializer extends Serializer<PhraseItem> {

        @Override
        void onExit(StringBuilder destination, PhraseItem item) { }

        @Override
        boolean serialize(StringBuilder destination, PhraseItem item) {
            return serialize(destination, item, true);
        }

        static boolean serialize(StringBuilder destination, PhraseItem phrase, boolean includeField) {
            String annotations = leafAnnotations(phrase);

            if (includeField)
                destination.append(normalizeIndexName(phrase.getIndexName())).append(" contains ");
            if (annotations.length() > 0)
                destination.append("({").append(annotations).append("}");

            destination.append(PHRASE).append('(');
            for (int i = 0; i < phrase.getItemCount(); ++i) {
                if (i > 0)
                    destination.append(", ");
                Item current = phrase.getItem(i);
                if (current instanceof WordItem) {
                    WordSerializer.serializeWordWithoutIndex(destination, current);
                } else if (current instanceof PhraseSegmentItem) {
                    PhraseSegmentSerializer.serialize(destination, current, false);
                } else if (current instanceof WordAlternativesItem) {
                    WordAlternativesSerializer.serialize(destination, (WordAlternativesItem) current, false);
                } else {
                    throw new IllegalArgumentException("Serializing of " + current.getClass().getSimpleName() +
                                                       " in phrases not implemented, please report this as a bug.");
                }
            }
            destination.append(')');
            if (annotations.length() > 0)
                destination.append(')');
            return false;
        }

    }

    private static class SameElementSerializer extends Serializer<SameElementItem> {

        @Override
        void onExit(StringBuilder destination, SameElementItem item) { }

        @Override
        boolean serialize(StringBuilder destination, SameElementItem item) {
            return serialize(destination, item, true);
        }

        static boolean serialize(StringBuilder destination, SameElementItem item, boolean includeField) {
            if (includeField) {
                destination.append(normalizeIndexName(item.getFieldName())).append(" contains ");
            }

            destination.append(SAME_ELEMENT).append('(');
            for (int i = 0; i < item.getItemCount(); ++i) {
                if (i > 0) {
                    destination.append(", ");
                }
                Item current = item.getItem(i);
                if (current instanceof WordItem) {
                    new WordSerializer().serialize(destination, (WordItem)current);
                } else if (current instanceof IntItem) {
                    new NumberSerializer().serialize(destination, (IntItem)current);
                } else {
                    throw new IllegalArgumentException("Serializing of " + current.getClass().getSimpleName() +
                                                       " in same_element is not implemented, please report this as a bug.");
                }
            }
            destination.append(')');

            return false;
        }

    }

    private static class GeoLocationSerializer extends Serializer<GeoLocationItem> {
        @Override
        void onExit(StringBuilder destination, GeoLocationItem item) { }
        @Override
        boolean serialize(StringBuilder destination, GeoLocationItem item) {
            String annotations = leafAnnotations(item);
            if (annotations.length() > 0) {
                destination.append("({").append(annotations).append("}");
            }
            destination.append(GEO_LOCATION).append('(');
            destination.append(item.getIndexName()).append(", ");
            var loc = item.getLocation();
            destination.append(loc.degNS()).append(", ");
            destination.append(loc.degEW()).append(", ");
            destination.append('"').append(loc.degRadius()).append(" deg").append('"');
            destination.append(')');
            return false;
        }
    }

    private static class NearestNeighborSerializer extends Serializer<NearestNeighborItem> {

        @Override
        void onExit(StringBuilder destination, NearestNeighborItem item) { }

        @Override
        boolean serialize(StringBuilder destination, NearestNeighborItem item) {
            destination.append("{");
            int initLen = destination.length();
            destination.append(leafAnnotations(item));
            comma(destination, initLen);
            int targetNumHits = item.getTargetNumHits();
            annotationKey(destination, YqlParser.TARGET_NUM_HITS).append(targetNumHits);
            double distanceThreshold = item.getDistanceThreshold();
            if (distanceThreshold < Double.POSITIVE_INFINITY) {
                comma(destination, initLen);
                String key = YqlParser.DISTANCE_THRESHOLD;
                annotationKey(destination, key).append(distanceThreshold);
            }
            int explore = item.getHnswExploreAdditionalHits();
            if (explore != 0) {
                comma(destination, initLen);
                String key = YqlParser.HNSW_EXPLORE_ADDITIONAL_HITS;
                annotationKey(destination, key).append(explore);
            }
            boolean allow_approx = item.getAllowApproximate();
            if (! allow_approx) {
                comma(destination, initLen);
                annotationKey(destination, "approximate").append(allow_approx);
            }
            destination.append("}");
            destination.append(NEAREST_NEIGHBOR).append('(');
            destination.append(item.getIndexName()).append(", ");
            destination.append(item.getQueryTensorName()).append(')');
            return false;
        }

    }

    private static class PredicateQuerySerializer extends Serializer<PredicateQueryItem> {

        @Override
        void onExit(StringBuilder destination, PredicateQueryItem item) { }

        @Override
        boolean serialize(StringBuilder destination, PredicateQueryItem item) {
            destination.append("predicate(").append(item.getIndexName()).append(',');
            appendFeatures(destination, item.getFeatures());
            destination.append(',');
            appendFeatures(destination, item.getRangeFeatures());
            destination.append(')');
            return false;
        }

        private void appendFeatures(StringBuilder destination, Collection<? extends PredicateQueryItem.EntryBase> features) {
            if (features.isEmpty()) {
                destination.append('0'); // Workaround for empty maps.
                return;
            }
            destination.append('{');
            boolean first = true;
            for (PredicateQueryItem.EntryBase entry : features) {
                if (!first) {
                    destination.append(',');
                }
                if (entry.getSubQueryBitmap() != PredicateQueryItem.ALL_SUB_QUERIES) {
                    destination.append("\"0x").append(Long.toHexString(entry.getSubQueryBitmap()));
                    destination.append("\":{");
                    appendKeyValue(destination, entry);
                    destination.append('}');
                } else {
                    appendKeyValue(destination, entry);
                }
                first = false;
            }
            destination.append('}');
        }

        private void appendKeyValue(StringBuilder destination, PredicateQueryItem.EntryBase entry) {
            destination.append('"');
            escape(entry.getKey(), destination);
            destination.append("\":");
            if (entry instanceof PredicateQueryItem.Entry) {
                destination.append('"');
                escape(((PredicateQueryItem.Entry) entry).getValue(), destination);
                destination.append('"');
            } else {
                destination.append(((PredicateQueryItem.RangeEntry) entry).getValue());
                destination.append('L');
            }
        }

    }

    private static class RangeSerializer extends Serializer<RangeItem> {

        @Override
        void onExit(StringBuilder destination, RangeItem item) { }

        @Override
        boolean serialize(StringBuilder destination, RangeItem range) {
            String annotations = leafAnnotations(range);
            if (annotations.length() > 0) {
                destination.append("{").append(annotations).append("}");
            }
            destination.append(RANGE).append('(')
                    .append(normalizeIndexName(range.getIndexName()))
                    .append(", ");
            appendNumberImage(destination, range.getFrom()); // TODO: Serialize
                                                             // inclusive/exclusive
            destination.append(", ");
            appendNumberImage(destination, range.getTo());
            destination.append(')');
            return false;
        }

        private void appendNumberImage(StringBuilder destination, Number number) {
            destination.append(number.toString());
            if (number instanceof Long) {
                destination.append('L');
            }
        }
    }

    private static class RankSerializer extends Serializer<RankItem> {

        @Override
        void onExit(StringBuilder destination, RankItem item) {
            destination.append(')');
        }

        @Override
        String separator(Deque<SerializerWrapper> state) {
            return ", ";
        }

        @Override
        boolean serialize(StringBuilder destination, RankItem item) {
            destination.append(RANK).append('(');
            return true;

        }

    }

    private static class WordAlternativesSerializer extends Serializer<WordAlternativesItem> {

        @Override
        void onExit(StringBuilder destination, WordAlternativesItem item) { }

        @Override
        boolean serialize(StringBuilder destination, WordAlternativesItem item) {
            return serialize(destination, item, true);
        }

        static boolean serialize(StringBuilder destination, WordAlternativesItem alternatives, boolean includeField) {
            String annotations = leafAnnotations(alternatives);
            Substring origin = alternatives.getOrigin();
            boolean isFromQuery = alternatives.isFromQuery();
            boolean needsAnnotations = annotations.length() > 0 || origin != null || !isFromQuery;

            if (includeField) {
                destination.append(normalizeIndexName(alternatives.getIndexName())).append(" contains ");
            }

            if (needsAnnotations) {
                destination.append("({");
                int initLen = destination.length();

                if (origin != null) {
                    String image = origin.getSuperstring();
                    int offset = origin.start;
                    int length = origin.end - origin.start;
                    serializeOrigin(destination, image, offset, length);
                }
                if (!isFromQuery) {
                    comma(destination, initLen);
                    destination.append(IMPLICIT_TRANSFORMS).append(": false");
                }
                if (annotations.length() > 0) {
                    comma(destination, initLen);
                    destination.append(annotations);
                }

                destination.append("}");
            }

            destination.append(ALTERNATIVES).append("({");
            int initLen = destination.length();
            List<WordAlternativesItem.Alternative> sortedAlternatives = new ArrayList<>(alternatives.getAlternatives());
            // ensure most precise forms first
            Collections.sort(sortedAlternatives, (x, y) -> Double.compare(y.exactness, x.exactness));
            for (WordAlternativesItem.Alternative alternative : sortedAlternatives) {
                comma(destination, initLen);
                destination.append('"');
                escape(alternative.word, destination);
                destination.append("\": ").append(alternative.exactness);
            }
            destination.append("})");
            if (needsAnnotations) {
                destination.append(')');
            }
            return false;
        }
    }

    private static class WandSerializer extends Serializer<WandItem> {

        @Override
        void onExit(StringBuilder destination, WandItem item) {
        }

        @Override
        boolean serialize(StringBuilder destination, WandItem item) {
            serializeWeightedSetContents(destination, WAND, item, specificAnnotations(item));
            return false;
        }

        private String specificAnnotations(WandItem w) {
            StringBuilder annotations = new StringBuilder();
            int targetNumHits = w.getTargetNumHits();
            double scoreThreshold = w.getScoreThreshold();
            double thresholdBoostFactor = w.getThresholdBoostFactor();
            if (targetNumHits != 10) {
                annotations.append(TARGET_NUM_HITS).append(": ").append(targetNumHits);
            }
            if (scoreThreshold != 0) {
                comma(annotations, 0);
                annotations.append(SCORE_THRESHOLD).append(": ").append(scoreThreshold);
            }
            if (thresholdBoostFactor != 1) {
                comma(annotations, 0);
                annotations.append(THRESHOLD_BOOST_FACTOR).append(": ").append(thresholdBoostFactor);
            }
            return annotations.toString();
        }

    }

    private static class WeakAndSerializer extends Serializer<WeakAndItem> {

        @Override
        void onExit(StringBuilder destination, WeakAndItem item) {
            destination.append(')');
            if (needsAnnotationBlock(item)) {
                destination.append(')');
            }
        }

        @Override
        String separator(Deque<SerializerWrapper> state) {
            return ", ";
        }

        private boolean needsAnnotationBlock(WeakAndItem item) {
            return nonDefaultTargetNumHits(item);
        }

        @Override
        boolean serialize(StringBuilder destination, WeakAndItem item) {
            if (needsAnnotationBlock(item)) {
                destination.append("({");
            }
            if (nonDefaultTargetNumHits(item)) {
                destination.append(TARGET_NUM_HITS).append(": ").append(item.getN());
            }
            if (needsAnnotationBlock(item)) {
                destination.append("}");
            }
            destination.append(WEAK_AND).append('(');
            return true;
        }

        private boolean nonDefaultTargetNumHits(WeakAndItem w) {
            return w.getN() != WeakAndItem.defaultN;
        }
    }

    private static class WeightedSetSerializer extends Serializer<WeightedSetItem> {

        @Override
        void onExit(StringBuilder destination, WeightedSetItem item) {
        }

        @Override
        boolean serialize(StringBuilder destination, WeightedSetItem item) {
            serializeWeightedSetContents(destination, WEIGHTED_SET, item);
            return false;
        }

    }

    private static class StringInSerializer extends Serializer<StringInItem> {
        @Override
        void onExit(StringBuilder destination, StringInItem item) {

        }

        @Override
        boolean serialize(StringBuilder destination, StringInItem item) {
            destination.append(item.getIndexName()).append(" in (");
            int initLen = destination.length();
            List<String> tokens = new ArrayList<>(item.getTokens());
            Collections.sort(tokens);
            for (var token : tokens) {
                comma(destination, initLen);
                destination.append('"');
                escape(token, destination);
                destination.append("\"");
            }
            destination.append(")");
            return false;
        }
    }

    private static class NumericInSerializer extends Serializer<NumericInItem> {
        @Override
        void onExit(StringBuilder destination, NumericInItem item) {
        }

        @Override
        boolean serialize(StringBuilder destination, NumericInItem item) {
            destination.append(item.getIndexName()).append(" in (");
            int initLen = destination.length();
            List<Long> tokens = new ArrayList<>(item.getTokens());
            Collections.sort(tokens);
            for (var token : tokens) {
                comma(destination, initLen);
                destination.append(token.toString());
                if (token < Integer.MIN_VALUE || token > Integer.MAX_VALUE)
                    destination.append("L");
            }
            destination.append(")");
            return false;
        }
    }

    private static class WordSerializer extends Serializer<WordItem> {

        @Override
        void onExit(StringBuilder destination, WordItem item) {
        }

        @Override
        boolean serialize(StringBuilder destination, WordItem item) {
            StringBuilder wordAnnotations = getAllAnnotations(item);

            destination.append(normalizeIndexName(item.getIndexName())).append(" contains ");
            VespaSerializer.annotatedTerm(destination, item, wordAnnotations.toString());
            return false;
        }

        static void serializeWordWithoutIndex(StringBuilder destination, Item item) {
            WordItem w = (WordItem) item;
            StringBuilder wordAnnotations = getAllAnnotations(w);

            VespaSerializer.annotatedTerm(destination, w, wordAnnotations.toString());
        }

        private static StringBuilder getAllAnnotations(WordItem w) {
            StringBuilder wordAnnotations = new StringBuilder(WordSerializer.wordAnnotations(w));
            String leafAnnotations = leafAnnotations(w);

            if (leafAnnotations.length() > 0) {
                comma(wordAnnotations, 0);
                wordAnnotations.append(leafAnnotations(w));
            }
            return wordAnnotations;
        }

        private static String wordAnnotations(WordItem item) {
            Substring origin = item.getOrigin();
            boolean usePositionData = item.usePositionData();
            boolean stemmed = item.isStemmed();
            boolean lowercased = item.isLowercased();
            boolean accentDrop = item.isNormalizable();
            SegmentingRule andSegmenting = item.getSegmentingRule();
            boolean isFromQuery = item.isFromQuery();
            StringBuilder annotation = new StringBuilder();
            boolean prefix = item instanceof PrefixItem;
            boolean suffix = item instanceof SuffixItem;
            boolean substring = item instanceof SubstringItem;
            int initLen = annotation.length();
            String image;
            int offset;
            int length;

            if (origin == null) {
                image = item.getRawWord();
                offset = 0;
                length = image.length();
            } else {
                image = origin.getSuperstring();
                offset = origin.start;
                length = origin.end - origin.start;
            }

            if (!image.substring(offset, offset + length).equals(item.getIndexedString())) {
                VespaSerializer.serializeOrigin(annotation, image, offset, length);
            }
            if ( ! usePositionData) {
                VespaSerializer.comma(annotation, initLen);
                annotation.append(USE_POSITION_DATA).append(": false");
            }
            if (stemmed) {
                VespaSerializer.comma(annotation, initLen);
                annotation.append(STEM).append(": false");
            }
            if (lowercased) {
                VespaSerializer.comma(annotation, initLen);
                annotation.append(NORMALIZE_CASE).append(": false");
            }
            if ( ! accentDrop) {
                VespaSerializer.comma(annotation, initLen);
                annotation.append(ACCENT_DROP).append(": false");
            }
            if (andSegmenting == SegmentingRule.BOOLEAN_AND) {
                VespaSerializer.comma(annotation, initLen);
                annotation.append(AND_SEGMENTING).append(": true");
            }
            if (!isFromQuery) {
                VespaSerializer.comma(annotation, initLen);
                annotation.append(IMPLICIT_TRANSFORMS).append(": false");
            }
            if (prefix) {
                VespaSerializer.comma(annotation, initLen);
                annotation.append(PREFIX).append(": true");
            }
            if (suffix) {
                VespaSerializer.comma(annotation, initLen);
                annotation.append(SUFFIX).append(": true");
            }
            if (substring) {
                VespaSerializer.comma(annotation, initLen);
                annotation.append(SUBSTRING).append(": true");
            }
            return annotation.toString();
        }

    }

    private static final class SerializerWrapper {
        int subItems;
        final Serializer type;
        final Item item;

        SerializerWrapper(Serializer type, Item item) {
            subItems = 0;
            this.type = type;
            this.item = item;
        }

    }

    private static final class TokenComparator implements Comparator<Entry<Object, Integer>> {

        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override
        public int compare(Entry<Object, Integer> o1, Entry<Object, Integer> o2) {
            Comparable c1 = (Comparable) o1.getKey();
            Comparable c2 = (Comparable) o2.getKey();
            return c1.compareTo(c2);
        }
    }

    private static class VespaVisitor extends QueryVisitor {

        final StringBuilder destination;
        final Deque<SerializerWrapper> state = new ArrayDeque<>();

        VespaVisitor(StringBuilder destination) {
            this.destination = destination;
        }

        @Override
        public void onExit() {
            SerializerWrapper w = state.removeFirst();
            w.type.onExit(destination, w.item);
            w = state.peekFirst();
            if (w != null) {
                w.subItems += 1;
            }
        }

        @Override
        public boolean visit(Item item) {
            Serializer doIt = dispatch.get(item.getClass());

            if (doIt == null) {
                throw new IllegalArgumentException(item.getClass() + " not supported for YQL marshalling.");
            }

            if (state.peekFirst() != null && state.peekFirst().subItems > 0) {
                destination.append(state.peekFirst().type.separator(state));
            }
            state.addFirst(new SerializerWrapper(doIt, item));
            return doIt.serialize(destination, item);

        }
    }

    private static final char[] DIGITS =
            new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    private static final Map<Class<?>, Serializer> dispatch;

    private static final Comparator<? super Entry<Object, Integer>> tokenComparator = new TokenComparator();

    static {
        Map<Class<?>, Serializer> dispatchBuilder = new HashMap<>();
        dispatchBuilder.put(AndItem.class, new AndSerializer());
        dispatchBuilder.put(AndSegmentItem.class, new AndSegmentSerializer());
        dispatchBuilder.put(DotProductItem.class, new DotProductSerializer());
        dispatchBuilder.put(EquivItem.class, new EquivSerializer());
        dispatchBuilder.put(ExactStringItem.class, new WordSerializer());
        dispatchBuilder.put(IntItem.class, new NumberSerializer());
        dispatchBuilder.put(GeoLocationItem.class, new GeoLocationSerializer());
        dispatchBuilder.put(BoolItem.class, new BoolSerializer());
        dispatchBuilder.put(TrueItem.class, new TrueSerializer());
        dispatchBuilder.put(FalseItem.class, new FalseSerializer());
        dispatchBuilder.put(MarkerWordItem.class, new WordSerializer()); // gotcha
        dispatchBuilder.put(NearItem.class, new NearSerializer());
        dispatchBuilder.put(NearestNeighborItem.class, new NearestNeighborSerializer());
        dispatchBuilder.put(NotItem.class, new NotSerializer());
        dispatchBuilder.put(NullItem.class, new NullSerializer());
        dispatchBuilder.put(ONearItem.class, new ONearSerializer());
        dispatchBuilder.put(OrItem.class, new OrSerializer());
        dispatchBuilder.put(PhraseItem.class, new PhraseSerializer());
        dispatchBuilder.put(SameElementItem.class, new SameElementSerializer());
        dispatchBuilder.put(PhraseSegmentItem.class, new PhraseSegmentSerializer());
        dispatchBuilder.put(PredicateQueryItem.class, new PredicateQuerySerializer());
        dispatchBuilder.put(PrefixItem.class, new WordSerializer()); // gotcha
        dispatchBuilder.put(WordAlternativesItem.class, new WordAlternativesSerializer());
        dispatchBuilder.put(RangeItem.class, new RangeSerializer());
        dispatchBuilder.put(RankItem.class, new RankSerializer());
        dispatchBuilder.put(SubstringItem.class, new WordSerializer()); // gotcha
        dispatchBuilder.put(SuffixItem.class, new WordSerializer()); // gotcha
        dispatchBuilder.put(WandItem.class, new WandSerializer());
        dispatchBuilder.put(WeakAndItem.class, new WeakAndSerializer());
        dispatchBuilder.put(WeightedSetItem.class, new WeightedSetSerializer());
        dispatchBuilder.put(WordItem.class, new WordSerializer());
        dispatchBuilder.put(RegExpItem.class, new RegExpSerializer());
        dispatchBuilder.put(UriItem.class, new UriSerializer());
        dispatchBuilder.put(FuzzyItem.class, new FuzzySerializer());
        dispatchBuilder.put(StringInItem.class, new StringInSerializer());
        dispatchBuilder.put(NumericInItem.class, new NumericInSerializer());
        dispatch = ImmutableMap.copyOf(dispatchBuilder);
    }

    /**
     * Do YQL+ escaping, which is basically the same as for JSON, of the
     * incoming string to the "quoted" buffer. The buffer returned is the same
     * as the one given in the "quoted" parameter.
     *
     * @param in a string to escape
     * @param escaped the target buffer for escaped data
     * @return the same buffer as given in the "quoted" parameter
     */
    private static StringBuilder escape(String in, StringBuilder escaped) {
        for (char c : in.toCharArray()) {
            switch (c) {
            case ('\b'):
                escaped.append("\\b");
                break;
            case ('\t'):
                escaped.append("\\t");
                break;
            case ('\n'):
                escaped.append("\\n");
                break;
            case ('\f'):
                escaped.append("\\f");
                break;
            case ('\r'):
                escaped.append("\\r");
                break;
            case ('"'):
                escaped.append("\\\"");
                break;
            case ('\''):
                escaped.append("\\'");
                break;
            case ('\\'):
                escaped.append("\\\\");
                break;
            case ('/'):
                escaped.append("\\/");
                break;
            default:
                if (c < 32 || c >= 127) {
                    escaped.append("\\u").append(fourDigitHexString(c));
                } else {
                    escaped.append(c);
                }
            }
        }
        return escaped;
    }

    private static char[] fourDigitHexString(char c) {
        char[] hex = new char[4];
        int in = ((c) & 0xFFFF);
        for (int i = 3; i >= 0; --i) {
            hex[i] = DIGITS[in & 0xF];
            in >>>= 4;
        }
        return hex;
    }

    static String getIndexName(Item item) {
        if (!(item instanceof IndexedItem))
            throw new IllegalArgumentException("Expected IndexedItem, got " + item.getClass());
        return normalizeIndexName(((IndexedItem) item).getIndexName());
    }

    public static String serialize(Query query) {
        return serialize(query, "");
    }

    public static String serialize(Query query, String insertBeforeGrouping) {
        StringBuilder out = new StringBuilder();
        serialize(query.getModel().getQueryTree().getRoot(), out);
        out.append(insertBeforeGrouping);
        for (GroupingRequest request : query.getSelect().getGrouping()) {
            out.append(" | ");
            serialize(request, out);
        }
        return out.toString();
    }

    private static void serialize(GroupingRequest request, StringBuilder out) {
        Iterator<Continuation> it = request.continuations().iterator();
        if (it.hasNext()) {
            out.append("{ continuations:[");
            while (it.hasNext()) {
                out.append('\'').append(it.next()).append('\'');
                if (it.hasNext()) {
                    out.append(", ");
                }
            }
            out.append("] }");
        }
        out.append(request.getRootOperation());
    }

    private static void serialize(Item item, StringBuilder out) {
        VespaVisitor visitor = new VespaVisitor(out);
        ToolBox.visit(visitor, item);
    }

    static String serialize(QueryTree item) {
        StringBuilder out = new StringBuilder();
        serialize(item.getRoot(), out);
        return out.toString();
    }

    static String serialize(Item item) {
        StringBuilder out = new StringBuilder();
        serialize(item, out);
        return out.toString();
    }

    private static void serializeWeightedSetContents(StringBuilder destination, String opName,
                                                     WeightedSetItem weightedSet) {
        serializeWeightedSetContents(destination, opName, weightedSet, "");
    }

    private static void serializeWeightedSetContents(StringBuilder destination, String opName,
                                                     WeightedSetItem weightedSet, String optionalAnnotations) {
        boolean addedAnnotations = addAnnotations(destination, weightedSet, optionalAnnotations);
        destination.append(opName).append('(')
                   .append(normalizeIndexName(weightedSet.getIndexName()))
                   .append(", {");
        int initLen = destination.length();
        List<Entry<Object, Integer>> tokens = new ArrayList<>(weightedSet.getNumTokens());
        for (Iterator<Entry<Object, Integer>> i = weightedSet.getTokens(); i.hasNext();) {
            tokens.add(i.next());
        }
        Collections.sort(tokens, tokenComparator);
        for (Entry<Object, Integer> entry : tokens) {
            comma(destination, initLen);
            destination.append('"');
            escape(entry.getKey().toString(), destination);
            destination.append("\": ").append(entry.getValue().toString());
        }
        destination.append("})");
        if (addedAnnotations)
            destination.append(")");
    }

    /** Adds annotations and returns whether any were added */
    private static boolean addAnnotations(StringBuilder destination, WeightedSetItem weightedSet,
                                          String optionalAnnotations) {
        int preAnnotationValueLen;
        int incomingLen = destination.length();
        String annotations = leafAnnotations(weightedSet);

        if (optionalAnnotations.length() > 0 || annotations.length() > 0) {
            destination.append("({");
        }
        preAnnotationValueLen = destination.length();
        if (annotations.length() > 0) {
            destination.append(annotations);
        }
        if (optionalAnnotations.length() > 0) {
            comma(destination, preAnnotationValueLen);
            destination.append(optionalAnnotations);
        }
        if (destination.length() > incomingLen) {
            destination.append("}");
            return true;
        }
        else {
            return false;
        }
    }

    private static StringBuilder annotationKey(StringBuilder annotation, String val) {
        annotation.append(val).append(": ");
        return annotation;
    }

    private static void comma(StringBuilder annotation, int initLen) {
        if (annotation.length() > initLen) {
            annotation.append(", ");
        }
    }

    private static String leafAnnotations(TaggableItem item) {
        // TODO: There is no usable API for the general annotations map in the Item instances
        StringBuilder annotation = new StringBuilder();
        int initLen = annotation.length();
        {
            int uniqueId = item.getUniqueID();
            double connectivity = item.getConnectivity();
            TaggableItem connectedTo = (TaggableItem) item.getConnectedItem();
            double significance = item.getSignificance();
            if (connectedTo != null && connectedTo.getUniqueID() != 0) {
                annotation.append(CONNECTIVITY).append(": {")
                        .append(CONNECTION_ID).append(": ")
                        .append(connectedTo.getUniqueID()).append(", ")
                        .append(CONNECTION_WEIGHT).append(": ")
                        .append(connectivity).append("}");
            }
            if (item.hasExplicitSignificance()) {
                comma(annotation, initLen);
                annotation.append(SIGNIFICANCE).append(": ").append(significance);
            }
            if (uniqueId != 0) {
                comma(annotation, initLen);
                annotation.append(UNIQUE_ID).append(": ").append(uniqueId);
            }
        }
        {
            Item leaf = (Item) item;
            boolean filter = leaf.isFilter();
            boolean isRanked = leaf.isRanked();
            String label = leaf.getLabel();
            int weight = leaf.getWeight();

            if (filter) {
                comma(annotation, initLen);
                annotation.append(FILTER).append(": true");
            }
            if ( ! isRanked) {
                comma(annotation, initLen);
                annotation.append(RANKED).append(": false");
            }
            if (label != null) {
                comma(annotation, initLen);
                annotation.append(LABEL).append(": \"");
                escape(label, annotation);
                annotation.append("\"");
            }
            if (weight != 100) {
                comma(annotation, initLen);
                annotation.append(WEIGHT).append(": ").append(weight);
            }
        }
        if (item instanceof IntItem) {
            int hitLimit = ((IntItem) item).getHitLimit();
            if (hitLimit != 0) {
                comma(annotation, initLen);
                annotation.append(HIT_LIMIT).append(": ").append(hitLimit);
            }
        }
        return annotation.toString();
    }

    private static void serializeOrigin(StringBuilder destination, String image, int offset, int length) {
        destination.append(ORIGIN).append(": {").append(ORIGIN_ORIGINAL).append(": \"");
        escape(image, destination);
        destination.append("\", ").append(ORIGIN_OFFSET).append(": ")
                .append(offset).append(", ").append(ORIGIN_LENGTH)
                .append(": ").append(length).append("}");
    }

    private static String normalizeIndexName(String indexName) {
        if (indexName.length() == 0) {
            return "default";
        } else {
            return indexName;
        }
    }

    private static void annotatedTerm(StringBuilder destination, IndexedItem w, String annotations) {
        if (annotations.length() > 0) {
            destination.append("({").append(annotations).append("}");
        }
        destination.append('"');
        escape(w.getIndexedString(), destination).append('"');
        if (annotations.length() > 0) {
            destination.append(')');
        }
    }

}