File: script_syntax.rst

package info (click to toggle)
groonga 15.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 163,080 kB
  • sloc: ansic: 770,564; cpp: 48,925; ruby: 40,447; javascript: 10,250; yacc: 7,045; sh: 5,602; python: 2,821; makefile: 1,672
file content (1596 lines) | stat: -rw-r--r-- 54,433 bytes parent folder | download | duplicates (2)
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
.. -*- rst -*-

.. groonga-command
.. database: reference_grn_expr_script_syntax

Script syntax
=============

Script syntax is a syntax to specify complex search condition. It is
similar to ECMAScript. For example, ``_key == "book"`` means that
groonga searches records that ``_key`` value is ``"book"``. All values
are string in :doc:`query_syntax` but its own type in script
syntax. For example, ``"book"`` is string, ``1`` is integer,
``TokenBigram`` is the object whose name is ``TokenBigram`` and so on.

Script syntax doesn't support full ECMAScript syntax. For example,
script syntax doesn't support statement such as ``if`` control
statement, ``for`` iteration statement and variable definition
statement. Function definion is not supported too. But script syntax
addes the original additional operators.  They are described after
ECMAScript syntax is described.

.. _script-syntax-security:

Security
--------

For security reason, you should not pass an input from users to
Groonga directly. If there is an evil user, the user may input a query
that retrieves records that should not be shown to the user.

Think about the following case.

A Groonga application constructs a Groonga request by the following
program::

  filter = "column @ \"#{user_input}\""
  select_options = {
    # ...
    :filter => filter,
  }
  groonga_client.select(select_options)

``user_input`` is an input from user. If the input is ``query``,
here is the constructed :ref:`select-filter` parameter::

  column @ "query"

If the input is ``x" || true || "``, here is the constructed
:ref:`select-filter` parameter::

  column @ "x" || true || ""

This query matches to all records. The user will get all records from
your database. The user may be evil.

It's better that you just receive an user input as a value. It means
that you don't accept that user input can contain operator such as
``@`` and ``&&``. If you accept operator, user can create evil query.

If user input has only value, you blocks evil query by escaping user
input value. Here is a list how to escape user input value:

  * True value: Convert it to ``true``.
  * False value: Convert it to ``false``.
  * Numerical value: Convert it to :ref:`script-syntax-literal-integer`
    or :ref:`script-syntax-literal-float`. For example, ``1.2``,
    ``-10``, ``314e-2`` and so on.
  * String value: Replace ``"`` with ``\"`` and ``\`` with ``\\`` in
    the string value and surround substituted string value by
    ``"``. For example, ``double " quote and back \ slash`` should be
    converted to ``"double \" quote and back \\ slash"``.

Sample data
-----------

Here are a schema definition and sample data to show usage.

.. groonga-command
.. include:: ../../example/reference/grn_expr/query_syntax/setup.log
.. table_create Entries TABLE_PAT_KEY ShortText
.. column_create Entries content COLUMN_SCALAR Text
.. column_create Entries n_likes COLUMN_SCALAR UInt32
.. table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
.. column_create Terms entries_key_index COLUMN_INDEX|WITH_POSITION Entries _key
.. column_create Terms entries_content_index COLUMN_INDEX|WITH_POSITION Entries content
.. load --table Entries
.. [
.. {"_key":    "The first post!",
..  "content": "Welcome! This is my first post!",
..  "n_likes": 5},
.. {"_key":    "Groonga",
..  "content": "I started to use Groonga. It's very fast!",
..  "n_likes": 10},
.. {"_key":    "Mroonga",
..  "content": "I also started to use Mroonga. It's also very fast! Really fast!",
..  "n_likes": 15},
.. {"_key":    "Good-bye Senna",
..  "content": "I migrated all Senna system!",
..  "n_likes": 3},
.. {"_key":    "Good-bye Tritonn",
..  "content": "I also migrated all Tritonn system!",
..  "n_likes": 3}
.. ]

There is a table, ``Entries``, for blog entries. An entry has title,
content and the number of likes for the entry. Title is key of
``Entries``. Content is value of ``Entries.content`` column. The
number of likes is value of ``Entries.n_likes`` column.

``Entries._key`` column and ``Entries.content`` column are indexed
using ``TokenBigram`` tokenizer. So both ``Entries._key`` and
``Entries.content`` are fulltext search ready.

OK. The schema and data for examples are ready.

.. _script-syntax-literals:

Literals
--------

.. _script-syntax-literal-integer:

Integer
^^^^^^^

Integer literal is sequence of ``0`` to ``9`` such as
``1234567890``. ``+`` or ``-`` can be prepended as sign such as
``+29`` and ``-29``. Integer literal must be decimal. Octal notation,
hex and so on can't be used.

The maximum value of integer literal is ``9223372036854775807`` (``= 2
** 63 - 1``). The minimum value of integer literal is
``-9223372036854775808`` (``= -(2 ** 63)``).

.. _script-syntax-literal-float:

Float
^^^^^

Float literal is sequence of ``0`` to ``9``, ``.`` and ``0`` to ``9``
such as ``3.14``. ``+`` or ``-`` can be prepended as sign such as
``+3.14`` and ``-3.14``. ``${RADIX}e${EXPORNENTIAL}`` and
``${RADIX}E${EXPORNENTIAL}`` formats are also supported. For example,
``314e-2`` is the same as ``3.14``.

.. _script-syntax-literal-string:

String
^^^^^^

String literal is ``"..."``. You need to escape ``"`` in literal by
prepending ``\\'' such as ``\"``. For example, ``"Say \"Hello!\"."`` is
a literal for ``Say "Hello!".`` string.

String encoding must be the same as encoding of database. The default
encoding is UTF-8. It can be changed by ``--with-default-encoding``
configure option, ``--encodiong`` :doc:`/reference/executables/groonga` option
and so on.

.. _script-syntax-literal-boolean:

Boolean
^^^^^^^

