| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_highlight_html |
| 5 |
|
| 6 |
``highlight_html`` |
| 7 |
================== |
| 8 |
|
| 9 |
.. versionadded:: 4.0.5 |
| 10 |
|
| 11 |
Summary |
| 12 |
------- |
| 13 |
|
| 14 |
``highlight_html`` tags target text. It can use to highlight the search |
| 15 |
keywords. The tagged text are prepared for embedding HTML. Special |
| 16 |
characters such as ``<`` and ``>`` are escapsed as ``<`` and ``>``. |
| 17 |
Keyword is surrounded with ``<span class="keyword">`` and ``</span>``. |
| 18 |
For example, a tagged text of ``I am a groonga user. <3`` for keyword |
| 19 |
``groonga`` is ``I am a <span class="keyword">groonga</span> user. <3``. |
| 20 |
|
| 21 |
Syntax |
| 22 |
------ |
| 23 |
|
| 24 |
This function has from one to two parameters:: |
| 25 |
|
| 26 |
highlight_html(text) |
| 27 |
highlight_html(text, {"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_html/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_html`` 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_html`` requires :doc:`/reference/command/command_version` 2 |
| 51 |
or later. |
| 52 |
|
| 53 |
You also need to specify ``--query`` and/or ``--filter``. Keywords are |
| 54 |
extracted from ``--query`` and ``--filter`` arguments. |
| 55 |
|
| 56 |
The following example uses ``--query "groonga mysql"``. In this case, |
| 57 |
``groonga`` and ``mysql`` are used as keywords. |
| 58 |
|
| 59 |
.. groonga-command |
| 60 |
.. include:: ../../example/reference/functions/highlight_html/usage_basic.log |
| 61 |
.. select Entries --output_columns --match_columns body --query 'groonga mysql' --output_columns 'highlight_html(body)' --command_version 2 |
| 62 |
|
| 63 |
The text are scanned by the keywords for tagging after they are normalized |
| 64 |
by ``NormalizerAuto`` normalizer. |
| 65 |
|
| 66 |
``--query "groonga mysql"`` matches to only the first record's body. |
| 67 |
``highlight_html(body)`` surrounds the keywords ``groonga`` or ``mysql`` |
| 68 |
contained in the text with ``<span class="keyword">`` and ``</span>``. |
| 69 |
|
| 70 |
You can specify string literal instead of column. |
| 71 |
|
| 72 |
.. groonga-command |
| 73 |
.. include:: ../../example/reference/functions/highlight_html/usage_string_literal.log |
| 74 |
.. select Entries --output_columns 'highlight_html("Groonga is very fast fulltext search engine.")' --command_version 2 --match_columns body --query "groonga" |
| 75 |
|
| 76 |
You can use different style(e.g. background color) for each keywords by ``sequential_class_tag_mode``. |
| 77 |
|
| 78 |
.. groonga-coomand |
| 79 |
.. include:: ../../example/reference/functions/highlight_html/usage_sequential_class_tag_mode.log |
| 80 |
.. select Entries --match_columns body --query 'groonga OR mroonga' --output_columns 'highlight_html(body, {"sequential_class_tag_mode": true})' |
| 81 |
|
| 82 |
Parameters |
| 83 |
---------- |
| 84 |
|
| 85 |
This section describes all parameters. |
| 86 |
|
| 87 |
Required parameters |
| 88 |
^^^^^^^^^^^^^^^^^^^ |
| 89 |
|
| 90 |
There is only one required parameter. |
| 91 |
|
| 92 |
``text`` |
| 93 |
"""""""" |
| 94 |
|
| 95 |
The text to be highlighted in HTML. |
| 96 |
|
| 97 |
Optional parameters |
| 98 |
^^^^^^^^^^^^^^^^^^^ |
| 99 |
|
| 100 |
``{"sequential_class_tag_mode": true}`` |
| 101 |
""""""""""""""""""""""""""""""""""""""" |
| 102 |
|
| 103 |
.. versionadded:: 13.0.2 |
| 104 |
|
| 105 |
The default value of ``sequential_class_tag_mode`` is ``false``. |
| 106 |
If ``sequential_class_tag_mode`` is ``true``, you can use different style(e.g. background color) for each keywords. |
| 107 |
|
| 108 |
If ``sequential_class_tag_mode`` is ``true``, class tags are ``<mark class="keyword-%d">/<mark>`` for now. |
| 109 |
|
| 110 |
Return value |
| 111 |
------------ |
| 112 |
|
| 113 |
``highlight_html`` returns a tagged string or ``null``. If |
| 114 |
``highlight_html`` can't find any keywords, it returns ``null``. |
| 115 |
|
| 116 |
See also |
| 117 |
-------- |
| 118 |
|
| 119 |
* :doc:`/reference/commands/select` |
| 120 |
* :doc:`/reference/commands/logical_select` |
| 121 |
* :doc:`/reference/functions/highlight_full` |