Skip to content

Commit

Permalink
add es movie search demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jillesvangurp committed Sep 14, 2023
1 parent e3b5b70 commit dd25a2b
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 25 deletions.
41 changes: 40 additions & 1 deletion src/jsMain/kotlin/examples/quotesearch/quotesearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ import com.jilesvangurp.rankquest.core.pluginconfiguration.SearchContextField
import com.jilesvangurp.rankquest.core.pluginconfiguration.SearchPluginConfiguration
import com.jilesvangurp.rankquest.core.plugins.PluginFactory
import com.jilesvangurp.rankquest.core.plugins.PluginFactoryRegistry
import com.jillesvangurp.ktsearch.SearchClient
import com.jillesvangurp.ktsearch.deleteIndex
import com.jillesvangurp.ktsearch.index
import com.jillesvangurp.ktsearch.repository.repository
import components.busyResult
import components.confirm
import components.runWithBusy
import dev.fritz2.core.RootStore
import dev.fritz2.remote.http
import handlerScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -115,7 +125,7 @@ fun List<MovieQuote>.searchPlugin(nice: Boolean = true): SearchPlugin {
should = listOf(
MatchQuery("quote", text, prefixMatch = true),
MatchQuery("movie", text, prefixMatch = true, boost = 0.25),
)
)
)
} else {
MatchAll()
Expand All @@ -139,6 +149,35 @@ class MovieQuotesStore : RootStore<List<MovieQuote>>(listOf()) {
quotes.indices.map { i -> quotes[i].copy(id = "$i") }
}
}
val delRecipesES = handle {
runWithBusy({
val client = SearchClient()
client.deleteIndex("moviequotes")
})
it
}

val indexEs = handle {
confirm(
"Write to localhost:9200?",
"This will attempt to write movies json to a locally running elasticsearch"
) {

runWithBusy({
val client = SearchClient() // localhost:9200 is default
val repository = client.repository(
"moviequotes",
MovieQuote.serializer()
)
repository.bulk {
current.forEach { m ->
index(m)
}
}
})
}
it
}