Boolean literal is ``true`` and ``false``. ``true`` means true and
``false`` means false.

.. _script-syntax-literal-null:

Null
^^^^

Null literal is ``null``. Groonga doesn't support null value but null
literal is supported.

.. _script-syntax-literal-time:

Time
^^^^

.. note::

   This is the groonga original notation.

Time literal doesn't exit. There are string time notation, integer
time notation and float time notation.

String time notation is ``"YYYY/MM/DD hh:mm:ss.uuuuuu"`` or
``"YYYY-MM-DD hh:mm:ss.uuuuuu"``. ``YYYY`` is year, ``MM`` is month,
``DD`` is day, ``hh`` is hour, ``mm`` is minute, ``ss`` is second and
``uuuuuu`` is micro second. It is local time. For example,
``"2012/07/23 02:41:10.436218"`` is ``2012-07-23T02:41:10.436218`` in
ISO 8601 format.

Integer time notation is the number of seconds that have elapsed since
midnight UTC, January 1, 1970. It is also known as POSIX time. For
example, ``1343011270`` is ``2012-07-23T02:41:10Z`` in ISO 8601 format.

Float time notation is the number of seconds and micro seconds that
have elapsed since midnight UTC, January 1, 1970. For example,
``1343011270.436218`` is ``2012-07-23T02:41:10.436218Z`` in ISO 8601
format.

.. _script-syntax-literal-geo-point:

Geo point
^^^^^^^^^

.. note::

   This is the groonga original notation.

Geo point literal doesn't exist. There is string geo point notation.

String geo point notation has the following patterns:

  * ``"LATITUDE_IN_MSECxLONGITUDE_IN_MSEC"``
  * ``"LATITUDE_IN_MSEC,LONGITUDE_IN_MSEC"``
  * ``"LATITUDE_IN_DEGREExLONGITUDE_IN_DEGREE"``
  * ``"LATITUDE_IN_DEGREE,LONGITUDE_IN_DEGREE"``

``x`` and ``,`` can be used for separator. Latitude and longitude can
be represented in milliseconds or degree.

.. _script-syntax-literal-array:

Array
^^^^^

Array literal is ``[element1, element2, ...]``.

.. _script-syntax-literal-object:

Object literal
^^^^^^^^^^^^^^

Object literal is ``{name1: value1, name2: value2, ...}``. Groonga
doesn't support object literal yet.

Control syntaxes
----------------

Script syntax doesn't support statement. So you cannot use control
statement such as ``if``. You can only use ``A ? B : C`` expression as
control syntax.

``A ? B : C`` returns ``B`` if ``A`` is true, ``C`` otherwise.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_control_syntax_ternary_operator.log
.. select Entries --filter 'n_likes == (_id == 1 ? 5 : 3)'

The expression matches records that ``_id`` column value is equal to ``1``
and ``n_likes`` column value is equal to ``5`` or ``_id`` column value is
not equal to 1 and ``n_likes`` column value is equal to ``3``.

Grouping
--------

Its syntax is ``(...)``. ``...`` is comma separated expression list.

``(...)`` groups one ore more expressions and they can be processed as
an expression. ``a && b || c`` means that ``a`` and ``b`` are matched
or ``c`` is matched. ``a && (b || c)`` means that ``a`` and one of
``b`` and ``c`` are matched.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_grouping.log
.. select Entries --filter 'n_likes < 5 && content @ "senna" || content @ "fast"'
.. select Entries --filter 'n_likes < 5 && (content @ "senna" || content @ "fast")'

The first expression doesn't use grouping. It matches records that
``n_likes < 5`` and ``content @ "senna"`` are matched or
``content @ "fast"`` is matched.

The second expression uses grouping. It matches records that ``n_likes
< 5`` and one of ``content @ "senna"`` or ``content @ "fast"`` are
matched.

Function call
-------------

Its syntax is ``name(arugment1, argument2, ...)``.

``name(argument1, argument2, ...)`` calls a function that is named
``name`` with arguments ``argument1``, ``argument2`` and ``...``.

See :doc:`/reference/function` for available functin list.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_function.log
.. select Entries --filter 'edit_distance(_key, "Groonga") <= 1'

The expression uses :doc:`/reference/functions/edit_distance`. It
matches records that ``_key`` column value is similar to
``"Groonga"``. Similality of ``"Groonga"`` is computed as edit
distance. If edit distance is less than or equal to 1, the value is
treated as similar. In this case, ``"Groonga"`` and ``"Mroonga"`` are
treated as similar.

Basic operators
---------------

Groonga supports operators defined in ECMAScript.

Arithmetic operators
^^^^^^^^^^^^^^^^^^^^

Here are arithmetic operators.

Addition operator
"""""""""""""""""

Its syntax is ``number1 + number2``.

The operator adds ``number1`` and ``number2`` and returns the result.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_addition_operator.log
.. select Entries --filter 'n_likes == 10 + 5'

The expression matches records that ``n_likes`` column value is equal
to ``15`` (= ``10 + 5``).

Subtraction operator
""""""""""""""""""""

Its syntax is ``number1 - number2``.

The operator subtracts ``number2`` from ``number1`` and returns the result.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_subtraction_operator.log
.. select Entries --filter 'n_likes == 20 - 5'

The expression matches records that ``n_likes`` column value is equal
to ``15`` (= ``20 - 5``).

Multiplication operator
"""""""""""""""""""""""

Its syntax is ``number1 * number2``.

The operator multiplies ``number1`` and ``number2`` and returns the result.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_multiplication_operator.log
.. select Entries --filter 'n_likes == 3 * 5'

The expression matches records that ``n_likes`` column value is equal
to ``15`` (= ``3 * 5``).

