.. -*- rst -*- .. highlightlang:: none .. groonga-command .. database: tutorial .. table_remove Site .. table_remove Terms Fundamental operation ===================== You can use groonga as a library of programming language C or an executable file. This tutorial explains how to use groonga as an executable file. Using its file, you can create and operate databases, start and connect to server, and so on. Create database --------------- You can create a new database in the following command. Form :: groonga -n DB_PATH_NAME '-n' option specifies to create a database. DB_PATH_NAME specifies full-path of new database. Groonga starts as interactive mode after you create a database with this command, and so groonga accepts commands from standard input. This mode is terminated with Ctrl-d. Execution example:: % groonga -n /tmp/tutorial.db > ctrl-d % Operate database ---------------- Form :: groonga DB_PATH_NAME [COMMAND] DB_PATH_NAME specifies full-path of existing database. If COMMAND is specified, result of COMMAND is returned. With no COMMAND, this command starts groonga as interactive-mode. Groonga of this mode reads a command from standard input evaluates it repeatedly. This tutorial uses interactive-mode mainly. For example, we will run the `status` command. This command returns status of groonga's execution. .. groonga-command .. include:: ../example/tutorial/introduction-1.log .. .. % groonga -n /tmp/groonga-databases/introduction.db .. status The mentioned above, results of executed commands are generally JSON style. The first element in a array of JSON has information of error-code, execution time, and so on. The second element has a result of exectuted command. Commands -------- You can operate database with various commands via execution file of groonga or groonga server. There are forms of commands in the following:: Form1: COMMAND ARGUMENT1 ARGUMENT2 .. Form2: COMMAND --ARAGUMENT1 VALUE1 --ARGUMENT2 VALUE2 .. You can mix these forms in commands running. In Form2, if you want to specify a value including some spaces or symbols("'()/), you should enclose its value with single-quote or double-quote. For detail, you can see paragraph of "command" in :doc:`/executables/groonga`. Basicaly commands ----------------- :doc:`/commands/status` Show status of groonga process. :doc:`/commands/table_list` Show lists of tables defined in a database. :doc:`/commands/column_list` Show lists of columns defined in a table. :doc:`/commands/table_create` Add table to a database. :doc:`/commands/column_create` Add column to a table. :doc:`/commands/select` Search and show records included a table. :doc:`/commands/load` Insert record to a table. Create table ------------ :doc:`/commands/table_create` creates table. In using groonga, to creating tables generally needed master key. Master key should be specified the types and the way to store. We're going to explain the types in tutorial after. Please imagine it as expressing sort of data. How to store master key defines speed of search with master key and advisability of begins-with-match search. This is also explained in this tutorial later. For example, we create 'Site' table. This table has master key of ShortText type, and the way to store its key is HASH. .. groonga-command .. include:: ../example/tutorial/introduction-2.log .. table_create --name Site --flags TABLE_HASH_KEY --key_type ShortText Search ------ :doc:`/commands/select` shows contents of table. .. groonga-command .. include:: ../example/tutorial/introduction-3.log .. select --table Site With name of a table, 'select' command shows 10 contents of its table. [0] shows the number of searched records. ["_id","Uint32"] is column named "_id" and type of this column's value is UInt32. ["_key","ShortText"] is "_key" column, type of this column's value is ShortText. 'table_create' command creates table including two columns, '_id' and '_key' first. '_id' has ID-number given automatically by groonga. '_key' column is stored master key. You cannot modify this column's name. Create columns -------------- :doc:`/commands/column_create` command create columns. We add a column named 'comment' that lets us store value whose type is ShortText. .. groonga-command .. include:: ../example/tutorial/introduction-4.log .. column_create --table Site --name title --flags COLUMN_SCALAR --type ShortText .. select --table Site COLUMN_SCALAR means this is normal column. Create terminology table with fulltext-searching ------------------------------------------------ This tutorial explains fulltext searching with entried data in groonga table. We need terminology table in fulltext-searching. Terminology table is a table whose master key's values are words in text. We create 'Terms' table, it has type of master key value is ShortText. .. groonga-command .. include:: ../example/tutorial/introduction-5.log .. table_create --name Terms --flags TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText --default_tokenizer TokenBigram Many parameters is specified in this execution example. You don't hove to understand all parameters. There are the simple explaination, but you can skipped. In this examples, 'TABLE_PAT_KEY|KEY_NORMALIZE' stores master key in patricia-trie and entries each teminology after nomalized. The 'default_tokenizer' parametar specifies the way to tokenize target texts. In this examples, we specifies 'TokenBigram' as this parameter, and so we choose 'N-gram' generally called. Create index-column with fulltext search ---------------------------------------- We will fulltext search 'title' column in 'Site' table. In this case, we create column whose type index in terminology table. .. groonga-command .. include:: ../example/tutorial/introduction-6.log .. column_create --table Terms --name blog_title --flags COLUMN_INDEX|WITH_POSITION --type Site --source title This command creates index column 'blog_title' in 'Term' table. '--type' option specifies target indexed table, and '--source' option does target index column. In execution example, 'COLUMN_INDEX|WITH_POSITION' for '--flags' option specifies that this column is index column for storing information of terminology existing position. This option should be specified 'COLUMN_INDEX|WITH_POSITION' in generally fulltext searching. This tutorial does not deal with the reason why store information of terminology existing position. Load data --------- :doc:`/commands/load` is used to load data for groonga database. This command stores json-formatted data in a table. .. groonga-command .. include:: ../example/tutorial/introduction-7.log .. load --table Site .. [ .. {"_key":"http://example.org/","title":"This is test record 1!"}, .. {"_key":"http://example.net/","title":"test record 2."}, .. {"_key":"http://example.com/","title":"test test record three."}, .. {"_key":"http://example.net/afr","title":"test record four."}, .. {"_key":"http://example.org/aba","title":"test test test record five."}, .. {"_key":"http://example.com/rab","title":"test test test test record six."}, .. {"_key":"http://example.net/atv","title":"test test test record seven."}, .. {"_key":"http://example.org/gat","title":"test test record eight."}, .. {"_key":"http://example.com/vdw","title":"test test record nine."}, .. ] Let's make sure that its table has data with 'select' command. .. groonga-command .. include:: ../example/tutorial/introduction-8.log .. select --table Site Search data ----------- '_id' and '_key' columns are unique in groonga's table, so let's search data in table using these columns. You can search data using 'select' command with 'query' parameter. .. groonga-command .. include:: ../example/tutorial/introduction-9.log .. select --table Site --query _id:1 '_id:1' specified 'query' parameter means to search records whose '_id' column has '1'. Let's search records with '_key' column. .. groonga-command .. include:: ../example/tutorial/introduction-10.log .. select --table Site --query "_key:\"http://example.org/\"" '_key:\"http://example.org/\"' specified 'query' parameter means to search records whose '_key' column has '"http://example.org/"'. Fulltext searching ------------------ Using 'query' parameter, you can fulltext search with index. .. groonga-command .. include:: ../example/tutorial/introduction-11.log .. select --table Site --query title:@this This command shows result of fulltext searching by string 'this' for 'title' column. "title:@this" specified 'query' parameter means to search records whose 'title' column including 'this' string. 'select' command has parameter 'match_columns'. If this parameter is specified, it means to search in columns specified 'match_columns' when 'query' parameter doesn't specify column-name condition.[1]_ If you specify 'match_columns' is 'title' and 'query' is 'this', you can take same result as above query. .. groonga-command .. include:: ../example/tutorial/introduction-12.log .. select --table Site --match_columns title --query this Specify output column --------------------- 'output_columns' parameter in 'select' command specifies columns shown in result of search. If you want to specify some columns, you should separate column names by comma(,). .. groonga-command .. include:: ../example/tutorial/introduction-13.log .. select --table Site --output_columns _key,title,_score --query title:@test "_score" column is added to The groonga's result. This column has the higher number, the more condition of fulltext seaching matches text. Ranges to display ----------------- 'select' command can display result in only specified ranges using 'offset' and 'limit' parameter. This parameters is useful when you want to show only a page in much result of searching. 'offset' parameter specifies starting point of result. If you want 'select' command to return from first records, this parameter specifies '0'. 'limit' parameter specifies how many records of searching result. .. groonga-command .. include:: ../example/tutorial/introduction-14.log .. select --table Site --offset 0 --limit 3 .. select --table Site --offset 3 --limit 3 .. select --table Site --offset 7 --limit 3 Sort ----- If you use 'sortby' parameter in 'select' command, this command sorts result of searching. When 'sortby' parameter specifies column name, result is sorted in ascending-order to its column's value. This 'select' command also sort in descending-order when you add hyphen(-) before column name. .. groonga-command .. include:: ../example/tutorial/introduction-15.log .. select --table Site --sortby -_id For condition of sort, you can use '_score' column introduced in the paragraph of "Specify output column". .. groonga-command .. include:: ../example/tutorial/introduction-16.log .. select --table Site --query title:@test --output_columns _id,_score,title --sortby _score If you want to specify some column names, you should use comma(,) between these names. In this case, when same value of records is existed in first column, this command sorts result of searching to value of second column. .. groonga-command .. include:: ../example/tutorial/introduction-17.log .. select --table Site --query title:@test --output_columns _id,_score,title --sortby _score,_id .. rubric:: footnote .. [1] In now groonga's version, you can only use 'match_columns' parameter in the case of existing index of fulltext searching. This parameter cannot be use in searching for ordinary columns.