<!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.11. クエリ拡張 — groonga v3.0.5 documentation</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> <link rel="shortcut icon" href="../_static/favicon.ico"/> <link rel="top" title="groonga v3.0.5 documentation" href="../index.html" /> <link rel="up" title="4. Tutorial" href="../tutorial.html" /> <link rel="next" title="5. Suggest" href="../suggest.html" /> <link rel="prev" title="4.10. マイクロブログ検索システムの作成" href="micro_blog.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/query_expansion.html"><img src="../_static/jp.png" alt="日本語">日本語版はこちら</a></li> </ul> </div> </div> <div class="related"> <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="../suggest.html" title="5. Suggest" accesskey="N">next</a> |</li> <li class="right" > <a href="micro_blog.html" title="4.10. マイクロブログ検索システムの作成" accesskey="P">previous</a> |</li> <li><a href="../index.html">groonga v3.0.5 documentation</a> »</li> <li><a href="../tutorial.html" accesskey="U">4. Tutorial</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="id1"> <h1>4.11. クエリ拡張<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h1> <p>groongaでは、 <a class="reference internal" href="../reference/commands/select.html"><em>select</em></a> コマンドにquery_expansionパラメータを指定することによって、ユーザが指定した検索文字列を適宜拡張することが可能です。</p> <p>たとえば、ユーザが'シークヮーサー'という文字列で検索した場合に、'シークヮーサー OR シークァーサー'で検索した場合と同一の結果を返すことによって、本来ユーザが必要とする結果をよりもれなく検索できるようになります。</p> <div class="section" id="id2"> <h2>4.11.1. 準備<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2> <p>クエリ拡張機能を使用するためには、検索対象となる文書を格納するテーブル(ここでは文書テーブルと呼びます)以外に、ユーザの指定した検索文字列を置換するためのテーブル(ここでは置換テーブルと呼びます)を準備します。置換テーブルでは、その主キーが置換前の文字列となり、文字列型(ShortText)のカラムの値が置換後の文字列となります。</p> <p>TODO: 文字列型のベクターカラムでも可能であり、その場合は各要素をORでつなげたものに置換されるということを記述する。</p> <p>実際に文書テーブルと置換テーブルを作成してみましょう。</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>table_create Doc TABLE_PAT_KEY ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Doc body COLUMN_SCALAR ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] table_create Term TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Term Doc_body COLUMN_INDEX|WITH_POSITION Doc body # [[0, 1337566253.89858, 0.000355720520019531], true] table_create Synonym TABLE_PAT_KEY ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Synonym body COLUMN_SCALAR ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] load --table Doc [ {"_key": "001", "body": "すっぱいブドウと甘いシークァーサー"}, {"_key": "002", "body": "シークヮーサージュースとゴーヤチャンプル"}, ] # [[0, 1337566253.89858, 0.000355720520019531], 2] load --table Synonym [ {"_key": "シークァーサー", "body": "(シークァーサー OR シークヮーサー)"}, {"_key": "シークヮーサー", "body": "(シークァーサー OR シークヮーサー)"}, ] # [[0, 1337566253.89858, 0.000355720520019531], 2] </pre></div> </div> <p>この例では、ユーザが"シークァーサー"と入力しても、"シークヮーサー"と入力しても、それぞれの異なる表記の文書をもれなく検索するための置換テーブルを作成しています。</p> </div> <div class="section" id="id3"> <h2>4.11.2. 検索<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h2> <p>それでは実際に、準備した置換テーブルを使ってみましょう。まずは、query_expanderパラメータを指定せずにselectコマンドを使って検索してみます。</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select Doc --match_columns body --query "シークァーサー" # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "body", # "ShortText" # ] # ], # [ # 1, # "001", # "すっぱいブドウと甘いシークァーサー" # ] # ] # ] # ] select Doc --match_columns body --query "シークヮーサー" # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "body", # "ShortText" # ] # ], # [ # 2, # "002", # "シークヮーサージュースとゴーヤチャンプル" # ] # ] # ] # ] </pre></div> </div> <p>指定された文字列に完全に一致するレコードのみがそれぞれヒットします。次に、query_expanderパラメータに、準備したSynonymテーブルのbodyカラムを指定してみましょう。</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select Doc --match_columns body --query "シークァーサー" --query_expander Synonym.body # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 2 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "body", # "ShortText" # ] # ], # [ # 1, # "001", # "すっぱいブドウと甘いシークァーサー" # ], # [ # 2, # "002", # "シークヮーサージュースとゴーヤチャンプル" # ] # ] # ] # ] select Doc --match_columns body --query "シークヮーサー" --query_expander Synonym.body # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 2 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "body", # "ShortText" # ] # ], # [ # 1, # "001", # "すっぱいブドウと甘いシークァーサー" # ], # [ # 2, # "002", # "シークヮーサージュースとゴーヤチャンプル" # ] # ] # ] # ] </pre></div> </div> <p>どちらのクエリ文字列も、"(シークァーサー OR シークヮーサー)"という文字列に置換されてから検索されるため、表記の揺れを吸収して検索できるようになりました。</p> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../index.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">4.11. クエリ拡張</a><ul> <li><a class="reference internal" href="#id2">4.11.1. 準備</a></li> <li><a class="reference internal" href="#id3">4.11.2. 検索</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="micro_blog.html" title="previous chapter">4.10. マイクロブログ検索システムの作成</a></p> <h4>Next topic</h4> <p class="topless"><a href="../suggest.html" title="next chapter">5. Suggest</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/tutorial/query_expansion.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <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="../suggest.html" title="5. Suggest" >next</a> |</li> <li class="right" > <a href="micro_blog.html" title="4.10. マイクロブログ検索システムの作成" >previous</a> |</li> <li><a href="../index.html">groonga v3.0.5 documentation</a> »</li> <li><a href="../tutorial.html" >4. Tutorial</a> »</li> </ul> </div> <div class="footer"> © Copyright 2009-2013, Brazil, Inc. </div> </body> </html>