Skip to content

Commit

Permalink
give up elasticsearch (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
imbananko authored Jun 27, 2024
1 parent 3d17a83 commit f52b7e6
Show file tree
Hide file tree
Showing 13 changed files with 6 additions and 359 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ dependencies {
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
implementation("co.elastic.clients:elasticsearch-java:8.6.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.6.4")

// spring
Expand Down
18 changes: 0 additions & 18 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,3 @@ services:
- POSTGRES_DB=tilly
ports:
- '5432:5432'
elasticsearch:
platform: linux/amd64
container_name: es-container
image: docker.elastic.co/elasticsearch/elasticsearch:7.11.0
environment:
- xpack.security.enabled=false
- "discovery.type=single-node"
ports:
- "9200:9200"
# kibana:
# container_name: kb-container
# image: docker.elastic.co/kibana/kibana:7.11.0
# environment:
# - ELASTICSEARCH_HOSTS=http://es-container:9200
# depends_on:
# - elasticsearch
# ports:
# - "5601:5601"
14 changes: 0 additions & 14 deletions src/main/kotlin/com/chsdngm/tilly/Configuration.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package com.chsdngm.tilly

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient
import co.elastic.clients.json.jackson.JacksonJsonpMapper
import co.elastic.clients.transport.rest_client.RestClientTransport
import com.chsdngm.tilly.config.MetadataProperties
import com.chsdngm.tilly.config.TelegramProperties
import com.chsdngm.tilly.handlers.AbstractHandler
import com.chsdngm.tilly.model.Timestampable
import org.apache.http.HttpHost
import org.elasticsearch.client.RestClient
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -22,12 +16,4 @@ class Configuration {
fun updateHandlers(updateHandlers: List<AbstractHandler<out Timestampable>>): List<AbstractHandler<Timestampable>> {
return updateHandlers as List<AbstractHandler<Timestampable>>
}

@Bean
fun elasticsearchClient(@Value("\${elasticsearch.url}") elasticsearchUrl: String): ElasticsearchAsyncClient {
val restClient = RestClient.builder(HttpHost(elasticsearchUrl, 9200)).build()
val transport = RestClientTransport(restClient, JacksonJsonpMapper())

return ElasticsearchAsyncClient(transport)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,22 @@ package com.chsdngm.tilly.handlers

import com.chsdngm.tilly.TelegramApi
import com.chsdngm.tilly.model.InlineCommandUpdate
import com.chsdngm.tilly.similarity.ElasticsearchService
import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.telegram.telegrambots.meta.api.methods.AnswerInlineQuery
import org.telegram.telegrambots.meta.api.objects.Update
import org.telegram.telegrambots.meta.api.objects.inlinequery.result.cached.InlineQueryResultCachedPhoto
import kotlin.time.ExperimentalTime
import kotlin.time.TimeSource

@Service
class InlineCommandHandler(
val elasticsearchService: ElasticsearchService,
val api: TelegramApi
) : AbstractHandler<InlineCommandUpdate>() {
class InlineCommandHandler(val api: TelegramApi) : AbstractHandler<InlineCommandUpdate>() {
private val log = LoggerFactory.getLogger(javaClass)
val chunkSize = 16

//TODO remove
@OptIn(ExperimentalTime::class)
private val timeSource = TimeSource.Monotonic

@OptIn(ExperimentalTime::class)
override fun handleSync(update: InlineCommandUpdate) {
if (update.value.isBlank() || update.value.length < 3) {
return
}

val mark = timeSource.markNow()
val offset = if (update.offset.isBlank()) 0 else update.offset.toInt()

val cachedPhotos = runBlocking {
elasticsearchService.searchMemesByText(update.value, offset, chunkSize).hits().hits().map {
InlineQueryResultCachedPhoto().apply {
photoFileId = it.id()
id = it.id().take(64)
}
}
}

AnswerInlineQuery().apply {
inlineQueryId = update.id
nextOffset = "${offset + 1}"
results = cachedPhotos
}.let { api.execute(it) }

log.info("InlineCommandHandler.handleSync elapsed ${mark.elapsedNow()}")
log.info("processed inline command update=$update")
}

Expand Down
38 changes: 0 additions & 38 deletions src/main/kotlin/com/chsdngm/tilly/schedulers/LogsSchedulers.kt

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/kotlin/com/chsdngm/tilly/similarity/ImageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import java.io.File

@Service
class ImageService(private val imageDao: ImageDao,
private val elasticsearchService: ElasticsearchService,
private val imageTextRecognizer: ImageTextRecognizer,
private val imageMatcher: ImageMatcher) {

Expand All @@ -22,15 +21,6 @@ class ImageService(private val imageDao: ImageDao,
rawLabels = analyzingResults?.labels
)

if (analyzingResults != null) {
if (!(analyzingResults.words.isNullOrEmpty() && analyzingResults.labels.isNullOrEmpty())) {
elasticsearchService.indexMeme(
update.fileId,
ElasticsearchService.MemeDocument(analyzingResults.words, analyzingResults.labels)
)
}
}

imageDao.insert(image)
imageMatcher.add(image)
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ spring:
vision:
enabled: false

elasticsearch:
url: http://localhost

#telegram:
# polling-enabled: false
3 changes: 0 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ telegram:
metadata:
commit.sha: local
moderation.threshold: 5

elasticsearch:
url: elasticsearch
58 changes: 0 additions & 58 deletions src/test/kotlin/com/chsdngm/tilly/LogsSchedulerTest.kt

This file was deleted.

Loading

0 comments on commit f52b7e6

Please sign in to comment.