File: vector.rst

package info (click to toggle)
groonga 9.0.0-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,496 kB
  • sloc: ansic: 608,707; ruby: 35,042; xml: 23,643; cpp: 10,319; sh: 7,453; yacc: 5,968; python: 3,033; makefile: 2,609; perl: 133
file content (332 lines) | stat: -rw-r--r-- 9,649 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
.. -*- rst -*-

.. highlightlang:: none

.. groonga-command
.. database: columns_vector

Vector column
=============

Summary
-------

Vector column is a data store object. It can stores zero or more
scalar values. In short, scalar value is a single value such as number
and string. See :doc:`scalar` about scalar value details.

One of vector column use cases is tags store. You can use a vector
column to store tag values.

You can use vector column as index search target in the same way as
scalar column. You can set weight for each element. The element that
has one or more weight is matched, the record has more score rather
than no weight case. It is a vector column specific feature. Vector
column that can store weight is called weight vector column.

You can also do full text search against each text element. But search
score is too high when weight is used. You should use full text search
with weight carefully.

Usage
-----

There are three vector column types:

  * Normal vector column
  * Reference vector column
  * Weight vector column

This section describes how to use these types.

.. _normal-vector-column:

Normal vector column
^^^^^^^^^^^^^^^^^^^^

Normal vector column stores zero or more scalar data. For example,
scalar data are number, string and so on.

A normal vector column can store the same type elements. You can't mix
types. For example, you can't store a number and a string in the same
normal vector column.

Normal vector column is useful when a record has multiple values with
a key. Tags are the most popular use case.

How to create
"""""""""""""

Use :doc:`/reference/commands/column_create` command to create a
normal vector column. The point is ``COLUMN_VECTOR`` flag:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_create_normal.log
.. table_create Bookmarks TABLE_HASH_KEY ShortText
.. column_create Bookmarks tags COLUMN_VECTOR ShortText

You can set zero or more tags to a bookmark.

How to load
"""""""""""

You can load vector data by JSON array syntax::

  [ELEMENT1, ELEMENT2, ELEMENT3, ...]

Let's load the following data:

.. list-table::
   :header-rows: 1

   * - ``_key``
     - ``tags``
   * - ``http://groonga.org/``
     - ``["groonga"]``
   * - ``http://mroonga.org/``
     - ``["mroonga", "mysql", "groonga"]``
   * - ``http://ranguba.org/``
     - ``["ruby", "groonga"]``

Here is a command that loads the data:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_load_normal.log
.. load --table Bookmarks
.. [
.. {"_key": "http://groonga.org/", "tags": ["groonga"]},
.. {"_key": "http://mroonga.org/", "tags": ["mroonga", "mysql", "groonga"]},
.. {"_key": "http://ranguba.org/", "tags": ["ruby", "groonga"]}
.. ]

The loaded data can be outputted as JSON array syntax:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_output_normal.log
.. select Bookmarks

How to search
"""""""""""""

You need to create an index to search normal vector column:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_index_normal.log
.. table_create Tags TABLE_PAT_KEY ShortText
.. column_create Tags bookmark_index COLUMN_INDEX Bookmarks tags

There is no vector column specific way. You can create an index like
a scalar column.

You can search an element in ``tags`` like full text search syntax.

With :ref:`select-match-columns` and :ref:`select-query`:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_query_normal.log
.. select Bookmarks --match_columns tags --query mysql --output_columns _key,tags,_score

You can also use weight in :ref:`select-match-columns`:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_query_weight_normal.log
.. select Bookmarks --match_columns 'tags * 3' --query mysql --output_columns _key,tags,_score

With :ref:`select-filter`:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_filter_normal.log
.. select Bookmarks --filter 'tags @ "msyql"' --output_columns _key,tags,_score

.. groonga-command
.. table_remove Tags
.. table_remove Bookmarks

.. _reference-vector-column:

Reference vector column
^^^^^^^^^^^^^^^^^^^^^^^

TODO

Reference vector column is space-efficient if there are many same
value elements. Reference vector column keeps reference record IDs not
value itself. Record ID is smaller than value itself.

How to create
"""""""""""""

TODO

How to load
"""""""""""

TODO

How to search
"""""""""""""

TODO

.. _weight-vector-column:

Weight vector column
^^^^^^^^^^^^^^^^^^^^

Weight vector column is similar to normal vector column. It can store
elements. It can also stores weights for them. Weight is degree of
importance of the element.

Weight is positive integer. ``0`` is the default weight. It means that
no weight.

If weight is one or larger, search score is increased by the
weight. If the weight is ``0``, score is ``1``. If the weight is
``10``, score is ``11`` (``= 1 + 10``).

