Dアニメを数ヶ月前に契約しました。月々500円位で最新作から懐かしのアニメまで好きな時間に見れるので重宝してます。欲を言えば、過激な作品はTV版と同じように規制がかかっているものがあるので、解除されていればより良かったのですが・・・
ただ、検索機能に不満あり
一般的なキーワードサーチ、あいうえお順、大まかな作品ジャンルは検索機能としてあるのですが、例えば人気順の並べ替えや、あらすじ内のキーワード検索、キャラクタの容姿 etcのような検索機能は、見た感じ準備されていません。
不思議なのは、内部的にデータを持っているので、もっといろいろな作品の見せ方ができるはずなのに、それをやっていないことです。
なので、勉強がてら作り始めました
Dアニメをもっと楽しみたいので自分仕様の検索機能を作ることにしました。
スクレイピングを行いDアニメの作品をelasticsearchに登録、kotlin + Springbootの構成で組むことにしました。
SpringbootからElasticsearchに検索を行う
ここからやっと本題です。Springbootからelasticsearchに対して検索をかけます。手順は以下の通り。
依存関係に以下を追加
dependencies {
implementation("org.springframework.data:spring-data-elasticsearch:4.1.7")
・・・
}
リポジトリクラスを作成
package xyz.sheltiegarage.dAnimeApi.repository
import org.springframework.data.domain.PageRequest
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
import xyz.sheltiegarage.dAnimeApi.data.DAnimeData
interface DAnimeRepository : ElasticsearchRepository<DAnimeData, Int> {
}
データクラスを追加
package xyz.sheltiegarage.dAnimeApi.data
import org.springframework.data.annotation.Id
import org.springframework.data.elasticsearch.annotations.Document
import org.springframework.data.elasticsearch.annotations.Field
@Document(indexName = "d_anime")
data class DAnimeData(
@Id
@Field(name="work_id")
val workId: Int,
@Field(name="work_title")
val workTitle: String,
@Field(name="link")
val link: String,
@Field(name="main_key_visual_path")
val mainKeyVisualPath: String,
@Field(name="main_key_visual_alt")
val mainKeyVisualAlt: String,
@Field(name="my_list_count")
val myListCount: Int,
@Field(name="favorite_count")
val favoriteCount: Int
)
サーチメソッドを呼び出し
@RestController
class SearchController(val dAnimeRepository: DAnimeRepository) {
@GetMapping("/search")
fun getAll() :List<DAnimeData> {
val data: List<DAnimeData> = dAnimeRepository.findAll(Sort.by(Sort.Order.desc("favorite_count"))).toList()
return data
}
}
application.propertiesに設定を追加
spring.elasticsearch.rest.uris=localhost:9200
参考にしたのは以下のサイトです
https://raphaeldelio.medium.com/how-to-connect-elasticsearch-to-kotlin-using-spring-boot-spring-data-and-ssl-f261a08727d8
リポジトリクラスを作成することで自動的に「selectAll(全検索)」といった基本的な検索が行なえます。リポジトリクラスは「メソッド名に基づき検索を行う」ことができるため、メソッド名の規約さえ守れば、メソッドの実装を行わずに検索を行うことができるので便利ですね。詳細は以下からご確認ください。
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.query-methods
複雑な検索はどうする?
上記のクエリメソッドを利用すれば検索は行なえますが、例えば「お気に入りが1000以上、声優にに「長縄」を含む、配信終了間近の作品」といったような複雑な検索を行う場合、クエリメソッドの仕組みでは対応できないため、別の検索手段を探す必要があります。その方法は・・・只今調査中です。(https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.operations.queries あたりを使えばできそうな気がするが・・・)
以上
雑な解説ですが、以上です。なぜなら自分もまだちゃんと理解していないから、きちんとした説明が行えないのです。とにかくkotlin + Springboot + elasticsearchの情報が少なく、何をするにも詰まってしまいますが、自分仕様の検索システムができるまでコツコツ開発は続けようと思っています。
お気に入り順に並び替えてみると「呪術廻戦」って「鬼滅の刃」超えのお気に入り登録数なのですね。無職転生やスライムなども人気が高いですね