Division operator
"""""""""""""""""

Its syntax is ``number1 / number2`` and ``number1 % number2``.

The operator divides ``number2`` by ``number1``. ``/`` returns the
quotient of result. ``%`` returns the remainder of result.

Here is simple examples.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_division_operator_quotient.log
.. select Entries --filter 'n_likes == 26 / 7'

The expression matches records that ``n_likes`` column value is equal
to ``3`` (= ``26 / 7``).

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_division_operator_remainder.log
.. select Entries --filter 'n_likes == 26 % 7'

The expression matches records that ``n_likes`` column value is equal
to ``5`` (= ``26 % 7``).

Logical operators
^^^^^^^^^^^^^^^^^

Here are logical operators.

Logical NOT operator
""""""""""""""""""""

Its syntax is ``!condition``.

The operator inverts boolean value of ``condition``.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_logical_not_operator.log
.. select Entries --filter '!(n_likes == 5)'

The expression matches records that ``n_likes`` column value is not
equal to ``5``.

Logical AND operator
""""""""""""""""""""

Its syntax is ``condition1 && condition2``.

The operator returns true if both of ``condition1`` and
``condition2`` are true, false otherwise.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_logical_and_operator.log
.. select Entries --filter 'content @ "fast" && n_likes >= 10'

The expression matches records that ``content`` column value has the
word ``fast`` and ``n_likes`` column value is greater or equal to
``10``.

Logical OR operator
"""""""""""""""""""

Its syntax is ``condition1 || condition2``.

The operator returns true if either ``condition1`` or ``condition2`` is
true, false otherwise.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_logical_or_operator.log
.. select Entries --filter 'n_likes == 5 || n_likes == 10'

The expression matches records that ``n_likes`` column value is equal
to ``5`` or ``10``.

Logical AND NOT operator
""""""""""""""""""""""""

Its syntax is ``condition1 &! condition2``.

The operator returns true if ``condition1`` is true but ``condition2``
is false, false otherwise. It returns difference set.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_logical_but_operator.log
.. select Entries --filter 'content @ "fast" &! content @ "mroonga"'

The expression matches records that ``content`` column value has the
word ``fast`` but doesn't have the word ``mroonga``.

.. _script-syntax-bitwise-operators:

Bitwise operators
^^^^^^^^^^^^^^^^^

Here are bitwise operators.

.. _script-syntax-bitwise-not:

Bitwise NOT operator
""""""""""""""""""""

Its syntax is ``~number``.

The operator returns bitwise NOT of ``number``.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_bitwise_not_operator.log
.. select Entries --filter '~n_likes == -6'

The expression matches records that ``n_likes`` column value is equal
to ``5`` because bitwise NOT of ``5`` is equal to ``-6``.

.. _script-syntax-bitwise-and:

Bitwise AND operator
""""""""""""""""""""

Its syntax is ``number1 & number2``.

The operator returns bitwise AND between ``number1`` and ``number2``.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_bitwise_and_operator.log
.. select Entries --filter '(n_likes & 1) == 1'

The expression matches records that ``n_likes`` column value is even
number because bitwise AND between an even number and ``1`` is equal
to ``1`` and bitwise AND between an odd number and ``1`` is equal to
``0``.

.. _script-syntax-bitwise-or:

Bitwise OR operator
^^^^^^^^^^^^^^^^^^^

Its syntax is ``number1 | number2``.

The operator returns bitwise OR between ``number1`` and ``number2``.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_bitwise_or_operator.log
.. select Entries --filter 'n_likes == (1 | 4)'

The expression matches records that ``n_likes`` column value is equal
to ``5`` (= ``1 | 4``).

.. _script-syntax-bitwise-xor:

Bitwise XOR operator
^^^^^^^^^^^^^^^^^^^^

Its syntax is ``number1 ^ number2``.

The operator returns bitwise XOR between ``number1`` and ``number2``.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_bitwise_xor_operator.log
.. select Entries --filter 'n_likes == (10 ^ 15)'

The expression matches records that ``n_likes`` column value is equal
to ``5`` (= ``10 ^ 15``).

.. _script-syntax-shift-operators:

Shift operators
^^^^^^^^^^^^^^^

Here are shift operators.

.. _script-syntax-shift-left:

Left shift operator
"""""""""""""""""""

Its syntax is ``number1 << number2``.

The operator performs a bitwise left shift operation on ``number1`` by
``number2``.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_left_shift_operator.log
.. select Entries --filter 'n_likes == (5 << 1)'

The expression matches records that ``n_likes`` column value is equal
to ``10`` (= ``5 << 1``).

.. _script-syntax-shift-signed-right:

Signed right shift operator
"""""""""""""""""""""""""""

Its syntax is ``number1 >> number2``.

The operator shifts bits of ``number1`` to right by ``number2``. The sign
of the result is the same as ``number1``.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_signed_right_shift_operator.log
.. select Entries --filter 'n_likes == -(-10 >> 1)'

The expression matches records that ``n_likes`` column value is equal
to ``5`` (= ``-(-10 >> 1)`` = ``-(-5)``).

.. _script-syntax-shift-unsigned-right:

Unsigned right shift operator
"""""""""""""""""""""""""""""

Its syntax is ``number1 >>> number2``.

The operator shifts bits of ``number1`` to right by ``number2``. The
leftmost ``number2`` bits are filled by ``0``.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_unsigned_right_shift_operator.log
.. select Entries --filter 'n_likes == (2147483648 - (-10 >>> 1))'

The expression matches records that ``n_likes`` column value is equal
to ``5`` (= ``2147483648 - (-10 >>> 1)`` = ``2147483648 - 2147483643``).

Comparison operators
^^^^^^^^^^^^^^^^^^^^

Here are comparison operators.

.. _script-syntax-equal-operator:

Equal operator
""""""""""""""

Its syntax is ``object1 == object2``.

The operator returns true if ``object1`` equals to ``object2``, false
otherwise.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_equal_operator.log
.. select Entries --filter 'n_likes == 5'

The expression matches records that ``n_likes`` column value is equal
to ``5``.

Not equal operator
""""""""""""""""""

Its syntax is ``object1 != object2``.

The operator returns true if ``object1`` does not equal to
``object2``, false otherwise.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_not_equal_operator.log
.. select Entries --filter 'n_likes != 5'

The expression matches records that ``n_likes`` column value is not
equal to ``5``.

Less than operator
""""""""""""""""""

TODO: ...

Less than or equal to operator
""""""""""""""""""""""""""""""

TODO: ...

Greater than operator
"""""""""""""""""""""