init {
load("moviequotes.json")
Expand Down
43 changes: 42 additions & 1 deletion src/jsMain/kotlin/searchpluginconfig/searchpluginconfig.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package searchpluginconfig

import com.jilesvangurp.rankquest.core.DEFAULT_JSON
import com.jilesvangurp.rankquest.core.DEFAULT_PRETTY_JSON
import com.jilesvangurp.rankquest.core.pluginconfiguration.*
import com.jilesvangurp.rankquest.core.plugins.BuiltinPlugins
import com.jilesvangurp.rankquest.core.plugins.PluginFactoryRegistry
import components.*
import dev.fritz2.core.*
import dev.fritz2.remote.http
import examples.quotesearch.MovieQuotesStore
import examples.quotesearch.movieQuotesNgramsSearchPluginConfig
import examples.quotesearch.movieQuotesSearchPluginConfig
import io.ktor.client.*
Expand Down Expand Up @@ -33,7 +36,7 @@ fun RenderContext.pluginConfiguration() {
val pluginConfigurationStore = koin.get<PluginConfigurationsStore>()
val activeSearchPluginConfigurationStore = koin.get<ActiveSearchPluginConfigurationStore>()
val showDemoContentStore = koin.get<Store<Boolean>>(named("showDemo")) as Store<Boolean>

val movieQuotesStore = koin.get<MovieQuotesStore>()
centeredMainPanel {

val editConfigurationStore = storeOf<SearchPluginConfiguration?>(null)
Expand Down Expand Up @@ -133,6 +136,44 @@ fun RenderContext.pluginConfiguration() {
}
}
}
secondaryButton {
+"ES Based moviesearch"
clicks handledBy {
val config = http("es-movie-quotes-config.json").get().body().let {
DEFAULT_JSON.decodeFromString<SearchPluginConfiguration>(it)
}
pluginConfigurationStore.addOrReplace(config)
}

}
secondaryButton {
+"Index movies into ES"
clicks handledBy movieQuotesStore.indexEs
}
secondaryButton {
+"Clean ES"
clicks handledBy movieQuotesStore.delRecipesES
}
infoPopup("Elasticsearch", """
This indexes the demo movie quote content into Elasticsearch.
Make sure it is running on localhost and port 9200
Important: **make sure your elasticsearch server is configured to send cors headers**. Without
this your browser will not allow this application to send requests to Elasticsearch.
If you use docker-compose, you can add these settings:
```
http.cors.enabled: "true"
http.cors.allow-origin: |-
"*"
http.cors.allow-methods: "OPTIONS, HEAD, GET, POST, PUT, DELETE"
http.cors.allow-headers: "X-Requested-With, X-Auth-Token, Content-Type, Content-Length, Authorization, Access-Control-Allow-Headers, Accept"
```
""".trimIndent())
}
} else {
a {
Expand Down
36 changes: 36 additions & 0 deletions src/jsMain/resources/es-movie-quotes-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"id": "jroFLKxA4n4USrmt7IPEiQ==",
"name": "movies search with elastic",
"pluginType": "ElasticSearch",
"fieldConfig": [
{
"type": "int",
"name": "size",
"defaultValue": 10,
"placeHolder": ""
},
{
"type": "str",
"name": "text",
"defaultValue": "",
"placeHolder": ""
}
],
"metrics": [
],
"pluginSettings": {
"queryTemplate": "{\n \"size\": {{size}},\n \"query\": {\n \"bool\": {\n \"should\": [\n {\n \"multi_match\": {\n \"query\": \"{{text}}\",\n \"fields\": [\"quote^2\",\"movie\"]\n }\n },\n {\n \"multi_match\": {\n \"query\": \"{{text}}\",\n \"fields\": [\"quote^2\",\"movie\"],\n \"type\": \"phrase_prefix\",\n \"boost\": 2.0\n }\n },\n {\n \"multi_match\": {\n \"query\": \"{{text}}\",\n \"fields\": [\"quote^2\",\"movie\"],\n \"type\": \"cross_fields\",\n \"boost\": 2.0\n }\n },\n {\n \"multi_match\": {\n \"query\": \"{{text}}\",\n \"fields\": [\"quote^2\",\"movie\"],\n \"type\": \"best_fields\",\n \"boost\": 0.1,\n \"fuzziness\": \"AUTO\"\n }\n }\n ]\n }\n }\n}",
"index": "",
"labelFields": [
"quote",
"movie",
"year"
],
"host": "localhost",
"port": 9200,
"https": false,
"user": "",
"password": "",
"logging": false
}
}
84 changes: 61 additions & 23 deletions src/jsMain/resources/movie-quotes-test-cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
"ratings": [
{
"documentId": "292",
"rating": 1,
"rating": 3,
"label": "The Lord of the Rings: The Two Towers, 2002: My precious (1.1936949131693646)"
},
{
"documentId": "295",
"rating": 1,
"label": "The Lord of the Rings: The Two Towers, 2002: My precious... (1.1936949131693646)"
},
{
"documentId": "284",
"rating": 1,
"label": "The Lord of the Rings: The Two Towers, 2002: I don't like sand. It's all coarse, and rough, and irritating. And it gets everywhere. (0.15079300502320062)"
"rating": 3,
"label": "The Lord of the Rings: The Two Towers, 2002: My precious... (1.1936949131693646)",
"comment": "Duplicate, we don't care which"
},
{
"documentId": "683",
"rating": 1,
"label": "A Beautiful Day in the Neighbourhood, 2019: I think the best thing we can do is to let people know that each one of them is precious. (0.11936949131693647)"
},
{
"documentId": "284",
"rating": 0,
"label": "The Lord of the Rings: The Two Towers, 2002: I don't like sand. It's all coarse, and rough, and irritating. And it gets everywhere. (0.04814088229950173)"
}
]
},
Expand Down Expand Up @@ -101,11 +102,6 @@
"documentId": "662",
"rating": 2,
"label": "Avengers: Infinity War, 2018: That does put a smile on my face. (0.19880052279985932)"
},
{
"documentId": "98",
"rating": 1,
"label": "Labyrinth, 1986: It's only forever, not long at all... (0.24517763675830603)"
}
]
},
Expand Down Expand Up @@ -175,16 +171,6 @@
"documentId": "499",
"rating": 1,
"label": "The Hobbit, 2012: Where there's life there's hope. (0.33948328526117105)"
},
{
"documentId": "290",
"rating": 1,
"label": "The Lord of the Rings: The Two Towers, 2002: There is always hope. (0.2697953115119062)"
},
{
"documentId": "603",
"rating": 1,
"label": "The Force Awakens, 2015: Chewie, we're home. (0.2697953115119062)"
}
]
},
Expand Down Expand Up @@ -221,5 +207,57 @@
"label": "Children Of Men, 2006: Y'know that ringing in your ears? That 'eeeeeeeeee'? That's the sound of the ear cells dying, like their swan song. Once it's gone you'll never hear that frequency again. Enjoy it while it lasts. (0.0674600285630108)"
}
]
},
{
"id": "+QmpiCGmc+PwtgHaYE4bcg==",
"searchContext": {
"size": "10",
"text": "the big le"
},
"ratings": [
{
"documentId": "hOf4k4oB33WtPo2L9ezG",
"rating": 5,
"label": "I’m the Dude, so that’s what you call me. That or, uh His Dudeness, or uh Duder, or El Duderino, if you’re not into the whole brevity thing. | The Big Lebowski | 1998"
}
]
},
{
"id": "/iD24zoP2QESFiyAfpxsGw==",
"searchContext": {
"size": "10",
"text": "dudenezz"
},
"ratings": [
{
"documentId": "hOf4k4oB33WtPo2L9ezG",
"rating": 5,
"label": "I’m the Dude, so that’s what you call me. That or, uh His Dudeness, or uh Duder, or El Duderino, if you’re not into the whole brevity thing. | The Big Lebowski | 1998"
}
]
},
{
"id": "cCQfWO10EyAso09XVvkzPw==",
"searchContext": {
"size": "10",
"text": "el duderino"
},
"ratings": [
{
"documentId": "hOf4k4oB33WtPo2L9ezG",
"rating": 5,
"label": "I’m the Dude, so that’s what you call me. That or, uh His Dudeness, or uh Duder, or El Duderino, if you’re not into the whole brevity thing. | The Big Lebowski | 1998"
},
{
"documentId": "Z-f5k4oB33WtPo2LBe7h",
"rating": 5,
"label": "You're really lucky, you know that? You didn't have to wait your whole life to do something special. | El Camino | 2019"
},
{
"documentId": "eef5k4oB33WtPo2LBe7h",
"rating": 5,
"label": "Dude, you are on fire. | El Camino | 2019"
}
]
}
]

0 comments on commit dd25a2b

Please sign in to comment.