| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: logical_count |
| 5 |
|
| 6 |
``logical_count`` |
| 7 |
================= |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
.. versionadded:: 5.0.0 |
| 13 |
|
| 14 |
``logical_count`` is a command that has only count feature in |
| 15 |
:doc:`logical_select`. :doc:`logical_select` searches records from |
| 16 |
multiple tables, outputs the number of matched records, outputs |
| 17 |
columns of the matched records and so on. ``logical_count`` only |
| 18 |
searches records from multiple tables and output the number of matched |
| 19 |
records. |
| 20 |
|
| 21 |
``logical_count`` is useful when you just want the number of matched |
| 22 |
records. You can use ``logical_count`` and :doc:`logical_range_filter` |
| 23 |
at once. You can show the first N matched records before you get the |
| 24 |
number of matched records. If you use only :doc:`logical_select`, you |
| 25 |
need to wait finishing all search. |
| 26 |
|
| 27 |
You need to :doc:`plugin_register` ``sharding`` plugin because |
| 28 |
this command is included in ``sharding`` plugin. |
| 29 |
|
| 30 |
Syntax |
| 31 |
------ |
| 32 |
|
| 33 |
This command takes many parameters. |
| 34 |
|
| 35 |
The required parameters are ``logical_table`` and ``shard_key``. Other |
| 36 |
parameters are optional:: |
| 37 |
|
| 38 |
logical_count |
| 39 |
logical_table |
| 40 |
shard_key |
| 41 |
[min=null] |
| 42 |
[min_border="include"] |
| 43 |
[max=null] |
| 44 |
[max_border="include"] |
| 45 |
[filter=null] |
| 46 |
[post_filter=null] |
| 47 |
|
| 48 |
There are some parameters that can be only used as named |
| 49 |
parameters. You can't use these parameters as ordered parameters. You |
| 50 |
must specify parameter name. |
| 51 |
|
| 52 |
Here are parameters that can be only used as named parameters: |
| 53 |
|
| 54 |
* ``cache=no`` |
| 55 |
|
| 56 |
.. versionadded:: 7.0.9 |
| 57 |
|
| 58 |
This command has the following named parameters for dynamic columns: |
| 59 |
|
| 60 |
* ``columns[${NAME}].stage=null`` |
| 61 |
* ``columns[${NAME}].flags=COLUMN_SCALAR`` |
| 62 |
* ``columns[${NAME}].type=null`` |
| 63 |
* ``columns[${NAME}].value=null`` |
| 64 |
* ``columns[${NAME}].window.sort_keys=null`` |
| 65 |
* ``columns[${NAME}].window.group_keys=null`` |
| 66 |
|
| 67 |
You can use one or more alphabets, digits, ``_`` for ``${NAME}``. For |
| 68 |
example, ``column1`` is a valid ``${NAME}``. This is the same rule as |
| 69 |
normal column. See also :ref:`column-create-name`. |
| 70 |
|
| 71 |
Parameters that have the same ``${NAME}`` are grouped. |
| 72 |
|
| 73 |
For example, the following parameters specify one dynamic column: |
| 74 |
|
| 75 |
* ``--columns[name].stage initial`` |
| 76 |
* ``--columns[name].type UInt32`` |
| 77 |
* ``--columns[name].value 29`` |
| 78 |
|
| 79 |
The following parameters specify two dynamic columns: |
| 80 |
|
| 81 |
* ``--columns[name1].stage initial`` |
| 82 |
* ``--columns[name1].type UInt32`` |
| 83 |
* ``--columns[name1].value 29`` |
| 84 |
* ``--columns[name2].stage initial`` |
| 85 |
* ``--columns[name2].type Float`` |
| 86 |
* ``--columns[name2].value '_score * 0.1'`` |
| 87 |
|
| 88 |
.. _logical-count-usage: |
| 89 |
|
| 90 |
Usage |
| 91 |
----- |
| 92 |
|
| 93 |
Let's learn about usage with examples. This section shows many popular |
| 94 |
usages. |
| 95 |
|
| 96 |
You need to register ``sharding`` plugin because this command is |
| 97 |
included in ``sharding`` plugin. |
| 98 |
|
| 99 |
.. groonga-command |
| 100 |
.. include:: ../../example/reference/commands/logical_count/usage_plugin_register.log |
| 101 |
.. plugin_register sharding |
| 102 |
|
| 103 |
|
| 104 |
Note that ``logical_count`` is implemented as an experimental plugin, and the specification may be changed in the future. |
| 105 |
|
| 106 |
Here is the simple example which shows how to use this feature. Let's consider to count specified logs which are stored into multiple tables. |
| 107 |
|
| 108 |
Here are a schema definition and sample data to show usage. |
| 109 |
|
| 110 |
.. groonga-command |
| 111 |
.. include:: ../../example/reference/commands/logical_select/usage_setup.log |
| 112 |
.. table_create Entries_20150708 TABLE_HASH_KEY ShortText |
| 113 |
.. column_create Entries_20150708 created_at COLUMN_SCALAR Time |
| 114 |
.. column_create Entries_20150708 content COLUMN_SCALAR Text |
| 115 |
.. column_create Entries_20150708 n_likes COLUMN_SCALAR UInt32 |
| 116 |
.. column_create Entries_20150708 tag COLUMN_SCALAR ShortText |
| 117 |
.. |
| 118 |
.. table_create Entries_20150709 TABLE_HASH_KEY ShortText |
| 119 |
.. column_create Entries_20150709 created_at COLUMN_SCALAR Time |
| 120 |
.. column_create Entries_20150709 content COLUMN_SCALAR Text |
| 121 |
.. column_create Entries_20150709 n_likes COLUMN_SCALAR UInt32 |
| 122 |
.. column_create Entries_20150709 tag COLUMN_SCALAR ShortText |
| 123 |
.. |
| 124 |
.. table_create Terms TABLE_PAT_KEY ShortText \ |
| 125 |
.. --default_tokenizer TokenBigram \ |
| 126 |
.. --normalizer NormalizerAuto |
| 127 |
.. column_create Terms entries_key_index_20150708 \ |
| 128 |
.. COLUMN_INDEX|WITH_POSITION Entries_20150708 _key |
| 129 |
.. column_create Terms entries_content_index_20150708 \ |
| 130 |
.. COLUMN_INDEX|WITH_POSITION Entries_20150708 content |
| 131 |
.. column_create Terms entries_key_index_20150709 \ |
| 132 |
.. COLUMN_INDEX|WITH_POSITION Entries_20150709 _key |
| 133 |
.. column_create Terms entries_content_index_20150709 \ |
| 134 |
.. COLUMN_INDEX|WITH_POSITION Entries_20150709 content |
| 135 |
.. |
| 136 |
.. load --table Entries_20150708 |
| 137 |
.. [ |
| 138 |
.. {"_key": "The first post!", |
| 139 |
.. "created_at": "2015/07/08 00:00:00", |
| 140 |
.. "content": "Welcome! This is my first post!", |
| 141 |
.. "n_likes": 5, |
| 142 |
.. "tag": "Hello"}, |
| 143 |
.. {"_key": "Groonga", |
| 144 |
.. "created_at": "2015/07/08 01:00:00", |
| 145 |
.. "content": "I started to use Groonga. It's very fast!", |
| 146 |
.. "n_likes": 10, |
| 147 |
.. "tag": "Groonga"}, |
| 148 |
.. {"_key": "Mroonga", |
| 149 |
.. "created_at": "2015/07/08 02:00:00", |
| 150 |
.. "content": "I also started to use Mroonga. It's also very fast! Really fast!", |
| 151 |
.. "n_likes": 15, |
| 152 |
.. "tag": "Groonga"} |
| 153 |
.. ] |
| 154 |
.. |
| 155 |
.. load --table Entries_20150709 |
| 156 |
.. [ |
| 157 |
.. {"_key": "Good-bye Senna", |
| 158 |
.. "created_at": "2015/07/09 00:00:00", |
| 159 |
.. "content": "I migrated all Senna system!", |
| 160 |
.. "n_likes": 3, |
| 161 |
.. "tag": "Senna"}, |
| 162 |
.. {"_key": "Good-bye Tritonn", |
| 163 |
.. "created_at": "2015/07/09 01:00:00", |
| 164 |
.. "content": "I also migrated all Tritonn system!", |
| 165 |
.. "n_likes": 3, |
| 166 |
.. "tag": "Senna"} |
| 167 |
.. ] |
| 168 |
|
| 169 |
There are two tables, ``Entries_20150708`` and ``Entries_20150709``, |
| 170 |
for blog entries. |
| 171 |
|
| 172 |
.. note:: |
| 173 |
|
| 174 |
You need to use ``${LOGICAL_TABLE_NAME}_${YYYYMMDD}`` naming rule |
| 175 |
for table names. In this example, ``LOGICAL_TABLE_NAME`` is |
| 176 |
``Entries`` and ``YYYYMMDD`` is ``20150708`` or ``20150709``. |
| 177 |
|
| 178 |
An entry has title, created time, content, the number of likes for the |
| 179 |
entry and tag. Title is key of ``Entries_YYYYMMDD``. Created time is |
| 180 |
value of ``Entries_YYYYMMDD.created_at`` column. Content is value of |
| 181 |
``Entries_YYYYMMDD.content`` column. The number of likes is value of |
| 182 |
``Entries_YYYYMMDD.n_likes`` column. Tag is value of |
| 183 |
``Entries_YYYYMMDD.tag`` column. |
| 184 |
|
| 185 |
``Entries_YYYYMMDD._key`` column and ``Entries_YYYYMMDD.content`` |
| 186 |
column are indexed using ``TokenBigram`` tokenizer. So both |
| 187 |
``Entries_YYYYMMDD._key`` and ``Entries_YYYYMMDD.content`` are |
| 188 |
fulltext search ready. |
| 189 |
|
| 190 |
OK. The schema and data for examples are ready. |
| 191 |
|
| 192 |
Here is an example to count the number of records which have |
| 193 |
``Groonga`` or ``Senna`` in ``content`` column. ``logical_count`` |
| 194 |
searches records over all ``Entries_YYYYMMDD`` tables. |
| 195 |
|
| 196 |
.. groonga-command |
| 197 |
.. include:: ../../example/reference/commands/logical_count/usage.log |
| 198 |
.. logical_count \ |
| 199 |
.. --logical_table Entries \ |
| 200 |
.. --shard_key created_at \ |
| 201 |
.. --filter 'query("content", "Groonga OR Senna")' |
| 202 |
|
| 203 |
Here are matched records: |
| 204 |
|
| 205 |
* ``_key:"Groonga"`` in ``Entries_20150708`` |
| 206 |
|
| 207 |
* ``_key:"Good-bye Senna"`` in ``Entries_20150709`` |
| 208 |
|
| 209 |
Parameters |
| 210 |
---------- |
| 211 |
|
| 212 |
This section describes parameters of this command. |
| 213 |
|
| 214 |
Required parameters |
| 215 |
^^^^^^^^^^^^^^^^^^^ |
| 216 |
|
| 217 |
There are required parameters, ``logical_table`` and ``shard_key``. |
| 218 |
|
| 219 |
.. _logical-count-logical-table: |
| 220 |
|
| 221 |
``logical_table`` |
| 222 |
""""""""""""""""" |
| 223 |
|
| 224 |
Specifies logical table name. It means table name without |
| 225 |
``_YYYYMMDD`` postfix. If you use actual table such as |
| 226 |
``Logs_20150203``, ``Logs_20150203`` and so on, logical table name is |
| 227 |
``Logs``. |
| 228 |
|
| 229 |
You can count all records by specifying only ``logical_table`` and |
| 230 |
``shard_key`` parameters. They are required parameters. |
| 231 |
|
| 232 |
.. groonga-command |
| 233 |
.. include:: ../../example/reference/commands/logical_count/logical_table_existent.log |
| 234 |
.. logical_count \ |
| 235 |
.. --logical_table Entries \ |
| 236 |
.. --shard_key created_at |
| 237 |
|
| 238 |
If nonexistent table is specified, an error is returned. |
| 239 |
|
| 240 |
.. groonga-command |
| 241 |
.. include:: ../../example/reference/commands/logical_count/logical_table_nonexistent.log |
| 242 |
.. logical_count \ |
| 243 |
.. --logical_table Nonexistent \ |
| 244 |
.. --shard_key created_at |
| 245 |
|
| 246 |
.. _logical-count-shard-key: |
| 247 |
|
| 248 |
``shard_key`` |
| 249 |
""""""""""""" |
| 250 |
|
| 251 |
Specifies column name which is treated as shared key. Shard key is a |
| 252 |
column that stores data that is used for distributing records to |
| 253 |
suitable shards. |
| 254 |
|
| 255 |
Shard key must be ``Time`` type for now. |
| 256 |
|
| 257 |
See :ref:`logical-count-logical-table` how to specify ``shard_key``. |
| 258 |
|
| 259 |
Optional parameters |
| 260 |
^^^^^^^^^^^^^^^^^^^ |
| 261 |
|
| 262 |
There are optional parameters. |
| 263 |
|
| 264 |
.. _logical-count-min: |
| 265 |
|
| 266 |
``min`` |
| 267 |
""""""" |
| 268 |
|
| 269 |
Specifies the minimum value of ``shard_key`` column. If shard doesn't |
| 270 |
have any matched records, the shard isn't searched. |
| 271 |
|
| 272 |
For example, ``min`` is ``"2015/07/09 00:00:00"``, ``Entry_20150708`` |
| 273 |
isn't searched. Because ``Entry_20150708`` has only records for |
| 274 |
``"2015/07/08"``. |
| 275 |
|
| 276 |
The following example only uses ``Entry_20150709`` |
| 277 |
table. ``Entry_20150708`` isn't used. |
| 278 |
|
| 279 |
.. groonga-command |
| 280 |
.. include:: ../../example/reference/commands/logical_count/min.log |
| 281 |
.. logical_count \ |
| 282 |
.. --logical_table Entries \ |
| 283 |
.. --shard_key created_at \ |
| 284 |
.. --min "2015/07/09 00:00:00" |
| 285 |
|
| 286 |
.. _logical-count-min-border: |
| 287 |
|
| 288 |
``min_border`` |
| 289 |
"""""""""""""" |
| 290 |
|
| 291 |
Specifies whether the minimum value is included or not. Here is |
| 292 |
available values. |
| 293 |
|
| 294 |
.. list-table:: |
| 295 |
:header-rows: 1 |
| 296 |
|
| 297 |
* - Value |
| 298 |
- Description |
| 299 |
* - ``include`` |
| 300 |
- Includes ``min`` value. This is the default. |
| 301 |
* - ``exclude`` |
| 302 |
- Doesn't include ``min`` value. |
| 303 |
|
| 304 |
Here is an example for ``exclude``. The result doesn't include the |
| 305 |
``"Good-bye Senna"`` record because its ``created_at`` value is |
| 306 |
``"2015/07/09 00:00:00"``. |
| 307 |
|
| 308 |
.. groonga-command |
| 309 |
.. include:: ../../example/reference/commands/logical_count/min_border.log |
| 310 |
.. logical_count \ |
| 311 |
.. --logical_table Entries \ |
| 312 |
.. --shard_key created_at \ |
| 313 |
.. --min "2015/07/09 00:00:00" \ |
| 314 |
.. --min_border "exclude" |
| 315 |
|
| 316 |
.. _logical-count-max: |
| 317 |
|
| 318 |
``max`` |
| 319 |
""""""" |
| 320 |
|
| 321 |
Specifies the maximum value of ``shard_key`` column. If shard doesn't |
| 322 |
have any matched records, the shard isn't searched. |
| 323 |
|
| 324 |
For example, ``max`` is ``"2015/07/08 23:59:59"``, ``Entry_20150709`` |
| 325 |
isn't searched. Because ``Entry_20150709`` has only records for |
| 326 |
``""2015/07/09"``. |
| 327 |
|
| 328 |
The following example only uses ``Entry_20150708`` |
| 329 |
table. ``Entry_20150709`` isn't used. |
| 330 |
|
| 331 |
.. groonga-command |
| 332 |
.. include:: ../../example/reference/commands/logical_count/max.log |
| 333 |
.. logical_count \ |
| 334 |
.. --logical_table Entries \ |
| 335 |
.. --shard_key created_at \ |
| 336 |
.. --max "2015/07/08 23:59:59" |
| 337 |
|
| 338 |
.. _logical-count-max-border: |
| 339 |
|
| 340 |
``max_border`` |
| 341 |
"""""""""""""" |
| 342 |
|
| 343 |
Specifies whether the maximum value is included or not. Here is |
| 344 |
available values. |
| 345 |
|
| 346 |
.. list-table:: |
| 347 |
:header-rows: 1 |
| 348 |
|
| 349 |
* - Value |
| 350 |
- Description |
| 351 |
* - ``include`` |
| 352 |
- Includes ``max`` value. This is the default. |
| 353 |
* - ``exclude`` |
| 354 |
- Doesn't include ``max`` value. |
| 355 |
|
| 356 |
Here is an example for ``exclude``. The result doesn't include the |
| 357 |
``"Good-bye Senna"`` record because its ``created_at`` value is |
| 358 |
``"2015/07/09 00:00:00"``. |
| 359 |
|
| 360 |
.. groonga-command |
| 361 |
.. include:: ../../example/reference/commands/logical_count/max_border.log |
| 362 |
.. logical_count \ |
| 363 |
.. --logical_table Entries \ |
| 364 |
.. --shard_key created_at \ |
| 365 |
.. --max "2015/07/09 00:00:00" \ |
| 366 |
.. --max_border "exclude" |
| 367 |
|
| 368 |
.. _logical-count-search-related-parameters: |
| 369 |
|
| 370 |
Search related parameters |
| 371 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 372 |
|
| 373 |
This command provides :doc:`select` compatible search related |
| 374 |
parameters. |
| 375 |
|
| 376 |
.. _logical-count-filter: |
| 377 |
|
| 378 |
``filter`` |
| 379 |
"""""""""" |
| 380 |
|
| 381 |
Corresponds to :ref:`select-filter` in :doc:`select`. See |
| 382 |
:ref:`select-filter` for details. |
| 383 |
|
| 384 |
Here is an example: |
| 385 |
|
| 386 |
.. groonga-command |
| 387 |
.. include:: ../../example/reference/commands/logical_count/filter.log |
| 388 |
.. logical_count \ |
| 389 |
.. --logical_table Entries \ |
| 390 |
.. --shard_key created_at \ |
| 391 |
.. --filter "n_likes <= 5" |
| 392 |
|
| 393 |
.. _logical-count-post-filter: |
| 394 |
|
| 395 |
``post_filter`` |
| 396 |
""""""""""""""" |
| 397 |
|
| 398 |
.. versionadded:: 8.0.1 |
| 399 |
|
| 400 |
Specifies the filter text that is processed after ``filtered`` stage |
| 401 |
dynamic columns are generated. You can use ``post_filter`` to filter |
| 402 |
by ``filtered`` stage dynamic columns. Others are the same as |
| 403 |
:ref:`select-filter`. |
| 404 |
|
| 405 |
Here is an example that shows entries only in popular tag. All target |
| 406 |
entries have ``system`` or ``use`` words: |
| 407 |
|
| 408 |
.. groonga-command |
| 409 |
.. include:: ../../example/reference/commands/logical_count/post_filter.log |
| 410 |
.. logical_count \ |
| 411 |
.. --logical_table Entries \ |
| 412 |
.. --shard_key created_at \ |
| 413 |
.. --columns[n_likes_sum_per_tag].stage filtered \ |
| 414 |
.. --columns[n_likes_sum_per_tag].type UInt32 \ |
| 415 |
.. --columns[n_likes_sum_per_tag].value 'window_sum(n_likes)' \ |
| 416 |
.. --columns[n_likes_sum_per_tag].window.group_keys 'tag' \ |
| 417 |
.. --filter 'content @ "system" || content @ "use"' \ |
| 418 |
.. --post_filter 'n_likes_sum_per_tag > 10' \ |
| 419 |
.. --output_columns _key,n_likes,n_likes_sum_per_tag |
| 420 |
|
| 421 |
.. _logical-count-dynamic-column-related-parameters: |
| 422 |
|
| 423 |
Dynamic column related parameters |
| 424 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 425 |
|
| 426 |
.. versionadded:: 7.0.9 |
| 427 |
|
| 428 |
All dynamic column related parameters in :doc:`select` are |
| 429 |
supported. See :ref:`select-dynamic-column-related-parameters` for |
| 430 |
details. |
| 431 |
|
| 432 |
.. _logical-count-columns-name-stage: |
| 433 |
|
| 434 |
``columns[${NAME}].stage`` |
| 435 |
"""""""""""""""""""""""""" |
| 436 |
|
| 437 |
.. versionadded:: 7.0.9 |
| 438 |
|
| 439 |
Corresponds to :ref:`select-columns-name-stage` in :doc:`select`. See |
| 440 |
:ref:`select-columns-name-stage` for details. |
| 441 |
|
| 442 |
This is a required parameter. |
| 443 |
|
| 444 |
``initial`` stage and ``filtered`` stage are valid. Because there are |
| 445 |
no processes after ``output`` stages. |
| 446 |
|
| 447 |
.. versionadded:: 8.0.1 |
| 448 |
|
| 449 |
``filtered`` stage is valid since 8.0.1. |
| 450 |
|
| 451 |
Here is an example that creates ``is_popular`` column at ``initial`` |
| 452 |
stage. You can use ``is_popular`` in all parameters such as ``filter``: |
| 453 |
|
| 454 |
.. groonga-command |
| 455 |
.. include:: ../../example/reference/commands/logical_count/columns_name_stage.log |
| 456 |
.. logical_count \ |
| 457 |
.. --logical_table Entries \ |
| 458 |
.. --shard_key created_at \ |
| 459 |
.. --columns[is_popular].stage initial \ |
| 460 |
.. --columns[is_popular].type Bool \ |
| 461 |
.. --columns[is_popular].value 'n_likes >= 10' \ |
| 462 |
.. --filter is_popular |
| 463 |
|
| 464 |
.. _logical-count-columns-name-flags: |
| 465 |
|
| 466 |
``columns[${NAME}].flags`` |
| 467 |
"""""""""""""""""""""""""" |
| 468 |
|
| 469 |
.. versionadded:: 7.0.9 |
| 470 |
|
| 471 |
Corresponds to :ref:`select-columns-name-flags` in :doc:`select`. See |
| 472 |
:ref:`select-columns-name-flags` for details. |
| 473 |
|
| 474 |
The default value is ``COLUMN_SCALAR``. |
| 475 |
|
| 476 |
Here is an example that creates a vector column by ``COLUMN_VECTOR`` |
| 477 |
flags. ``plugin_register functions/vector`` is for using |
| 478 |
:doc:`/reference/functions/vector_new` function: |
| 479 |
|
| 480 |
.. groonga-command |
| 481 |
.. include:: ../../example/reference/commands/logical_count/columns_name_flags.log |
| 482 |
.. plugin_register functions/vector |
| 483 |
.. logical_count \ |
| 484 |
.. --logical_table Entries \ |
| 485 |
.. --shard_key created_at \ |
| 486 |
.. --columns[vector].stage initial \ |
| 487 |
.. --columns[vector].flags COLUMN_VECTOR \ |
| 488 |
.. --columns[vector].type UInt32 \ |
| 489 |
.. --columns[vector].value 'vector_new(1, 2, 3)' \ |
| 490 |
.. --filter 'vector_size(vector) > 2' |
| 491 |
|
| 492 |
.. _logical-count-columns-name-type: |
| 493 |
|
| 494 |
``columns[${NAME}].type`` |
| 495 |
""""""""""""""""""""""""" |
| 496 |
|
| 497 |
.. versionadded:: 7.0.9 |
| 498 |
|
| 499 |
Corresponds to :ref:`select-columns-name-type` in :doc:`select`. See |
| 500 |
:ref:`select-columns-name-type` for details. |
| 501 |
|
| 502 |
This is a required parameter. |
| 503 |
|
| 504 |
Here is an example that creates a ``ShortText`` type column. Stored |
| 505 |
value is casted to ``ShortText`` automatically. In this example, |
| 506 |
number is casted to ``ShortText``: |
| 507 |
|
| 508 |
.. groonga-command |
| 509 |
.. include:: ../../example/reference/commands/logical_count/columns_name_type.log |
| 510 |
.. logical_count \ |
| 511 |
.. --logical_table Entries \ |
| 512 |
.. --shard_key created_at \ |
| 513 |
.. --columns[n_likes_string].stage initial \ |
| 514 |
.. --columns[n_likes_string].type ShortText \ |
| 515 |
.. --columns[n_likes_string].value n_likes \ |
| 516 |
.. --filter 'n_likes_string == "3"' |
| 517 |
|
| 518 |
.. _logical-count-columns-name-value: |
| 519 |
|
| 520 |
``columns[${NAME}].value`` |
| 521 |
"""""""""""""""""""""""""" |
| 522 |
|
| 523 |
.. versionadded:: 7.0.9 |
| 524 |
|
| 525 |
Corresponds to :ref:`select-columns-name-value` in :doc:`select`. See |
| 526 |
:ref:`select-columns-name-value` for details. |
| 527 |
|
| 528 |
You need to specify :doc:`/reference/window_function` as ``value`` |
| 529 |
value and other window function related parameters when you use window |
| 530 |
function. See :ref:`logical-count-window-function-related-parameters` |
| 531 |
for details. |
| 532 |
|
| 533 |
This is a required parameter. |
| 534 |
|
| 535 |
Here is an example that creates a new dynamic column that stores the |
| 536 |
number of characters of content. This example uses |
| 537 |
:doc:`/reference/functions/string_length` function in |
| 538 |
``functions/string`` plugin to compute the number of characters in a |
| 539 |
string. :doc:`plugin_register` is used to register |
| 540 |
``functions/string`` plugin: |
| 541 |
|
| 542 |
.. groonga-command |
| 543 |
.. include:: ../../example/reference/commands/logical_count/columns_name_value.log |
| 544 |
.. plugin_register functions/string |
| 545 |
.. logical_count \ |
| 546 |
.. --logical_table Entries \ |
| 547 |
.. --shard_key created_at \ |
| 548 |
.. --columns[content_length].stage initial \ |
| 549 |
.. --columns[content_length].type UInt32 \ |
| 550 |
.. --columns[content_length].value 'string_length(content)' \ |
| 551 |
.. --filter 'content_length >= 40' |
| 552 |
|
| 553 |
.. _logical-count-window-function-related-parameters: |
| 554 |
|
| 555 |
Window function related parameters |
| 556 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 557 |
|
| 558 |
.. versionadded:: 7.0.9 |
| 559 |
|
| 560 |
All window function related parameters in :doc:`select` are |
| 561 |
supported. See :ref:`select-window-function-related-parameters` for |
| 562 |
details. |
| 563 |
|
| 564 |
.. note:: |
| 565 |
|
| 566 |
Window function over multiple tables aren't supported version 9.0.1 or before. |
| 567 |
Groonga supported it since version 9.0.2 or later. |
| 568 |
However, we need to align the same order for shard key and leading group key or sort key. |
| 569 |
|
| 570 |
For example, we can apply the window function to over multiple tables as below case. |
| 571 |
Because the below example aligns the same order for shard key and leading group key. |
| 572 |
|
| 573 |
The leading group key is ``price`` and shard key is ``timestamp`` in the below example. |
| 574 |
We can apply the window function to over multiple tables in the below example. |
| 575 |
Because ``price`` and ``timestamp`` aligns the same order. |
| 576 |
|
| 577 |
.. groonga-command |
| 578 |
.. include:: ../../example/reference/commands/logical_count/window_function_for_over_shard.log |
| 579 |
.. plugin_register sharding |
| 580 |
.. |
| 581 |
.. table_create Logs_20170415 TABLE_NO_KEY |
| 582 |
.. column_create Logs_20170415 timestamp COLUMN_SCALAR Time |
| 583 |
.. column_create Logs_20170415 price COLUMN_SCALAR UInt32 |
| 584 |
.. column_create Logs_20170415 n_likes COLUMN_SCALAR UInt32 |
| 585 |
.. |
| 586 |
.. table_create Logs_20170416 TABLE_NO_KEY |
| 587 |
.. column_create Logs_20170416 timestamp COLUMN_SCALAR Time |
| 588 |
.. column_create Logs_20170416 price COLUMN_SCALAR UInt32 |
| 589 |
.. column_create Logs_20170416 n_likes COLUMN_SCALAR UInt32 |
| 590 |
.. |
| 591 |
.. load --table Logs_20170415 |
| 592 |
.. [ |
| 593 |
.. {"timestamp": "2017/04/15 00:00:00", "n_likes": 2, "price": 100}, |
| 594 |
.. {"timestamp": "2017/04/15 01:00:00", "n_likes": 1, "price": 100}, |
| 595 |
.. {"timestamp": "2017/04/15 01:00:00", "n_likes": 2, "price": 200} |
| 596 |
.. ] |
| 597 |
.. |
| 598 |
.. load --table Logs_20170416 |
| 599 |
.. [ |
| 600 |
.. {"timestamp": "2017/04/16 10:00:00", "n_likes": 1, "price": 200}, |
| 601 |
.. {"timestamp": "2017/04/16 11:00:00", "n_likes": 2, "price": 300}, |
| 602 |
.. {"timestamp": "2017/04/16 11:00:00", "n_likes": 1, "price": 300} |
| 603 |
.. ] |
| 604 |
.. |
| 605 |
.. logical_count Logs \ |
| 606 |
.. --shard_key timestamp \ |
| 607 |
.. --columns[count].stage initial \ |
| 608 |
.. --columns[count].type UInt32 \ |
| 609 |
.. --columns[count].flags COLUMN_SCALAR \ |
| 610 |
.. --columns[count].value 'window_count()' \ |
| 611 |
.. --columns[count].window.group_keys price |
| 612 |
|
| 613 |
.. _logical-count-columns-name-window-sort-keys: |
| 614 |
|
| 615 |
``columns[${NAME}].window.sort_keys`` |
| 616 |
""""""""""""""""""""""""""""""""""""" |
| 617 |
|
| 618 |
.. versionadded:: 7.0.9 |
| 619 |
|
| 620 |
Corresponds to :ref:`select-columns-name-window-sort-keys` in |
| 621 |
:doc:`select`. See :ref:`select-columns-name-window-sort-keys` for |
| 622 |
details. |
| 623 |
|
| 624 |
You must specify :ref:`logical-count-columns-name-window-sort-keys` |
| 625 |
or :ref:`logical-count-columns-name-window-group-keys` to use window |
| 626 |
function. |
| 627 |
|
| 628 |
Here is an example that computes cumulative sum per |
| 629 |
``Entries.tag``. Each group is sorted by ``Entries._key``: |
| 630 |
|
| 631 |
.. groonga-command |
| 632 |
.. include:: ../../example/reference/commands/logical_count/columns_name_window_sort_keys.log |
| 633 |
.. logical_count \ |
| 634 |
.. --logical_table Entries \ |
| 635 |
.. --shard_key created_at \ |
| 636 |
.. --columns[n_likes_cumulative_sum_per_tag].stage initial \ |
| 637 |
.. --columns[n_likes_cumulative_sum_per_tag].type UInt32 \ |
| 638 |
.. --columns[n_likes_cumulative_sum_per_tag].value 'window_sum(n_likes)' \ |
| 639 |
.. --columns[n_likes_cumulative_sum_per_tag].window.sort_keys _key \ |
| 640 |
.. --columns[n_likes_cumulative_sum_per_tag].window.group_keys tag \ |
| 641 |
.. --filter 'n_likes_cumulative_sum_per_tag > 5' |
| 642 |
|
| 643 |
.. _logical-count-columns-name-window-group-keys: |
| 644 |
|
| 645 |
``columns[${NAME}].window.group_keys`` |
| 646 |
"""""""""""""""""""""""""""""""""""""" |
| 647 |
|
| 648 |
.. versionadded:: 7.0.9 |
| 649 |
|
| 650 |
Corresponds to :ref:`select-columns-name-window-group-keys` in |
| 651 |
:doc:`select`. See :ref:`select-columns-name-window-group-keys` for |
| 652 |
details. |
| 653 |
|
| 654 |
You must specify :ref:`logical-count-columns-name-window-sort-keys` |
| 655 |
or :ref:`logical-count-columns-name-window-group-keys` to use window |
| 656 |
function. |
| 657 |
|
| 658 |
Here is an example that computes sum per ``Entries.tag``: |
| 659 |
|
| 660 |
.. groonga-command |
| 661 |
.. include:: ../../example/reference/commands/logical_count/columns_name_window_group_keys.log |
| 662 |
.. logical_count \ |
| 663 |
.. --logical_table Entries \ |
| 664 |
.. --shard_key created_at \ |
| 665 |
.. --columns[n_likes_sum_per_tag].stage initial \ |
| 666 |
.. --columns[n_likes_sum_per_tag].type UInt32 \ |
| 667 |
.. --columns[n_likes_sum_per_tag].value 'window_sum(n_likes)' \ |
| 668 |
.. --columns[n_likes_sum_per_tag].window.group_keys tag \ |
| 669 |
.. --filter 'n_likes_sum_per_tag > 5' |
| 670 |
|
| 671 |
Cache related parameter |
| 672 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 673 |
|
| 674 |
.. _logical-count-cache: |
| 675 |
|
| 676 |
``cache`` |
| 677 |
""""""""" |
| 678 |
|
| 679 |
Specifies whether caching the result of this query or not. |
| 680 |
|
| 681 |
If the result of this query is cached, the next same query returns |
| 682 |
response quickly by using the cache. |
| 683 |
|
| 684 |
It doesn't control whether existing cached result is used or not. |
| 685 |
|
| 686 |
Here are available values: |
| 687 |
|
| 688 |
.. list-table:: |
| 689 |
:header-rows: 1 |
| 690 |
|
| 691 |
* - Value |
| 692 |
- Description |
| 693 |
* - ``no`` |
| 694 |
- Don't cache the output of this query. |
| 695 |
* - ``yes`` |
| 696 |
- Cache the output of this query. |
| 697 |
It's the default value. |
| 698 |
|
| 699 |
Here is an example to disable caching the result of this query: |
| 700 |
|
| 701 |
.. groonga-command |
| 702 |
.. include:: ../../example/reference/commands/logical_count/cache_no.log |
| 703 |
.. logical_count \ |
| 704 |
.. --logical_table Entries \ |
| 705 |
.. --shard_key created_at \ |
| 706 |
.. --cache no |
| 707 |
|
| 708 |
The default value is ``yes``. |
| 709 |
|
| 710 |
Return value |
| 711 |
------------ |
| 712 |
|
| 713 |
The command returns a response with the following format:: |
| 714 |
|
| 715 |
[HEADER, N_HITS] |
| 716 |
|
| 717 |
If the command fails, error details are in ``HEADER``. |
| 718 |
|
| 719 |
See :doc:`/reference/command/output_format` for ``HEADER``. |
| 720 |
|
| 721 |
``N_HITS`` is the number of matched records. |