LoginSignup
0
0

More than 5 years have passed since last update.

elastic4sで_analyzeを試す

Posted at

elasticsearchのembeddedでkuromoji使えるようになったので_analyzeで解析結果を確認したかったのですがAPIが見つからず。。

stackoverflowで調べたらjavaで実現する方法を見つけました

例によってAPI一部変わってましたが実現できました

      createIndex("bands").mappings(
        mapping("artist") as (
          textField("name"),
          textField("description") analyzer "my_analyzer"
        )
      )
        .analysis(
          CustomAnalyzerDefinition(
            "my_analyzer",
            KuromojiTokenizer,
            KuromojiBaseformFilter
          )
        )
    }.await

こんな感じでschemaを定義してみます。

    val response: AnalyzeResponse = client.java.admin().indices().analyze(new AnalyzeRequest("bands").analyzer("my_analyzer").text("飲み放題")).actionGet()
    response.forEach { token =>
      {
        println(s"""
        |token: ${token.getTerm()}
        |start_offset: ${token.getStartOffset()}
        |end_offset: ${token.getEndOffset()}
        |type: ${token.getType}
        |position: ${token.getPosition}
        """.stripMargin)
      }

出力結果

token: 飲む
start_offset: 0
end_offset: 2
type: word
position: 0


token: 放題
start_offset: 2
end_offset: 4
type: word
position: 1

Kuromoji_baseformのおかげで「飲み」が「飲む」に変換されているのがわかります

コードはこちら

0
0
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
0