Skip to content

Commit

Permalink
-- elastic search
Browse files Browse the repository at this point in the history
  • Loading branch information
djnzx committed Sep 13, 2023
1 parent f89892e commit 35d4f43
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 2 deletions.
28 changes: 26 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,30 @@ lazy val ce3 = (project in file("ce3"))
)
)

lazy val es68 = (project in file("es68"))
.settings(
Settings.common,
description := "Elastic Search 6.8 Learning",
libraryDependencies ++= Seq(
"com.sksamuel.elastic4s" %% "elastic4s-core" % "6.7.8",
"com.sksamuel.elastic4s" %% "elastic4s-http" % "6.7.8",
"io.circe" %% "circe-parser" % "0.14.6",
"io.circe" %% "circe-generic-extras" % "0.14.3",
"org.typelevel" %% "cats-core" % "2.10.0",
),
)

lazy val es89 = (project in file("es89"))
.settings(
Settings.common,
description := "Elastic Search 8 Learning",
libraryDependencies ++= Seq(
"com.sksamuel.elastic4s" %% "elastic4s-client-esjava" % "8.9.2",
"com.sksamuel.elastic4s" %% "elastic4s-testkit" % "8.9.2",
"org.typelevel" %% "cats-core" % "2.10.0",
),
)

lazy val httpt = (project in file("httpt"))
.settings(
Settings.common,
Expand Down Expand Up @@ -256,8 +280,8 @@ lazy val plain2 = (project in file("plain2"))
.settings(
Settings.common,
libraryDependencies ++= Seq(
"io.chymyst" %% "curryhoward" % "0.3.8",
"com.softwaremill.quicklens" %% "quicklens" % "1.9.6",
"io.chymyst" %% "curryhoward" % "0.3.8",
"com.softwaremill.quicklens" %% "quicklens" % "1.9.6",
)
)

Expand Down
20 changes: 20 additions & 0 deletions ce3/src/main/scala/pmatch/PatternMatchPlayground.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pmatch

object PatternMatchPlayground extends App {

case class Id(id: Int)

val myValue = 13
val id1 = Id(13)
val id2 = Id(1)

Seq(
id1,
id2,
).foreach {
case Id(`myValue`) => pprint.pprintln(13)
case myId => pprint.pprintln(myId)
}


}
104 changes: 104 additions & 0 deletions es68/src/main/scala/es/ExploreElastic68.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package es

import com.sksamuel.elastic4s.IndexAndType
import com.sksamuel.elastic4s.RefreshPolicy
import com.sksamuel.elastic4s.http.ElasticClient
import com.sksamuel.elastic4s.http.ElasticDsl._
import com.sksamuel.elastic4s.http.ElasticProperties
import com.sksamuel.elastic4s.http.RequestFailure
import com.sksamuel.elastic4s.http.RequestSuccess
import com.sksamuel.elastic4s.http.Response
import com.sksamuel.elastic4s.http.index.CreateIndexResponse
import com.sksamuel.elastic4s.http.index.IndexResponse
import com.sksamuel.elastic4s.http.index.admin.DeleteIndexResponse
import com.sksamuel.elastic4s.http.search.SearchResponse
import com.sksamuel.elastic4s.indexes.CreateIndexRequest
import com.sksamuel.elastic4s.indexes.DeleteIndexRequest
import com.sksamuel.elastic4s.indexes.IndexRequest
import com.sksamuel.elastic4s.mappings.MappingDefinition
import com.sksamuel.elastic4s.mappings.dynamictemplate.DynamicMapping
import com.sksamuel.elastic4s.searches.SearchRequest
import io.circe.generic.AutoDerivation
import org.scalatest.BeforeAndAfterAll
import org.scalatest.funsuite.AnyFunSuite

class ExploreElastic68 extends AnyFunSuite with BeforeAndAfterAll {

type EsIndex = String
type EsType = String
val props: ElasticProperties = ElasticProperties("http://localhost:9200")
val client: ElasticClient = ElasticClient(props)
val myIndex: EsIndex = "artists"
val myType: EsType = "myType"
val myIndexAndType: IndexAndType = myIndex / myType

override protected def afterAll(): Unit = client.close()

test("delete index") {
val rqDeleteIdx: DeleteIndexRequest = deleteIndex(myIndex)
val x: Response[DeleteIndexResponse] = client.execute(rqDeleteIdx).await
pprint.pprintln(x)
}

test("create index with mapping - strict") {

val mapDef: MappingDefinition = mapping(myType)
.fields(
textField("name")
)
.dynamic(DynamicMapping.Strict)

val rqCreateIdx: CreateIndexRequest = createIndex(myIndex).mappings(mapDef)
val x: Response[CreateIndexResponse] = client.execute(rqCreateIdx).await
pprint.pprintln(x)
}

test("insert into index - by fields - good") {
val rqInsertIntoIndex: IndexRequest = indexInto(myIndexAndType)
.fields("name" -> "Ben")
.refresh(RefreshPolicy.Immediate)

val x: Response[IndexResponse] = client.execute(rqInsertIntoIndex).await
pprint.pprintln(x)
}

test("insert into index - by JSON - good") {

case class MyData(name: String)
object MyData extends AutoDerivation

val data = MyData("Doe33")

import es.IndexableDerivation.indexableWithCirce

val rqInsertIntoIndex: IndexRequest = indexInto(myIndexAndType)
.doc(data)

val x: Response[IndexResponse] = client.execute(rqInsertIntoIndex).await
pprint.pprintln(x)
}

test("insert into index - by fields - extra field - FAILED due to Strictness") {
val rqInsertIntoIndex: IndexRequest =
indexInto(myIndexAndType)
.fields("name" -> "Beam", "t" -> 1)
.refresh(RefreshPolicy.Immediate)

val x: Response[IndexResponse] = client.execute(rqInsertIntoIndex).await
pprint.pprintln(x)
}

test("query index") {
val rqSearch: SearchRequest = searchWithType(myIndexAndType).query("Doe33")
val resp: Response[SearchResponse] = client.execute(rqSearch).await

resp match {
case failure: RequestFailure => pprint.pprintln("We failed " -> failure.error)
case results: RequestSuccess[SearchResponse] => pprint.pprintln(results.result.hits.hits.toList)
case results => pprint.pprintln(results.result)
}

resp.foreach((x: SearchResponse) => println("There were" -> x.totalHits))
}

}
40 changes: 40 additions & 0 deletions es68/src/main/scala/es/IndexableDerivation.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package es

import com.sksamuel.elastic4s.{AggReader, Hit, HitReader, Indexable}
import io.circe.{Decoder, Encoder, Json, Printer}
import io.circe.jawn.decode

import scala.annotation.implicitNotFound
import scala.util.{Failure, Success, Try}

/**
* this is just copy-paste of
* "elastic4s-circe" to fix dependency on "circe-core":"0.12.0-M3"
*/
object IndexableDerivation {

@implicitNotFound(
"No Decoder for type ${T} found. Use 'import io.circe.generic.auto._' or provide an implicit Decoder instance "
)
implicit def hitReaderWithCirce[T](implicit decoder: Decoder[T]): HitReader[T] = new HitReader[T] {
override def read(hit: Hit): Try[T] = decode[T](hit.sourceAsString).fold(Failure(_), Success(_))
}

@implicitNotFound(
"No Encoder for type ${T} found. Use 'import io.circe.generic.auto._' or provide an implicit Encoder instance "
)
implicit def indexableWithCirce[T](implicit encoder: Encoder[T],
printer: Json => String = Printer.noSpaces.print): Indexable[T] =
new Indexable[T] {
override def json(t: T): String = printer(encoder(t))
}

@implicitNotFound(
"No Decoder for type ${T} found. Use 'import io.circe.generic.auto._' or provide an implicit Decoder instance "
)
implicit def aggReaderWithCirce[T](implicit encoder: Decoder[T]): AggReader[T] = new AggReader[T] {
override def read(json: String): Try[T] = decode[T](json).fold(Failure(_), Success(_))
}
}


67 changes: 67 additions & 0 deletions es89/src/main/scala/es/ExploreElastic89.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package es

import cats._
import cats.implicits._
import com.sksamuel.elastic4s.{ElasticClient, ElasticProperties, RequestFailure, RequestSuccess, Response}
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.http.JavaClient
import com.sksamuel.elastic4s.requests.common.RefreshPolicy
import com.sksamuel.elastic4s.requests.indexes.admin.DeleteIndexResponse
import com.sksamuel.elastic4s.requests.indexes.{CreateIndexRequest, CreateIndexResponse, DeleteIndexRequest, IndexRequest, IndexResponse}
import com.sksamuel.elastic4s.requests.mappings.MappingDefinition
import com.sksamuel.elastic4s.requests.mappings.dynamictemplate.DynamicMapping
import com.sksamuel.elastic4s.requests.searches.SearchRequest
import com.sksamuel.elastic4s.requests.searches.SearchResponse
import org.scalatest.BeforeAndAfterAll
import org.scalatest.funsuite.AnyFunSuite

import scala.concurrent.ExecutionContext.Implicits.global

class ExploreElastic89 extends AnyFunSuite with BeforeAndAfterAll {

val props: ElasticProperties = ElasticProperties("http://localhost:9200")
val client: ElasticClient = ElasticClient(JavaClient(props))

override protected def afterAll(): Unit = client.close()

test("delete index") {
val rqDeleteIdx: DeleteIndexRequest = deleteIndex("artists")
val x: Response[DeleteIndexResponse] = client.execute(rqDeleteIdx).await
pprint.pprintln(x)
}

test("create index with mapping - strict") {
val mapDef: MappingDefinition = properties(
textField("name")
).dynamic(DynamicMapping.Strict)
val rqCreateIdx: CreateIndexRequest = createIndex("artists").mapping(mapDef)
val x: Response[CreateIndexResponse] = client.execute(rqCreateIdx).await
pprint.pprintln(x)
}

test("insert into index - good") {
val rqInsertIntoIndex: IndexRequest = indexInto("artists").fields("name" -> "Jim").refresh(RefreshPolicy.Immediate)
val x: Response[IndexResponse] = client.execute(rqInsertIntoIndex).await
pprint.pprintln(x)
}

test("insert into index - extra field") {
val rqInsertIntoIndex: IndexRequest = indexInto("artists").fields("name" -> "Beam", "t" -> 1).refresh(RefreshPolicy.Immediate)
val x: Response[IndexResponse] = client.execute(rqInsertIntoIndex).await
pprint.pprintln(x)
}

test("query index") {
val rqSearch: SearchRequest = search("artists").query("jim")
val resp: Response[SearchResponse] = client.execute(rqSearch).await

resp match {
case failure: RequestFailure => pprint.pprintln("We failed " -> failure.error)
case results: RequestSuccess[SearchResponse] => pprint.pprintln(results.result.hits.hits.toList)
case results => pprint.pprintln(results.result)
}

resp.foreach((x: SearchResponse) => println("There were" -> x.totalHits))
}

}

0 comments on commit 35d4f43

Please sign in to comment.