File: introduction.html

package info (click to toggle)
groonga 11.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 121,576 kB
  • sloc: ansic: 801,209; javascript: 62,121; ruby: 46,206; cpp: 33,790; xml: 24,951; yacc: 13,333; sh: 7,776; python: 3,266; makefile: 2,315; perl: 133
file content (1069 lines) | stat: -rw-r--r-- 44,031 bytes parent folder | download
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


<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>4.1. Basic operations &#8212; Groonga v10.1.1-31-g1e46ba6 documentation</title>
    <link rel="stylesheet" href="../_static/groonga.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../_static/language_data.js"></script>
    
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="4.2. Remote access" href="network.html" />
    <link rel="prev" title="4. Tutorial" href="../tutorial.html" /> 
  </head><body>
<div class="header">
  <h1 class="title">
    <a id="top-link" href="../index.html">
      <span class="project">groonga</span>
      <span class="separator">-</span>
      <span class="description">An open-source fulltext search engine and column store.</span>
    </a>
  </h1>

  <div class="other-language-links">
    <ul>
      <li><a href="../../../ja/html/tutorial/introduction.html">日本語</a></li>
    </ul>
  </div>
</div>
  

    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="network.html" title="4.2. Remote access"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../tutorial.html" title="4. Tutorial"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Groonga v10.1.1-31-g1e46ba6 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../tutorial.html" accesskey="U"><span class="section-number">4. </span>Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href=""><span class="section-number">4.1. </span>Basic operations</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="basic-operations">
