-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
257 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(_)) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
} |