0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ElasticSearch Token filter と Character filterどっちで処理すればええねん!

Posted at

概要

実装中に、Token filter と Character filterについて調べたのでその内容をメモ

Token fiilter

Tokenizerへ渡す前に入力文字列を処理する

Character filter

Tokenizerで処理され分割された文字列に対して処理する

明確な使い分けのシチュエーション

「ディズニー・ランド」
という文字があるとする。これを keyword typeとしてドキュメントに保存するとしよう。

要件

「・」という文字がなくても検索できるようにしたい

実装方法

「・」を除去した状態で、文字列を保存するようにする。
「ディズニー・ランド」 -> 「ディズニーランド」

この処理は、Token fiilter、Character filterのどちらで行えばよいだろうか?

Token filterの場合

よく理解していなかった自分は、Token filterの Stop Token Filter使えばいいんじゃね?って思っていた。
しかし、今回対象とするドキュメントの型は keyword である。
よって、anayalzeされないので文字列は分割されずそのまま保存される。
したがって、Token filterが適用される文字列は、今回の例でいうと「ディズニー・ランド」そのものである。
これに対してStop Token Filterに stopword として「・」を指定しても、「・」が除去されるわけでない・・・
したがって、Stop Token filterを使えない。

この時点で、自分は Character filterへの実装へ切り替えた。
先にいうとCharacter filterの Mapping を用いた。
しかし、今記事を書いていてTokne filterの referenceを見てみると、 Token filterにもPatter replaceでも実装できそうなことに気づいた。あうち

Character filterの場合

Mapping か Patter replaceで先に「・」を空文字にする処理を行う。

mapping
"type": 'mapping',
"mappings": ['・ =>  ', '& =>  ']

結論

今回のシチュエーションでは多分どっちでもできる。
しかし、Stop Token Filterについては、normalizer に指定した時エラーでてたのでそこは謎
normalizer は結局keyword tokenizerのような挙動をするので、tokenizerによって使えるfilterが変わるのかな?とか思ってた

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?