| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_prefix_rk_search |
| 5 |
|
| 6 |
``prefix_rk_search`` |
| 7 |
==================== |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
``prefix_rk_search`` selects records by |
| 13 |
:doc:`/reference/operations/prefix_rk_search`. |
| 14 |
|
| 15 |
You need to create :ref:`table-pat-key` table for prefix RK search. |
| 16 |
|
| 17 |
You can't use ``prefix_rk_search`` for sequential scan. It's a |
| 18 |
selector only procedure. |
| 19 |
|
| 20 |
Syntax |
| 21 |
------ |
| 22 |
|
| 23 |
``prefix_rk_search`` requires two arguments. They are ``column`` and |
| 24 |
``query``:: |
| 25 |
|
| 26 |
prefix_rk_search(column, query) |
| 27 |
|
| 28 |
``column`` must be ``_key`` for now. |
| 29 |
|
| 30 |
``query`` must be string. |
| 31 |
|
| 32 |
Usage |
| 33 |
----- |
| 34 |
|
| 35 |
Here are a schema definition and sample data to show usage: |
| 36 |
|
| 37 |
.. groonga-command |
| 38 |
.. include:: ../../example/reference/functions/prefix_rk_search/usage_setup.log |
| 39 |
.. table_create Readings TABLE_PAT_KEY ShortText --normalizer NormalizerAuto |
| 40 |
.. load --table Readings |
| 41 |
.. [ |
| 42 |
.. {"_key": "ニホン"}, |
| 43 |
.. {"_key": "ニッポン"}, |
| 44 |
.. {"_key": "ローマジ"} |
| 45 |
.. ] |
| 46 |
|
| 47 |
Here is the simple usage of ``prefix_rk_search()`` function which |
| 48 |
selects ``ニホン`` and ``ニッポン`` by ``ni``: |
| 49 |
|
| 50 |
.. groonga-command |
| 51 |
.. include:: ../../example/reference/functions/prefix_rk_search/usage_simple.log |
| 52 |
.. select Readings --filter 'prefix_rk_search(_key, "ni")' |
| 53 |
|
| 54 |
You can implement :doc:`/reference/suggest/completion` like feature by |
| 55 |
combining :doc:`sub_filter`. |
| 56 |
|
| 57 |
Create a table that has candidates of completion as records. Each |
| 58 |
records have zero or more readings. They are stored into ``Readings`` |
| 59 |
table. Don't forget define an index column for ``Items.readings`` in |
| 60 |
``Readings`` table. The index column is needed for :doc:`sub_filter`: |
| 61 |
|
| 62 |
.. groonga-command |
| 63 |
.. include:: ../../example/reference/functions/prefix_rk_search/usage_setup_completion.log |
| 64 |
.. table_create Items TABLE_HASH_KEY ShortText |
| 65 |
.. column_create Items readings COLUMN_VECTOR Readings |
| 66 |
.. |
| 67 |
.. column_create Readings items_index COLUMN_INDEX Items readings |
| 68 |
.. |
| 69 |
.. load --table Items |
| 70 |
.. [ |
| 71 |
.. {"_key": "日本", "readings": ["ニホン", "ニッポン"]}, |
| 72 |
.. {"_key": "ローマ字", "readings": ["ローマジ"]}, |
| 73 |
.. {"_key": "漢字", "readings": ["カンジ"]} |
| 74 |
.. ] |
| 75 |
|
| 76 |
You can find ``日本`` record in ``Items`` table by ``niho``. Because |
| 77 |
prefix RK search with ``niho`` selects ``ニホン`` reading and ``ニホン`` |
| 78 |
reading is one of readings of ``日本`` record: |
| 79 |
|
| 80 |
.. groonga-command |
| 81 |
.. include:: ../../example/reference/functions/prefix_rk_search/usage_prefix_rk_only_completion.log |
| 82 |
.. select Items \ |
| 83 |
.. --filter 'sub_filter(readings, "prefix_rk_search(_key, \\"niho\\")")' |
| 84 |
|
| 85 |
You need to combine :ref:`script-syntax-prefix-search-operator` to |
| 86 |
support no reading completion targets. |
| 87 |
|
| 88 |
Add one no reading completion target: |
| 89 |
|
| 90 |
.. groonga-command |
| 91 |
.. include:: ../../example/reference/functions/prefix_rk_search/usage_add_no_reading_completion_target.log |
| 92 |
.. load --table Items |
| 93 |
.. [ |
| 94 |
.. {"_key": "nihon", "readings": []} |
| 95 |
.. ] |
| 96 |
|
| 97 |
Combine :ref:`script-syntax-prefix-search-operator` to |
| 98 |
support no reading completion targets: |
| 99 |
|
| 100 |
.. groonga-command |
| 101 |
.. include:: ../../example/reference/functions/prefix_rk_search/usage_prefix_search_combined_completion.log |
| 102 |
.. select Items \ |
| 103 |
.. --filter 'sub_filter(readings, "prefix_rk_search(_key, \\"niho\\")") || _key @^ "niho"' |
| 104 |
|
| 105 |
Normally, you want to use case insensitive search for completion. Use |
| 106 |
``--normalizer NormalizerAuto`` and ``label`` column for the case: |
| 107 |
|
| 108 |
.. groonga-command |
| 109 |
.. include:: ../../example/reference/functions/prefix_rk_search/usage_setup_loose_completion.log |
| 110 |
.. table_create LooseItems TABLE_HASH_KEY ShortText --normalizer NormalizerAuto |
| 111 |
.. column_create LooseItems label COLUMN_SCALAR ShortText |
| 112 |
.. column_create LooseItems readings COLUMN_VECTOR Readings |
| 113 |
.. |
| 114 |
.. column_create Readings loose_items_index COLUMN_INDEX LooseItems readings |
| 115 |
.. |
| 116 |
.. load --table LooseItems |
| 117 |
.. [ |
| 118 |
.. {"_key": "日本", "label": "日本", "readings": ["ニホン", "ニッポン"]}, |
| 119 |
.. {"_key": "ローマ字", "label": "ローマ字", "readings": ["ローマジ"]}, |
| 120 |
.. {"_key": "漢字", "label": "漢字", "readings": ["カンジ"]}, |
| 121 |
.. {"_key": "Nihon", "label": "日本", "readings": []} |
| 122 |
.. ] |
| 123 |
|
| 124 |
Use ``LooseItems.label`` for display: |
| 125 |
|
| 126 |
.. groonga-command |
| 127 |
.. include:: ../../example/reference/functions/prefix_rk_search/usage_loose_completion.log |
| 128 |
.. select LooseItems \ |
| 129 |
.. --filter 'sub_filter(readings, "prefix_rk_search(_key, \\"nIhO\\")") || _key @^ "nIhO"' \ |
| 130 |
.. --output_columns '_key,label' |
| 131 |
|
| 132 |
Parameters |
| 133 |
---------- |
| 134 |
|
| 135 |
There are two required parameter, ``column`` and ``query``. |
| 136 |
|
| 137 |
``column`` |
| 138 |
^^^^^^^^^^ |
| 139 |
|
| 140 |
Always specifies ``_key`` for now. |
| 141 |
|
| 142 |
``query`` |
| 143 |
^^^^^^^^^ |
| 144 |
|
| 145 |
Specifies a query in romaji, katakana or hiragana as string. |
| 146 |
|
| 147 |
Return value |
| 148 |
------------ |
| 149 |
|
| 150 |
``prefix_rk_search`` function returns matched records. |
| 151 |
|
| 152 |
See also |
| 153 |
-------- |
| 154 |
|
| 155 |
* :doc:`/reference/operations/prefix_rk_search` |
| 156 |
* :doc:`/reference/functions/sub_filter` |