Weight vector column is useful for tuning search score. See also
:ref:`select-adjuster`. You can increase search score of specific
records.

Limitations
"""""""""""

There are some limitations for now. They will be resolved in the
future.

Here are limitations:

  * You need to use string representation for element value on load.
    For example, you can't use ``29`` for number 29. You need to use
    ``"29"`` for number 29.

How to create
"""""""""""""

Use :doc:`/reference/commands/column_create` command to create a
weight vector column. The point is ``COLUMN_VECTOR|WITH_WEIGHT``
flags:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_create_weight.log
.. table_create Bookmarks TABLE_HASH_KEY ShortText
.. column_create Bookmarks tags COLUMN_VECTOR|WITH_WEIGHT ShortText

If you don't specify ``WITH_WEIGHT`` flag, it is just a normal vector
column.

You can set zero or more tags with weight to a bookmark.

How to load
"""""""""""

You can load vector data by JSON object syntax::

  {"ELEMENT1": WEIGHT1, "ELEMENT2": WEIGHT2, "ELEMENT3": WEIGHT3, ...}

Let's load the following data:

.. list-table::
   :header-rows: 1

   * - ``_key``
     - ``tags``
   * - ``http://groonga.org/``
     - ``{"groonga": 100}``
   * - ``http://mroonga.org/``
     - ``{"mroonga": 100, "mysql": 50, "groonga": 10}``
   * - ``http://ranguba.org/``
     - ``{"ruby": 100, "groonga": 50}``

Here is a command that loads the data:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_load_weight.log
.. load --table Bookmarks
.. [
.. {"_key": "http://groonga.org/",
..  "tags": {"groonga": 100}},
.. {"_key": "http://mroonga.org/",
..  "tags": {"mroonga": 100,
..           "mysql":   50,
..           "groonga": 10}},
.. {"_key": "http://ranguba.org/",
..  "tags": {"ruby": 100,
..           "groonga": 50}}
.. ]

The loaded data can be outputted as JSON object syntax:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_output_weight.log
.. select Bookmarks

How to search
"""""""""""""

You need to create an index to search weight vector column. You don't
forget to specify ``WITH_WEIGHT`` flag to ``column_create``:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_index_weight.log
.. table_create Tags TABLE_PAT_KEY ShortText
.. column_create Tags bookmark_index COLUMN_INDEX|WITH_WEIGHT Bookmarks tags

There is no weight vector column specific way except ``WITH_WEIGHT``
flag. You can create an index like a scalar column.

You can search an element in ``tags`` like full text search syntax.

With :ref:`select-match-columns` and :ref:`select-query`:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_query_weight.log
.. select Bookmarks --match_columns tags --query groonga --output_columns _key,tags,_score

You can also use weight in :ref:`select-match-columns`. The score is
``(1 + weight_in_weight_vector) * weight_in_match_columns``:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_query_weight_weight.log
.. select Bookmarks --match_columns 'tags * 3' --query groonga --output_columns _key,tags,_score

With :ref:`select-filter`:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_filter_weight.log
.. select Bookmarks --filter 'tags @ "groonga"' --output_columns _key,tags,_score

How to apply just weight
""""""""""""""""""""""""

You can use weight in weight vector column to just increase search
score without changing a set of matched records.

Use :ref:`select-adjuster` for the purpose:

.. groonga-command
.. include:: ../../example/reference/columns/vector/usage_adjuster_weight.log
.. select Bookmarks \
..   --filter true \
..   --adjuster 'tags @ "mysql" * 10 + tags @ "groonga" * 5' \
..   --output_columns _key,tags,_score

The ``select`` command uses ``--filter true``. So all records are
matched with score 1. Then it applies ``--adjuster``. The adjuster
does the following:

  * ``tags @ "mysql" * 10`` increases score by ``(1 + weight) * 10``
    of records that has ``"mysql"`` tag.
  * ``tags @ "groonga" * 5`` increases score by ``(1 + weight) * 5``
    of records that has ``"groonga"`` tag.

For example, record ``"http://mroonga.org/"`` has both ``"mysql"`` tag
and ``"groonga"`` tag. So its score is increased by ``565`` (``=
((1 + 50) * 10) + ((1 + 10) * 5) = (51 * 10) + (11 * 5) = 510 + 55``).
The search score is 1 by ``--filter true`` before applying
``--adjuster``. So the final search score is ``566`` (``= 1 + 565``)
of record ``"http://mroonga.org/"``.

.. groonga-command
.. table_remove Tags
.. table_remove Bookmarks