<h1><span class="section-number">4.1. </span>Basic operations<a class="headerlink" href="#basic-operations" title="Permalink to this headline">¶</a></h1>
<p>A Groonga package provides a C library (libgroonga) and a command line tool (groonga). This tutorial explains how to use the command line tool, with which you can create/operate databases, start a server, establish a connection with a server, etc.</p>
<div class="section" id="create-a-database">
<h2><span class="section-number">4.1.1. </span>Create a database<a class="headerlink" href="#create-a-database" title="Permalink to this headline">¶</a></h2>
<p>The first step to using Groonga is to create a new database. The following shows how to do it.</p>
<p>Form:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>groonga -n DB_PATH
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">-n</span></code> option specifies to create a new database and DB_PATH specifies the path of the new database. Actually, a database consists of a series of files and DB_PATH specifies the file which will be the entrance to the new database. DB_PATH also specifies the path prefix for other files. Note that database creation fails if DB_PATH points to an existing file (For example, <code class="docutils literal notranslate"><span class="pre">db</span> <span class="pre">open</span> <span class="pre">failed</span> <span class="pre">(DB_PATH):</span> <span class="pre">syscall</span> <span class="pre">error</span> <span class="pre">'DB_PATH'</span> <span class="pre">(File</span> <span class="pre">exists)</span></code>. You can operate an existing database in a way that is in the next chapter).</p>
<p>This command creates a new database and then enters into interactive mode in which Groonga prompts you to enter commands for operating that database. You can terminate this mode with Ctrl-d.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>% groonga -n /tmp/groonga-databases/introduction.db
</pre></div>
</div>
<p>After this database creation, you can find a series of files in /tmp/groonga-databases.</p>
</div>
<div class="section" id="operate-a-database">
<h2><span class="section-number">4.1.2. </span>Operate a database<a class="headerlink" href="#operate-a-database" title="Permalink to this headline">¶</a></h2>
<p>The following shows how to operate an existing database.</p>
<p>Form:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>groonga DB_PATH [COMMAND]
</pre></div>
</div>
<p>DB_PATH specifies the path of a target database. This command fails if the specified database does not exist.</p>
<p>If COMMAND is specified, Groonga executes COMMAND and returns the result. Otherwise, Groonga starts in interactive mode that reads commands from the standard input and executes them one by one. This tutorial focuses on the interactive mode.</p>
<p>Let’s see the status of a Groonga process by using a <a class="reference internal" href="../reference/commands/status.html"><span class="doc">status</span></a> command.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>% groonga /tmp/groonga-databases/introduction.db
status
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   {
#     &quot;uptime&quot;: 0,
#     &quot;max_command_version&quot;: 3,
#     &quot;start_time&quot;: 1514346060,
#     &quot;cache_hit_rate&quot;: 0.0,
#     &quot;version&quot;: &quot;7.0.9&quot;,
#     &quot;alloc_count&quot;: 13365,
#     &quot;command_version&quot;: 1,
#     &quot;starttime&quot;: 1514346060,
#     &quot;default_command_version&quot;: 1,
#     &quot;n_queries&quot;: 0
#   }
# ]
</pre></div>
</div>
<p>As shown in the above example, a command returns a JSON array. The first element contains an error code, execution time, etc. The second element is the result of an operation.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>You can format a JSON using additional tools. For example, <a class="reference external" href="https://github.com/michisu/grnwrap">grnwrap</a>, <a class="reference external" href="https://github.com/yoshihara/grnline">Grnline</a>, <a class="reference external" href="http://stedolan.github.io/jq/">jq</a> and so on.</p>
</div>
</div>
<div class="section" id="command-format">
<h2><span class="section-number">4.1.3. </span>Command format<a class="headerlink" href="#command-format" title="Permalink to this headline">¶</a></h2>
<p>Commands for operating a database accept arguments as follows:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Form_1: COMMAND VALUE_1 VALUE_2 ..

Form_2: COMMAND --NAME_1 VALUE_1 --NAME_2 VALUE_2 ..
</pre></div>
</div>
<p>In the first form, arguments must be passed in order. This kind of arguments are called positional arguments because the position of each argument determines its meaning.</p>
<p>In the second form, you can specify a parameter name with its value. So, the order of arguments is not defined. This kind of arguments are known as named parameters or keyword arguments.</p>
<p>If you want to specify a value which contains white-spaces or special characters, such as quotes and parentheses, please enclose the value with single-quotes or double-quotes.</p>
<p>For details, see also the paragraph of “command” in <a class="reference internal" href="../reference/executables/groonga.html"><span class="doc">groonga executable file</span></a>.</p>
</div>
<div class="section" id="basic-commands">
<h2><span class="section-number">4.1.4. </span>Basic commands<a class="headerlink" href="#basic-commands" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div><dl class="simple">
<dt><a class="reference internal" href="../reference/commands/status.html"><span class="doc">status</span></a></dt><dd><p>shows status of a Groonga process.</p>
</dd>
<dt><a class="reference internal" href="../reference/commands/table_list.html"><span class="doc">table_list</span></a></dt><dd><p>shows a list of tables in a database.</p>
</dd>
<dt><a class="reference internal" href="../reference/commands/column_list.html"><span class="doc">column_list</span></a></dt><dd><p>shows a list of columns in a table.</p>
</dd>
<dt><a class="reference internal" href="../reference/commands/table_create.html"><span class="doc">table_create</span></a></dt><dd><p>adds a table to a database.</p>
</dd>
<dt><a class="reference internal" href="../reference/commands/column_create.html"><span class="doc">column_create</span></a></dt><dd><p>adds a column to a table.</p>
</dd>
<dt><a class="reference internal" href="../reference/commands/select.html"><span class="doc">select</span></a></dt><dd><p>searches records from a table and shows the result.</p>
</dd>
<dt><a class="reference internal" href="../reference/commands/load.html"><span class="doc">load</span></a></dt><dd><p>inserts records to a table.</p>
</dd>
</dl>
</div></blockquote>
</div>
<div class="section" id="create-a-table">
<span id="tutorial-introduction-create-table"></span><h2><span class="section-number">4.1.5. </span>Create a table<a class="headerlink" href="#create-a-table" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="../reference/commands/table_create.html"><span class="doc">table_create</span></a> command creates a new table.</p>
<p>In most cases, a table has a primary key which must be specified with its data type and index type.</p>
<p>There are various data types such as integers, strings, etc. See also <a class="reference internal" href="../reference/types.html"><span class="doc">Data types</span></a> for more details. The index type determines the search performance and the availability of prefix searches. The details will be described later.</p>
<p>Let’s create a table. The following example creates a table with a primary key. The <code class="docutils literal notranslate"><span class="pre">name</span></code> parameter specifies the name of the table. The <code class="docutils literal notranslate"><span class="pre">flags</span></code> parameter specifies the index type for the primary key. The <code class="docutils literal notranslate"><span class="pre">key_type</span></code> parameter specifies the data type of the primary key.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>table_create --name Site --flags TABLE_HASH_KEY --key_type ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
</pre></div>
</div>
<p>The second element of the result indicates that the operation succeeded.</p>
</div>
<div class="section" id="view-a-table">
<h2><span class="section-number">4.1.6. </span>View a table<a class="headerlink" href="#view-a-table" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="../reference/commands/select.html"><span class="doc">select</span></a> command can enumerate records in a table.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         0
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ]
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>When only a table name is specified with a <code class="docutils literal notranslate"><span class="pre">table</span></code> parameter, a <a class="reference internal" href="../reference/commands/select.html"><span class="doc">select</span></a> command returns the first (at most) 10 records in the table. [0] in the result shows the number of records in the table. The next array is a list of columns. [“_id”,”Uint32”] is a column of UInt32, named _id. [“_key”,”ShortText”] is a column of ShortText, named _key.</p>
<p>The above two columns, _id and _key, are the necessary columns. The _id column stores IDs those are automatically allocated by Groonga. The _key column is associated with the primary key. You are not allowed to rename these columns.</p>
</div>
<div class="section" id="create-a-column">
<h2><span class="section-number">4.1.7. </span>Create a column<a class="headerlink" href="#create-a-column" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="../reference/commands/column_create.html"><span class="doc">column_create</span></a> command creates a new column.</p>
<p>Let’s add a column. The following example adds a column to the Site table. The <code class="docutils literal notranslate"><span class="pre">table</span></code> parameter specifies the target table. The <code class="docutils literal notranslate"><span class="pre">name</span></code> parameter specifies the name of the column. The <code class="docutils literal notranslate"><span class="pre">type</span></code> parameter specifies the data type of the column.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>column_create --table Site --name title --type ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
select --table Site
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         0
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
</div>
<div class="section" id="load-records">
<h2><span class="section-number">4.1.8. </span>Load records<a class="headerlink" href="#load-records" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="../reference/commands/load.html"><span class="doc">load</span></a> command loads JSON-formatted records into a table.</p>
<p>The following example loads nine records into the Site table.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>load --table Site
[
{&quot;_key&quot;:&quot;http://example.org/&quot;,&quot;title&quot;:&quot;This is test record 1!&quot;},
{&quot;_key&quot;:&quot;http://example.net/&quot;,&quot;title&quot;:&quot;test record 2.&quot;},
{&quot;_key&quot;:&quot;http://example.com/&quot;,&quot;title&quot;:&quot;test test record three.&quot;},
{&quot;_key&quot;:&quot;http://example.net/afr&quot;,&quot;title&quot;:&quot;test record four.&quot;},
{&quot;_key&quot;:&quot;http://example.org/aba&quot;,&quot;title&quot;:&quot;test test test record five.&quot;},
{&quot;_key&quot;:&quot;http://example.com/rab&quot;,&quot;title&quot;:&quot;test test test test record six.&quot;},
{&quot;_key&quot;:&quot;http://example.net/atv&quot;,&quot;title&quot;:&quot;test test test record seven.&quot;},
{&quot;_key&quot;:&quot;http://example.org/gat&quot;,&quot;title&quot;:&quot;test test record eight.&quot;},
{&quot;_key&quot;:&quot;http://example.com/vdw&quot;,&quot;title&quot;:&quot;test test record nine.&quot;},
]
# [[0, 1337566253.89858, 0.000355720520019531], 9]
</pre></div>
</div>
<p>The second element of the result indicates how many records were successfully loaded. In this case, all the records are successfully loaded.</p>
<p>Let’s make sure that these records are correctly stored.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         9
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;http://example.org/&quot;,
#         &quot;This is test record 1!&quot;
#       ],
#       [
#         2,
#         &quot;http://example.net/&quot;,
#         &quot;test record 2.&quot;
#       ],
#       [
#         3,
#         &quot;http://example.com/&quot;,
#         &quot;test test record three.&quot;
#       ],
#       [
#         4,
#         &quot;http://example.net/afr&quot;,
#         &quot;test record four.&quot;
#       ],
#       [
#         5,
#         &quot;http://example.org/aba&quot;,
#         &quot;test test test record five.&quot;
#       ],
#       [
#         6,
#         &quot;http://example.com/rab&quot;,
#         &quot;test test test test record six.&quot;
#       ],
#       [
#         7,
#         &quot;http://example.net/atv&quot;,
#         &quot;test test test record seven.&quot;
#       ],
#       [
#         8,
#         &quot;http://example.org/gat&quot;,
#         &quot;test test record eight.&quot;
#       ],
#       [
#         9,
#         &quot;http://example.com/vdw&quot;,
#         &quot;test test record nine.&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
</div>
<div class="section" id="get-a-record">
<h2><span class="section-number">4.1.9. </span>Get a record<a class="headerlink" href="#get-a-record" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="../reference/commands/select.html"><span class="doc">select</span></a> command can search records in a table.</p>
<p>If a search condition is specified with a <code class="docutils literal notranslate"><span class="pre">query</span></code> parameter, a <a class="reference internal" href="../reference/commands/select.html"><span class="doc">select</span></a> command searches records matching the search condition and returns the matched records.</p>
<p>Let’s get a record having a specified record ID. The following example gets the first record in the Site table. More precisely, the <code class="docutils literal notranslate"><span class="pre">query</span></code> parameter specifies a record whose _id column stores 1.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --query _id:1
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         1
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;http://example.org/&quot;,
#         &quot;This is test record 1!&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>Next, let’s get a record having a specified key. The following example gets the record whose primary key is “<a class="reference external" href="http://example.org/">http://example.org/</a>”. More precisely, the <code class="docutils literal notranslate"><span class="pre">query</span></code> parameter specifies a record whose _key column stores “<a class="reference external" href="http://example.org/">http://example.org/</a>”.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --query &#39;_key:&quot;http://example.org/&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         1
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;http://example.org/&quot;,
#         &quot;This is test record 1!&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
</div>
<div class="section" id="create-a-lexicon-table-for-full-text-search">
<h2><span class="section-number">4.1.10. </span>Create a lexicon table for full text search<a class="headerlink" href="#create-a-lexicon-table-for-full-text-search" title="Permalink to this headline">¶</a></h2>
<p>Let’s go on to how to make full text search.</p>
<p>Groonga uses an inverted index to provide fast full text search. So, the first step is to create a lexicon table which stores an inverted index, also known as postings lists. The primary key of this table is associated with a vocabulary made up of index terms and each record stores postings lists for one index term.</p>
<p>The following shows a command which creates a lexicon table named Terms. The data type of its primary key is ShortText.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>table_create --name Terms --flags TABLE_PAT_KEY --key_type ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
# [[0, 1337566253.89858, 0.000355720520019531], true]
</pre></div>
</div>
<p>The <a class="reference internal" href="../reference/commands/table_create.html"><span class="doc">table_create</span></a> command takes many parameters but you don’t need to understand all of them. Please skip the next paragraph if you are not interested in how it works.</p>
<p>The TABLE_PAT_KEY flag specifies to store index terms in a patricia trie. The <code class="docutils literal notranslate"><span class="pre">default_tokenizer</span></code> parameter specifies the method for tokenizing text. This example uses TokenBigram that is generally called N-gram.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">normalizer</span></code> parameter specifies to normalize index terms.</p>
</div>
<div class="section" id="create-an-index-column-for-full-text-search">
<h2><span class="section-number">4.1.11. </span>Create an index column for full text search<a class="headerlink" href="#create-an-index-column-for-full-text-search" title="Permalink to this headline">¶</a></h2>
<p>The second step is to create an index column, which allows you to search records from its associated column. That is to say this step specifies which column needs an index.</p>
<p>Let’s create an index column. The following example creates an index column for a column in the Site table.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>column_create --table Terms --name blog_title --flags COLUMN_INDEX|WITH_POSITION --type Site --source title
# [[0, 1337566253.89858, 0.000355720520019531], true]
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">table</span></code> parameter specifies the index table and the <code class="docutils literal notranslate"><span class="pre">name</span></code> parameter specifies the index column. The <code class="docutils literal notranslate"><span class="pre">type</span></code> parameter specifies the target table and the <code class="docutils literal notranslate"><span class="pre">source</span></code> parameter specifies the target column. The COLUMN_INDEX flag specifies to create an index column and the WITH_POSITION flag specifies to create a full inverted index, which contains the positions of each index term. This combination, COLUMN_INDEX|WITH_POSITION, is recommended for the general purpose.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>You can create a lexicon table and index columns before/during/after loading records. If a target column already has records, Groonga creates an inverted index in a static manner. In contrast, if you load records into an already indexed column, Groonga updates the inverted index in a dynamic manner.</p>
</div>
</div>
<div class="section" id="full-text-search">
<h2><span class="section-number">4.1.12. </span>Full text search<a class="headerlink" href="#full-text-search" title="Permalink to this headline">¶</a></h2>
<p>It’s time. You can make full text search with a <a class="reference internal" href="../reference/commands/select.html"><span class="doc">select</span></a> command.</p>
<p>A query for full text search is specified with a <code class="docutils literal notranslate"><span class="pre">query</span></code> parameter. The following example searches records whose “title” column contains “this”. The ‘&#64;’ specifies to make full text search. Note that a lower case query matches upper case and capitalized terms in a record if NormalizerAuto was specified when creating a lexcon table.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --query title:@this
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         1
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;http://example.org/&quot;,
#         &quot;This is test record 1!&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>In this example, the first record matches the query because its title contains “This”, that is the capitalized form of the query.</p>
<p>A <a class="reference internal" href="../reference/commands/select.html"><span class="doc">select</span></a> command accepts an optional parameter, named <cite>match_columns</cite>, that specifies the default target columns. This parameter is used if target columns are not specified in a query. <a class="footnote-reference brackets" href="#id2" id="id1">1</a></p>
<p>The combination of “<cite>–match_columns</cite> title” and “<cite>–query</cite> this” brings you the same result that “<cite>–query</cite> title:&#64;this” does.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --match_columns title --query this
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         1
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;http://example.org/&quot;,
#         &quot;This is test record 1!&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
</div>
<div class="section" id="specify-output-columns">
<h2><span class="section-number">4.1.13. </span>Specify output columns<a class="headerlink" href="#specify-output-columns" title="Permalink to this headline">¶</a></h2>
<p>An <code class="docutils literal notranslate"><span class="pre">output_columns</span></code> parameter of a <a class="reference internal" href="../reference/commands/select.html"><span class="doc">select</span></a> command specifies columns to appear in the search result. If you want to specify more than one columns, please separate column names by commas (‘,’).</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --output_columns _key,title,_score --query title:@test
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         9
#       ],
#       [
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;_score&quot;,
#           &quot;Int32&quot;
#         ]
#       ],
#       [
#         &quot;http://example.org/&quot;,
#         &quot;This is test record 1!&quot;,
#         1
#       ],
#       [
#         &quot;http://example.net/&quot;,
#         &quot;test record 2.&quot;,
#         1
#       ],
#       [
#         &quot;http://example.com/&quot;,
#         &quot;test test record three.&quot;,
#         2
#       ],
#       [
#         &quot;http://example.net/afr&quot;,
#         &quot;test record four.&quot;,
#         1
#       ],
#       [
#         &quot;http://example.org/aba&quot;,
#         &quot;test test test record five.&quot;,
#         3
#       ],
#       [
#         &quot;http://example.com/rab&quot;,
#         &quot;test test test test record six.&quot;,
#         4
#       ],
#       [
#         &quot;http://example.net/atv&quot;,
#         &quot;test test test record seven.&quot;,
#         3
#       ],
#       [
#         &quot;http://example.org/gat&quot;,
#         &quot;test test record eight.&quot;,
#         2
#       ],
#       [
#         &quot;http://example.com/vdw&quot;,
#         &quot;test test record nine.&quot;,
#         2
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>This example specifies three output columns including the _score column, which stores the relevance score of each record.</p>
</div>
<div class="section" id="specify-output-ranges">
<h2><span class="section-number">4.1.14. </span>Specify output ranges<a class="headerlink" href="#specify-output-ranges" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="../reference/commands/select.html"><span class="doc">select</span></a> command returns a part of its search result if <code class="docutils literal notranslate"><span class="pre">offset</span></code> and/or <code class="docutils literal notranslate"><span class="pre">limit</span></code> parameters are specified. These parameters are useful to paginate a search result, a widely-used interface which shows a search result on a page by page basis.</p>
<p>An <code class="docutils literal notranslate"><span class="pre">offset</span></code> parameter specifies the starting point and a <code class="docutils literal notranslate"><span class="pre">limit</span></code> parameter specifies the maximum number of records to be returned. If you need the first record in a search result, the offset parameter must be 0 or omitted.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --offset 0 --limit 3
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         9
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;http://example.org/&quot;,
#         &quot;This is test record 1!&quot;
#       ],
#       [
#         2,
#         &quot;http://example.net/&quot;,
#         &quot;test record 2.&quot;
#       ],
#       [
#         3,
#         &quot;http://example.com/&quot;,
#         &quot;test test record three.&quot;
#       ]
#     ]
#   ]
# ]
select --table Site --offset 3 --limit 3
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         9
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         4,
#         &quot;http://example.net/afr&quot;,
#         &quot;test record four.&quot;
#       ],
#       [
#         5,
#         &quot;http://example.org/aba&quot;,
#         &quot;test test test record five.&quot;
#       ],
#       [
#         6,
#         &quot;http://example.com/rab&quot;,
#         &quot;test test test test record six.&quot;
#       ]
#     ]
#   ]
# ]
select --table Site --offset 7 --limit 3
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         9
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         8,
#         &quot;http://example.org/gat&quot;,
#         &quot;test test record eight.&quot;
#       ],
#       [
#         9,
#         &quot;http://example.com/vdw&quot;,
#         &quot;test test record nine.&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
</div>
<div class="section" id="sort-a-search-result">
<h2><span class="section-number">4.1.15. </span>Sort a search result<a class="headerlink" href="#sort-a-search-result" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="../reference/commands/select.html"><span class="doc">select</span></a> command sorts its result when used with a <code class="docutils literal notranslate"><span class="pre">sort_keys</span></code> parameter.</p>
<p>A <code class="docutils literal notranslate"><span class="pre">sort_keys</span></code> parameter specifies a column as a sorting creteria. A search result is arranged in ascending order of the column values. If you want to sort a search result in reverse order, please add a leading hyphen (‘-‘) to the column name in a parameter.</p>
<p>The following example shows records in the Site table in reverse order.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --sort_keys -_id
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         9
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_key&quot;,
#           &quot;ShortText&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         9,
#         &quot;http://example.com/vdw&quot;,
#         &quot;test test record nine.&quot;
#       ],
#       [
#         8,
#         &quot;http://example.org/gat&quot;,
#         &quot;test test record eight.&quot;
#       ],
#       [
#         7,
#         &quot;http://example.net/atv&quot;,
#         &quot;test test test record seven.&quot;
#       ],
#       [
#         6,
#         &quot;http://example.com/rab&quot;,
#         &quot;test test test test record six.&quot;
#       ],
#       [
#         5,
#         &quot;http://example.org/aba&quot;,
#         &quot;test test test record five.&quot;
#       ],
#       [
#         4,
#         &quot;http://example.net/afr&quot;,
#         &quot;test record four.&quot;
#       ],
#       [
#         3,
#         &quot;http://example.com/&quot;,
#         &quot;test test record three.&quot;
#       ],
#       [
#         2,
#         &quot;http://example.net/&quot;,
#         &quot;test record 2.&quot;
#       ],
#       [
#         1,
#         &quot;http://example.org/&quot;,
#         &quot;This is test record 1!&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>The next example uses the _score column as the sorting criteria for ranking the search result. The result is sorted in relevance order.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --query title:@test --output_columns _id,_score,title --sort_keys -_score
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         9
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_score&quot;,
#           &quot;Int32&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         6,
#         4,
#         &quot;test test test test record six.&quot;
#       ],
#       [
#         5,
#         3,
#         &quot;test test test record five.&quot;
#       ],
#       [
#         7,
#         3,
#         &quot;test test test record seven.&quot;
#       ],
#       [
#         8,
#         2,
#         &quot;test test record eight.&quot;
#       ],
#       [
#         3,
#         2,
#         &quot;test test record three.&quot;
#       ],
#       [
#         9,
#         2,
#         &quot;test test record nine.&quot;
#       ],
#       [
#         1,
#         1,
#         &quot;This is test record 1!&quot;
#       ],
#       [
#         4,
#         1,
#         &quot;test record four.&quot;
#       ],
#       [
#         2,
#         1,
#         &quot;test record 2.&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>If you want to specify more than one columns, please separate column names by commas (‘,’). In such a case, a search result is sorted in order of the values in the first column, and then records having the same values in the first column are sorted in order of the second column values.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --query title:@test --output_columns _id,_score,title --sort_keys -_score,_id
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         9
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;_score&quot;,
#           &quot;Int32&quot;
#         ],
#         [
#           &quot;title&quot;,
#           &quot;ShortText&quot;
#         ]
#       ],
#       [
#         6,
#         4,
#         &quot;test test test test record six.&quot;
#       ],
#       [
#         5,
#         3,
#         &quot;test test test record five.&quot;
#       ],
#       [
#         7,
#         3,
#         &quot;test test test record seven.&quot;
#       ],
#       [
#         3,
#         2,
#         &quot;test test record three.&quot;
#       ],
#       [
#         8,
#         2,
#         &quot;test test record eight.&quot;
#       ],
#       [
#         9,
#         2,
#         &quot;test test record nine.&quot;
#       ],
#       [
#         1,
#         1,
#         &quot;This is test record 1!&quot;
#       ],
#       [
#         2,
#         1,
#         &quot;test record 2.&quot;
#       ],
#       [
#         4,
#         1,
#         &quot;test record four.&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p class="rubric">footnote</p>
<dl class="footnote brackets">
<dt class="label" id="id2"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p>Currently, a <code class="docutils literal notranslate"><span class="pre">match_columns</span></code> parameter is available iff there exists an inverted index for full text search. A <code class="docutils literal notranslate"><span class="pre">match_columns</span></code> parameter for a regular column is not supported.</p>
</dd>
</dl>
</div>
</div>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">4.1. Basic operations</a><ul>
<li><a class="reference internal" href="#create-a-database">4.1.1. Create a database</a></li>
<li><a class="reference internal" href="#operate-a-database">4.1.2. Operate a database</a></li>
<li><a class="reference internal" href="#command-format">4.1.3. Command format</a></li>
<li><a class="reference internal" href="#basic-commands">4.1.4. Basic commands</a></li>
<li><a class="reference internal" href="#create-a-table">4.1.5. Create a table</a></li>
<li><a class="reference internal" href="#view-a-table">4.1.6. View a table</a></li>
<li><a class="reference internal" href="#create-a-column">4.1.7. Create a column</a></li>
<li><a class="reference internal" href="#load-records">4.1.8. Load records</a></li>
<li><a class="reference internal" href="#get-a-record">4.1.9. Get a record</a></li>
<li><a class="reference internal" href="#create-a-lexicon-table-for-full-text-search">4.1.10. Create a lexicon table for full text search</a></li>
<li><a class="reference internal" href="#create-an-index-column-for-full-text-search">4.1.11. Create an index column for full text search</a></li>
<li><a class="reference internal" href="#full-text-search">4.1.12. Full text search</a></li>
<li><a class="reference internal" href="#specify-output-columns">4.1.13. Specify output columns</a></li>
<li><a class="reference internal" href="#specify-output-ranges">4.1.14. Specify output ranges</a></li>
<li><a class="reference internal" href="#sort-a-search-result">4.1.15. Sort a search result</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../tutorial.html"
                        title="previous chapter"><span class="section-number">4. </span>Tutorial</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="network.html"
                        title="next chapter"><span class="section-number">4.2. </span>Remote access</a></p>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="network.html" title="4.2. Remote access"
             >next</a> |</li>
        <li class="right" >
          <a href="../tutorial.html" title="4. Tutorial"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Groonga v10.1.1-31-g1e46ba6 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../tutorial.html" ><span class="section-number">4. </span>Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href=""><span class="section-number">4.1. </span>Basic operations</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2009-2021, Brazil, Inc.
    </div>
  </body>
</html>