<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>4.1. 基本的な操作 — groonga v3.0.5ドキュメント</title> <link rel="stylesheet" href="../_static/groonga.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '3.0.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/translations.js"></script> <link rel="shortcut icon" href="../_static/favicon.ico"/> <link rel="top" title="groonga v3.0.5ドキュメント" href="../index.html" /> <link rel="up" title="4. チュートリアル" href="../tutorial.html" /> <link rel="next" title="4.2. リモートアクセス" href="network.html" /> <link rel="prev" title="4. チュートリアル" 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">オープンソースのカラムストア機能付き全文検索エンジン</span> </a> </h1> <div class="other-language-links"> <ul> <li><a href="../../../en/html/tutorial/introduction.html"><img src="../_static/us.png" alt="English">English page</a></li> </ul> </div> </div> <div class="related"> <h3>ナビゲーション</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="総合索引" accesskey="I">索引</a></li> <li class="right" > <a href="network.html" title="4.2. リモートアクセス" accesskey="N">次へ</a> |</li> <li class="right" > <a href="../tutorial.html" title="4. チュートリアル" accesskey="P">前へ</a> |</li> <li><a href="../index.html">groonga v3.0.5ドキュメント</a> »</li> <li><a href="../tutorial.html" accesskey="U">4. チュートリアル</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="basic-operations"> <h1>4.1. 基本的な操作<a class="headerlink" href="#basic-operations" title="このヘッドラインへのパーマリンク">¶</a></h1> <p>groongaには、Cのライブラリとして使用する方法と、groonga実行ファイルを通して使用する方法があります。本チュートリアルでは、groonga実行ファイルを使用する方法について説明します。groonga実行ファイルを使って、データベースの作成・操作・サーバの起動・サーバへの接続などの操作が行えます。</p> <div class="section" id="create-a-database"> <h2>4.1.1. データベースの作成<a class="headerlink" href="#create-a-database" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>groongaユーザへの第一歩はデータベースの作成です。まずは以下の書式をご覧ください。</p> <p>書式:</p> <div class="highlight-none"><div class="highlight"><pre>groonga -n DB_PATH </pre></div> </div> <p><cite>-n</cite> オプションは、データベースの作成を指示します。DB_PATHは、新しく作成するデータベースのパスを指定します。データベースは複数のファイルによって構成されるため、正確には、データベースの入り口となるファイルのパスとして使用されます。また、データベースを構成する他のファイルについては、DB_PATHがパスのプレフィックスとして使用されます。指定されたパスにファイルが存在しているときは失敗するので注意してください。</p> <p>上記のコマンドは、データベースを作成してから、コマンドの入力を受け付ける対話モードに入ります。Ctrlキーを押しながらdキーを押すと、対話モードから抜けることができます。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>% groonga -n /tmp/groonga-databases/introduction.db </pre></div> </div> <p>データベースを作成に成功すれば、/tmp/groonga-databases以下にデータベースを構成するファイルが配置されます。</p> </div> <div class="section" id="operate-a-database"> <h2>4.1.2. データベースの操作<a class="headerlink" href="#operate-a-database" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>以下の書式は、既存のデータベースを操作する方法を示しています。</p> <p>書式:</p> <div class="highlight-none"><div class="highlight"><pre>groonga DB_PATH [COMMAND] </pre></div> </div> <p>DB_PATHには操作対象のデータベースを指定します。指定したデータベースが存在しないときは失敗します。</p> <p>COMMAND が指定された場合、COMMAND を実行した後、実行結果を返します。指定されなかった場合には、対話モードに入ります。対話モードでは、標準入力からコマンドを読み込み、順次実行します。本チュートリアルでは、対話モードを主に使用します。</p> <p>それでは、 <a class="reference internal" href="../reference/commands/status.html"><em>status</em></a> コマンドを実行して、groongaの実行状態を確認してみましょう。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>% groonga /tmp/groonga-databases/introduction.db status # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # { # "uptime": 0, # "max_command_version": 2, # "n_queries": 0, # "cache_hit_rate": 0.0, # "version": "3.0.5", # "alloc_count": 157, # "command_version": 1, # "starttime": 1372040812, # "default_command_version": 1 # } # ] </pre></div> </div> <p>以上のように、コマンドの実行結果は基本的にjson形式の配列として返却されます。配列の先頭には、エラーコードや実行時間などの情報が入ります。2番目の要素には、指示された操作の実行結果が入ります。</p> </div> <div class="section" id="command-format"> <h2>4.1.3. コマンドの書式<a class="headerlink" href="#command-format" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>データベースを操作するコマンドには、以下の書式で引数を与えます。:</p> <div class="highlight-none"><div class="highlight"><pre>Form_1: COMMAND VALUE_1 VALUE_2 .. Form_2: COMMAND --NAME_1 VALUE_1 --NAME_2 VALUE_2 .. </pre></div> </div> <p>書式1では値を適切な順番で渡す必要があります。このような引数は、位置によって値の意味が決まるため、位置固定引数などと呼ばれることもあります。</p> <p>書式2では値を名前と一緒に渡します。そのため、任意の順序で引数を指定することができます。このような引数は、名前付き引数やキーワード引数と呼ばれることもあります。</p> <p>空白や特殊な記号(ダブルクォート、シングルクォート、および丸括弧)を含む値を指定したいときは、シングルクォート(')かダブルクォート(")で値を囲むようにしてください。</p> <p>詳しくは、 <a class="reference internal" href="../reference/executables/groonga.html"><em>groongaコマンド</em></a> のコマンドの項を参考にしてください。</p> </div> <div class="section" id="basic-commands"> <h2>4.1.4. 主なコマンド<a class="headerlink" href="#basic-commands" title="このヘッドラインへのパーマリンク">¶</a></h2> <blockquote> <div><dl class="docutils"> <dt><a class="reference internal" href="../reference/commands/status.html"><em>status</em></a></dt> <dd><p class="first last">groongaプロセスの状態を表示します。</p> </dd> <dt><a class="reference internal" href="../reference/commands/table_list.html"><em>table_list</em></a></dt> <dd><p class="first last">データベースに定義されているテーブルのリストを表示します。</p> </dd> <dt><a class="reference internal" href="../reference/commands/column_list.html"><em>column_list</em></a></dt> <dd><p class="first last">テーブルに定義されているカラムのリストを表示します。</p> </dd> <dt><a class="reference internal" href="../reference/commands/table_create.html"><em>table_create</em></a></dt> <dd><p class="first last">データベースにテーブルを追加します。</p> </dd> <dt><a class="reference internal" href="../reference/commands/column_create.html"><em>column_create</em></a></dt> <dd><p class="first last">テーブルにカラムを追加します。</p> </dd> <dt><a class="reference internal" href="../reference/commands/select.html"><em>select</em></a></dt> <dd><p class="first last">テーブルに含まれるレコードを検索して表示します。</p> </dd> <dt><a class="reference internal" href="../reference/commands/load.html"><em>load</em></a></dt> <dd><p class="first last">テーブルにレコードを挿入します。</p> </dd> </dl> </div></blockquote> </div> <div class="section" id="create-a-table"> <h2>4.1.5. テーブルの作成<a class="headerlink" href="#create-a-table" title="このヘッドラインへのパーマリンク">¶</a></h2> <p><a class="reference internal" href="../reference/commands/table_create.html"><em>table_create</em></a> コマンドを使用してテーブルを作成します。</p> <p>groongaのテーブルには基本的に主キーが必要であり、テーブルを作成する際には型と格納方法も併せて指定する必要があります。</p> <p>型には数値や文字列などがあります。ここではデータの種類を表しているものという程度に考えてください。詳細は <a class="reference internal" href="../reference/types.html"><em>データ型</em></a> に記述されています。主キーの格納方法は、主キーを条件とする検索にかかる時間や、前方一致検索の可否などを左右します。こちらも後で説明します。</p> <p>それでは、テーブルを作成してみましょう。以下の例では、主キーのあるテーブルを作成します。 <cite>name</cite> 引数はテーブルの引数を指定します。 <cite>flags</cite> 引数は主キーの格納方法を指定するために使っています。 <cite>key_type</cite> 引数は主キーの型を指定します。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>table_create --name Site --flags TABLE_HASH_KEY --key_type ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] </pre></div> </div> <p>実行結果の第2要素は、操作が成功したことを示しています。</p> </div> <div class="section" id="view-a-table"> <h2>4.1.6. テーブルの表示<a class="headerlink" href="#view-a-table" title="このヘッドラインへのパーマリンク">¶</a></h2> <p><a class="reference internal" href="../reference/commands/select.html"><em>select</em></a> コマンドを用いて、テーブルの中身を表示することができます。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 0 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ] # ] # ] # ] # ] </pre></div> </div> <p><a class="reference internal" href="../reference/commands/select.html"><em>select</em></a> にテーブル名のみを指定すると、指定したテーブルの中身を10件まで表示します。実行結果の[0]はテーブルに含まれるレコードの数を示しています。今回は何も登録されていないため0件です。レコード数の次に表示されている配列はテーブルの構成を示しています。["_id","Uint32"]はUInt32型の値を持つ_idという名前のカラム、["_key","ShortText"]はShortText型の値を持つ_keyという名前のカラムをそれぞれ表しています。</p> <p>上記の_idカラムと_keyカラムの2つのカラムは必須のカラムです。_idカラムはgroongaが自動的に割り当てるIDを格納します。_keyカラムは主キーを格納します。これらのカラム名を変更することはできません。</p> </div> <div class="section" id="create-a-column"> <h2>4.1.7. カラムの作成<a class="headerlink" href="#create-a-column" title="このヘッドラインへのパーマリンク">¶</a></h2> <p><a class="reference internal" href="../reference/commands/column_create.html"><em>column_create</em></a> コマンドを用いて、カラムを作成することができます。</p> <p>それでは、カラムを作成してみましょう。以下の例では、新しいカラムをSiteテーブルに追加します。 <cite>table</cite> 引数はテーブルの名前を指定します。 <cite>name</cite> 引数は新しいカラムの名前を指定します。 <cite>type</cite> 引数はカラムに格納する値の型を指定します。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>column_create --table Site --name title --type ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] select --table Site # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 0 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ] # ] # ] # ] </pre></div> </div> </div> <div class="section" id="load-records"> <h2>4.1.8. データのロード<a class="headerlink" href="#load-records" title="このヘッドラインへのパーマリンク">¶</a></h2> <p><a class="reference internal" href="../reference/commands/load.html"><em>load</em></a> コマンドは、JSON形式のレコードを受け取り、テーブルに格納します。</p> <p>以下の例では9つのレコードをSiteテーブルに格納します。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>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."}, ] # [[0, 1337566253.89858, 0.000355720520019531], 9] </pre></div> </div> <p>実行結果の第2要素はロードされたレコードの数を示しています。上記の操作では、すべてのレコードを問題なくロードできています。</p> <p>念のため、データが入っていることを確認してみましょう。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 9 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 1, # "http://example.org/", # "This is test record 1!" # ], # [ # 2, # "http://example.net/", # "test record 2." # ], # [ # 3, # "http://example.com/", # "test test record three." # ], # [ # 4, # "http://example.net/afr", # "test record four." # ], # [ # 5, # "http://example.org/aba", # "test test test record five." # ], # [ # 6, # "http://example.com/rab", # "test test test test record six." # ], # [ # 7, # "http://example.net/atv", # "test test test record seven." # ], # [ # 8, # "http://example.org/gat", # "test test record eight." # ], # [ # 9, # "http://example.com/vdw", # "test test record nine." # ] # ] # ] # ] </pre></div> </div> </div> <div class="section" id="get-a-record"> <h2>4.1.9. レコードの取得<a class="headerlink" href="#get-a-record" title="このヘッドラインへのパーマリンク">¶</a></h2> <p><a class="reference internal" href="../reference/commands/select.html"><em>select</em></a> コマンドを用いて、テーブルの中身を表示することができます。</p> <p><cite>query</cite> 引数を使って検索条件が指定された場合、 <a class="reference internal" href="../reference/commands/select.html"><em>select</em></a> コマンドは検索条件に適合するレコードを検索し、検索結果を出力します。</p> <p>それでは、IDを指定してレコードを取り出してみましょう。以下の例では、Siteテーブルの先頭レコードを取り出します。正確には、 <cite>query</cite> 引数を使って _id カラムに1が格納されているレコードを要求しています。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --query _id:1 # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 1, # "http://example.org/", # "This is test record 1!" # ] # ] # ] # ] </pre></div> </div> <p>次に、主キーを指定してレコードを取り出してみましょう。以下の例では、主キーが "<a class="reference external" href="http://example.org/">http://example.org/</a>" のキーを取り出します。正確には、 <cite>query</cite> 引数を使って _key カラムに "<a class="reference external" href="http://example.org/">http://example.org/</a>" が格納されているレコードを要求しています。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --query '_key:"http://example.org/"' # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 1, # "http://example.org/", # "This is test record 1!" # ] # ] # ] # ] </pre></div> </div> </div> <div class="section" id="create-a-lexicon-table-for-full-text-search"> <h2>4.1.10. 全文検索用の語彙表の作成<a class="headerlink" href="#create-a-lexicon-table-for-full-text-search" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>そろそろ全文検索の使い方について見ていきましょう。</p> <p>groongaでは転置インデックスを使って高速な全文検索を実現しています。そのため、まずは転置インデックスとして用いるテーブルを作成する必要があります。テーブルの内容は、文書に含まれる単語やN-gramなどの索引語を主キーとして、各カラムに索引語の位置情報を格納するという構成になります。結果として、主キーのカラムは全文検索における語彙表の役割を果たします。</p> <p>以下の例では、Termsという名前のテーブルを転置インデックスの語彙表として作成しています。索引語を格納するため、主キーの型はShortTextです。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>table_create --name Terms --flags TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText --default_tokenizer TokenBigram # [[0, 1337566253.89858, 0.000355720520019531], true] </pre></div> </div> <p><a class="reference internal" href="../reference/commands/table_create.html"><em>table_create</em></a> には多くの引数が指定されているものの、本チュートリアルではすべてを理解する必要はありません。以下に簡単な説明を述べますが、読み飛ばしてもらってかまいません。</p> <p>TABLE_PAT_KEYフラグは、主キーをパトリシア木に格納することを指示しています。KEY_NORMALIZEフラグは、索引語を正規化して登録することを指示しています。 <cite>default_tokenizer</cite> 引数には、検索対象の文書をトークナイズする方式を指定するようになっています。上記の例では、一般的にN-gramと呼ばれるインデックス方式に対応するTokenBigramを指定しています。</p> </div> <div class="section" id="create-an-index-column-for-full-text-search"> <h2>4.1.11. 全文検索用のインデックスカラムの作成<a class="headerlink" href="#create-an-index-column-for-full-text-search" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>次に必要なのは、インデックス型のカラムを作成することです。このカラムは、関連付けられたカラムに対する全文検索に利用されます。つまり、全文検索を行いたいカラムに対してインデックスを作成することに相当します。</p> <p>それでは、インデックスカラムを作成してみましょう。以下の例では、Siteテーブルのカラムに対するインデックスカラムを作成します。それでは、Siteテーブルのtitleカラムを全文検索の対象とするべく、インデックス型のカラムを作成してみましょう。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>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><cite>table</cite> 引数は語彙表を指定し、 <cite>name</cite> 引数はインデックスカラムを指定します。また、 <cite>type</cite> 引数はインデックスの対象となるテーブルを指定し、 <cite>source</cite> 引数はインデックスの対象となるカラムを指定します。COLUMN_INDEXフラグはインデックスカラムの作成を指示し、WITH_POSITIONフラグは各索引語の位置情報をインデックスに含めることを指示します。一般的な全文検索インデックスを作成したいときは、COLUMN_INDEX|WITH_POSITIONを指定してください。索引語の位置情報については、本チュートリアルでは触れません。</p> <div class="admonition note"> <p class="first admonition-title">ノート</p> <p class="last">語彙表とインデックスカラムを作成するタイミングは、データをロードする前後のどちらでも問題ありません。データをロードした後でインデックスを作成し、さらに新しいデータをロードすることもできます。インデックスの作成を指示したタイミングでレコードが既に存在するときは、静的にインデックスを作成します。一方、インデックスを作成した後で追加されたレコードについては、動的にインデックスを更新します。</p> </div> </div> <div class="section" id="full-text-search"> <h2>4.1.12. 全文検索<a class="headerlink" href="#full-text-search" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>インデックスを作成したことにより、 <a class="reference internal" href="../reference/commands/select.html"><em>select</em></a> コマンドによる全文検索が可能になります。</p> <p>全文検索のクエリは <cite>query</cite> 引数により指定することができます。以下の例では、titleカラムに "this" という文字列が含まれているレコードを検索します。 <cite>query</cite> 引数に含まれる '@' は、全文検索を指示しています。語彙表の作成において KEY_NORMALIZE フラグを指定したときは、全角・半角や大文字・小文字などの違いが吸収されることに注意してください。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --query title:@this # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 1, # "http://example.org/", # "This is test record 1!" # ] # ] # ] # ] </pre></div> </div> <p>上記の例では、"This" という単語を含む先頭レコードのみが検索条件に適合します。</p> <p><a class="reference internal" href="../reference/commands/select.html"><em>select</em></a> コマンドには、 <cite>match_columns</cite> という引数が存在します。このパラメータはデフォルトで検索対象にするカラムを指定するもので、カラム名を指定しない検索条件にのみ適用されます。 <a class="footnote-reference" href="#id2" id="id1">[1]</a></p> <p>"<cite>--match_columns</cite> title" と "<cite>--query</cite> this" の組み合わせを指定すると、 "<cite>--query</cite> title:@this" を指定したときと同じ検索条件になります。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --match_columns title --query this # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 1, # "http://example.org/", # "This is test record 1!" # ] # ] # ] # ] </pre></div> </div> </div> <div class="section" id="specify-output-columns"> <h2>4.1.13. 出力カラムの指定<a class="headerlink" href="#specify-output-columns" title="このヘッドラインへのパーマリンク">¶</a></h2> <p><a class="reference internal" href="../reference/commands/select.html"><em>select</em></a> コマンドにおいて <cite>output_columns</cite> 引数を用いることで、検索結果に含めるカラムを指定することができます。複数のカラムを指定するときは、カンマ(,)区切りでカラムを列挙します。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --output_columns _key,title,_score --query title:@test # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 9 # ], # [ # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ], # [ # "_score", # "Int32" # ] # ], # [ # "http://example.org/", # "This is test record 1!", # 1 # ], # [ # "http://example.net/", # "test record 2.", # 1 # ], # [ # "http://example.com/", # "test test record three.", # 2 # ], # [ # "http://example.net/afr", # "test record four.", # 1 # ], # [ # "http://example.org/aba", # "test test test record five.", # 3 # ], # [ # "http://example.com/rab", # "test test test test record six.", # 4 # ], # [ # "http://example.net/atv", # "test test test record seven.", # 3 # ], # [ # "http://example.org/gat", # "test test record eight.", # 2 # ], # [ # "http://example.com/vdw", # "test test record nine.", # 2 # ] # ] # ] # ] </pre></div> </div> <p>上記の例では、_scoreカラムを含む3つのカラムを指定しています。_scoreカラムはgroongaの検索結果に含まれるカラムであり、全文検索の条件に合致するレコードほど高い数値が入ります。</p> </div> <div class="section" id="specify-output-ranges"> <h2>4.1.14. 表示範囲指定<a class="headerlink" href="#specify-output-ranges" title="このヘッドラインへのパーマリンク">¶</a></h2> <p><a class="reference internal" href="../reference/commands/select.html"><em>select</em></a> コマンドにおいて <cite>offset</cite> 引数と <cite>limit</cite> 引数を用いることで、検索結果の一部のみを表示することができます。大量の検索結果を分割してページ単位で表示したい場合などに有用です。</p> <p><cite>offset</cite> 引数には、検索結果における始点を指定します。検索結果の1件目が必要な場合、 <cite>offset</cite> 引数を省略するか、0を指定するようにしてください。 <cite>limit</cite> 引数には、検索結果の表示件数を指定します。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --offset 0 --limit 3 # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 9 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 1, # "http://example.org/", # "This is test record 1!" # ], # [ # 2, # "http://example.net/", # "test record 2." # ], # [ # 3, # "http://example.com/", # "test test record three." # ] # ] # ] # ] select --table Site --offset 3 --limit 3 # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 9 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 4, # "http://example.net/afr", # "test record four." # ], # [ # 5, # "http://example.org/aba", # "test test test record five." # ], # [ # 6, # "http://example.com/rab", # "test test test test record six." # ] # ] # ] # ] select --table Site --offset 7 --limit 3 # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 9 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 8, # "http://example.org/gat", # "test test record eight." # ], # [ # 9, # "http://example.com/vdw", # "test test record nine." # ] # ] # ] # ] </pre></div> </div> </div> <div class="section" id="sort-a-search-result"> <h2>4.1.15. 検索結果の並べ替え<a class="headerlink" href="#sort-a-search-result" title="このヘッドラインへのパーマリンク">¶</a></h2> <p><a class="reference internal" href="../reference/commands/select.html"><em>select</em></a> コマンドに <cite>sortby</cite> 引数を渡すことにより、検索結果を並べ替えることができます。</p> <p><cite>sortby</cite> 引数には、整列の基準として用いるカラムを指定します。検索結果は指定したカラムの値が昇順になるように並べ替えられます。 <cite>sortby</cite> 引数の中でカラム名の前にハイフン(-)を付けることにより、降順に並べ替えることもできます。</p> <p>以下の例では、Siteテーブルのレコードを逆順に表示しています。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --sortby -_id # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 9 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 9, # "http://example.com/vdw", # "test test record nine." # ], # [ # 8, # "http://example.org/gat", # "test test record eight." # ], # [ # 7, # "http://example.net/atv", # "test test test record seven." # ], # [ # 6, # "http://example.com/rab", # "test test test test record six." # ], # [ # 5, # "http://example.org/aba", # "test test test record five." # ], # [ # 4, # "http://example.net/afr", # "test record four." # ], # [ # 3, # "http://example.com/", # "test test record three." # ], # [ # 2, # "http://example.net/", # "test record 2." # ], # [ # 1, # "http://example.org/", # "This is test record 1!" # ] # ] # ] # ] </pre></div> </div> <p>次の例では、_scoreカラムを整列の基準とすることにより、検索結果のランキングをおこなっています。検索結果はクエリとの関連性が高い順に並べ替えられます。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --query title:@test --output_columns _id,_score,title --sortby -_score # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 9 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_score", # "Int32" # ], # [ # "title", # "ShortText" # ] # ], # [ # 6, # 4, # "test test test test record six." # ], # [ # 5, # 3, # "test test test record five." # ], # [ # 7, # 3, # "test test test record seven." # ], # [ # 8, # 2, # "test test record eight." # ], # [ # 3, # 2, # "test test record three." # ], # [ # 9, # 2, # "test test record nine." # ], # [ # 1, # 1, # "This is test record 1!" # ], # [ # 4, # 1, # "test record four." # ], # [ # 2, # 1, # "test record 2." # ] # ] # ] # ] </pre></div> </div> <p>整列の基準となるカラムを複数指定したいときは、カンマ(,)区切りでカラムを列挙します。複数のカラムを指定したときは、最初のカラムを基準として整列した後、最初のカラムに同じ値が格納されているレコードを次のカラムを基準として整列します。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --query title:@test --output_columns _id,_score,title --sortby -_score,_id # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 9 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_score", # "Int32" # ], # [ # "title", # "ShortText" # ] # ], # [ # 6, # 4, # "test test test test record six." # ], # [ # 5, # 3, # "test test test record five." # ], # [ # 7, # 3, # "test test test record seven." # ], # [ # 3, # 2, # "test test record three." # ], # [ # 8, # 2, # "test test record eight." # ], # [ # 9, # 2, # "test test record nine." # ], # [ # 1, # 1, # "This is test record 1!" # ], # [ # 2, # 1, # "test record 2." # ], # [ # 4, # 1, # "test record four." # ] # ] # ] # ] </pre></div> </div> <p class="rubric">脚注</p> <table class="docutils footnote" frame="void" id="id2" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td><p class="first last">現在のバージョンでは、全文検索インデックスが存在する場合にのみ、 <cite>match_columns</cite> 引数を利用することができます。通常のカラムでの絞り込みには利用できません。</p> </td></tr> </tbody> </table> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../index.html">目次</a></h3> <ul> <li><a class="reference internal" href="#">4.1. 基本的な操作</a><ul> <li><a class="reference internal" href="#create-a-database">4.1.1. データベースの作成</a></li> <li><a class="reference internal" href="#operate-a-database">4.1.2. データベースの操作</a></li> <li><a class="reference internal" href="#command-format">4.1.3. コマンドの書式</a></li> <li><a class="reference internal" href="#basic-commands">4.1.4. 主なコマンド</a></li> <li><a class="reference internal" href="#create-a-table">4.1.5. テーブルの作成</a></li> <li><a class="reference internal" href="#view-a-table">4.1.6. テーブルの表示</a></li> <li><a class="reference internal" href="#create-a-column">4.1.7. カラムの作成</a></li> <li><a class="reference internal" href="#load-records">4.1.8. データのロード</a></li> <li><a class="reference internal" href="#get-a-record">4.1.9. レコードの取得</a></li> <li><a class="reference internal" href="#create-a-lexicon-table-for-full-text-search">4.1.10. 全文検索用の語彙表の作成</a></li> <li><a class="reference internal" href="#create-an-index-column-for-full-text-search">4.1.11. 全文検索用のインデックスカラムの作成</a></li> <li><a class="reference internal" href="#full-text-search">4.1.12. 全文検索</a></li> <li><a class="reference internal" href="#specify-output-columns">4.1.13. 出力カラムの指定</a></li> <li><a class="reference internal" href="#specify-output-ranges">4.1.14. 表示範囲指定</a></li> <li><a class="reference internal" href="#sort-a-search-result">4.1.15. 検索結果の並べ替え</a></li> </ul> </li> </ul> <h4>前のトピックへ</h4> <p class="topless"><a href="../tutorial.html" title="前の章へ">4. チュートリアル</a></p> <h4>次のトピックへ</h4> <p class="topless"><a href="network.html" title="次の章へ">4.2. リモートアクセス</a></p> <h3>このページ</h3> <ul class="this-page-menu"> <li><a href="../_sources/tutorial/introduction.txt" rel="nofollow">ソースコードを表示</a></li> </ul> <div id="searchbox" style="display: none"> <h3>クイック検索</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="検索" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> モジュール、クラス、または関数名を入力してください </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>ナビゲーション</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="総合索引" >索引</a></li> <li class="right" > <a href="network.html" title="4.2. リモートアクセス" >次へ</a> |</li> <li class="right" > <a href="../tutorial.html" title="4. チュートリアル" >前へ</a> |</li> <li><a href="../index.html">groonga v3.0.5ドキュメント</a> »</li> <li><a href="../tutorial.html" >4. チュートリアル</a> »</li> </ul> </div> <div class="footer"> © Copyright 2009-2013, Brazil, Inc. </div> </body> </html>