TODO: ...

Greater than or equal to operator
"""""""""""""""""""""""""""""""""

TODO: ...

Assignment operators
--------------------


Addition assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column1 += column2``.

The operator performs addition assignment operation on column1 by column2.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_addition_assignment_operator.log
.. select Entries --output_columns _key,n_likes,_score --filter true --scorer '_score += n_likes'

The value of ``_score`` by ``--filter`` is always 1 in this case,
then performs addition assignment operation such as '_score = _score + n_likes' for each records.

For example, the value of ``_score`` about the record which stores "Good-bye Senna" as the ``_key``
is 3.

So the expression ``1 + 3`` is evaluated and stored to ``_score`` column as the execution result.

Subtraction assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column1 -= column2``.

The operator performs subtraction assignment operation on column1 by column2.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_subtraction_assignment_operator.log
.. select Entries --output_columns _key,n_likes,_score --filter true --scorer '_score -= n_likes'

The value of ``_score`` by ``--filter`` is always 1 in this case,
then performs subtraction assignment operation such as '_score = _score - n_likes' for each records.

For example, the value of ``_score`` about the record which stores "Good-bye Senna" as the ``_key``
is 3.

So the expression ``1 - 3`` is evaluated and stored to ``_score`` column as the execution result.

Multiplication assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column1 *= column2``.

The operator performs multiplication assignment operation on column1 by column2.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_multiplication_assignment_operator.log
.. select Entries --output_columns _key,n_likes,_score --filter true --scorer '_score *= n_likes'

The value of ``_score`` by ``--filter`` is always 1 in this case,
then performs subtraction assignment operation such as '_score = _score * n_likes' for each records.

For example, the value of ``_score`` about the record which stores "Good-bye Senna" as the ``_key``
is 3.

So the expression ``1 * 3`` is evaluated and stored to ``_score`` column as the execution result.

Division assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column1 /= column2``.

The operator performs division assignment operation on column1 by column2.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_division_assignment_operator.log
.. select Entries --output_columns _key,n_likes,_score --filter true --scorer '_score /= n_likes'

The value of ``_score`` by ``--filter`` is always 1 in this case,
then performs subtraction assignment operation such as '_score = _score / n_likes' for each records.

For example, the value of ``_score`` about the record which stores "Good-bye Senna" as the ``_key``
is 3.

So the expression ``1 / 3`` is evaluated and stored to ``_score`` column as the execution result.

Modulo assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column1 %= column2``.

The operator performs modulo assignment operation on column1 by column2.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_modulo_assignment_operator.log
.. select Entries --output_columns _key,n_likes,_score --filter true --scorer '_score %= n_likes'

The value of ``_score`` by ``--filter`` is always 1 in this case,
then performs subtraction assignment operation such as '_score = _score % n_likes' for each records.

For example, the value of ``_score`` about the record which stores "Good-bye Senna" as the ``_key``
is 3.

So the expression ``1 % 3`` is evaluated and stored to ``_score`` column as the execution result.

Bitwise left shift assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column1 <<= column2``.

The operator performs left shift assignment operation on column1 by column2.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_left_shift_assignment_operator.log
.. select Entries --output_columns _key,n_likes,_score --filter true --scorer '_score <<= n_likes'

The value of ``_score`` by ``--filter`` is always 1 in this case,
then performs subtraction assignment operation such as '_score = _score << n_likes' for each records.

For example, the value of ``_score`` about the record which stores "Good-bye Senna" as the ``_key``
is 3.

So the expression ``1 << 3`` is evaluated and stored to ``_score`` column as the execution result.

Bitwise signed right shift assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column2 >>= column2``.

The operator performs signed right shift assignment operation on column1 by column2.

Bitwise unsigned right shift assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column1 >>>= column2``.

The operator performs unsigned right shift assignment operation on column1 by column2.

Bitwise AND assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column1 &= column2``.

The operator performs bitwise AND assignment operation on column1 by column2.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_and_assignment_operator.log
.. select Entries --output_columns _key,n_likes,_score --filter true --scorer '_score &= n_likes'

The value of ``_score`` by ``--filter`` is always 1 in this case,
then performs subtraction assignment operation such as '_score = _score & n_likes' for each records.

For example, the value of ``_score`` about the record which stores "Groonga" as the ``_key``
is 10.

So the expression ``1 & 10`` is evaluated and stored to ``_score`` column as the execution result.

.. _script-syntax-bitwise-or-assign:

Bitwise OR assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column1 |= column2``.

The operator performs bitwise OR assignment operation on column1 by column2.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_or_assignment_operator.log
.. select Entries --output_columns _key,n_likes,_score --filter true --scorer '_score |= n_likes'

The value of ``_score`` by ``--filter`` is always 1 in this case,
then performs subtraction assignment operation such as '_score = _score | n_likes' for each records.

For example, the value of ``_score`` about the record which stores "Groonga" as the ``_key``
is 10.

So the expression ``1 | 10`` is evaluated and stored to ``_score`` column as the execution result.

Bitwise XOR assignment operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column1 ^= column2``.

The operator performs bitwise XOR assignment operation on column1 by column2.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_xor_assignment_operator.log
.. select Entries --output_columns _key,n_likes,_score --filter true --scorer '_score ^= n_likes'

The value of ``_score`` by ``--filter`` is always 1 in this case,
then performs subtraction assignment operation such as '_score = _score ^ n_likes' for each records.

For example, the value of ``_score`` about the record which stores "Good-bye Senna" as the ``_key``
is 3.

So the expression ``1 ^ 3`` is evaluated and stored to ``_score`` column as the execution result.

Original operators
------------------

Script syntax adds the original binary opearators to ECMAScript
syntax. They operate search specific operations. They are starts with
``@`` or ``*``.

.. _script-syntax-match-operator:

Match operator
^^^^^^^^^^^^^^

Its syntax is ``column @ value``.

The operator searches ``value`` by inverted index of ``column``.
Normally, full text search is operated but tag search can be operated.
Because tag search is also implemented by inverted index.

:doc:`query_syntax` uses this operator by default.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_match_operator.log
.. select Entries --filter 'content @ "fast"' --output_columns content

The expression matches records that contain a word ``fast`` in
``content`` column value.

.. _script-syntax-prefix-search-operator:

Prefix search operator
^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column @^ value``.

The operator does prefix search with ``value``. Prefix search searches
records that contain a word that starts with ``value``.

You can use fast prefix search against a column. The column must be
indexed and index table must be patricia trie table
(``TABLE_PAT_KEY``) or double array trie table
(``TABLE_DAT_KEY``). You can also use fast prefix search against
``_key`` pseudo column of patricia trie table or double array trie
table. You don't need to index ``_key``.

Prefix search can be used with other table types but it causes all
records scan. It's not problem for small records but it spends more
time for large records.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_prefix_search_operator.log
.. select Entries --filter '_key @^ "Goo"' --output_columns _key

The expression matches records that contain a word that starts with
``Goo`` in ``_key`` pseudo column value. ``Good-bye Senna`` and
``Good-bye Tritonn`` are matched with the expression.

.. _script-syntax-suffix-search-operator:

Suffix search operator
^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column @$ value``.

This operator does suffix search with ``value``. Suffix search
searches records that contain a word that ends with ``value``.

You can use fast suffix search against a column. The column must be
indexed and index table must be patricia trie table
(``TABLE_PAT_KEY``) with ``KEY_WITH_SIS`` flag. You can also use fast
suffix search against ``_key`` pseudo column of patricia trie table
(``TABLE_PAT_KEY``) with ``KEY_WITH_SIS`` flag. You don't need to
index ``_key``. We recommended that you use index column based fast
suffix search instead of ``_key`` based fast suffix search. ``_key``
based fast suffix search returns automatically registered
substrings. (TODO: write document about suffix search and link to it
from here.)

.. note::

   Fast suffix search can be used only for non-ASCII characters such
   as hiragana in Japanese. You cannot use fast suffix search for
   ASCII character.

Suffix search can be used with other table types or patricia trie
table without ``KEY_WITH_SIS`` flag but it causes all records
scan. It's not problem for small records but it spends more time for
large records.

Here is a simple example. It uses fast suffix search for hiragana in
Japanese that is one of non-ASCII characters.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_suffix_search_operator.log
.. table_create Titles TABLE_NO_KEY
.. column_create Titles content COLUMN_SCALAR ShortText
.. table_create SuffixSearchTerms TABLE_PAT_KEY|KEY_WITH_SIS ShortText
.. column_create SuffixSearchTerms index COLUMN_INDEX Titles content
.. load --table Titles
.. [
.. {"content": "ぐるんが"},
.. {"content": "むるんが"},
.. {"content": "せな"},
.. {"content": "とりとん"}
.. ]
.. select Titles --query 'content:$んが'

The expression matches records that have value that ends with ``んが``
in ``content`` column value. ``ぐるんが`` and ``むるんが`` are matched
with the expression.

.. _script-syntax-near-search-operator:

Near search operator
^^^^^^^^^^^^^^^^^^^^

Its syntax is one of them::

  column *N "word1 word2 ..."
  column *N${MAX_INTERVAL} "word1 word2 ..."
  column *N${MAX_INTERVAL},${MAX_TOKEN_INTERVAL_1}|${MAX_TOKEN_INTERVAL_2}|... "word1 word2 ..."

Here are the examples of the second form::

  column *N29 "word1 word2 ..."
  column *N-1 "word1 word2 ..."

The first example means that ``29`` is used for the max interval.

The second example means that ``-1`` is used for the max interval.
``-1`` max interval means no limit.

Here are examples of the third form::

  column *N10,2|3 "word1 word2 word3"
  column *N10,2 "word1 word2 word3"

The first example means that ``2`` is used for the max interval of the
first interval and ``3`` is used for the max interval of the second
interval.

The second example means that ``2`` is used for the first max interval
of the first interval and ``-1`` is used for the max interval of the
second interval. Because the omitted max interval is treated as
``-1``.

The max intervals of each token (word) are described later.

The operator does near search with words ``word1 word2 ...``. Near
search searches records that contain the words and the words are
appeared in the specified order and the max interval.

The max interval is ``10`` by default. The unit of the max interval is
the number of characters in N-gram family tokenizers and the number of
words in morphological analysis family tokenizers.

However, ``TokenBigram`` doesn't split ASCII only word into tokens.
Because ``TokenBigram`` uses white-space-separate like tokenize method
for ASCII characters in this case.

So the unit for ASCII words with ``TokenBigram`` is the number of
words even if ``TokenBigram`` is a N-gram family tokenizer.

Note that an index column for full text search must be defined for
``column``.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_near_search_operator.log
.. select Entries --filter 'content *N "I fast"'      --output_columns content
.. select Entries --filter 'content *N "I Really"'    --output_columns content
.. select Entries --filter 'content *N "also Really"' --output_columns content

The first expression matches records that contain ``I`` and ``fast``
and the max interval of those words are in 10 words. So the record
that its content is ``I started to use Groonga. It's very fast!`` is matched. 
The number of words between ``I`` and ``fast`` is 7.

The second expression matches records that contain ``I`` and
``Really`` and the max interval of those words are in 10 words. So the
record that its content is ``I also started to use mroonga. It's also
very fast! Really fast!`` is not matched. The number of words between
``I`` and ``Really`` is 11.

The third expression matches records that contain ``also`` and
``Really`` and the max interval of those words are in 10 words. So
the record that its content is ``I also st arted to use mroonga. It's
also very fast! Really fast!`` is matched. The number of words between
``also`` and ``Really`` is 10.

.. versionadded:: 12.0.1
   The max intervals of each token.

You can specify the max intervals of each token. The default is no
limit. It means that all intervals of each token are valid as long as
the max interval is satisfied.

Here is an example that use ``2`` for the max interval of the first
interval and ``4`` for the max interval of the second interval::

    content *N10,2|4 "a b c"

``10`` is the max interval.

``|`` is the separator of the max intervals of each token.

This matches ``a x b x x x c``. But this doesn't match ``a x x b c``,
``a b x x x x c`` and so on because the former has ``3`` interval for
the first interval that is larger than ``2`` and the latter has ``5``
interval for the second interval that is later than ``4``.

Here is an example that specifies the max intervals of each token:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/near_search_operator_max_element_intervals.log
.. select Entries --filter 'content *N11,5|3 "first welcome post"'
.. select Entries --filter 'content *N11,4|3 "first welcome post"'

You can omit one or more intervals. Omitted intervals are treated as
``-1``. It means that ``*N11,5`` equals ``*N11,5|-1``. ``-1`` means
that no limit.

Here is an example that omits an interval:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/near_search_operator_max_element_intervals_omit.log
.. select Entries --filter 'content *N11,5 "first welcome post"'
.. select Entries --filter 'content *N11,5|-1 "first welcome post"'

You can specify extra intervals. They are just ignored:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/near_search_operator_max_element_intervals_extra.log
.. select Entries --filter 'content *N11,5|6|1|2|3 "first welcome post"'
.. select Entries --filter 'content *N11,5|6 "first welcome post"'

.. _script-syntax-near-phrase-search-operator:

Near phrase search operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is one of them::

  column *NP "phrase1 phrase2 ..."
  column *NP${MAX_INTERVAL} "phrase1 phrase2 ..."
  column *NP${MAX_INTERVAL},${ADDITIONAL_LAST_INTERVAL} "phrase1 phrase2 ..."
  column *NP${MAX_INTERVAL},${ADDITIONAL_LAST_INTERVAL},${MAX_PHRASE_INTERVAL_1}|${MAX_PHRASE_INTERVAL_2}|... "phrase1 phrase2 ..."

Here are examples of the second form::

  column *NP29 "phrase1 phrase2 ..."
  column *NP-1 "phrase1 phrase2 ..."

The first example means that ``29`` is used for the max interval.

The second example means that ``-1`` is used for the max interval.

The max interval is described later.

Here are examples of the third form::

  column *NP10,29 "phrase1 phrase2 ..."
  column *NP10,-1 "phrase1 phrase2 ..."

The first example means that ``29`` is used for the additional last
interval.

The second example means that ``-1`` is used for the additional last
interval.

The additional last interval is described later.

.. versionadded:: 12.0.1
   The max intervals of each phrase.

Here are examples of the forth form::

  column *NP10,0,2|3 "phrase1 phrase2 phrase3"
  column *NP10,0,2 "phrase1 phrase2 phrase3"

The first example means that ``2`` is used for the max interval of the
first interval and ``3`` is used for the max interval of the second
interval.

The second example means that ``2`` is used for the first max interval
of the first interval and ``-1`` is used for the max interval of the
second interval. Because the omitted max interval is treated as
``-1``.

See :ref:`script-syntax-near-phrase-search-operator` for the max
intervals of each phrase.

The operator does near phrase search with phrases ``phrase1 phrase2
...``. Near phrase search searches records that contain the phrases
and the phrases are appeared in the specified order and the max
interval.

The max interval is ``10`` by default. The unit of the max interval is
the number of characters in N-gram family tokenizers and the number of
words in morphological analysis family tokenizers.

However, ``TokenBigram`` doesn't split ASCII only word into tokens.
Because ``TokenBigram`` uses white-space-separate like tokenize method
for ASCII characters in this case.

So the unit for ASCII words with ``TokenBigram`` is the number of
words even if ``TokenBigram`` is a N-gram family tokenizer.

Note that an index column for full text search must be defined for
``column``.

TODO: Use index that has ``TokenNgram("unify_alphabet", false)``
tokenizer to show difference with near search with English text.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_near_phrase_search_operator.log
.. select Entries --filter 'content *NP "I fast"'      --output_columns content
.. select Entries --filter 'content *NP "I Really"'    --output_columns content
.. select Entries --filter 'content *NP "also Really"' --output_columns content

The first expression matches records that contain ``I`` and ``fast``
and the max interval of those words are in 10 words. So the record
that its content is ``I also started to use mroonga. It's also very
fast! ...`` is matched. The number of words between ``I`` and ``fast``
is just 10.

The second expression matches records that contain ``I`` and
``Really`` and the max interval of those words are in 10 words. So the
record that its content is ``I also started to use mroonga. It's also
very fast! Really fast!`` is not matched. The number of words between
``I`` and ``Really`` is 14.

The third expression matches records that contain ``also`` and
``Really`` and the max interval of those words are in 10 words. So
the record that its content is ``I also st arted to use mroonga. It's
also very fast! Really fast!`` is matched. The number of words between
``also`` and ``Really`` is 10.

Here is an example to use the custom max interval:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/near_phrase_search_operator_max_interval.log
.. select Entries --filter 'content *NP14 "I Really"' --output_columns content
.. select Entries --filter 'content *NP-1 "I Really"' --output_columns content

The first expression matches ``I also started to use
mroonga. It's also very fast! Really fast!`` because the number of
words between ``I`` and ``Really`` is 14.

The second expression also matches ``I also started to use
mroonga. It's also very fast! Really fast!`` because ``-1`` means that
there is no limitation the number of words between ``I`` and
``Really``.

You can use additional interval only for the last phrase. It means
that you can accept more distance only between the second to last
phrase and the last phrase. This is useful for implementing a near
phrase search in the same sentence. If you specify ``.`` (sentence end
phrase) as the last phrase and specify ``-1`` as the additional last
interval, the other specified phrases must be appeared before
``.``. You must append ``$`` to the last phrase like ``.$``.

Here is an example that uses ``-1`` as the additional last interval of
the given phrases::

    column *NP10,-1 "a b .$"

Here is an example to customize the additional last interval of the
given phrases:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/near_phrase_search_operator_additional_last_interval_negative.log
.. select Entries --filter 'content *NP1,-1 "I started .$"' --output_columns content

You can also use positive number for the additional last interval. If
you specify positive number as the additional last interval, all of
the following conditions must be satisfied:

1. The interval between the first phrase and the second to last
   phrase is less than or equals to ``the max interval``.

2. The interval between the first phrase and the last phrase is less
   than or equals to ``the max interval`` + ``the additional last
   interval``.

If you specify negative number as the additional last interval, the
second condition isn't required. Appearing the last phrase is just
needed.

Here is an example to use positive number as the additional last interval:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/near_phrase_search_operator_additional_last_interval_positive.log
.. select Entries --filter 'content *NP1,4 "I started .$"' --output_columns content

.. _script-syntax-near-phrase-product-search-operator:

Near phrase product search operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. versionadded:: 11.1.1

Its syntax is one of them::

  column *NPP "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."
  column *NPP${MAX_INTERVAL} "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."
  column *NPP${MAX_INTERVAL},${ADDITIONAL_LAST_INTERVAL} "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."
  column *NPP${MAX_INTERVAL},${ADDITIONAL_LAST_INTERVAL},${MAX_PHRASE_INTERVAL_1}|${MAX_PHRASE_INTERVAL_2}|... "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."

Here are examples of the second form::

  column *NPP29 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."
  column *NPP-1 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."

The first example means that ``29`` is used for the max interval.

The second example means that ``-1`` is used for the max interval.

Here are examples of the third form::

  column *NPP10,29 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."
  column *NPP10,-1 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."

The first example means that ``29`` is used for the additional last
interval.

The second example means that ``-1`` is used for the additional last
interval.

.. versionadded:: 12.0.1
   The max intervals of each phrase.

Here are examples of the forth form::

  column *NPP10,0,2|3 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) (phrase3-1 phrase3-2 ...)"
  column *NPP10,0,2 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) (phrase3-1 phrase3-2 ...)"

The first example means that ``2`` is used for the max interval of the
first interval and ``3`` is used for the max interval of the second
interval.

The second example means that ``2`` is used for the first max interval
of the first interval and ``-1`` is used for the max interval of the
second interval. Because the omitted max interval is treated as
``-1``.

See :ref:`script-syntax-near-phrase-search-operator` for the max
intervals of each phrase.

This operator does multiple
:ref:`script-syntax-near-phrase-search-operator`. Phrases for each
:ref:`script-syntax-near-phrase-search-operator` are computed as
product of ``{phrase1_1, phrase1_2, ...}``, ``{phrase2_1, phrase2_2,
...}`` and ``...``. For example, ``column *NPP "(a b c) (d e)"`` uses
the following phrases for near phrase searches:

* ``a d``
* ``a e``
* ``b d``
* ``b e``
* ``c d``
* ``c e``

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_near_phrase_product_search_operator.log
.. select Entries \
..   --filter 'content *NPP "(I It) (migrated fast)"' \
..   --output_columns content

You can use the all features of
:ref:`script-syntax-near-phrase-search-operator` such as the max
interval, ``$`` for the last phrase and the additional last interval.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/near_phrase_product_search_operator_options.log
.. select Entries \
..   --filter 'content *NPP2,-1 "(I It) (migrated fast) (.$)"' \
..   --output_columns content

This is more effective than multiple
:ref:`script-syntax-near-phrase-search-operator` .

.. _script-syntax-ordered-near-phrase-search-operator:

Ordered near phrase search operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. versionadded:: 11.0.9

Its syntax is one of them::

  column *ONP "phrase1 phrase2 ..."
  column *ONP${MAX_INTERVAL} "phrase1 phrase2 ..."
  column *ONP${MAX_INTERVAL},${ADDITIONAL_LAST_INTERVAL} "phrase1 phrase2 ..."
  column *ONP${MAX_INTERVAL},${ADDITIONAL_LAST_INTERVAL},${MAX_PHRASE_INTERVAL_1}|${MAX_PHRASE_INTERVAL_2}|... "phrase1 phrase2 ..."

Here are examples of the second form::

  column *ONP29 "phrase1 phrase2 ..."
  column *ONP-1 "phrase1 phrase2 ..."

The first example means that ``29`` is used for the max interval.

The second example means that ``-1`` is used for the max interval.

Here are examples of the third form::

  column *ONP10,29 "phrase1 phrase2 ..."
  column *ONP10,-1 "phrase1 phrase2 ..."

The first example means that ``29`` is used for the additional last
interval.

The second example means that ``-1`` is used for the additional last
interval.

.. versionadded:: 12.0.1
   The max intervals of each phrase.

Here are examples of the forth form::

  column *ONP10,0,2|3 "phrase1 phrase2 phrase3"
  column *ONP10,0,2 "phrase1 phrase2 phrase3"

The first example means that ``2`` is used for the max interval of the
first interval and ``3`` is used for the max interval of the second
interval.

The second example means that ``2`` is used for the first max interval
of the first interval and ``-1`` is used for the max interval of the
second interval. Because the omitted max interval is treated as
``-1``.

See :ref:`script-syntax-near-phrase-search-operator` for the max
intervals of each phrase.

This operator does ordered near phrase search with ``phrase1``,
``phrase2`` and ``...``. Ordered near phrase search is similar to
:ref:`script-syntax-near-phrase-search-operator` but ordered near
phrase search checks phrases order. For example, ``column *ONP
"groonga mroonga pgroonga"`` matches ``groonga mroonga rroonga
pgroonga`` but doesn't match ``groonga rroonga pgroonga
mroonga``. Because the latter uses different order.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_ordered_near_phrase_search_operator.log
.. select Entries \
..   --filter 'content *ONP "I Groonga"' \
..   --output_columns content
.. select Entries \
..   --filter 'content *ONP "Groonga I"' \
..   --output_columns content

You can use the all features of
:ref:`script-syntax-near-phrase-search-operator` such as the max
interval and the additional last interval. But you don't need to
specify ``$`` for the last phrase because the last phrase in query is
the last phrase.

.. _script-syntax-ordered-near-phrase-product-search-operator:

Ordered near phrase product search operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. versionadded:: 11.1.1

Its syntax is one of them::

  column *ONPP "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."
  column *ONPP${MAX_INTERVAL} "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."
  column *ONPP${MAX_INTERVAL},${ADDITIONAL_LAST_INTERVAL} "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."
  column *ONPP${MAX_INTERVAL},${ADDITIONAL_LAST_INTERVAL},${MAX_PHRASE_INTERVAL_1}|${MAX_PHRASE_INTERVAL_2}|... "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."

Here are examples of the second form::

  column *ONPP29 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."
  column *ONPP-1 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."

The first example means that ``29`` is used for the max interval.

The second example means that ``-1`` is used for the max interval.

Here are examples of the third form::

  column *ONPP10,29 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."
  column *ONPP10,-1 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) ..."

The first example means that ``29`` is used for the additional last
interval.

The second example means that ``-1`` is used for the additional last
interval.

.. versionadded:: 12.0.1
   The max intervals of each phrase.

Here are examples of the forth form::

  column *ONPP10,0,2|3 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) (phrase3-1 phrase3-2 ...)"
  column *ONPP10,0,2 "(phrase1-1 phrase1-2 ...) (phrase2-1 phrase2-2 ...) (phrase3-1 phrase3-2 ...)"

The first example means that ``2`` is used for the max interval of the
first interval and ``3`` is used for the max interval of the second
interval.

The second example means that ``2`` is used for the first max interval
of the first interval and ``-1`` is used for the max interval of the
second interval. Because the omitted max interval is treated as
``-1``.

See :ref:`script-syntax-near-phrase-search-operator` for the max
intervals of each phrase.

This operator does ordered near phrase product search. Ordered near
phrase product search is similar to
:ref:`script-syntax-near-phrase-product-search-operator` but ordered
near phrase product search checks phrases order like
:ref:`script-syntax-ordered-near-phrase-search-operator`. For example,
``column *ONPP "(a b c) (d e)"`` matches ``a 1 d`` but doesn't match
``d 1 a``. Because the latter uses different order.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_ordered_near_phrase_product_search_operator.log
.. select Entries \
..   --filter 'content *ONPP "(I It) (migrated fast) (.)"' \
..   --output_columns content

You can use the all features of
:ref:`script-syntax-near-phrase-search-operator` such as the max
interval and the additional last interval. But you don't need to
specify ``$`` for the last phrase because the last phrase in query is
the last phrase.

.. _script-syntax-similar-search-operator:

Similar search operator
^^^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``column *S "document"``.

The operator does similar search with document ``document``. Similar
search searches records that have similar content to
``document``.

Note that an index column for full text search must be defined for
``column``.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_similar_search_operator.log
.. select Entries --filter 'content *S "I migrated all Solr system!"' --output_columns content

The expression matches records that have similar content to ``I
migrated all Solr system!``. In this case, records that have ``I
migrated all XXX system!`` content are matched.

You should use ``TokenMecab`` tokenizer for similar search against Japanese documents.
Because ``TokenMecab`` will tokenize target documents to almost words, it improves similar search precision.

.. _script-syntax-term-extract-operator:

Term extract operator
^^^^^^^^^^^^^^^^^^^^^

Its syntax is ``_key *T "document"``.

The operator extracts terms from ``document``. Terms must be
registered as keys of the table of ``_key``.

Note that the table must be patricia trie (``TABLE_PAT_KEY``) or
double array trie (``TABLE_DAT_KEY``). You can't use hash table
(``TABLE_HASH_KEY``) and array (``TABLE_NO_KEY``) because they don't
support longest common prefix search. Longest common prefix search is
used to implement the operator.

Here is a simple example:

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_term_extract_operator.log
.. table_create Words TABLE_PAT_KEY ShortText --normalizer NormalizerAuto
.. load --table Words
.. [
.. {"_key": "groonga"},
.. {"_key": "mroonga"},
.. {"_key": "Senna"},
.. {"_key": "Tritonn"}
.. ]
.. select Words --filter '_key *T "Groonga is the successor project to Senna."' --output_columns _key

The expression extrcts terms that included in document ``Groonga is
the successor project to Senna.``. In this case, ``NormalizerAuto``
normalizer is specified to ``Words``. So ``Groonga`` can be extracted
even if it is loaded as ``groonga`` into ``Words``. All of extracted
terms are also normalized.

.. _script-syntax-regular-expression-operator:

Regular expression operator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. versionadded:: 5.0.1

Its syntax is ``column @~ "pattern"``.

The operator searches records by the regular expression
``pattern``. If a record's ``column`` value is matched to ``pattern``,
the record is matched.

``pattern`` must be valid regular expression syntax. See
:doc:`/reference/regular_expression` about regular expression syntax
details.

The following example uses ``.roonga`` as pattern. It matches
``Groonga``, ``Mroonga`` and so on.

.. groonga-command
.. include:: ../../example/reference/grn_expr/script_syntax/simple_regular_expression_operator.log
.. select Entries --filter 'content @~ ".roonga"'

In most cases, regular expression is evaluated sequentially. So it may
be slow against many records.

In some cases, Groonga evaluates regular expression by index. It's
very fast. See :doc:`/reference/regular_expression` for details.