| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_vector_find |
| 5 |
|
| 6 |
``vector_find`` |
| 7 |
=============== |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
.. versionadded:: 8.0.4 |
| 13 |
|
| 14 |
It returns the first element that matches the given condition from the |
| 15 |
given vector. If no element is found, it returns ``null``. |
| 16 |
|
| 17 |
You can use not only equal condition but also less than condition, |
| 18 |
prefix equal condition and so on. |
| 19 |
|
| 20 |
To enable this function, register ``functions/vector`` plugin by |
| 21 |
the following command:: |
| 22 |
|
| 23 |
plugin_register functions/vector |
| 24 |
|
| 25 |
.. _vector-find-syntax: |
| 26 |
|
| 27 |
Syntax |
| 28 |
------ |
| 29 |
|
| 30 |
``vector_find`` has two or three parameters:: |
| 31 |
|
| 32 |
vector_find(vector, value) |
| 33 |
vector_find(vector, value, mode) |
| 34 |
|
| 35 |
If you omit the third argument, each element in the ``vector`` is |
| 36 |
compared with ``value`` by equality comparison. |
| 37 |
|
| 38 |
.. _vector-find-usage: |
| 39 |
|
| 40 |
Usage |
| 41 |
----- |
| 42 |
|
| 43 |
You need to register ``functions/vector`` plugin at first: |
| 44 |
|
| 45 |
.. groonga-command |
| 46 |
.. include:: ../../example/reference/functions/vector_find/usage_register.log |
| 47 |
.. plugin_register functions/vector |
| 48 |
|
| 49 |
Here is a schema definition and sample data. |
| 50 |
|
| 51 |
Sample schema: |
| 52 |
|
| 53 |
.. groonga-command |
| 54 |
.. include:: ../../example/reference/functions/vector_find/usage_setup_schema.log |
| 55 |
.. table_create Memos TABLE_HASH_KEY ShortText |
| 56 |
.. column_create Memos tags COLUMN_VECTOR ShortText |
| 57 |
|
| 58 |
Sample data: |
| 59 |
|
| 60 |
.. groonga-command |
| 61 |
.. include:: ../../example/reference/functions/vector_find/usage_setup_data.log |
| 62 |
.. load --table Memos |
| 63 |
.. [ |
| 64 |
.. {"_key": "Groonga is fast", "tags": ["groonga"]}, |
| 65 |
.. {"_key": "Mroonga is fast", "tags": ["mroonga", "groonga"]}, |
| 66 |
.. {"_key": "Groonga is very good!", "tags": ["groonga"]}, |
| 67 |
.. {"_key": "Droonga is fast", "tags": ["droonga", "groonga"]}, |
| 68 |
.. {"_key": "Groonga is a HTTP server", "tags": ["groonga", "http"]} |
| 69 |
.. ] |
| 70 |
|
| 71 |
Here is a simple usage of ``vector_find`` that searches an element in |
| 72 |
``tags`` column and returns the first found element: |
| 73 |
|
| 74 |
.. groonga-command |
| 75 |
.. include:: ../../example/reference/functions/vector_find/usage_find.log |
| 76 |
.. select \ |
| 77 |
.. --table Memos \ |
| 78 |
.. --output_columns 'tags, vector_find(tags, "mroonga")' |
| 79 |
|
| 80 |
It returns ``"mroonga"`` when the ``tags`` column value includes |
| 81 |
``"mroonga"`` element. It returns ``null`` otherwise. |
| 82 |
|
| 83 |
You can customize how to compare with each value by the third |
| 84 |
argument. Here is a usage to use full text search to find an element: |
| 85 |
|
| 86 |
.. groonga-command |
| 87 |
.. include:: ../../example/reference/functions/vector_find/usage_find_mode.log |
| 88 |
.. select \ |
| 89 |
.. --table Memos \ |
| 90 |
.. --output_columns 'tags, vector_find(tags, "roonga", "@")' |
| 91 |
|
| 92 |
It returns ``"groonga"``, ``"mroonga"`` or ``"droonga"`` when the |
| 93 |
``tags`` column value includes one of them. The returned value is the |
| 94 |
first found element. For example, ``"droonga"`` is returned for |
| 95 |
``["droonga", "groonga"]``. ``"groonga"`` isn't returned because the |
| 96 |
first element ``"droonga"`` is found before the second element |
| 97 |
``"groonga"`` is searched. |
| 98 |
|
| 99 |
It returns ``null`` when ``tags`` column value doesn't include them. |
| 100 |
|
| 101 |
.. _vector-find-parameters: |
| 102 |
|
| 103 |
Parameters |
| 104 |
---------- |
| 105 |
|
| 106 |
It requires two parameters. |
| 107 |
|
| 108 |
There is an optional parameter. |
| 109 |
|
| 110 |
.. _vector-find-required-parameters: |
| 111 |
|
| 112 |
Required parameters |
| 113 |
^^^^^^^^^^^^^^^^^^^ |
| 114 |
|
| 115 |
``vector`` and ``value`` are required. |
| 116 |
|
| 117 |
.. _vector-find-vector: |
| 118 |
|
| 119 |
``vector`` |
| 120 |
"""""""""" |
| 121 |
|
| 122 |
Specifies a vector value to be searched an element. |
| 123 |
|
| 124 |
.. _vector-find-value: |
| 125 |
|
| 126 |
``value`` |
| 127 |
""""""""" |
| 128 |
|
| 129 |
Specifies a value to be compared. |
| 130 |
|
| 131 |
.. _vector-find-optional-parameters: |
| 132 |
|
| 133 |
Optional parameters |
| 134 |
^^^^^^^^^^^^^^^^^^^ |
| 135 |
|
| 136 |
``mode`` is optional. |
| 137 |
|
| 138 |
.. _vector-find-mode: |
| 139 |
|
| 140 |
``mode`` |
| 141 |
"""""""" |
| 142 |
|
| 143 |
Specifies a mode that specifies how to compare each element with |
| 144 |
:ref:`vector-find-value`. |
| 145 |
|
| 146 |
See :ref:`query-default-mode` for available mode names. All mode names |
| 147 |
except ``"NEAR"``, ``"SIMILAR"`` and ``"SUFFIX"`` are supported. |
| 148 |
|
| 149 |
The default mode is ``"EQUAL"``. Note that :ref:`query-default-mode` |
| 150 |
says the default mode is ``"MATCH"`` but the default mode of |
| 151 |
``vector_find`` is ``"EQUAL"``. |
| 152 |
|
| 153 |
Return value |
| 154 |
------------ |
| 155 |
|
| 156 |
The matched element on match, ``null`` otherwise. |