<!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.4. さまざまな検索条件の指定 — 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="4.5. ドリルダウン" href="drilldown.html" /> <link rel="prev" title="4.3. Various data types" href="data.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/search.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="drilldown.html" title="4.5. ドリルダウン" accesskey="N">next</a> |</li> <li class="right" > <a href="data.html" title="4.3. Various data types" 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.4. さまざまな検索条件の指定<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h1> <p>groongaは、JavaScriptに似た文法での条件絞込や、計算した値を用いたソートを行うことができます。また、位置情報(緯度・経度)を用いた絞込・ソートを行うことができます。</p> <div class="section" id="javascript"> <h2>4.4.1. JavaScriptに似た文法での絞込・全文検索<a class="headerlink" href="#javascript" title="Permalink to this headline">¶</a></h2> <p>selectコマンドのfilterパラメータは、queryパラメータと同様に、レコードの検索条件を指定します。filterパラメータとqueryパラメータが異なる点は、filterパラメータには、JavaScriptの式に似た文法で条件を指定する点です。</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --filter "_id <= 1" --output_columns _id,_key # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ] # ], # [ # 1, # "http://example.org/" # ] # ] # ] # ] </pre></div> </div> <p>ここで、filterパラメータには</p> <blockquote> <div>_id <= 1</div></blockquote> <p>という条件を指定しています。この場合は_idの値が1以下のレコードが検索結果として得られます。</p> <p>また、&& や || を使って、条件のAND・OR指定をすることもできます。</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --filter "_id >= 4 && _id <= 6" --output_columns _id,_key # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 3 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ] # ], # [ # 4, # "http://example.net/afr" # ], # [ # 5, # "http://example.org/aba" # ], # [ # 6, # "http://example.com/rab" # ] # ] # ] # ] select --table Site --filter "_id <= 2 || _id >= 7" --output_columns _id,_key # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 5 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ] # ], # [ # 1, # "http://example.org/" # ], # [ # 2, # "http://example.net/" # ], # [ # 7, # "http://example.net/atv" # ], # [ # 8, # "http://example.org/gat" # ], # [ # 9, # "http://example.com/vdw" # ] # ] # ] # ] </pre></div> </div> <p>queryパラメータとfilterパラメータを同時に指定すると、両者の条件をともに満たすレコードが結果として返ります。</p> </div> <div class="section" id="scorer"> <h2>4.4.2. scorerを利用したソート<a class="headerlink" href="#scorer" title="Permalink to this headline">¶</a></h2> <p>selectコマンドのscorerパラメータは、 全文検索を行った結果の各レコードに対して処理を行うためのパラメータです。</p> <p>filterパラメータと同様に、 JavaScriptの式に似たな文法で様々な条件を指定することができます。</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --filter "1" --scorer "_score = rand()" --output_columns _id,_key,_score --sortby _score # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 9 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "_score", # "Int32" # ] # ], # [ # 6, # "http://example.com/rab", # 424238335 # ], # [ # 9, # "http://example.com/vdw", # 596516649 # ], # [ # 7, # "http://example.net/atv", # 719885386 # ], # [ # 2, # "http://example.net/", # 846930886 # ], # [ # 8, # "http://example.org/gat", # 1649760492 # ], # [ # 3, # "http://example.com/", # 1681692777 # ], # [ # 4, # "http://example.net/afr", # 1714636915 # ], # [ # 1, # "http://example.org/", # 1804289383 # ], # [ # 5, # "http://example.org/aba", # 1957747793 # ] # ] # ] # ] select --table Site --filter "1" --scorer "_score = rand()" --output_columns _id,_key,_score --sortby _score # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 9 # ], # [ # [ # "_id", # "UInt32" # ], # [ # "_key", # "ShortText" # ], # [ # "_score", # "Int32" # ] # ], # [ # 4, # "http://example.net/afr", # 783368690 # ], # [ # 2, # "http://example.net/", # 1025202362 # ], # [ # 5, # "http://example.org/aba", # 1102520059 # ], # [ # 1, # "http://example.org/", # 1189641421 # ], # [ # 3, # "http://example.com/", # 1350490027 # ], # [ # 8, # "http://example.org/gat", # 1365180540 # ], # [ # 9, # "http://example.com/vdw", # 1540383426 # ], # [ # 7, # "http://example.net/atv", # 1967513926 # ], # [ # 6, # "http://example.com/rab", # 2044897763 # ] # ] # ] # ] </pre></div> </div> <p>検索結果には、'_score'という名前の、全文検索のスコアが代入されている仮想的なカラムが付与されることをチュートリアル中ソートの項目で説明しました。</p> <p>上記の実行例では、scorerパラメータに</p> <blockquote> <div>_score = rand()</div></blockquote> <p>という条件を指定しています。ここでは、rand()という乱数を返す関数を用いて、全文検索のスコアを乱数で上書きしています。</p> <p>sortbyパラメータには、</p> <blockquote> <div>_score</div></blockquote> <p>を指定しています。これは、スコア順に昇順にソートすることを意味しています。</p> <p>よって、上記のクエリは実行されるたびに検索結果の並び順がランダムに変わります。</p> </div> <div class="section" id="id2"> <h2>4.4.3. 位置情報を用いた絞込・ソート<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2> <p>groongaでは、位置情報(経緯度)を保存することができます。また、保存した経緯度を用いて絞込やソートができます。</p> <p>位置情報を保存するためのカラムの型として、TokyoGeoPoint/WGS84GeoPointの2つの型があります。前者は日本測地系、後者は世界測地系(WGS84相当)の経緯度を保存します。</p> <p>経緯度は以下のいずれかの形式の文字列として指定します。</p> <ul class="simple"> <li>"[緯度のミリ秒]x[経度のミリ秒]"(例: "128452975x503157902")</li> <li>"[緯度のミリ秒],[経度のミリ秒]"(例: "128452975,503157902")</li> <li>"[緯度の小数表記の度数]x[経度の小数表記の度数]"(例: "35.6813819x139.7660839")</li> <li>"[緯度の小数表記の度数],[経度の小数表記の度数]"(例: "35.6813819,139.7660839")</li> </ul> <p>ここでは、ためしに東京駅と新宿駅とついて、世界測地系での位置情報を保存してみましょう。東京駅は緯度が35度40分52.975秒、経度が139度45分57.902秒です。新宿駅は緯度が35度41分27.316秒、経度が139度42分0.929秒です。よって、ミリ秒表記の場合はそれぞれ"128452975x503157902"/"128487316x502920929"となります。度数表記の場合はそれぞれ"35.6813819x139.7660839"/"35.6909211x139.7002581"となります。ここではミリ秒表記で登録しましょう。</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>column_create --table Site --name location --type WGS84GeoPoint # [[0, 1337566253.89858, 0.000355720520019531], true] load --table Site [ {"_key":"http://example.org/","location":"128452975x503157902"} {"_key":"http://example.net/","location":"128487316x502920929"}, ] # [[0, 1337566253.89858, 0.000355720520019531], 2] select --table Site --query "_id:1 OR _id:2" --output_columns _key,location # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 2 # ], # [ # [ # "_key", # "ShortText" # ], # [ # "location", # "WGS84GeoPoint" # ] # ], # [ # "http://example.org/", # "128452975x503157902" # ], # [ # "http://example.net/", # "128487316x502920929" # ] # ] # ] # ] </pre></div> </div> <p>scorerパラメータにおいて、 <a class="reference internal" href="../reference/functions/geo_distance.html"><em>geo_distance</em></a> 関数を用いることにより、2点間の距離を計算することができます。</p> <p>ここでは、秋葉原駅からの距離を表示させてみましょう。世界測地系では、秋葉原駅の位置は緯度が35度41分55.259秒、経度が139度46分27.188秒です。よって、geo_distance関数に与える文字列は"128515259x503187188"となります。</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --query "_id:1 OR _id:2" --output_columns _key,location,_score --scorer '_score = geo_distance(location, "128515259x503187188")' # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 2 # ], # [ # [ # "_key", # "ShortText" # ], # [ # "location", # "WGS84GeoPoint" # ], # [ # "_score", # "Int32" # ] # ], # [ # "http://example.org/", # "128452975x503157902", # 2054 # ], # [ # "http://example.net/", # "128487316x502920929", # 6720 # ] # ] # ] # ] </pre></div> </div> <p>この結果を見ると、東京駅と秋葉原駅は2054m、秋葉原駅と新宿駅は6720m離れているようです。</p> <p>geo_distance関数は、_scoreを通じてソートでも用いることができます。</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --query "_id:1 OR _id:2" --output_columns _key,location,_score --scorer '_score = geo_distance(location, "128515259x503187188")' --sortby -_score # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 2 # ], # [ # [ # "_key", # "ShortText" # ], # [ # "location", # "WGS84GeoPoint" # ], # [ # "_score", # "Int32" # ] # ], # [ # "http://example.net/", # "128487316x502920929", # 6720 # ], # [ # "http://example.org/", # "128452975x503157902", # 2054 # ] # ] # ] # ] </pre></div> </div> <p>「ある地点から何m以内に存在する」といった絞込も可能です。</p> <p>filterパラメータにおいて、 <a class="reference internal" href="../reference/functions/geo_in_circle.html"><em>geo_in_circle</em></a> 関数を用いることにより、2点間の距離が指定のm以下におさまるかどうかを判定することができます。</p> <p>たとえば、秋葉原駅から5000m以内にあるレコードを検索してみましょう。</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select --table Site --output_columns _key,location --filter 'geo_in_circle(location, "128515259x503187188", 5000)' # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "_key", # "ShortText" # ], # [ # "location", # "WGS84GeoPoint" # ] # ], # [ # "http://example.org/", # "128452975x503157902" # ] # ] # ] # ] </pre></div> </div> <p>また、経緯度が指定の矩形領域内であるかどうかを判定する <a class="reference internal" href="../reference/functions/geo_in_rectangle.html"><em>geo_in_rectangle</em></a> 関数も存在します。</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.4. さまざまな検索条件の指定</a><ul> <li><a class="reference internal" href="#javascript">4.4.1. JavaScriptに似た文法での絞込・全文検索</a></li> <li><a class="reference internal" href="#scorer">4.4.2. scorerを利用したソート</a></li> <li><a class="reference internal" href="#id2">4.4.3. 位置情報を用いた絞込・ソート</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="data.html" title="previous chapter">4.3. Various data types</a></p> <h4>Next topic</h4> <p class="topless"><a href="drilldown.html" title="next chapter">4.5. ドリルダウン</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/tutorial/search.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="drilldown.html" title="4.5. ドリルダウン" >next</a> |</li> <li class="right" > <a href="data.html" title="4.3. Various data types" >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>