| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_query |
| 5 |
|
| 6 |
``query`` |
| 7 |
========= |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
``query`` provides ``--match_columns`` and ``--query`` parameters of |
| 13 |
:doc:`/reference/commands/select` feature as function. You can specify |
| 14 |
multiple ``query`` functions in ``--filter`` parameter in |
| 15 |
:doc:`/reference/commands/select`. |
| 16 |
|
| 17 |
Because of such flexibility, you can control full text search behavior |
| 18 |
by combination of multiple ``query`` functions. |
| 19 |
|
| 20 |
``query`` can be used in only ``--filter`` in |
| 21 |
:doc:`/reference/commands/select`. |
| 22 |
|
| 23 |
Syntax |
| 24 |
------ |
| 25 |
|
| 26 |
``query`` requires two parameters - ``match_columns`` and |
| 27 |
``query_string``. |
| 28 |
|
| 29 |
The parameter ``query_expander`` and ``options`` are optional:: |
| 30 |
|
| 31 |
query(match_columns, query_string) |
| 32 |
query(match_columns, query_string, query_expander) |
| 33 |
query(match_columns, query_string, options) |
| 34 |
|
| 35 |
``options`` uses the following format. All of key-value pairs are |
| 36 |
optional:: |
| 37 |
|
| 38 |
{ |
| 39 |
"expander": query_expander, |
| 40 |
"default_mode": default_mode, |
| 41 |
"default_operator": default_operator, |
| 42 |
"flags": flags |
| 43 |
} |
| 44 |
|
| 45 |
Usage |
| 46 |
----- |
| 47 |
|
| 48 |
Here are a schema definition and sample data to show usage. |
| 49 |
|
| 50 |
Sample schema: |
| 51 |
|
| 52 |
.. groonga-command |
| 53 |
.. include:: ../../example/reference/functions/query/usage_setup_schema.log |
| 54 |
.. table_create Documents TABLE_NO_KEY |
| 55 |
.. column_create Documents content COLUMN_SCALAR Text |
| 56 |
.. table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto |
| 57 |
.. column_create Terms documents_content_index COLUMN_INDEX|WITH_POSITION Documents content |
| 58 |
.. table_create Users TABLE_NO_KEY |
| 59 |
.. column_create Users name COLUMN_SCALAR ShortText |
| 60 |
.. column_create Users memo COLUMN_SCALAR ShortText |
| 61 |
.. table_create Lexicon TABLE_HASH_KEY ShortText \ |
| 62 |
.. --default_tokenizer TokenBigramSplitSymbolAlphaDigit \ |
| 63 |
.. --normalizer NormalizerAuto |
| 64 |
.. column_create Lexicon users_name COLUMN_INDEX|WITH_POSITION Users name |
| 65 |
.. column_create Lexicon users_memo COLUMN_INDEX|WITH_POSITION Users memo |
| 66 |
|
| 67 |
Sample data: |
| 68 |
|
| 69 |
.. groonga-command |
| 70 |
.. include:: ../../example/reference/functions/query/usage_setup_data.log |
| 71 |
.. load --table Users |
| 72 |
.. [ |
| 73 |
.. {"name": "Alice", "memo": "groonga user"}, |
| 74 |
.. {"name": "Alisa", "memo": "mroonga user"}, |
| 75 |
.. {"name": "Bob", "memo": "rroonga user"}, |
| 76 |
.. {"name": "Tom", "memo": "nroonga user"}, |
| 77 |
.. {"name": "Tobby", "memo": "groonga and mroonga user. mroonga is ..."}, |
| 78 |
.. ] |
| 79 |
|
| 80 |
Here is the simple usage of ``query`` function which executes full |
| 81 |
text search by keyword ``alice`` without using ``--match_columns`` and |
| 82 |
``--query`` arguments in ``--filter``. |
| 83 |
|
| 84 |
.. groonga-command |
| 85 |
.. include:: ../../example/reference/functions/query/usage_basic.log |
| 86 |
.. select Users --output_columns name,_score --filter 'query("name * 10", "alice")' |
| 87 |
|
| 88 |
When executing above query, the keyword ``alice`` is weighted to the |
| 89 |
value ``10``. |
| 90 |
|
| 91 |
Here are the contrasting examples with/without ``query``. |
| 92 |
|
| 93 |
.. groonga-command |
| 94 |
.. include:: ../../example/reference/functions/query/usage_without_query.log |
| 95 |
.. select Users --output_columns name,memo,_score --match_columns "memo * 10" --query "memo:@groonga OR memo:@mroonga OR memo:@user" --sort_keys -_score |
| 96 |
|
| 97 |
In this case, the all keywords ``groonga``, ``mroonga`` and ``user`` |
| 98 |
use the default weight. You can't pass different weight value to each |
| 99 |
keyword in this way. |
| 100 |
|
| 101 |
.. groonga-command |
| 102 |
.. include:: ../../example/reference/functions/query/usage_with_query.log |
| 103 |
.. select Users --output_columns name,memo,_score --filter 'query("memo * 10", "groonga") || query("memo * 20", "mroonga") || query("memo * 1", "user")' --sort_keys -_score |
| 104 |
|
| 105 |
On the other hand, by specifying multiple ``query``, the keywords |
| 106 |
``groonga``, ``mroonga`` and ``user`` use different weight. |
| 107 |
|
| 108 |
As a result, you can control full text search result score by |
| 109 |
specifying different weight to the keywords on your purpose. |
| 110 |
|
| 111 |
Parameters |
| 112 |
---------- |
| 113 |
|
| 114 |
Required parameters |
| 115 |
^^^^^^^^^^^^^^^^^^^ |
| 116 |
|
| 117 |
There are two required parameters, ``match_columns`` and ``query_string``. |
| 118 |
|
| 119 |
``match_columns`` |
| 120 |
""""""""""""""""" |
| 121 |
|
| 122 |
Specifies the default target columns for full text search by |
| 123 |
``query_string`` parameter value. It is the same role as |
| 124 |
:ref:`select-match-columns` parameter in ``select``. |
| 125 |
|
| 126 |
``query_string`` |
| 127 |
"""""""""""""""" |
| 128 |
|
| 129 |
Specifies the search condition in |
| 130 |
:doc:`/reference/grn_expr/query_syntax`. It is the same role as |
| 131 |
``query`` parameter in ``select``. |
| 132 |
|
| 133 |
See :ref:`select-match-columns` about ``query`` parameter in |
| 134 |
``select``. |
| 135 |
|
| 136 |
Optional parameters |
| 137 |
^^^^^^^^^^^^^^^^^^^ |
| 138 |
|
| 139 |
There are some optional parameters. |
| 140 |
|
| 141 |
.. _query-query-expander: |
| 142 |
|
| 143 |
``query_expander`` |
| 144 |
"""""""""""""""""" |
| 145 |
|
| 146 |
Specifies the query expander name or substitution column name for |
| 147 |
query expansion. |
| 148 |
|
| 149 |
See :doc:`/reference/query_expanders` for available query expanders. |
| 150 |
|
| 151 |
Substitution column name uses ``${TABLE}.${COLUMN}`` format. |
| 152 |
|
| 153 |
See :ref:`select-query-expander` for details. |
| 154 |
|
| 155 |
.. _query-default-mode: |
| 156 |
|
| 157 |
``default_mode`` |
| 158 |
"""""""""""""""" |
| 159 |
|
| 160 |
Specifies the default search mode. You can change search mode by |
| 161 |
``column:@keyword`` like syntax. The default search mode is used when |
| 162 |
you just specify ``keyword`` instead of ``column:@keyword``. See |
| 163 |
:doc:`/reference/grn_expr/query_syntax` for more syntax details. |
| 164 |
|
| 165 |
Here are available modes. The default is ``"MATCH"`` mode. It does |
| 166 |
full text search. |
| 167 |
|
| 168 |
.. list-table:: |
| 169 |
:header-rows: 1 |
| 170 |
|
| 171 |
* - Mode |
| 172 |
- Aliases |
| 173 |
- Description |
| 174 |
* - ``"EQUAL"`` |
| 175 |
- ``"=="`` |
| 176 |
- It uses :ref:`query-syntax-equal-condition` as the default mode. |
| 177 |
* - ``"NOT_EQUAL"`` |
| 178 |
- ``"!="`` |
| 179 |
- It uses :ref:`query-syntax-not-equal-condition` as the default mode. |
| 180 |
* - ``"LESS"`` |
| 181 |
- ``"<"`` |
| 182 |
- It uses :ref:`query-syntax-less-than-condition` as the default mode. |
| 183 |
* - ``"GREATER"`` |
| 184 |
- ``">"`` |
| 185 |
- It uses :ref:`query-syntax-greater-than-condition` as the default mode. |
| 186 |
* - ``"LESS_EQUAL"`` |
| 187 |
- ``"<="`` |
| 188 |
- It uses :ref:`query-syntax-less-than-or-equal-condition` as the |
| 189 |
default mode. |
| 190 |
* - ``"GREATER_EQUAL"`` |
| 191 |
- ``">="`` |
| 192 |
- It uses :ref:`query-syntax-greater-than-or-equal-condition` as the |
| 193 |
default mode. |
| 194 |
* - ``"MATCH"`` |
| 195 |
- ``"@"`` |
| 196 |
- It uses :ref:`query-syntax-full-text-search-condition` as the |
| 197 |
default mode. |
| 198 |
|
| 199 |
It's the default. |
| 200 |
* - ``"NEAR"`` |
| 201 |
- ``"*N"`` |
| 202 |
- It uses :ref:`query-syntax-near-search-condition` as the default |
| 203 |
mode. |
| 204 |
* - ``"SIMILAR"`` |
| 205 |
- ``"*S"`` |
| 206 |
- It uses :ref:`query-syntax-similar-search-condition` as the default |
| 207 |
mode. |
| 208 |
* - ``"PREFIX"`` |
| 209 |
- ``"^"``, ``"@^"`` |
| 210 |
- It uses :ref:`query-syntax-prefix-search-condition` as the default |
| 211 |
mode. |
| 212 |
* - ``"SUFFIX"`` |
| 213 |
- ``"$"``, ``"@$"`` |
| 214 |
- It uses :ref:`query-syntax-suffix-search-condition` as the default |
| 215 |
mode. |
| 216 |
* - ``"REGEXP"`` |
| 217 |
- ``"~"``, ``"@~"`` |
| 218 |
- It uses :ref:`query-syntax-regular-expression-condition` as the default |
| 219 |
mode. |
| 220 |
|
| 221 |
.. _query-default-operator: |
| 222 |
|
| 223 |
``default_operator`` |
| 224 |
"""""""""""""""""""" |
| 225 |
|
| 226 |
Specifies the default logical operator. It's used when no logical |
| 227 |
operator such as ``OR`` and ``-`` are specified between conditional |
| 228 |
expressions like ``keyword1 keyword2``. The default logical operator |
| 229 |
is used to combine ``keyword1`` result set and ``keyword2`` result |
| 230 |
set. The default logical operator is ``AND``. So ``keyword1 keyword2`` |
| 231 |
result set includes only records that they are included in both |
| 232 |
``keyword1`` result set and ``keyword2`` result set. |
| 233 |
|
| 234 |
Here are available logical operators. The default is ``"AND"``. |
| 235 |
|
| 236 |
.. list-table:: |
| 237 |
:header-rows: 1 |
| 238 |
|
| 239 |
* - Logical operator |
| 240 |
- Aliases |
| 241 |
- Description |
| 242 |
* - ``"AND"`` |
| 243 |
- ``"&&"``, ``"+"`` |
| 244 |
- It uses :ref:`query-syntax-logical-and` as the default logical |
| 245 |
operator. |
| 246 |
* - ``"OR"`` |
| 247 |
- ``"||"`` |
| 248 |
- It uses :ref:`query-syntax-logical-or` as the default logical |
| 249 |
operator. |
| 250 |
* - ``"AND_NOT"`` |
| 251 |
- ``"&!"``, ``"-"`` |
| 252 |
- It uses :ref:`query-syntax-logical-and-not` as the default |
| 253 |
logical operator. |
| 254 |
|
| 255 |
.. _query-flags: |
| 256 |
|
| 257 |
``flags`` |
| 258 |
""""""""" |
| 259 |
|
| 260 |
Specifies the flags that customizes how to parse query. |
| 261 |
|
| 262 |
You can specify multiple flags by separating each flags by ``|``. Here |
| 263 |
is the example to specify multiple flags:: |
| 264 |
|
| 265 |
query("title * 10 || content", |
| 266 |
"keyword", |
| 267 |
{"flags": "ALLOW_COLUMN|ALLOW_LEADING_NOT"}) |
| 268 |
|
| 269 |
See :ref:`select-query-flags` for available flags. |
| 270 |
|
| 271 |
Return value |
| 272 |
------------ |
| 273 |
|
| 274 |
This function returns whether a record is matched or not as boolean. |
| 275 |
|
| 276 |
This function is also worked as a selector. It means that this |
| 277 |
function can be executable effectively. |
| 278 |
|
| 279 |
See also |
| 280 |
-------- |
| 281 |
|
| 282 |
* :doc:`/reference/commands/select` |
| 283 |
* :doc:`query_parallel_or` |