| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_in_records |
| 5 |
|
| 6 |
``in_records`` |
| 7 |
============== |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
.. versionadded:: 7.0.2 |
| 13 |
|
| 14 |
You can use ``in_records`` for using an existing table as condition patterns. Each record in the existing table is treated as a condition pattern. |
| 15 |
|
| 16 |
You may be able to reduce multiple queries to only one query by ``in_records``. |
| 17 |
|
| 18 |
``in_records`` is similar to :doc:`sub_filter`. Here are differences of them: |
| 19 |
|
| 20 |
* ``sub_filter`` requires a reference column to condition table but ``in_records`` doesn't require. |
| 21 |
|
| 22 |
* ``sub_filter`` requires an index column for the reference column but ``in_records`` doesn't require. |
| 23 |
|
| 24 |
* ``sub_filter`` can use all logical operations but ``in_records`` can use only AND logical operation in one pattern. |
| 25 |
|
| 26 |
* ``sub_filter`` can use only the value of one reference column for condition but ``in_records`` can use one or more values for condition. You can use multiple columns, functions and so on for multiple values. |
| 27 |
|
| 28 |
* ``sub_filter`` uses index search but ``in_records`` uses sequential search. |
| 29 |
|
| 30 |
.. _in-records-syntax: |
| 31 |
|
| 32 |
Syntax |
| 33 |
------ |
| 34 |
|
| 35 |
``in_records`` has four or more parameters:: |
| 36 |
|
| 37 |
in_records(condition_table, |
| 38 |
value1, mode1, condition_column_name1, |
| 39 |
..., |
| 40 |
valueN, modeN, condition_column_nameN) |
| 41 |
|
| 42 |
.. _in-records-usage: |
| 43 |
|
| 44 |
Usage |
| 45 |
----- |
| 46 |
|
| 47 |
Here is a schema definition and sample data. |
| 48 |
|
| 49 |
Sample schema: |
| 50 |
|
| 51 |
.. groonga-command |
| 52 |
.. include:: ../../example/reference/functions/in_records/usage_setup_schema.log |
| 53 |
.. table_create Tags TABLE_PAT_KEY ShortText |
| 54 |
.. |
| 55 |
.. table_create Conditions TABLE_PAT_KEY ShortText |
| 56 |
.. column_create Conditions user_pattern COLUMN_SCALAR ShortText |
| 57 |
.. column_create Conditions tag COLUMN_SCALAR Tags |
| 58 |
.. column_create Conditions max_length COLUMN_SCALAR UInt32 |
| 59 |
.. |
| 60 |
.. table_create Memos TABLE_HASH_KEY ShortText |
| 61 |
.. column_create Memos user COLUMN_SCALAR ShortText |
| 62 |
.. column_create Memos tag COLUMN_SCALAR Tags |
| 63 |
|
| 64 |
Sample data: |
| 65 |
|
| 66 |
.. groonga-command |
| 67 |
.. include:: ../../example/reference/functions/in_records/usage_setup_data.log |
| 68 |
.. load --table Memos |
| 69 |
.. [ |
| 70 |
.. {"_key": "Groonga is fast", "user": "alice", "tag": "groonga"}, |
| 71 |
.. {"_key": "Mroonga is fast", "user": "alice", "tag": "mroonga"}, |
| 72 |
.. {"_key": "Groonga is very good!", "user": "alice", "tag": "groonga"}, |
| 73 |
.. {"_key": "Droonga is fast", "user": "david", "tag": "droonga"}, |
| 74 |
.. {"_key": "Groonga is a HTTP server", "user": "david", "tag": "groonga"} |
| 75 |
.. ] |
| 76 |
|
| 77 |
Sample conditions: |
| 78 |
|
| 79 |
.. groonga-command |
| 80 |
.. include:: ../../example/reference/functions/in_records/usage_setup_conditions.log |
| 81 |
.. load --table Conditions |
| 82 |
.. [ |
| 83 |
.. {"_key": "lic + groonga", "user_pattern": "lic", "tag": "groonga", max_length: 20}, |
| 84 |
.. {"_key": "dav + droonga", "user_pattern": "dav", "tag": "droonga", max_length: 50} |
| 85 |
.. ] |
| 86 |
|
| 87 |
Here is a simple usage of ``in_records`` that searches by records in ``Conditions`` table. Each record is used as a condition: |
| 88 |
|
| 89 |
.. groonga-command |
| 90 |
.. include:: ../../example/reference/functions/in_records/usage_search.log |
| 91 |
.. plugin_register functions/string |
| 92 |
.. select \ |
| 93 |
.. --table Memos \ |
| 94 |
.. --filter 'in_records(Conditions, \ |
| 95 |
.. user, "@", "user_pattern", \ |
| 96 |
.. tag, "==", "tag", \ |
| 97 |
.. string_length(_key), "<=", "max_length")' \ |
| 98 |
.. --sort_by _id \ |
| 99 |
.. --output_columns _key,user,tag |
| 100 |
|
| 101 |
The ``filter`` tries the following three conditions for each record: |
| 102 |
|
| 103 |
* ``Memos.user`` matches (``@``) ``Conditions.user_pattern``. |
| 104 |
|
| 105 |
* ``Memos.tag`` equals (``==``) ``Conditions.tag``. |
| 106 |
|
| 107 |
* The number of characters in ``Memos._key`` is less than or equals to ``Conditions.max_length``. |
| 108 |
|
| 109 |
If at least one record in ``Conditions`` table returns true for the all three conditions, the record in ``Memos`` is matched. |
| 110 |
|
| 111 |
The first record in ``Conditions`` table use the following conditions: |
| 112 |
|
| 113 |
* ``Memos.user`` has ``"lic"`` substring. |
| 114 |
|
| 115 |
* ``Memos.tag`` is ``"groonga"``. |
| 116 |
|
| 117 |
* The number of characters in ``Memos._key`` is less than or equals to ``20``. |
| 118 |
|
| 119 |
This condition matches the following records: |
| 120 |
|
| 121 |
* ``{"_key": "Groonga is fast", "user": "alice", "tag": "groonga"}`` |
| 122 |
|
| 123 |
The second record in ``Conditions`` table use the following conditions: |
| 124 |
|
| 125 |
* ``Memos.user`` has ``"dav"`` substring. |
| 126 |
|
| 127 |
* ``Memos.tag`` is ``"droonga"``. |
| 128 |
|
| 129 |
* The number of characters in ``Memos._key`` is less than or equals to ``50``. |
| 130 |
|
| 131 |
This condition matches the following records: |
| 132 |
|
| 133 |
* ``{"_key": "Droonga is fast", "user": "david", "tag": "droonga"}`` |
| 134 |
|
| 135 |
The result has the all above records. |
| 136 |
|
| 137 |
.. _in-records-parameters: |
| 138 |
|
| 139 |
Parameters |
| 140 |
---------- |
| 141 |
|
| 142 |
``in_records`` requires four or more parameters. |
| 143 |
|
| 144 |
.. _in-records-required-parameters: |
| 145 |
|
| 146 |
Required parameters |
| 147 |
^^^^^^^^^^^^^^^^^^^ |
| 148 |
|
| 149 |
``condition_table`` and tuples of ``value``, ``mode_name`` and ``condition_column_name`` are required. You can specify multiple tuples of ``value``, ``mode_name`` and ``condition_column_name`` |
| 150 |
|
| 151 |
.. _in-records-condition-table: |
| 152 |
|
| 153 |
``condition_table`` |
| 154 |
""""""""""""""""""" |
| 155 |
|
| 156 |
Specifies a table that has conditions as its records. |
| 157 |
|
| 158 |
.. _in-records-value: |
| 159 |
|
| 160 |
``value`` |
| 161 |
^^^^^^^^^ |
| 162 |
|
| 163 |
Specifies a value to be compared. |
| 164 |
|
| 165 |
.. _in-records-mode-name: |
| 166 |
|
| 167 |
``mode_name`` |
| 168 |
^^^^^^^^^^^^^ |
| 169 |
|
| 170 |
Specifies a mode name that specifies how to compare :ref:`in-records-value` with a value of :ref:`in-records-condition-column-name`. |
| 171 |
|
| 172 |
See :ref:`query-default-mode` for available mode names. All mode names |
| 173 |
except ``"NEAR"``, ``"SIMILAR"`` and ``"SUFFIX"`` are supported. |
| 174 |
|
| 175 |
.. _in-records-condition-column-name: |
| 176 |
|
| 177 |
``condition_column_name`` |
| 178 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 179 |
|
| 180 |
Specifies a column name of :ref:`in-records-condition-table` to be used as condition. |
| 181 |
|
| 182 |
.. _in-records-optional-parameters: |
| 183 |
|
| 184 |
Optional parameter |
| 185 |
^^^^^^^^^^^^^^^^^^ |
| 186 |
|
| 187 |
There are no optional parameter. |
| 188 |
|
| 189 |
Return value |
| 190 |
------------ |
| 191 |
|
| 192 |
``in_records`` returns whether the record is matched one of records of the specified condition table or not. |
| 193 |
|
| 194 |
If the record is matched, it returns ``true``. Otherwise, it returns ``false``. |