| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_snippet |
| 5 |
|
| 6 |
``snippet`` |
| 7 |
=========== |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
This function extracts snippets of target text around search |
| 13 |
keywords (``KWIC``. ``KeyWord In Context``). |
| 14 |
|
| 15 |
If you want to use this function for normal Web application, |
| 16 |
:doc:`snippet_html` may be suitable. It's a HTML specific version of |
| 17 |
this function. |
| 18 |
|
| 19 |
Syntax |
| 20 |
------ |
| 21 |
|
| 22 |
``snippet`` requires at least one parameter that is the snippet target |
| 23 |
text:: |
| 24 |
|
| 25 |
snippet(column, ...) |
| 26 |
|
| 27 |
You can specify one ore more tuples of keyword, open tag and close tag:: |
| 28 |
|
| 29 |
snippet(column, |
| 30 |
"keyword1", "open-tag1", "close-tag1", |
| 31 |
"keyword2", "open-tag2", "close-tag2", |
| 32 |
...) |
| 33 |
|
| 34 |
If you specify default open tag and default close tag, you can specify |
| 35 |
only keywords:: |
| 36 |
|
| 37 |
snippet(column, |
| 38 |
"keyword1", |
| 39 |
"keyword2", |
| 40 |
..., |
| 41 |
{ |
| 42 |
"default_open_tag": "open-tag", |
| 43 |
"default_close_tag": "close-tag" |
| 44 |
}) |
| 45 |
|
| 46 |
.. versionadded:: 11.0.9 |
| 47 |
|
| 48 |
If you specify default open tag and default close tag and omit |
| 49 |
keywords, keywords are extracted from the current condition |
| 50 |
automatically like :doc:`snippet_html`:: |
| 51 |
|
| 52 |
snippet(column, |
| 53 |
{ |
| 54 |
"default_open_tag": "open-tag", |
| 55 |
"default_close_tag": "close-tag" |
| 56 |
}) |
| 57 |
|
| 58 |
You can specify options as the last argument with all syntaxes:: |
| 59 |
|
| 60 |
snippet(column, |
| 61 |
..., |
| 62 |
{ |
| 63 |
"width": 200, |
| 64 |
"max_n_results": 3, |
| 65 |
"skip_leading_spaces": true, |
| 66 |
"html_escape": false, |
| 67 |
"prefix": null, |
| 68 |
"suffix": null, |
| 69 |
"normalizer": null, |
| 70 |
"default_open_tag": null, |
| 71 |
"default_close_tag": null, |
| 72 |
"default": null, |
| 73 |
"delimiter_pattern": null, |
| 74 |
}) |
| 75 |
|
| 76 |
Usage |
| 77 |
----- |
| 78 |
|
| 79 |
Here are a schema definition and sample data to show usage. |
| 80 |
|
| 81 |
.. groonga-command |
| 82 |
.. include:: ../../example/reference/functions/snippet/usage_setup.log |
| 83 |
.. table_create Documents TABLE_NO_KEY |
| 84 |
.. column_create Documents content COLUMN_SCALAR Text |
| 85 |
.. table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto |
| 86 |
.. column_create Terms documents_content_index COLUMN_INDEX|WITH_POSITION Documents content |
| 87 |
.. load --table Documents |
| 88 |
.. [ |
| 89 |
.. ["content"], |
| 90 |
.. ["Groonga is a fast and accurate full text search engine based on inverted index. One of the characteristics of groonga is that a newly registered document instantly appears in search results. Also, groonga allows updates without read locks. These characteristics result in superior performance on real-time applications."], |
| 91 |
.. ["Groonga is also a column-oriented database management system (DBMS). Compared with well-known row-oriented systems, such as MySQL and PostgreSQL, column-oriented systems are more suited for aggregate queries. Due to this advantage, groonga can cover weakness of row-oriented systems."] |
| 92 |
.. ] |
| 93 |
|
| 94 |
``snippet`` extracts keywords from conditions specified in ``--query`` |
| 95 |
and/or ``--filter`` automatically when you specify |
| 96 |
``default_open_tag`` option and ``default_close_tag`` and don't |
| 97 |
specify keywords. It's similar to :doc:`snippet_html`. |
| 98 |
|
| 99 |
The following example uses ``--query "fast performance"``. In this |
| 100 |
case, ``fast`` and ``performance`` are used as keywords. |
| 101 |
|
| 102 |
.. groonga-command |
| 103 |
.. include:: ../../example/reference/functions/snippet/usage_keywords_from_conditions.log |
| 104 |
.. select Documents \ |
| 105 |
.. --output_columns 'snippet(content, \ |
| 106 |
.. { \ |
| 107 |
.. "default_open_tag": "[", \ |
| 108 |
.. "default_close_tag": "]" \ |
| 109 |
.. })' \ |
| 110 |
.. --match_columns content \ |
| 111 |
.. --query "fast performance" |
| 112 |
|
| 113 |
``--query "fast performance"`` matches to only the first record's |
| 114 |
content. This ``snippet`` extracts two text parts that include the |
| 115 |
keywords ``fast`` or ``performance`` and surrounds the keywords with |
| 116 |
``[`` and ``]``. |
| 117 |
|
| 118 |
The max number of text parts is 3 by default. You can change it by |
| 119 |
``max_n_results`` option: |
| 120 |
|
| 121 |
.. groonga-command |
| 122 |
.. include:: ../../example/reference/functions/snippet/usage_max_n_results.log |
| 123 |
.. select Documents \ |
| 124 |
.. --output_columns 'snippet(content, \ |
| 125 |
.. { \ |
| 126 |
.. "default_open_tag": "[", \ |
| 127 |
.. "default_close_tag": "]", \ |
| 128 |
.. "max_n_results": 1 \ |
| 129 |
.. })' \ |
| 130 |
.. --match_columns content \ |
| 131 |
.. --query "fast performance" |
| 132 |
|
| 133 |
It returns only one snippet because ``"max_n_results": 1`` is specified. |
| 134 |
|
| 135 |
The max size of a text part is 200byte by default. The unit is bytes |
| 136 |
not characters. The size doesn't include inserted ``[`` and ``[``. You |
| 137 |
can change it by ``width`` option: |
| 138 |
|
| 139 |
.. groonga-command |
| 140 |
.. include:: ../../example/reference/functions/snippet/usage_width.log |
| 141 |
.. select Documents \ |
| 142 |
.. --output_columns 'snippet(content, \ |
| 143 |
.. { \ |
| 144 |
.. "default_open_tag": "[", \ |
| 145 |
.. "default_close_tag": "]", \ |
| 146 |
.. "width": 50 \ |
| 147 |
.. })' \ |
| 148 |
.. --match_columns content \ |
| 149 |
.. --query "fast performance" |
| 150 |
|
| 151 |
You can detect snippet delimiter with regular expression by |
| 152 |
``delimiter_regexp`` option. You can use ``\.\s*`` to use only text in |
| 153 |
the target sentence. Note that you need to escape ``\`` in string: |
| 154 |
|
| 155 |
.. groonga-command |
| 156 |
.. include:: ../../example/reference/functions/snippet/usage_delimiter_regexp.log |
| 157 |
.. select Documents \ |
| 158 |
.. --output_columns 'snippet(content, \ |
| 159 |
.. { \ |
| 160 |
.. "default_open_tag": "[", \ |
| 161 |
.. "default_close_tag": "]", \ |
| 162 |
.. "delimiter_regexp": "\\\\.\\\\s*" \ |
| 163 |
.. })' \ |
| 164 |
.. --match_columns content \ |
| 165 |
.. --query "fast performance" |
| 166 |
|
| 167 |
You can see the detected delimiters (``.`` and following white spaces) |
| 168 |
aren't included in the result snippets. This is intentional behavior. |
| 169 |
|
| 170 |
You can specify keywords explicitly instead of extracting keywords |
| 171 |
from the current condition: |
| 172 |
|
| 173 |
.. groonga-command |
| 174 |
.. include:: ../../example/reference/functions/snippet/usage_keywords.log |
| 175 |
.. select Documents \ |
| 176 |
.. --output_columns 'snippet(content, \ |
| 177 |
.. "fast", \ |
| 178 |
.. "performance", \ |
| 179 |
.. { \ |
| 180 |
.. "default_open_tag": "[", \ |
| 181 |
.. "default_close_tag": "]" \ |
| 182 |
.. })' |
| 183 |
|
| 184 |
This ``snippet`` returns two snippets for the first record and |
| 185 |
``null`` for the second record. Because the second record doesn't have |
| 186 |
any specified keywords. |
| 187 |
|
| 188 |
You can specify open tag and close tag for each keyword: |
| 189 |
|
| 190 |
.. groonga-command |
| 191 |
.. include:: ../../example/reference/functions/snippet/usage_tags.log |
| 192 |
.. select Documents \ |
| 193 |
.. --output_columns 'snippet(content, \ |
| 194 |
.. "fast", "[", "]", \ |
| 195 |
.. "performance", "(", ")")' |
| 196 |
|
| 197 |
This ``snippet`` surrounds ``fast`` with ``[`` and ``]]`` and |
| 198 |
``performance`` with ``(`` and ``)``. |
| 199 |
|
| 200 |
TODO: ``html_escape`` option and so on |
| 201 |
|
| 202 |
Parameters |
| 203 |
---------- |
| 204 |
|
| 205 |
Required parameters |
| 206 |
^^^^^^^^^^^^^^^^^^^ |
| 207 |
|
| 208 |
TODO |
| 209 |
|
| 210 |
Optional parameters |
| 211 |
^^^^^^^^^^^^^^^^^^^ |
| 212 |
|
| 213 |
TODO |
| 214 |
|
| 215 |
.. _snippet-max-n-results: |
| 216 |
|
| 217 |
``max_n_results`` |
| 218 |
""""""""""""""""" |
| 219 |
|
| 220 |
TODO |
| 221 |
|
| 222 |
.. _snippet-width: |
| 223 |
|
| 224 |
``width`` |
| 225 |
""""""""" |
| 226 |
|
| 227 |
TODO |
| 228 |
|
| 229 |
Return value |
| 230 |
------------ |
| 231 |
|
| 232 |
This function returns an array of string or ``null``. If This function |
| 233 |
can't find any snippets, it returns ``null``. |
| 234 |
|
| 235 |
An element of array is a snippet:: |
| 236 |
|
| 237 |
[SNIPPET1, SNIPPET2, ...] |
| 238 |
|
| 239 |
A snippet includes one or more keywords. The max byte size of a |
| 240 |
snippet except open tag and close tag is 200byte. The unit isn't the |
| 241 |
number of characters. |
| 242 |
|
| 243 |
You can change this by :ref:`snippet-width` option. |
| 244 |
|
| 245 |
The array size is larger than or equal to 1 and less than or equal |
| 246 |
to 3. |
| 247 |
|
| 248 |
You can change this by :ref:`snippet-max-n-results` option. |
| 249 |
|
| 250 |
See also |
| 251 |
-------- |
| 252 |
|
| 253 |
* :doc:`snippet_html` |
| 254 |
* :doc:`/reference/commands/select` |