| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_sub_filter |
| 5 |
|
| 6 |
``sub_filter`` |
| 7 |
============== |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
``sub_filter`` evaluates ``filter_string`` in ``scope`` context. |
| 13 |
|
| 14 |
``sub_filter`` can be used in only ``--filter`` in |
| 15 |
:doc:`/reference/commands/select`. |
| 16 |
|
| 17 |
Syntax |
| 18 |
------ |
| 19 |
|
| 20 |
``sub_filter`` requires two arguments. They are ``scope`` and |
| 21 |
``filter_string``. |
| 22 |
|
| 23 |
:: |
| 24 |
|
| 25 |
sub_filter(scope, filter_string) |
| 26 |
|
| 27 |
|
| 28 |
Usage |
| 29 |
----- |
| 30 |
|
| 31 |
Here are a schema definition and sample data to show usage. |
| 32 |
|
| 33 |
Sample schema: |
| 34 |
|
| 35 |
.. groonga-command |
| 36 |
.. include:: ../../example/reference/functions/sub_filter/usage_setup_schema.log |
| 37 |
.. table_create Comment TABLE_PAT_KEY UInt32 |
| 38 |
.. column_create Comment name COLUMN_SCALAR ShortText |
| 39 |
.. column_create Comment content COLUMN_SCALAR ShortText |
| 40 |
.. table_create Blog TABLE_PAT_KEY ShortText |
| 41 |
.. column_create Blog title COLUMN_SCALAR ShortText |
| 42 |
.. column_create Blog content COLUMN_SCALAR ShortText |
| 43 |
.. column_create Blog comments COLUMN_VECTOR Comment |
| 44 |
.. column_create Comment blog_comment_index COLUMN_INDEX Blog comments |
| 45 |
.. table_create Lexicon TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram |
| 46 |
.. column_create Lexicon comment_content COLUMN_INDEX|WITH_POSITION Comment content |
| 47 |
.. column_create Lexicon comment_name COLUMN_INDEX|WITH_POSITION Comment name |
| 48 |
.. column_create Lexicon blog_content COLUMN_INDEX|WITH_POSITION Blog content |
| 49 |
|
| 50 |
Sample data: |
| 51 |
|
| 52 |
.. groonga-command |
| 53 |
.. include:: ../../example/reference/functions/sub_filter/usage_setup_data.log |
| 54 |
.. load --table Comment |
| 55 |
.. [ |
| 56 |
.. {"_key": 1, "name": "A", "content": "groonga"}, |
| 57 |
.. {"_key": 2, "name": "B", "content": "groonga"}, |
| 58 |
.. {"_key": 3, "name": "C", "content": "rroonga"}, |
| 59 |
.. {"_key": 4, "name": "A", "content": "mroonga"}, |
| 60 |
.. ] |
| 61 |
.. load --table Blog |
| 62 |
.. [ |
| 63 |
.. {"_key": "groonga's blog", "content": "content of groonga's blog", comments: [1, 2, 3]}, |
| 64 |
.. {"_key": "mroonga's blog", "content": "content of mroonga's blog", comments: [2, 3, 4]}, |
| 65 |
.. {"_key": "rroonga's blog", "content": "content of rroonga's blog", comments: [3]}, |
| 66 |
.. ] |
| 67 |
|
| 68 |
Here is the simple usage of ``sub_filter`` function which extracts the |
| 69 |
blog entry commented by user 'A'. |
| 70 |
|
| 71 |
.. groonga-command |
| 72 |
.. include:: ../../example/reference/functions/sub_filter/usage_without_sub_filter.log |
| 73 |
.. select Blog --output_columns _key --filter "comments.name @ \"A\" && comments.content @ \"groonga\"" |
| 74 |
|
| 75 |
When executing the above query, not only "groonga's blog", but also "mroonga's blog". |
| 76 |
This is not what you want because user "A" does not mention "groonga" to "mroonga's blog". |
| 77 |
|
| 78 |
Without sub_filter, it means that following conditions are met. |
| 79 |
|
| 80 |
* There is at least one record that user "A" commented out. |
| 81 |
* There is at least one record that mentioned about "groonga". |
| 82 |
|
| 83 |
.. groonga-command |
| 84 |
.. include:: ../../example/reference/functions/sub_filter/usage_with_sub_filter.log |
| 85 |
.. select Blog --output_columns _key --filter 'sub_filter(comments, "name @ \\"A\\" && content @ \\"groonga\\"")' |
| 86 |
|
| 87 |
On the other hand, executing the above query returns the intended result. |
| 88 |
Because the arguments of sub_filter is evaluated in comments column's context. |
| 89 |
|
| 90 |
It means that sub_filter requires the following condition is met. |
| 91 |
|
| 92 |
* There are the records that user "A" mentions about "groonga". |
| 93 |
|
| 94 |
|
| 95 |
Parameters |
| 96 |
---------- |
| 97 |
|
| 98 |
There are two required parameter, ``scope`` and ``filter_string``. |
| 99 |
|
| 100 |
``scope`` |
| 101 |
^^^^^^^^^ |
| 102 |
|
| 103 |
Specifies a column of the table that is specified by ``table`` |
| 104 |
parameter in ``select``. The column has a limitation. The limitation |
| 105 |
is described later. ``filter_string`` is evaluated in the column |
| 106 |
context. It means that ``filter_string`` is evaluated like |
| 107 |
``select --table TYPE_OF_THE_COLUMN --filter FILTER_STRING``. |
| 108 |
|
| 109 |
The specified column type must be a table. In other words, the column |
| 110 |
type must be reference type. |
| 111 |
|
| 112 |
You can chain columns by ``COLUMN_1.COLUMN_2.COLUMN_3...COLUMN_N`` |
| 113 |
syntax. For example, ``user.group.name``. |
| 114 |
|
| 115 |
See :ref:`select-table` about ``table`` parameter in ``select``. |
| 116 |
|
| 117 |
``filter_string`` |
| 118 |
^^^^^^^^^^^^^^^^^ |
| 119 |
|
| 120 |
Specifies a search condition in |
| 121 |
:doc:`/reference/grn_expr/script_syntax`. It is evaluated in ``scope`` |
| 122 |
context. |
| 123 |
|
| 124 |
Return value |
| 125 |
------------ |
| 126 |
|
| 127 |
``sub_filter`` returns whether any record is matched or not. If one or |
| 128 |
more records are matched, it returns ``true``. Otherwise, it returns |
| 129 |
``false``. |
| 130 |
|
| 131 |
See also |
| 132 |
-------- |
| 133 |
|
| 134 |
* :doc:`/reference/commands/select` |
| 135 |
* :doc:`/reference/grn_expr/script_syntax` |