| 1 |
.. -*- rst -*- |
| 2 |
.. Groonga Project |
| 3 |
|
| 4 |
.. groonga-include : search.rst |
| 5 |
|
| 6 |
.. groonga-command |
| 7 |
.. database: tutorial |
| 8 |
|
| 9 |
Drilldown |
| 10 |
========= |
| 11 |
|
| 12 |
You learned how to search and sort searched results in the previous sections. |
| 13 |
Now that you can search as you likes, but how do you summarize the number of records which has specific value in the column? |
| 14 |
|
| 15 |
As you know, there is a naive solution to execute query by every the value of column, then you can get the number of records as a result. It is a simple way, but it is not reasonable to many records. |
| 16 |
|
| 17 |
If you are familiar with SQL, you will doubt with "Is there a similar SQL functionality to ``GROUP BY`` in Groonga?". |
| 18 |
|
| 19 |
Of course, Groonga provides such a functionality. It's called as ``drilldown``. |
| 20 |
|
| 21 |
``drilldown`` enables you to get the number of records which belongs to specific the value of column at once. |
| 22 |
|
| 23 |
To illustrate this feature, imagine the case that classification by domain and grouping by country that domain belongs to. |
| 24 |
|
| 25 |
Here is the concrete examples how to use this feature. |
| 26 |
|
| 27 |
In this example, we add two columns to ``Site`` table. ``domain`` column is used for TLD (top level domain). ``country`` column is used for country name. The type of these columns are ``SiteDomain`` table which uses domain name as a primary key and ``SiteCountry`` table which uses country name as a primary key. |
| 28 |
|
| 29 |
.. groonga-command |
| 30 |
.. include:: ../example/tutorial/drilldown-1.log |
| 31 |
.. table_create --name SiteDomain --flags TABLE_HASH_KEY --key_type ShortText |
| 32 |
.. table_create --name SiteCountry --flags TABLE_HASH_KEY --key_type ShortText |
| 33 |
.. column_create --table Site --name domain --flags COLUMN_SCALAR --type SiteDomain |
| 34 |
.. column_create --table Site --name country --flags COLUMN_SCALAR --type SiteCountry |
| 35 |
.. load --table Site |
| 36 |
.. [ |
| 37 |
.. {"_key":"http://example.org/","domain":".org","country":"japan"}, |
| 38 |
.. {"_key":"http://example.net/","domain":".net","country":"brazil"}, |
| 39 |
.. {"_key":"http://example.com/","domain":".com","country":"japan"}, |
| 40 |
.. {"_key":"http://example.net/afr","domain":".net","country":"usa"}, |
| 41 |
.. {"_key":"http://example.org/aba","domain":".org","country":"korea"}, |
| 42 |
.. {"_key":"http://example.com/rab","domain":".com","country":"china"}, |
| 43 |
.. {"_key":"http://example.net/atv","domain":".net","country":"china"}, |
| 44 |
.. {"_key":"http://example.org/gat","domain":".org","country":"usa"}, |
| 45 |
.. {"_key":"http://example.com/vdw","domain":".com","country":"japan"} |
| 46 |
.. ] |
| 47 |
|
| 48 |
Here is a example of drilldown with ``domain`` column. Three kind of values are used in ``domain`` column - ".org", ".net" and ".com". |
| 49 |
|
| 50 |
.. groonga-command |
| 51 |
.. include:: ../example/tutorial/drilldown-domain.log |
| 52 |
.. select --table Site --limit 0 --drilldown domain |
| 53 |
|
| 54 |
|
| 55 |
Here is a summary of above query. |
| 56 |
|
| 57 |
.. list-table:: Drilldown by ``domain`` column |
| 58 |
:header-rows: 1 |
| 59 |
|
| 60 |
* - Group by |
| 61 |
- The number of group records |
| 62 |
- Group records means following records |
| 63 |
* - .org |
| 64 |
- 3 |
| 65 |
- * http://example.org/ |
| 66 |
* http://example.org/aba |
| 67 |
* http://example.org/gat |
| 68 |
* - .net |
| 69 |
- 3 |
| 70 |
- * http://example.net/ |
| 71 |
* http://example.net/afr |
| 72 |
* http://example.net/atv |
| 73 |
* - .com |
| 74 |
- 3 |
| 75 |
- * http://example.com/ |
| 76 |
* http://example.com/rab |
| 77 |
* http://example.com/vdw |
| 78 |
|
| 79 |
|
| 80 |
The value of drilldown are returned as the value of ``_nsubrecs`` column. In this case, ``Site`` table is grouped by ".org", ".net", ".com" domain. ``_nsubrecs`` shows that each three domain has three records. |
| 81 |
|
| 82 |
If you execute drilldown to the column which has table as a type, you can get the value of column which is stored in referenced table. |
| 83 |
``_nsubrecs`` pseudo column is added to the table which is used for drilldown. this pseudo column stores the number of records which is grouped by. |
| 84 |
|
| 85 |
Then, investigate referenced table in detail. As ``Site`` table use ``SiteDomain`` table as column type of ``domain``, you can use ``--drilldown_output_columns`` to know detail of referenced column. |
| 86 |
|
| 87 |
.. groonga-command |
| 88 |
.. include:: ../example/tutorial/drilldown-output-columns.log |
| 89 |
.. select --table Site --limit 0 --drilldown domain --drilldown_output_columns _id,_key,_nsubrecs |
| 90 |
|
| 91 |
Now, you can see detail of each grouped domain, drilldown by ``country`` column which has ".org" as column value. |
| 92 |
|
| 93 |
.. groonga-command |
| 94 |
.. include:: ../example/tutorial/drilldown-country.log |
| 95 |
.. select --table Site --limit 0 --filter "domain._id == 1" --drilldown country |
| 96 |
|
| 97 |
Drilldown with multiple column |
| 98 |
------------------------------ |
| 99 |
|
| 100 |
Drilldown feature supports multiple column. Use comma separated multiple column names as ``drilldown`` parameter. |
| 101 |
You can get the each result of drilldown at once. |
| 102 |
|
| 103 |
.. groonga-command |
| 104 |
.. include:: ../example/tutorial/drilldown-multiple-column.log |
| 105 |
.. select --table Site --limit 0 --drilldown domain,country |
| 106 |
|
| 107 |
|
| 108 |
Sorting drilldown results |
| 109 |
------------------------- |
| 110 |
|
| 111 |
Use ``--drilldown_sort_keys`` if you want to sort the result of drilldown. For example, specify ``_nsubrecs`` as ascending order. |
| 112 |
|
| 113 |
.. groonga-command |
| 114 |
.. include:: ../example/tutorial/drilldown-sortby.log |
| 115 |
.. select --table Site --limit 0 --drilldown country --drilldown_sort_keys _nsubrecs |
| 116 |
|
| 117 |
|
| 118 |
limits drilldown results |
| 119 |
------------------------ |
| 120 |
|
| 121 |
The number of drilldown results is limited to 10 as a default. Use ``drilldown_limit`` and ``drilldown_offset`` parameter to customize orilldown results. |
| 122 |
|
| 123 |
.. groonga-command |
| 124 |
.. include:: ../example/tutorial/drilldown-limit.log |
| 125 |
.. select --table Site --limit 0 --drilldown country --drilldown_sort_keys _nsubrecs --drilldown_limit 2 --drilldown_offset 2 |
| 126 |
|
| 127 |
Note that drilldown to the column which stores string is slower than the columns which stores the other types. |
| 128 |
If you drilldown to string type of column, create the table that type of primary key is string, then create the column which refers that table. |