7.15.24. snippet_html#
7.15.24.1. 概要#
この関数は対象テキスト中から検索キーワード周辺のテキスト( KWIC 。 KeyWord In Context )を抽出します。抽出されたテキストのことをスニペットと呼びます。
スニペットはHTML中に埋め込みやすいように処理されています。 < や > などの特殊文字は < や > にエスケープされています。キーワードは <span class="keyword"> と </span> で囲まれています。たとえば、 I am a groonga user. <3 という対象テキストのキーワード groonga でのスニペットは I am a <span class="keyword">groonga</span> user. <3 となります。
この関数は snippet のHTML特化バージョンです。通常、普通のWebアプリケーションでの使用ではこの関数で十分です。
7.15.24.2. 構文#
snippet_html の引数は1つだけです:
snippet_html(column)
7.15.24.3. 使い方#
使い方を示すために使うスキーマ定義とサンプルデータは以下の通りです。
実行例:
table_create Documents TABLE_NO_KEY
# [[0,1337566253.89858,0.000355720520019531],true]
column_create Documents content COLUMN_SCALAR Text
# [[0,1337566253.89858,0.000355720520019531],true]
table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
# [[0,1337566253.89858,0.000355720520019531],true]
column_create Terms documents_content_index COLUMN_INDEX|WITH_POSITION Documents content
# [[0,1337566253.89858,0.000355720520019531],true]
load --table Documents
[
["content"],
["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."],
["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."]
]
# [[0,1337566253.89858,0.000355720520019531],2]
snippet_html は select コマンドの --output_columns 内でのみ指定できます。
また、 --query と --filter オプションも指定する必要があります。(どちらか一方でも構いません。)これは、 --query と --filter オプションからキーワードを抽出しているためです。
以下の例は --query "fast performance" を使っています。この場合は、キーワードとして fast と performance を使います。
実行例:
select Documents --output_columns "snippet_html(content)" --command_version 2 --match_columns content --query "fast performance"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "snippet_html",
# null
# ]
# ],
# [
# [
# "Groonga is a <span class=\"keyword\">fast</span> 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, gro",
# "onga allows updates without read locks. These characteristics result in superior <span class=\"keyword\">performance</span> on real-time applications."
# ]
# ]
# ]
# ]
# ]
--query "fast performance" は最初のレコードの内容にだけマッチします。 snippet_html(content) は、テキスト中からキーワード fast と performance の少なくともどちらか一方を含んでいるテキストのスニペットを抽出します。今回は2箇所抽出しています。そして、抽出したテキストのスニペット内にあるキーワードを <span class="keyword"> と </span> で囲みます。
テキストのスニペット数は多くても3です。4つ以上のテキストのスニペットが抽出できるときは、最初の3つだけを使います。
テキストのスニペットの最大サイズは200バイトです。単位は文字数ではなくバイトです。挿入される <span class="keyword"> と </span> のバイト数はこのサイズの中には含まれません。
テキストのスニペットの最大数とテキストのスニペットの最大サイズはカスタマイズできません。
これらをカスタマイズしたい場合はこの関数ではなく snippet を使ってください。
カラムの代わりに文字列リテラルを指定することもできます。
実行例:
select Documents --output_columns 'snippet_html("Groonga is very fast fulltext search engine.")' --command_version 2 --match_columns content --query "fast performance"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "snippet_html",
# null
# ]
# ],
# [
# [
# "Groonga is very <span class=\"keyword\">fast</span> fulltext search engine."
# ]
# ]
# ]
# ]
# ]
7.15.24.4. 戻り値#
この関数は文字列の配列もしくは null を返します。この関数はは該当するスニペットがない場合に null を返します。
配列の要素がスニペットになります:
[SNIPPET1, SNIPPET2, SNIPPET3]
スニペットには1つ以上のキーワードが含まれています。開始タグと終了タグを除いたスニペットの最大サイズは200byteです。単位は文字数ではなくてバイトです。
開始タグは <span class="keyword"> で終了タグは </span> です。
配列のサイズは1以上3以下です。
スニペットの最大サイズまたは最大スニペット数を変更したい場合は snippet を使ってください。