| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: columns_vector |
| 5 |
|
| 6 |
Vector column |
| 7 |
============= |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
Vector column is a data store object. |
| 13 |
|
| 14 |
It can stores zero or more scalar values per records. In short, scalar |
| 15 |
value is a single value such as number and string. See :doc:`scalar` |
| 16 |
about scalar value details. |
| 17 |
|
| 18 |
One of vector column use cases is tags store. You can use a vector |
| 19 |
column to store tag values. |
| 20 |
|
| 21 |
You can use vector column as index search target in the same way as |
| 22 |
scalar column. You can set weight for each element. The element that |
| 23 |
has one or more weight is matched, the record has more score rather |
| 24 |
than no weight case. It is a vector column specific feature. Vector |
| 25 |
column that can store weight is called weight vector column. |
| 26 |
|
| 27 |
You can also do full text search against each text element. But search |
| 28 |
score is too high when weight is used. You should use full text search |
| 29 |
with weight carefully. |
| 30 |
|
| 31 |
Usage |
| 32 |
----- |
| 33 |
|
| 34 |
There are four vector column types: |
| 35 |
|
| 36 |
* Normal vector column |
| 37 |
* Reference vector column |
| 38 |
* Weight vector column |
| 39 |
* Generated vector column |
| 40 |
|
| 41 |
This section describes how to use these types. |
| 42 |
|
| 43 |
.. _normal-vector-column: |
| 44 |
|
| 45 |
Normal vector column |
| 46 |
^^^^^^^^^^^^^^^^^^^^ |
| 47 |
|
| 48 |
Normal vector column stores zero or more scalar data. For example, |
| 49 |
scalar data are number, string and so on. |
| 50 |
|
| 51 |
A normal vector column can store the same type elements. You can't mix |
| 52 |
types. For example, you can't store a number and a string in the same |
| 53 |
normal vector column. |
| 54 |
|
| 55 |
Normal vector column is useful when a record has multiple values with |
| 56 |
a key. Tags are the most popular use case. |
| 57 |
|
| 58 |
How to create |
| 59 |
""""""""""""" |
| 60 |
|
| 61 |
Use :doc:`/reference/commands/column_create` command to create a |
| 62 |
normal vector column. The point is ``COLUMN_VECTOR`` flag: |
| 63 |
|
| 64 |
.. groonga-command |
| 65 |
.. include:: ../../example/reference/columns/vector/usage_create_normal.log |
| 66 |
.. table_create Bookmarks TABLE_HASH_KEY ShortText |
| 67 |
.. column_create Bookmarks tags COLUMN_VECTOR ShortText |
| 68 |
|
| 69 |
You can set zero or more tags to a bookmark. |
| 70 |
|
| 71 |
How to load |
| 72 |
""""""""""" |
| 73 |
|
| 74 |
You can load vector data by JSON array syntax:: |
| 75 |
|
| 76 |
[ELEMENT1, ELEMENT2, ELEMENT3, ...] |
| 77 |
|
| 78 |
Let's load the following data: |
| 79 |
|
| 80 |
.. list-table:: |
| 81 |
:header-rows: 1 |
| 82 |
|
| 83 |
* - ``_key`` |
| 84 |
- ``tags`` |
| 85 |
* - ``http://groonga.org/`` |
| 86 |
- ``["groonga"]`` |
| 87 |
* - ``http://mroonga.org/`` |
| 88 |
- ``["mroonga", "mysql", "groonga"]`` |
| 89 |
* - ``http://ranguba.org/`` |
| 90 |
- ``["ruby", "groonga"]`` |
| 91 |
|
| 92 |
Here is a command that loads the data: |
| 93 |
|
| 94 |
.. groonga-command |
| 95 |
.. include:: ../../example/reference/columns/vector/usage_load_normal.log |
| 96 |
.. load --table Bookmarks |
| 97 |
.. [ |
| 98 |
.. {"_key": "http://groonga.org/", "tags": ["groonga"]}, |
| 99 |
.. {"_key": "http://mroonga.org/", "tags": ["mroonga", "mysql", "groonga"]}, |
| 100 |
.. {"_key": "http://ranguba.org/", "tags": ["ruby", "groonga"]} |
| 101 |
.. ] |
| 102 |
|
| 103 |
The loaded data can be outputted as JSON array syntax: |
| 104 |
|
| 105 |
.. groonga-command |
| 106 |
.. include:: ../../example/reference/columns/vector/usage_output_normal.log |
| 107 |
.. select Bookmarks |
| 108 |
|
| 109 |
How to search |
| 110 |
""""""""""""" |
| 111 |
|
| 112 |
You need to create an index to search normal vector column: |
| 113 |
|
| 114 |
.. groonga-command |
| 115 |
.. include:: ../../example/reference/columns/vector/usage_index_normal.log |
| 116 |
.. table_create Tags TABLE_PAT_KEY ShortText |
| 117 |
.. column_create Tags bookmark_index COLUMN_INDEX Bookmarks tags |
| 118 |
|
| 119 |
There is no vector column specific way. You can create an index like |
| 120 |
a scalar column. |
| 121 |
|
| 122 |
You can search an element in ``tags`` like full text search syntax. |
| 123 |
|
| 124 |
With :ref:`select-match-columns` and :ref:`select-query`: |
| 125 |
|
| 126 |
.. groonga-command |
| 127 |
.. include:: ../../example/reference/columns/vector/usage_query_normal.log |
| 128 |
.. select Bookmarks --match_columns tags --query mysql --output_columns _key,tags,_score |
| 129 |
|
| 130 |
You can also use weight in :ref:`select-match-columns`: |
| 131 |
|
| 132 |
.. groonga-command |
| 133 |
.. include:: ../../example/reference/columns/vector/usage_query_weight_normal.log |
| 134 |
.. select Bookmarks --match_columns 'tags * 3' --query mysql --output_columns _key,tags,_score |
| 135 |
|
| 136 |
With :ref:`select-filter`: |
| 137 |
|
| 138 |
.. groonga-command |
| 139 |
.. include:: ../../example/reference/columns/vector/usage_filter_normal.log |
| 140 |
.. select Bookmarks --filter 'tags @ "msyql"' --output_columns _key,tags,_score |
| 141 |
|
| 142 |
.. groonga-command |
| 143 |
.. table_remove Tags |
| 144 |
.. table_remove Bookmarks |
| 145 |
|
| 146 |
.. _reference-vector-column: |
| 147 |
|
| 148 |
Reference vector column |
| 149 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 150 |
|
| 151 |
TODO |
| 152 |
|
| 153 |
Reference vector column is space-efficient if there are many same |
| 154 |
value elements. Reference vector column keeps reference record IDs not |
| 155 |
value itself. Record ID is smaller than value itself. |
| 156 |
|
| 157 |
How to create |
| 158 |
""""""""""""" |
| 159 |
|
| 160 |
TODO |
| 161 |
|
| 162 |
How to load |
| 163 |
""""""""""" |
| 164 |
|
| 165 |
TODO |
| 166 |
|
| 167 |
How to search |
| 168 |
""""""""""""" |
| 169 |
|
| 170 |
TODO |
| 171 |
|
| 172 |
.. _weight-vector-column: |
| 173 |
|
| 174 |
Weight vector column |
| 175 |
^^^^^^^^^^^^^^^^^^^^ |
| 176 |
|
| 177 |
Weight vector column is similar to normal vector column. It can store |
| 178 |
elements. It can also stores weights for them. Weight is degree of |
| 179 |
importance of the element. |
| 180 |
|
| 181 |
Weight is positive integer. ``0`` is the default weight. It means that |
| 182 |
no weight. |
| 183 |
|
| 184 |
If weight is one or larger, search score is increased by the |
| 185 |
weight. If the weight is ``0``, score is ``1``. If the weight is |
| 186 |
``10``, score is ``11`` (``= 1 + 10``). |
| 187 |
|
| 188 |
Weight vector column is useful for tuning search score. See also |
| 189 |
:ref:`select-adjuster`. You can increase search score of specific |
| 190 |
records. |
| 191 |
|
| 192 |
Limitations |
| 193 |
""""""""""" |
| 194 |
|
| 195 |
There are some limitations for now. They will be resolved in the |
| 196 |
future. |
| 197 |
|
| 198 |
Here are limitations: |
| 199 |
|
| 200 |
* You need to use string representation for element value on load. |
| 201 |
For example, you can't use ``29`` for number 29. You need to use |
| 202 |
``"29"`` for number 29. |
| 203 |
|
| 204 |
How to create |
| 205 |
""""""""""""" |
| 206 |
|
| 207 |
Use :doc:`/reference/commands/column_create` command to create a |
| 208 |
weight vector column. The point is ``COLUMN_VECTOR|WITH_WEIGHT`` |
| 209 |
flags: |
| 210 |
|
| 211 |
.. groonga-command |
| 212 |
.. include:: ../../example/reference/columns/vector/usage_create_weight.log |
| 213 |
.. table_create Bookmarks TABLE_HASH_KEY ShortText |
| 214 |
.. column_create Bookmarks tags COLUMN_VECTOR|WITH_WEIGHT ShortText |
| 215 |
|
| 216 |
If you don't specify ``WITH_WEIGHT`` flag, it is just a normal vector |
| 217 |
column. |
| 218 |
|
| 219 |
You can set zero or more tags with weight to a bookmark. |
| 220 |
|
| 221 |
How to load |
| 222 |
""""""""""" |
| 223 |
|
| 224 |
You can load vector data by JSON object syntax:: |
| 225 |
|
| 226 |
{"ELEMENT1": WEIGHT1, "ELEMENT2": WEIGHT2, "ELEMENT3": WEIGHT3, ...} |
| 227 |
|
| 228 |
Let's load the following data: |
| 229 |
|
| 230 |
.. list-table:: |
| 231 |
:header-rows: 1 |
| 232 |
|
| 233 |
* - ``_key`` |
| 234 |
- ``tags`` |
| 235 |
* - ``http://groonga.org/`` |
| 236 |
- ``{"groonga": 100}`` |
| 237 |
* - ``http://mroonga.org/`` |
| 238 |
- ``{"mroonga": 100, "mysql": 50, "groonga": 10}`` |
| 239 |
* - ``http://ranguba.org/`` |
| 240 |
- ``{"ruby": 100, "groonga": 50}`` |
| 241 |
|
| 242 |
Here is a command that loads the data: |
| 243 |
|
| 244 |
.. groonga-command |
| 245 |
.. include:: ../../example/reference/columns/vector/usage_load_weight.log |
| 246 |
.. load --table Bookmarks |
| 247 |
.. [ |
| 248 |
.. {"_key": "http://groonga.org/", |
| 249 |
.. "tags": {"groonga": 100}}, |
| 250 |
.. {"_key": "http://mroonga.org/", |
| 251 |
.. "tags": {"mroonga": 100, |
| 252 |
.. "mysql": 50, |
| 253 |
.. "groonga": 10}}, |
| 254 |
.. {"_key": "http://ranguba.org/", |
| 255 |
.. "tags": {"ruby": 100, |
| 256 |
.. "groonga": 50}} |
| 257 |
.. ] |
| 258 |
|
| 259 |
The loaded data can be outputted as JSON object syntax: |
| 260 |
|
| 261 |
.. groonga-command |
| 262 |
.. include:: ../../example/reference/columns/vector/usage_output_weight.log |
| 263 |
.. select Bookmarks |
| 264 |
|
| 265 |
How to search |
| 266 |
""""""""""""" |
| 267 |
|
| 268 |
You need to create an index to search weight vector column. You don't |
| 269 |
forget to specify ``WITH_WEIGHT`` flag to ``column_create``: |
| 270 |
|
| 271 |
.. groonga-command |
| 272 |
.. include:: ../../example/reference/columns/vector/usage_index_weight.log |
| 273 |
.. table_create Tags TABLE_PAT_KEY ShortText |
| 274 |
.. column_create Tags bookmark_index COLUMN_INDEX|WITH_WEIGHT Bookmarks tags |
| 275 |
|
| 276 |
There is no weight vector column specific way except ``WITH_WEIGHT`` |
| 277 |
flag. You can create an index like a scalar column. |
| 278 |
|
| 279 |
You can search an element in ``tags`` like full text search syntax. |
| 280 |
|
| 281 |
With :ref:`select-match-columns` and :ref:`select-query`: |
| 282 |
|
| 283 |
.. groonga-command |
| 284 |
.. include:: ../../example/reference/columns/vector/usage_query_weight.log |
| 285 |
.. select Bookmarks --match_columns tags --query groonga --output_columns _key,tags,_score |
| 286 |
|
| 287 |
You can also use weight in :ref:`select-match-columns`. The score is |
| 288 |
``(1 + weight_in_weight_vector) * weight_in_match_columns``: |
| 289 |
|
| 290 |
.. groonga-command |
| 291 |
.. include:: ../../example/reference/columns/vector/usage_query_weight_weight.log |
| 292 |
.. select Bookmarks --match_columns 'tags * 3' --query groonga --output_columns _key,tags,_score |
| 293 |
|
| 294 |
With :ref:`select-filter`: |
| 295 |
|
| 296 |
.. groonga-command |
| 297 |
.. include:: ../../example/reference/columns/vector/usage_filter_weight.log |
| 298 |
.. select Bookmarks --filter 'tags @ "groonga"' --output_columns _key,tags,_score |
| 299 |
|
| 300 |
How to apply just weight |
| 301 |
"""""""""""""""""""""""" |
| 302 |
|
| 303 |
You can use weight in weight vector column to just increase search |
| 304 |
score without changing a set of matched records. |
| 305 |
|
| 306 |
Use :ref:`select-adjuster` for the purpose: |
| 307 |
|
| 308 |
.. groonga-command |
| 309 |
.. include:: ../../example/reference/columns/vector/usage_adjuster_weight.log |
| 310 |
.. select Bookmarks \ |
| 311 |
.. --filter true \ |
| 312 |
.. --adjuster 'tags @ "mysql" * 10 + tags @ "groonga" * 5' \ |
| 313 |
.. --output_columns _key,tags,_score |
| 314 |
|
| 315 |
The ``select`` command uses ``--filter true``. So all records are |
| 316 |
matched with score 1. Then it applies ``--adjuster``. The adjuster |
| 317 |
does the following: |
| 318 |
|
| 319 |
* ``tags @ "mysql" * 10`` increases score by ``(1 + weight) * 10`` |
| 320 |
of records that has ``"mysql"`` tag. |
| 321 |
* ``tags @ "groonga" * 5`` increases score by ``(1 + weight) * 5`` |
| 322 |
of records that has ``"groonga"`` tag. |
| 323 |
|
| 324 |
For example, record ``"http://mroonga.org/"`` has both ``"mysql"`` tag |
| 325 |
and ``"groonga"`` tag. So its score is increased by ``565`` (``= |
| 326 |
((1 + 50) * 10) + ((1 + 10) * 5) = (51 * 10) + (11 * 5) = 510 + 55``). |
| 327 |
The search score is 1 by ``--filter true`` before applying |
| 328 |
``--adjuster``. So the final search score is ``566`` (``= 1 + 565``) |
| 329 |
of record ``"http://mroonga.org/"``. |
| 330 |
|
| 331 |
.. groonga-command |
| 332 |
.. table_remove Tags |
| 333 |
.. table_remove Bookmarks |
| 334 |
|
| 335 |
.. _generated-vector-column: |
| 336 |
|
| 337 |
Generated vector column |
| 338 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 339 |
|
| 340 |
You can use a vector column as a :ref:`generated-column`. |
| 341 |
|
| 342 |
How to create |
| 343 |
""""""""""""" |
| 344 |
|
| 345 |
See :ref:`column-create-generated-column` for details. |