Groongaにはトークナイズされたトークンに所定の処理を行うトークンフィルターモジュールがあります。
トークンフィルターモジュールはプラグインとして追加できます。
トークンフィルタープラグインをGroongaに追加することでトークナイズされたトークンをカスタマイズできます。
テーブルは0個以上のトークンフィルターを持てます。テーブルにトークンフィルターを付けるには table_create の token_filters オプションを使います。
以下は TokenFilterStopWord トークンフィルターモジュールを使う table_create の例です。
実行例:
register token_filters/stop_word
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create Terms TABLE_PAT_KEY ShortText \
--default_tokenizer TokenBigram \
--normalizer NormalizerAuto \
--token_filters TokenFilterStopWord
# [[0, 1337566253.89858, 0.000355720520019531], true]
以下は組み込みのトークンフィルターのリストです。
TokenFilterStopWord は、文書を検索する時にトークナイズされたトークンからストップワードを除去します。
TokenFilterStopWord は、文書を検索する時のみトークン除去するため、文書を追加した後でストップワードを指定することもできます。
ストップワードは、語彙表の is_stop_word カラムで指定します。
以下は TokenFilterStopWord トークンフィルターを使う例です。
実行例:
register token_filters/stop_word
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create Memos TABLE_NO_KEY
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Memos content COLUMN_SCALAR ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create Terms TABLE_PAT_KEY ShortText \
--default_tokenizer TokenBigram \
--normalizer NormalizerAuto \
--token_filters TokenFilterStopWord
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Terms memos_content COLUMN_INDEX|WITH_POSITION Memos content
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Terms is_stop_word COLUMN_SCALAR Bool
# [[0, 1337566253.89858, 0.000355720520019531], true]
load --table Terms
[
{"_key": "and", "is_stop_word": true}
]
# [[0, 1337566253.89858, 0.000355720520019531], 1]
load --table Memos
[
{"content": "Hello"},
{"content": "Hello and Good-bye"},
{"content": "Good-bye"}
]
# [[0, 1337566253.89858, 0.000355720520019531], 3]
select Memos --match_columns content --query "Hello and"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 2
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "content",
# "ShortText"
# ]
# ],
# [
# 1,
# "Hello"
# ],
# [
# 2,
# "Hello and Good-bye"
# ]
# ]
# ]
# ]
and というトークンは Terms テーブルでストップワードと指定されています。
"Hello" は文書内に and がありませんがマッチしています。なぜなら、 and はストップワードと指定されているため、クエリーから除去されているからです。
TokenFilterStem は、トークナイズされたトークンをステミングします。
以下は TokenFilterStem トークンフィルターを使う例です。
実行例:
register token_filters/stem
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create Memos TABLE_NO_KEY
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Memos content COLUMN_SCALAR ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create Terms TABLE_PAT_KEY ShortText \
--default_tokenizer TokenBigram \
--normalizer NormalizerAuto \
--token_filters TokenFilterStem
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Terms memos_content COLUMN_INDEX|WITH_POSITION Memos content
# [[0, 1337566253.89858, 0.000355720520019531], true]
load --table Memos
[
{"content": "I develop Groonga"},
{"content": "I'm developing Groonga"},
{"content": "I developed Groonga"}
]
# [[0, 1337566253.89858, 0.000355720520019531], 3]
select Memos --match_columns content --query "develops"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 3
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "content",
# "ShortText"
# ]
# ],
# [
# 1,
# "I develop Groonga"
# ],
# [
# 2,
# "I'm developing Groonga"
# ],
# [
# 3,
# "I developed Groonga"
# ]
# ]
# ]
# ]
develop も developing も developed も develops も、すべてステミングすると develop になります。そのため、 develops というクエリーで develop も developing も developed も検索できます。