| 1 | <!-- groonga-command --> |
| 2 | <!-- database: extractors_extractor_html --> |
| 3 | |
| 4 | (extractor-html)= |
| 5 | |
| 6 | # `ExtractorHTML` |
| 7 | |
| 8 | ## Summary |
| 9 | |
| 10 | ```{versionadded} 16.0.3 |
| 11 | |
| 12 | ``` |
| 13 | |
| 14 | ```{note} |
| 15 | This is an experimental feature. Currently, this feature is still not stable. |
| 16 | ``` |
| 17 | |
| 18 | `ExtractorHTML` extracts text content from HTML. It removes HTML tags |
| 19 | and expands character references (HTML entities such as `<`) by |
| 20 | default. You can use this extractor to index only the text in HTML |
| 21 | without markup. |
| 22 | |
| 23 | `ExtractorHTML` does nothing for a value that isn't a text type such |
| 24 | as `ShortText`, `Text` and `LongText`. The value is returned as-is in |
| 25 | this case. |
| 26 | |
| 27 | ## Syntax |
| 28 | |
| 29 | `ExtractorHTML` has optional parameters. |
| 30 | |
| 31 | No options: |
| 32 | |
| 33 | ``` |
| 34 | ExtractorHTML |
| 35 | ``` |
| 36 | |
| 37 | Specify options: |
| 38 | |
| 39 | ``` |
| 40 | ExtractorHTML("remove_tag", true) |
| 41 | |
| 42 | ExtractorHTML("expand_character_reference", true) |
| 43 | ``` |
| 44 | |
| 45 | ## Usage |
| 46 | |
| 47 | Here is an example that extracts text content from HTML by the default |
| 48 | parameters. HTML tags (`<html>`, `<body>` and so on) are removed and |
| 49 | character references (`<` and `>`) are expanded: |
| 50 | |
| 51 | <!-- groonga-command --> |
| 52 | |
| 53 | ```{include} ../../example/reference/extractors/extractor_html/default.md |
| 54 | extract \ |
| 55 | --extractors ExtractorHTML \ |
| 56 | --value "<html><body>He<ll>o</body></html>" |
| 57 | ``` |
| 58 | |
| 59 | You can keep HTML tags by setting {ref}`extractor-html-remove-tag` to |
| 60 | `false`. Character references are still expanded in this case: |
| 61 | |
| 62 | <!-- groonga-command --> |
| 63 | |
| 64 | ```{include} ../../example/reference/extractors/extractor_html/remove_tag.md |
| 65 | extract \ |
| 66 | --extractors 'ExtractorHTML("remove_tag", false)' \ |
| 67 | --value "<html><body>He<ll>o</body></html>" |
| 68 | ``` |
| 69 | |
| 70 | You can keep character references as-is by setting |
| 71 | {ref}`extractor-html-expand-character-reference` to `false`. HTML tags |
| 72 | are still removed in this case: |
| 73 | |
| 74 | <!-- groonga-command --> |
| 75 | |
| 76 | ```{include} ../../example/reference/extractors/extractor_html/expand_character_reference.md |
| 77 | extract \ |
| 78 | --extractors 'ExtractorHTML("expand_character_reference", false)' \ |
| 79 | --value "<html><body>He<ll>o</body></html>" |
| 80 | ``` |
| 81 | |
| 82 | When you attach `ExtractorHTML` to a lexicon, the lexicon indexes the |
| 83 | extracted text content. The original HTML is kept in the data column: |
| 84 | |
| 85 | <!-- groonga-command --> |
| 86 | |
| 87 | ```{include} ../../example/reference/extractors/extractor_html/index.md |
| 88 | table_create Contents TABLE_NO_KEY |
| 89 | column_create Contents html COLUMN_SCALAR Text |
| 90 | |
| 91 | table_create Terms TABLE_PAT_KEY ShortText \ |
| 92 | --default_tokenizer TokenBigram \ |
| 93 | --normalizers NormalizerNFKC \ |
| 94 | --extractors ExtractorHTML |
| 95 | column_create Terms contents_html COLUMN_INDEX|WITH_POSITION Contents html |
| 96 | |
| 97 | load --table Contents |
| 98 | [ |
| 99 | {"html": "<p>Groonga is a <b>fast</b> full text search engine.</p>"} |
| 100 | ] |
| 101 | |
| 102 | select Contents \ |
| 103 | --match_columns html \ |
| 104 | --query "fast" \ |
| 105 | --output_columns html |
| 106 | select Contents \ |
| 107 | --match_columns html \ |
| 108 | --query "<b>" \ |
| 109 | --output_columns html |
| 110 | ``` |
| 111 | |
| 112 | The query `fast` matches but the query `<b>` doesn't match because the |
| 113 | indexed token comes from the extracted text `Groonga is a fast full |
| 114 | text search engine.` instead of the raw HTML. |
| 115 | |
| 116 | ## Parameters |
| 117 | |
| 118 | ### Optional parameters |
| 119 | |
| 120 | (extractor-html-remove-tag)= |
| 121 | |
| 122 | #### `remove_tag` |
| 123 | |
| 124 | Specifies whether HTML tags are removed. |
| 125 | |
| 126 | If this is `true`, HTML tags such as `<p>` are removed. If this is |
| 127 | `false`, HTML tags are kept as-is. |
| 128 | |
| 129 | The default value is `true`. |
| 130 | |
| 131 | (extractor-html-expand-character-reference)= |
| 132 | |
| 133 | #### `expand_character_reference` |
| 134 | |
| 135 | Specifies whether character references (HTML entities) are expanded. |
| 136 | |
| 137 | If this is `true`, both named character references such as `<` and |
| 138 | numeric character references such as `あ` are expanded to the |
| 139 | corresponding characters. If this is `false`, character references are |
| 140 | kept as-is. |
| 141 | |
| 142 | The default value is `true`. |
| 143 | |
| 144 | ## See also |
| 145 | |
| 146 | - {doc}`/reference/extractors` |
| 147 | - {doc}`/reference/commands/table_create` |