| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: alias |
| 5 |
|
| 6 |
Alias |
| 7 |
===== |
| 8 |
|
| 9 |
.. versionadded:: 5.1.2 |
| 10 |
|
| 11 |
You can refer a table and column by multiple names by using alias |
| 12 |
feature. |
| 13 |
|
| 14 |
Summary |
| 15 |
------- |
| 16 |
|
| 17 |
The alias feature is useful for the following cases: |
| 18 |
|
| 19 |
* You want to rename a table but you can't change some Groonga |
| 20 |
clients that uses the current table name. |
| 21 |
|
| 22 |
* You want to change column type without downtime. |
| 23 |
|
| 24 |
In the former case, some Groonga clients can use the current table |
| 25 |
name after you rename a table. Because the alias feature maps the |
| 26 |
current table name to the renamed new table name. |
| 27 |
|
| 28 |
In the latter case, all Groonga clients access the column by aliased |
| 29 |
name such as ``aliased_column``. ``aliased_column`` refers |
| 30 |
``current_column``. You create a new column ``new_column`` with new |
| 31 |
type and copy data from ``current_column`` by |
| 32 |
:doc:`/reference/commands/column_copy`. You change ``aliased_column`` |
| 33 |
to refer ``new_column`` from ``current_column``. Now, all Groonga |
| 34 |
clients access ``new_column`` by ``aliased_column`` without stopping |
| 35 |
search requests. |
| 36 |
|
| 37 |
Usage |
| 38 |
----- |
| 39 |
|
| 40 |
You manage alias to real name mapping by a normal table and a normal |
| 41 |
column. |
| 42 |
|
| 43 |
You can use any table type except :ref:`table-no-key` for the |
| 44 |
table. :ref:`table-hash-key` is recommended because exact key match |
| 45 |
search is only used for the alias feature. :ref:`table-hash-key` is |
| 46 |
the fastest table type for exact key match search. |
| 47 |
|
| 48 |
The column must be :doc:`/reference/columns/scalar` and type is |
| 49 |
``ShortText``. You can also use ``Text`` and ``LongText`` types but |
| 50 |
they are meaningless. Because the max table/column name size is |
| 51 |
4KiB. ``ShortText`` can store 4KiB data. |
| 52 |
|
| 53 |
Here are example definitions of table and column for managing aliases: |
| 54 |
|
| 55 |
.. groonga-command |
| 56 |
.. include:: ../example/reference/alias/table_and_column.log |
| 57 |
.. table_create Aliases TABLE_HASH_KEY ShortText |
| 58 |
.. column_create Aliases real_name COLUMN_SCALAR ShortText |
| 59 |
|
| 60 |
You need to register the table and column by :doc:`configuration`. |
| 61 |
The alias feature uses ``alias.column`` configuration item. You can |
| 62 |
register the table and column by the following |
| 63 |
:doc:`/reference/commands/config_set`: |
| 64 |
|
| 65 |
.. groonga-command |
| 66 |
.. include:: ../example/reference/alias/register.log |
| 67 |
.. config_set alias.column Aliases.real_name |
| 68 |
|
| 69 |
Here are schema and data to show how to use alias: |
| 70 |
|
| 71 |
.. groonga-command |
| 72 |
.. include:: ../example/reference/alias/schema.log |
| 73 |
.. table_create Users TABLE_HASH_KEY ShortText |
| 74 |
.. column_create Users age COLUMN_SCALAR UInt8 |
| 75 |
.. |
| 76 |
.. load --table Users |
| 77 |
.. [ |
| 78 |
.. {"_key": "alice", "age": 14}, |
| 79 |
.. {"_key": "bob", "age": 29} |
| 80 |
.. ] |
| 81 |
|
| 82 |
You can use ``Users.age`` in :doc:`/reference/commands/select`: |
| 83 |
|
| 84 |
.. groonga-command |
| 85 |
.. include:: ../example/reference/alias/select_age.log |
| 86 |
.. select Users --filter 'age < 20' |
| 87 |
|
| 88 |
You can't use ``Users.age`` when you rename ``Users.age`` to |
| 89 |
``Users.years`` by :doc:`/reference/commands/column_rename`: |
| 90 |
|
| 91 |
.. groonga-command |
| 92 |
.. include:: ../example/reference/alias/select_age_after_rename.log |
| 93 |
.. column_rename Users age years |
| 94 |
.. select Users --filter 'age < 20' |
| 95 |
|
| 96 |
But you can use ``Users.age`` by registering ``Users.age`` to |
| 97 |
``Users.years`` mapping to ``Aliases``. |
| 98 |
|
| 99 |
.. groonga-command |
| 100 |
.. include:: ../example/reference/alias/select_age_by_alias.log |
| 101 |
.. load --table Aliases |
| 102 |
.. [ |
| 103 |
.. {"_key": "Users.age", "real_name": "Users.years"} |
| 104 |
.. ] |
| 105 |
.. |
| 106 |
.. select Users --filter 'age < 20' |
| 107 |
|
| 108 |
Now, you can use ``Users.age`` as alias of ``Users.years``. |
| 109 |
|
| 110 |
How to resolve alias |
| 111 |
-------------------- |
| 112 |
|
| 113 |
This section describes how to resolve alias. |
| 114 |
|
| 115 |
Groonga uses the alias feature when nonexistent object name (table |
| 116 |
name, column name, command name, function name and so on) is |
| 117 |
referred. It means that you can't override existing object (table, |
| 118 |
column, command, function and so on) by the alias feature. |
| 119 |
|
| 120 |
For example, alias isn't resolved in the following example because |
| 121 |
``Users.years`` exists: |
| 122 |
|
| 123 |
.. groonga-command |
| 124 |
.. include:: ../example/reference/alias/existing_name.log |
| 125 |
.. load --table Aliases |
| 126 |
.. [ |
| 127 |
.. {"_key": "Users.years", "real_name": "Users.years_old"} |
| 128 |
.. ] |
| 129 |
.. |
| 130 |
.. select Users --filter 'years < 20' |
| 131 |
|
| 132 |
Alias is resolved recursively. If you rename ``Users.years`` to |
| 133 |
``Users.years_old`` and you refer ``Users.age``, Groonga replaces |
| 134 |
``Users.age`` with ``Users.years`` and then ``Users.years`` with |
| 135 |
``Users.years_old``. Because ``Aliases`` table has the following |
| 136 |
records: |
| 137 |
|
| 138 |
.. list-table:: |
| 139 |
:header-rows: 1 |
| 140 |
|
| 141 |
* - ``_key`` |
| 142 |
- ``real_name`` |
| 143 |
* - ``Users.age`` |
| 144 |
- ``Users.years`` |
| 145 |
* - ``Users.years`` |
| 146 |
- ``Users.years_old`` |
| 147 |
|
| 148 |
Here is an example to ``Users.age`` is resolved recursively: |
| 149 |
|
| 150 |
.. groonga-command |
| 151 |
.. include:: ../example/reference/alias/existing_name.log |
| 152 |
.. column_rename Users years years_old |
| 153 |
.. select Users --filter 'age < 20' |
| 154 |
|
| 155 |
See also |
| 156 |
-------- |
| 157 |
|
| 158 |
* :doc:`/reference/configuration` |
| 159 |
* :doc:`/reference/commands/config_set` |
| 160 |
* :doc:`/reference/commands/table_create` |
| 161 |
* :doc:`/reference/commands/column_create` |
| 162 |
* :doc:`/reference/commands/select` |