<!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>8.11.7. query — 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="8.11. Function" href="../function.html" /> <link rel="next" title="8.11.8. rand" href="rand.html" /> <link rel="prev" title="8.11.6. now" href="now.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/reference/functions/query.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="rand.html" title="8.11.8. rand" accesskey="N">next</a> |</li> <li class="right" > <a href="now.html" title="8.11.6. now" accesskey="P">previous</a> |</li> <li><a href="../../index.html">groonga v3.0.5 documentation</a> »</li> <li><a href="../../reference.html" >8. リファレンスマニュアル</a> »</li> <li><a href="../function.html" accesskey="U">8.11. Function</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="query"> <h1>8.11.7. query<a class="headerlink" href="#query" title="Permalink to this headline">¶</a></h1> <div class="section" id="summary"> <h2>8.11.7.1. Summary<a class="headerlink" href="#summary" title="Permalink to this headline">¶</a></h2> <p><tt class="docutils literal"><span class="pre">query</span></tt> enables you to specify <tt class="docutils literal"><span class="pre">--match_columns</span></tt> and <tt class="docutils literal"><span class="pre">--query</span></tt> parameters as function arguments.</p> <p><tt class="docutils literal"><span class="pre">query</span></tt> is one of the groonga builtin functions, so you can specify multiple <tt class="docutils literal"><span class="pre">query</span></tt> function as parameters of <tt class="docutils literal"><span class="pre">--filter</span></tt> option.</p> <p>Because of such flexibility, you can control full text search behavior by combination of multiple <tt class="docutils literal"><span class="pre">query</span></tt> function.</p> <p><tt class="docutils literal"><span class="pre">query</span></tt> can be used in only <tt class="docutils literal"><span class="pre">--filter</span></tt> in <a class="reference internal" href="../commands/select.html"><em>select</em></a>.</p> </div> <div class="section" id="syntax"> <h2>8.11.7.2. Syntax<a class="headerlink" href="#syntax" title="Permalink to this headline">¶</a></h2> <p><tt class="docutils literal"><span class="pre">query</span></tt> requires two arguments - <tt class="docutils literal"><span class="pre">match_column</span></tt> and <tt class="docutils literal"><span class="pre">query_string</span></tt>.</p> <p>The parameter <tt class="docutils literal"><span class="pre">query_expander</span></tt> or <tt class="docutils literal"><span class="pre">substitution_table</span></tt> is optional.</p> <div class="highlight-none"><div class="highlight"><pre>query(match_column, query_string) query(match_column, query_string, query_expander) query(match_column, query_string, substitution_table) </pre></div> </div> </div> <div class="section" id="usage"> <h2>8.11.7.3. Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2> <p>Here are a schema definition and sample data to show usage.</p> <p>Sample schema:</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>table_create Documents TABLE_NO_KEY # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Documents content COLUMN_SCALAR Text # [[0, 1337566253.89858, 0.000355720520019531], true] table_create Terms TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Terms documents_content_index COLUMN_INDEX|WITH_POSITION Documents content # [[0, 1337566253.89858, 0.000355720520019531], true] table_create Users TABLE_NO_KEY # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Users name COLUMN_SCALAR ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Users memo COLUMN_SCALAR ShortText # [[0, 1337566253.89858, 0.000355720520019531], true] table_create Lexicon TABLE_HASH_KEY ShortText \ --default_tokenizer TokenBigramSplitSymbolAlphaDigit \ --normalizer NormalizerAuto column_create Lexicon users_name COLUMN_INDEX|WITH_POSITION Users name # [[0, 1337566253.89858, 0.000355720520019531], true] column_create Lexicon users_memo COLUMN_INDEX|WITH_POSITION Users memo # [[0, 1337566253.89858, 0.000355720520019531], true] </pre></div> </div> <p>Sample data:</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>load --table Users [ {"name": "Alice", "memo": "groonga user"}, {"name": "Alisa", "memo": "mroonga user"}, {"name": "Bob", "memo": "rroonga user"}, {"name": "Tom", "memo": "nroonga user"}, {"name": "Tobby", "memo": "groonga and mroonga user. mroonga is ..."}, ] # [[0, 1337566253.89858, 0.000355720520019531], true] # [[0, 1337566253.89858, 0.000355720520019531], 5] </pre></div> </div> <p>Here is the simple usage of <tt class="docutils literal"><span class="pre">query</span></tt> function which execute full text search by keyword 'alice' without using <tt class="docutils literal"><span class="pre">--match_column</span></tt> and <tt class="docutils literal"><span class="pre">--query</span></tt> arguments in <tt class="docutils literal"><span class="pre">--filter</span></tt>.</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select Users --output_columns name,_score --filter 'query("name * 10", "alice")' # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 1 # ], # [ # [ # "name", # "ShortText" # ], # [ # "_score", # "Int32" # ] # ], # [ # "Alice", # 10 # ] # ] # ] # ] </pre></div> </div> <p>When executing above query, the keyword 'alice' is weighted to the value - '10'.</p> <p>Here are the contrasting examples with/without <tt class="docutils literal"><span class="pre">query</span></tt>.</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select Users --output_columns name,memo,_score --match_columns "memo * 10" --query "memo:@groonga OR memo:@mroonga OR memo:@user" --sortby -_score # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 5 # ], # [ # [ # "name", # "ShortText" # ], # [ # "memo", # "ShortText" # ], # [ # "_score", # "Int32" # ] # ], # [ # "Tobby", # "groonga and mroonga user. mroonga is ...", # 4 # ], # [ # "Alice", # "groonga user", # 2 # ], # [ # "Alisa", # "mroonga user", # 2 # ], # [ # "Bob", # "rroonga user", # 1 # ], # [ # "Tom", # "nroonga user", # 1 # ] # ] # ] # ] </pre></div> </div> <p>In this case, the keywords 'groonga' and 'mroonga' and 'user' are given same weight value. You can't pass different weight value to each keyword in this way.</p> <p>Execution example:</p> <div class="highlight-none"><div class="highlight"><pre>select Users --output_columns name,memo,_score --filter 'query("memo * 10", "groonga") || query("memo * 20", "mroonga") || query("memo * 1", "user")' --sortby -_score # [ # [ # 0, # 1337566253.89858, # 0.000355720520019531 # ], # [ # [ # [ # 5 # ], # [ # [ # "name", # "ShortText" # ], # [ # "memo", # "ShortText" # ], # [ # "_score", # "Int32" # ] # ], # [ # "Tobby", # "groonga and mroonga user. mroonga is ...", # 51 # ], # [ # "Alisa", # "mroonga user", # 21 # ], # [ # "Alice", # "groonga user", # 11 # ], # [ # "Tom", # "nroonga user", # 1 # ], # [ # "Bob", # "rroonga user", # 1 # ] # ] # ] # ] </pre></div> </div> <p>On the other hand, by specifying multiple <tt class="docutils literal"><span class="pre">query</span></tt>, the keywords 'groonga' and 'mroonga' and 'user' are given different value of weight.</p> <p>As a result, you can control full text search result by giving different weight to the keywords on your purpose.</p> </div> <div class="section" id="parameters"> <h2>8.11.7.4. Parameters<a class="headerlink" href="#parameters" title="Permalink to this headline">¶</a></h2> <div class="section" id="required-parameter"> <h3>8.11.7.4.1. Required parameter<a class="headerlink" href="#required-parameter" title="Permalink to this headline">¶</a></h3> <p>There are two required parameter, <tt class="docutils literal"><span class="pre">match_column</span></tt> and <tt class="docutils literal"><span class="pre">query_string</span></tt>.</p> <div class="section" id="match-column"> <h4>8.11.7.4.1.1. <tt class="docutils literal"><span class="pre">match_column</span></tt><a class="headerlink" href="#match-column" title="Permalink to this headline">¶</a></h4> <p>It specifies the default target column for fulltext search by <tt class="docutils literal"><span class="pre">query_string</span></tt> parameter value. It is the same role as <tt class="docutils literal"><span class="pre">match_column</span></tt> parameter in <tt class="docutils literal"><span class="pre">select</span></tt>.</p> <p>See <a class="reference internal" href="../commands/select.html#select-match-columns"><em>match_columns</em></a> about <tt class="docutils literal"><span class="pre">match_column</span></tt> parameter in <tt class="docutils literal"><span class="pre">select</span></tt>.</p> </div> <div class="section" id="query-string"> <h4>8.11.7.4.1.2. <tt class="docutils literal"><span class="pre">query_string</span></tt><a class="headerlink" href="#query-string" title="Permalink to this headline">¶</a></h4> <p>It specifies the search condition in <a class="reference internal" href="../grn_expr/query_syntax.html"><em>Query syntax</em></a>. It is the same role as <tt class="docutils literal"><span class="pre">query</span></tt> parameter in <tt class="docutils literal"><span class="pre">select</span></tt>.</p> <p>See <a class="reference internal" href="../commands/select.html#select-match-columns"><em>match_columns</em></a> about <tt class="docutils literal"><span class="pre">query</span></tt> parameter in <tt class="docutils literal"><span class="pre">select</span></tt>.</p> </div> </div> <div class="section" id="optional-parameter"> <h3>8.11.7.4.2. Optional parameter<a class="headerlink" href="#optional-parameter" title="Permalink to this headline">¶</a></h3> <p>There are some optional parameters.</p> <div class="section" id="query-expander"> <h4>8.11.7.4.2.1. <tt class="docutils literal"><span class="pre">query_expander</span></tt><a class="headerlink" href="#query-expander" title="Permalink to this headline">¶</a></h4> <p>It specifies the plugin name for query expansion.</p> <p>There is one plugin bundled in official release - <a class="reference internal" href="../query_expanders/tsv.html"><em>QueryExpanderTSV</em></a>.</p> <p>See <a class="reference internal" href="../query_expanders/tsv.html"><em>QueryExpanderTSV</em></a> about details.</p> </div> <div class="section" id="substitution-table"> <h4>8.11.7.4.2.2. <tt class="docutils literal"><span class="pre">substitution_table</span></tt><a class="headerlink" href="#substitution-table" title="Permalink to this headline">¶</a></h4> <p>It specifies the substitution table and substitution column name by following format such as <tt class="docutils literal"><span class="pre">${TABLE}.${COLUMN}</span></tt> for query expansion.</p> <p>See <a class="reference internal" href="../commands/select.html#query-expander"><em>query_expander</em></a> about details.</p> </div> </div> </div> <div class="section" id="return-value"> <h2>8.11.7.5. Return value<a class="headerlink" href="#return-value" title="Permalink to this headline">¶</a></h2> <p><tt class="docutils literal"><span class="pre">query</span></tt> returns whether any record is matched or not. If one or more records are matched, it returns <tt class="docutils literal"><span class="pre">true</span></tt>. Otherwise, it returns <tt class="docutils literal"><span class="pre">false</span></tt>.</p> </div> <div class="section" id="todo"> <h2>8.11.7.6. TODO<a class="headerlink" href="#todo" title="Permalink to this headline">¶</a></h2> <ul class="simple"> <li>Support query_flags</li> </ul> </div> <div class="section" id="see-also"> <h2>8.11.7.7. See also<a class="headerlink" href="#see-also" title="Permalink to this headline">¶</a></h2> <ul class="simple"> <li><a class="reference internal" href="../commands/select.html"><em>select</em></a></li> </ul> </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="#">8.11.7. query</a><ul> <li><a class="reference internal" href="#summary">8.11.7.1. Summary</a></li> <li><a class="reference internal" href="#syntax">8.11.7.2. Syntax</a></li> <li><a class="reference internal" href="#usage">8.11.7.3. Usage</a></li> <li><a class="reference internal" href="#parameters">8.11.7.4. Parameters</a><ul> <li><a class="reference internal" href="#required-parameter">8.11.7.4.1. Required parameter</a><ul> <li><a class="reference internal" href="#match-column">8.11.7.4.1.1. <tt class="docutils literal"><span class="pre">match_column</span></tt></a></li> <li><a class="reference internal" href="#query-string">8.11.7.4.1.2. <tt class="docutils literal"><span class="pre">query_string</span></tt></a></li> </ul> </li> <li><a class="reference internal" href="#optional-parameter">8.11.7.4.2. Optional parameter</a><ul> <li><a class="reference internal" href="#query-expander">8.11.7.4.2.1. <tt class="docutils literal"><span class="pre">query_expander</span></tt></a></li> <li><a class="reference internal" href="#substitution-table">8.11.7.4.2.2. <tt class="docutils literal"><span class="pre">substitution_table</span></tt></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#return-value">8.11.7.5. Return value</a></li> <li><a class="reference internal" href="#todo">8.11.7.6. TODO</a></li> <li><a class="reference internal" href="#see-also">8.11.7.7. See also</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="now.html" title="previous chapter">8.11.6. now</a></p> <h4>Next topic</h4> <p class="topless"><a href="rand.html" title="next chapter">8.11.8. rand</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/reference/functions/query.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="rand.html" title="8.11.8. rand" >next</a> |</li> <li class="right" > <a href="now.html" title="8.11.6. now" >previous</a> |</li> <li><a href="../../index.html">groonga v3.0.5 documentation</a> »</li> <li><a href="../../reference.html" >8. リファレンスマニュアル</a> »</li> <li><a href="../function.html" >8.11. Function</a> »</li> </ul> </div> <div class="footer"> © Copyright 2009-2013, Brazil, Inc. </div> </body> </html>