ご用心
この機能は実験的です。APIが変わる可能性があります。
highlight_html は対象テキストをタグ付けします。検索文字列をハイライトさせるために利用することができます。タグ付けされたテキストはHTML中に埋め込みやすいように処理されています。< や > などの特殊文字は < や > にエスケープされています。キーワードは <span class="keyword"> と </span> で囲まれています。たとえば、 I am a groonga user. <3 という対象テキストのキーワード groonga でタグ付けされたテキストは I am a <span class="keyword">groonga</span> user. <3 となります。
使い方を示すために使うスキーマ定義とサンプルデータは以下の通りです。
実行例:
table_create Entries TABLE_NO_KEY
# [[0,1407692435.35498,0.0364797115325928],true]
column_create Entries body COLUMN_SCALAR ShortText
# [[0,1407692435.39156,0.0256640911102295],true]
table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
# [[0,1407692435.423,0.0274741649627686],true]
column_create Terms document_index COLUMN_INDEX|WITH_POSITION Entries body
# [[0,1407692435.45051,0.0534985065460205],true]
load --table Entries
[
{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."}
]
# [[0,1407692435.50406,0.378907442092896],1]
highlight_html は select コマンドの --output_columns 内でのみ指定できます。
highlight_html を使うにはGroonga 4.0.5以降が必要です。
highlight_html を使うには コマンドバージョン 2以降を使う必要があります。
また、 --query と --filter オプションも指定する必要があります。(どちらか一方でも構いません。)これは、 --query と --filter オプションからキーワードを抽出しているためです。
以下の例は --query "groonga mysql" を使っています。この場合は、キーワードとして groonga と mysql を使います。
実行例:
select Entries --output_columns --match_columns body --query 'groonga mysql' --output_columns 'highlight_html(body)' --command_version 2
# [
# [
# 0,
# 1407692444.86092,
# 0.00188779830932617
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "highlight_html",
# "null"
# ]
# ],
# [
# "Mroonga is a <span class=\"keyword\">MySQL</span> storage engine based on <span class=\"keyword\">Groonga</span>. <b>Rroonga</b> is a Ruby binding of <span class=\"keyword\">Groonga</span>."
# ]
# ]
# ]
# ]
キーワードとテキストは NormalizerAuto ノーマライザーで正規化されてタグ付けのためにスキャンされます。
--query "groonga mysql" は最初のレコードにマッチします。highlight_html(body) は、テキスト中に含まれるキーワード groonga と mysql を <span class="keyword"> と </span> で囲みます。
カラムの代わりに文字列リテラルを指定することもできます。
実行例:
select Entries --output_columns 'highlight_html("Groonga is very fast fulltext search engine.")' --command_version 2 --match_columns body --query "groonga"
# [
# [
# 0,
# 1407693081.99183,
# 0.000126123428344727
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "highlight_html",
# "null"
# ]
# ],
# [
# "<span class=\"keyword\">Groonga</span> is very fast fulltext search engine."
# ]
# ]
# ]
# ]
highlight_html はタグ付の文字列もしくは null を返します。highlight_html は該当するキーワードがない場合に null を返します。