| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_highlight |
| 5 |
|
| 6 |
``highlight`` |
| 7 |
============= |
| 8 |
|
| 9 |
.. versionadded:: 6.0.0 |
| 10 |
|
| 11 |
Summary |
| 12 |
------- |
| 13 |
|
| 14 |
``highlight`` tags target text. It can use to highlight the search |
| 15 |
keyword. It can specify use/not use HTML escape, the normalizer name and |
| 16 |
change the tag for each keyword. |
| 17 |
|
| 18 |
Syntax |
| 19 |
------ |
| 20 |
|
| 21 |
``highlight`` has required parameter and optional parameter:: |
| 22 |
|
| 23 |
highlight(column, |
| 24 |
keyword1, open_tag1, close_tag1, |
| 25 |
... |
| 26 |
[keywordN, open_tagN, close_tagN], |
| 27 |
[{"option": "value of option"}]) |
| 28 |
|
| 29 |
Usage |
| 30 |
----- |
| 31 |
|
| 32 |
Here are a schema definition and sample data to show usage. |
| 33 |
|
| 34 |
.. groonga-command |
| 35 |
.. include:: ../../example/reference/functions/highlight/usage_setup.log |
| 36 |
.. table_create Entries TABLE_NO_KEY |
| 37 |
.. column_create Entries body COLUMN_SCALAR ShortText |
| 38 |
.. table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto |
| 39 |
.. column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body |
| 40 |
.. load --table Entries |
| 41 |
.. [ |
| 42 |
.. {"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."} |
| 43 |
.. ] |
| 44 |
|
| 45 |
``highlight`` can be used in only ``--output_columns`` in |
| 46 |
:doc:`/reference/commands/select` before version 10.0.6 (exclusive). |
| 47 |
However, it can be also used in ``--output_columns`` in |
| 48 |
:doc:`/reference/commands/logical_select` since version 10.0.6. |
| 49 |
|
| 50 |
``highlight`` requires :doc:`/reference/command/command_version` 2 |
| 51 |
or later. |
| 52 |
|
| 53 |
The following example uses HTML escape and normalizer is ``NormalizeAuto``. |
| 54 |
It specifies the tags ``<span class="keyword1">`` and ``</span>`` of the |
| 55 |
keyword ``groonga``, and the tags ``<span class="keyword2">`` and ``</span>`` |
| 56 |
of the keyword ``mysql``. |
| 57 |
|
| 58 |
.. groonga-command |
| 59 |
.. include:: ../../example/reference/functions/highlight/usage_basic.log |
| 60 |
.. select Entries --output_columns 'highlight(body, "Groonga", "<span class=\\"keyword1\\">", "</span>", "mysql", "<span class=\\"keyword2\\">", "</span>", {"normalizers": "NormalizerAuto", "html_mode": true})' --command_version 2 |
| 61 |
|
| 62 |
The text are scanned by the keywords for tagging after they are normalized |
| 63 |
by ``NormalizerAuto`` normalizer. |
| 64 |
|
| 65 |
``--query "groonga mysql"`` matches to the first record's body. |
| 66 |
``highlight`` surrounds the keywords ``groonga`` contained in the text |
| 67 |
with ``<span class="keyword1">`` and ``</span>``, and the keywords ``mysql`` |
| 68 |
contained in the text with ``<span class="keyword2">`` and ``</span>``. |
| 69 |
|
| 70 |
Special characters such as ``<`` and ``>`` are escapsed as ``<`` and |
| 71 |
``>``. |
| 72 |
|
| 73 |
You can specify string literal instead of column. |
| 74 |
|
| 75 |
.. groonga-command |
| 76 |
.. include:: ../../example/reference/functions/highlight/usage_string_literal.log |
| 77 |
.. select Entries --output_columns 'highlight("Groonga is very fast fulltext search engine.", "Groonga", "<span class=\\"keyword1\\">", "</span>", "mysql", "<span class=\\"keyword2\\">", "</span>", {"normalizers": "NormalizerAuto", "html_mode": true})' --command_version 2 --match_columns body --query "groonga" |
| 78 |
|
| 79 |
Parameters |
| 80 |
---------- |
| 81 |
There are multiple required parameters and multiple optional parameters. |
| 82 |
|
| 83 |
``column`` |
| 84 |
^^^^^^^^^^ |
| 85 |
|
| 86 |
Specifies a highlight target column. |
| 87 |
|
| 88 |
``keywordN`` |
| 89 |
^^^^^^^^^^^^ |
| 90 |
|
| 91 |
Specifies a keyword for tagging. |
| 92 |
You can specify multiple keywords. |
| 93 |
|
| 94 |
``open_tagN`` |
| 95 |
^^^^^^^^^^^^^ |
| 96 |
|
| 97 |
Specifies a open tag. |
| 98 |
You can specify multiple open tags for each keywords. |
| 99 |
|
| 100 |
``close_tagN`` |
| 101 |
^^^^^^^^^^^^^^ |
| 102 |
|
| 103 |
Specifies a close tag. |
| 104 |
You can specify multiple close tags for each keywords. |
| 105 |
|
| 106 |
``{"default_open_tag": "open_tag"}`` |
| 107 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 108 |
|
| 109 |
Specifies a open tag. |
| 110 |
You can not specify multiple open tags unlike ``open_tagN`` parameter. |
| 111 |
|
| 112 |
``{"default_close_tag": "close_tag"}`` |
| 113 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 114 |
|
| 115 |
Specifies a close tag. |
| 116 |
You can not specify multiple close tags unlike ``close_tagN`` parameter. |
| 117 |
|
| 118 |
``{"html_escape": true}`` or ``{"html_mode": true}`` |
| 119 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 120 |
|
| 121 |
.. versionadded:: 13.0.2 |
| 122 |
|
| 123 |
Specifies use or not use HTML escape. If it is ``true`` , use HTML escape. |
| 124 |
If it is ``false`` , not use HTML escape. |
| 125 |
|
| 126 |
``{"normalizer": "Normalizerxxx"}`` |
| 127 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 128 |
|
| 129 |
Specifies a normalizer name. |
| 130 |
|
| 131 |
``{"normalizers": "Normalizerxxx"}`` |
| 132 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 133 |
|
| 134 |
.. versionadded:: 13.0.2 |
| 135 |
|
| 136 |
Specifies a normalizer name. |
| 137 |
You can specify multiple normalizers. |
| 138 |
|
| 139 |
This option is useful when combining :doc:`/reference/normalizers/normalizer_table`. |
| 140 |
|
| 141 |
``{"sequential_class_tag_mode": true}`` |
| 142 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 143 |
|
| 144 |
.. versionadded:: 13.0.2 |
| 145 |
|
| 146 |
The default value of ``sequential_class_tag_mode`` is ``false``. |
| 147 |
If ``sequential_class_tag_mode`` is ``true``, you can use different style(e.g. background color) for each keywords. |
| 148 |
|
| 149 |
If ``sequential_class_tag_mode`` is ``true``, class tags are ``<mark class="keyword-%d">/<mark>`` for now. |
| 150 |
|
| 151 |
Return value |
| 152 |
------------ |
| 153 |
|
| 154 |
``highlight`` returns a tagged string or ``null``. If |
| 155 |
``highlight`` can't find any keywords, it returns ``null``. |
| 156 |
|
| 157 |
See also |
| 158 |
-------- |
| 159 |
|
| 160 |
* :doc:`/reference/commands/select` |
| 161 |
* :doc:`/reference/commands/logical_select` |
| 162 |
* :doc:`/reference/functions/highlight_html` |