<!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.7. match_columnsパラメータ — 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.8. パトリシア木による前方一致検索" href="patricia_trie.html" /> <link rel="prev" title="4.6. タグ検索・参照関係の逆引き" href="index.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/match_columns.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="patricia_trie.html" title="4.8. パトリシア木による前方一致検索" accesskey="N">次へ</a> |</li> <li class="right" > <a href="index.html" title="4.6. タグ検索・参照関係の逆引き" 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="match-columns"> <h1>4.7. match_columnsパラメータ<a class="headerlink" href="#match-columns" title="このヘッドラインへのパーマリンク">¶</a></h1> <div class="section" id="id1"> <h2>4.7.1. 複数のカラムを対象とした全文検索<a class="headerlink" href="#id1" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>groongaでは、複数のカラムを対象とした全文検索を行うことができます。例えば、ブログのテーブルで、タイトルと内容とがそれぞれ別のカラムに入ったものがあるとしましょう。「タイトルもしくは内容に特定の単語を含む」検索を行いたいとします。</p> <p>この場合、2つのインデックス作成方式があります。1つは、それぞれのカラムに1つずつインデックスを付与する方式です。もう1つは、複数のカラムに対して1つのインデックスを付与する方式です。groongaでは、どちらの形式のインデックスが存在している場合でも、同一の記法で全文検索を行うことができます。</p> <div class="section" id="id2"> <h3>4.7.1.1. カラムごとにインデックスを付与する場合<a class="headerlink" href="#id2" title="このヘッドラインへのパーマリンク">¶</a></h3> <p>Blog1テーブルを作り、タイトル文字列のtitleカラム、本文のmessageカラムを追加しています。 インデックス用のIndexBlog1テーブルも作り、titleカラムのインデックス用にindex_titleカラム、messageカラムのインデック用にindex_messageカラムと、それぞれ1カラムごとに1つずつ追加しています。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>table_create --name Blog1 --flags TABLE_HASH_KEY --key_type ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] column_create --table Blog1 --name title --flags COLUMN_SCALAR --type ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] column_create --table Blog1 --name message --flags COLUMN_SCALAR --type ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] table_create --name IndexBlog1 --flags TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText --default_tokenizer TokenBigram # [[0, 1337566253.89858, 0.000355720520019531], true] column_create --table IndexBlog1 --name index_title --flags COLUMN_INDEX|WITH_POSITION --type Blog1 --source title # [[0, 1337566253.89858, 0.000355720520019531], true] column_create --table IndexBlog1 --name index_message --flags COLUMN_INDEX|WITH_POSITION --type Blog1 --source message # [[0, 1337566253.89858, 0.000355720520019531], true] load --table Blog1 [ {"_key":"grn1","title":"groonga test","message":"groonga message"}, {"_key":"grn2","title":"baseball result","message":"rakutan eggs 4 - 4 groonga moritars"}, {"_key":"grn3","title":"groonga message","message":"none"} ] # [[0, 1337566253.89858, 0.000355720520019531], 3] </pre></div> </div> <p>match_columnsオプションで、検索対象のカラムを複数指定することが出来ます。検索する文字列はqueryオプションで指定します。これを使うことで、タイトルと本文を全文検索することができます。</p> <p>実際に検索してみましょう。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Blog1 --match_columns title||message --query groonga # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 3 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "message", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 1, # "grn1", # "groonga message", # "groonga test" # ], # [ # 3, # "grn3", # "none", # "groonga message" # ], # [ # 2, # "grn2", # "rakutan eggs 4 - 4 groonga moritars", # "baseball result" # ] # ] # ] # ] select --table Blog1 --match_columns title||message --query message # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 2 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "message", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 3, # "grn3", # "none", # "groonga message" # ], # [ # 1, # "grn1", # "groonga message", # "groonga test" # ] # ] # ] # ] select --table Blog1 --match_columns title --query message # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "message", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 3, # "grn3", # "none", # "groonga message" # ] # ] # ] # ] </pre></div> </div> </div> <div class="section" id="id3"> <h3>4.7.1.2. 複数のカラムにまたがったインデックスを付与する場合<a class="headerlink" href="#id3" title="このヘッドラインへのパーマリンク">¶</a></h3> <p>内容は上の例とほぼ同じですが、titleとmessageの2つのカラムに対するインデックスが共通になっており、インデックスカラムが1つしかありません。</p> <p>共通のインデックスを用いても、titleカラムのみでの検索、messageカラムのみでの検索、titleもしくはmessageカラムでの検索、全ての検索を行うことができます。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>table_create --name Blog2 --flags TABLE_HASH_KEY --key_type ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] column_create --table Blog2 --name title --flags COLUMN_SCALAR --type ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] column_create --table Blog2 --name message --flags COLUMN_SCALAR --type ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] table_create --name IndexBlog2 --flags TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText --default_tokenizer TokenBigram # [[0, 1337566253.89858, 0.000355720520019531], true] column_create --table IndexBlog2 --name index_blog --flags COLUMN_INDEX|WITH_POSITION|WITH_SECTION --type Blog2 --source title,message # [[0, 1337566253.89858, 0.000355720520019531], true] load --table Blog2 [ {"_key":"grn1","title":"groonga test","message":"groonga message"}, {"_key":"grn2","title":"baseball result","message":"rakutan eggs 4 - 4 groonga moritars"}, {"_key":"grn3","title":"groonga message","message":"none"} ] # [[0, 1337566253.89858, 0.000355720520019531], 3] </pre></div> </div> <p>実際に検索してみましょう。結果は上の例と同じになります。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Blog2 --match_columns title||message --query groonga # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 3 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "message", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 1, # "grn1", # "groonga message", # "groonga test" # ], # [ # 2, # "grn2", # "rakutan eggs 4 - 4 groonga moritars", # "baseball result" # ], # [ # 3, # "grn3", # "none", # "groonga message" # ] # ] # ] # ] select --table Blog2 --match_columns title||message --query message # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 2 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "message", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 1, # "grn1", # "groonga message", # "groonga test" # ], # [ # 3, # "grn3", # "none", # "groonga message" # ] # ] # ] # ] select --table Blog2 --match_columns title --query message # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "message", # "ShortText" # ], # [ # "title", # "ShortText" # ] # ], # [ # 3, # "grn3", # "none", # "groonga message" # ] # ] # ] # ] </pre></div> </div> </div> </div> <div class="section" id="id4"> <h2>4.7.2. インデックス名を指定した全文検索<a class="headerlink" href="#id4" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>執筆中です。</p> </div> <div class="section" id="nested-index-search-among-related-table-by-column-index"> <span id="nested-index-search"></span><h2>4.7.3. カラムインデックスによる関連テーブルをまたぐ検索<a class="headerlink" href="#nested-index-search-among-related-table-by-column-index" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>複数のテーブルがカラムインデックスで関連付けられているなら、参照カラム名を指定して複数のテーブルにまたがって検索することができます。</p> <p>具体的な例を示します。</p> <p>ブログ記事や記事のコメントを保存するテーブルがあります。記事を保存するテーブルには記事とコメントのためのカラムがあります。そしてそのコメントカラムはCommentsテーブルを参照しています。コメントを保存するテーブルにはコメントと記事テーブルに対するカラムインデックスが設定されています。</p> <p>特定のキーワードをコメントに含む記事を探すには、コメントテーブルを全文検索する必要があり、全文検索結果を含むレコードをさらに検索する必要があります。</p> <p>しかし、カラムインデックスを指定することで一度にレコードを検索することができます。</p> <p>サンプルのスキーマ定義はこちらです。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>table_create Comments TABLE_HASH_KEY UInt32 # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Comments content COLUMN_SCALAR ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] table_create Articles TABLE_NO_KEY # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Articles content COLUMN_SCALAR Text # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Articles comment COLUMN_SCALAR Comments # [[0, 1337566253.89858, 0.000355720520019531], true] table_create Lexicon TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Lexicon articles_content COLUMN_INDEX|WITH_POSITION Articles content # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Lexicon comments_content COLUMN_INDEX|WITH_POSITION Comments content # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Comments article COLUMN_INDEX Articles comment # [[0, 1337566253.89858, 0.000355720520019531], true] </pre></div> </div> <p>サンプルデータはこちらです。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>load --table Comments [ {"_key": 1, "content": "I'm using groonga too!"}, {"_key": 2, "content": "I'm using groonga and mroonga!"}, {"_key": 3, "content": "I'm using mroonga too!"} ] # [[0, 1337566253.89858, 0.000355720520019531], 3] load --table Articles [ {"content": "Groonga is fast!", "comment": 1}, {"content": "Groonga is useful!"}, {"content": "Mroonga is fast!", "comment": 3} ] # [[0, 1337566253.89858, 0.000355720520019531], 3] </pre></div> </div> <p>特定のキーワードをコメントに含むレコードを検索するクエリを書くことができ、それによりレコードを参照する記事を取得します。</p> <p>これまでに記述したレコードを検索するクエリ:</p> <div class="highlight-none"><div class="highlight"><pre>select Articles --match_columns comment.content --query groonga --output_columns "_id, _score, *" </pre></div> </div> <p>ArticlesテーブルのcommentカラムとCommentsテーブルのcontentカラムをピリオド(.)で連結し <tt class="docutils literal"><span class="pre">--match_columns</span></tt> の引数とする必要があります。</p> <p>最初に、このクエリはCommentsテーブルのcontentを全文検索し、次にCommentsテーブルを検索した結果のレコードを参照するArticlesテーブルのレコードを取得します。(これにより、Commentsテーブルの'article'カラムインデックスを生成するクエリをコメントアウトすると、意図した検索結果が得られません。)</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select Articles --match_columns comment.content --query groonga --output_columns "_id, _score, *" # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_score", # "Int32" # ], # [ # "comment", # "Comments" # ], # [ # "content", # "Text" # ] # ], # [ # 1, # 1, # 1, # "Groonga is fast!" # ] # ] # ] # ] </pre></div> </div> <p>これで、特定のキーワードをコメントとして含む記事を検索できます。</p> <p>このネストしたインデックスの検索という特徴には関連するテーブルが2つだけに制限されません。</p> <p>前のものと似たサンプルのスキーマ定義です。違いは'返信'を表現するテーブルの追加と関連するテーブルが3つに増えたことです。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>table_create Replies2 TABLE_HASH_KEY UInt32 # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Replies2 content COLUMN_SCALAR ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] table_create Comments2 TABLE_HASH_KEY UInt32 # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Comments2 content COLUMN_SCALAR ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Comments2 comment COLUMN_SCALAR Replies2 # [[0, 1337566253.89858, 0.000355720520019531], true] table_create Articles2 TABLE_NO_KEY # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Articles2 content COLUMN_SCALAR Text # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Articles2 comment COLUMN_SCALAR Comments2 # [[0, 1337566253.89858, 0.000355720520019531], true] table_create Lexicon2 TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Lexicon2 articles_content COLUMN_INDEX|WITH_POSITION Articles2 content # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Lexicon2 comments_content COLUMN_INDEX|WITH_POSITION Comments2 content # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Lexicon2 replies_content COLUMN_INDEX|WITH_POSITION Replies2 content # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Comments2 article COLUMN_INDEX Articles2 comment # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Replies2 reply_to COLUMN_INDEX Comments2 comment # [[0, 1337566253.89858, 0.000355720520019531], true] </pre></div> </div> <p>サンプルデータはこちらです。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>load --table Replies2 [ {"_key": 1, "content": "I'm using rroonga too!"}, {"_key": 2, "content": "I'm using groonga and mroonga and rroonga!"}, {"_key": 3, "content": "I'm using nroonga too!"} ] # [[0, 1337566253.89858, 0.000355720520019531], 3] load --table Comments2 [ {"_key": 1, "content": "I'm using groonga too!", "comment": 1}, {"_key": 2, "content": "I'm using groonga and mroonga!", "comment": 2}, {"_key": 3, "content": "I'm using mroonga too!"} ] # [[0, 1337566253.89858, 0.000355720520019531], 3] load --table Articles2 [ {"content": "Groonga is fast!", "comment": 1}, {"content": "Groonga is useful!", "comment": 2}, {"content": "Mroonga is fast!", "comment": 3} ] # [[0, 1337566253.89858, 0.000355720520019531], 3] </pre></div> </div> <p>これまでに記述したレコードを検索するクエリ:</p> <div class="highlight-none"><div class="highlight"><pre>select Articles2 --match_columns comment.content --query mroonga --output_columns "_id, _score, *" select Articles2 --match_columns comment.comment.content --query mroonga --output_columns "_id, _score, *" </pre></div> </div> <p>最初のクエリはComments2テーブルから'mroonga'を検索します。2つめのクエリはReplies2とComments2テーブルからカラムインデックスによる参照を用いて'mroonga'を検索します。</p> <p>実行例:</p> <div class="highlight-none"><div class="highlight"><pre>select Articles2 --match_columns comment.content --query mroonga --output_columns "_id, _score, *" # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 2 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_score", # "Int32" # ], # [ # "comment", # "Comments2" # ], # [ # "content", # "Text" # ] # ], # [ # 2, # 1, # 2, # "Groonga is useful!" # ], # [ # 3, # 1, # 3, # "Mroonga is fast!" # ] # ] # ] # ] select Articles2 --match_columns comment.comment.content --query mroonga --output_columns "_id, _score, *" # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_score", # "Int32" # ], # [ # "comment", # "Comments2" # ], # [ # "content", # "Text" # ] # ], # [ # 2, # 1, # 2, # "Groonga is useful!" # ] # ] # ] # ] </pre></div> </div> <p>結果として、最初のクエリはComments2テーブルに'mroonga'をキーワードとして含むレコードが2つあるので、該当する記事2つにマッチします。</p> <p>一方、2つめのクエリはReplies2テーブルに'mroonga'というキーワードにマッチするレコードが1つしかなく、Comments2テーブルでそのキーワードを含むレコードを参照するコメントが1つなので、該当する記事は1つだけとなります。</p> </div> <div class="section" id="id5"> <h2>4.7.4. インデックスの重み<a class="headerlink" href="#id5" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>執筆中です。</p> </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.7. match_columnsパラメータ</a><ul> <li><a class="reference internal" href="#id1">4.7.1. 複数のカラムを対象とした全文検索</a><ul> <li><a class="reference internal" href="#id2">4.7.1.1. カラムごとにインデックスを付与する場合</a></li> <li><a class="reference internal" href="#id3">4.7.1.2. 複数のカラムにまたがったインデックスを付与する場合</a></li> </ul> </li> <li><a class="reference internal" href="#id4">4.7.2. インデックス名を指定した全文検索</a></li> <li><a class="reference internal" href="#nested-index-search-among-related-table-by-column-index">4.7.3. カラムインデックスによる関連テーブルをまたぐ検索</a></li> <li><a class="reference internal" href="#id5">4.7.4. インデックスの重み</a></li> </ul> </li> </ul> <h4>前のトピックへ</h4> <p class="topless"><a href="index.html" title="前の章へ">4.6. タグ検索・参照関係の逆引き</a></p> <h4>次のトピックへ</h4> <p class="topless"><a href="patricia_trie.html" title="次の章へ">4.8. パトリシア木による前方一致検索</a></p> <h3>このページ</h3> <ul class="this-page-menu"> <li><a href="../_sources/tutorial/match_columns.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="patricia_trie.html" title="4.8. パトリシア木による前方一致検索" >次へ</a> |</li> <li class="right" > <a href="index.html" title="4.6. タグ検索・参照関係の逆引き" >前へ</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>