-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into update/elasticsearch-rest-client-8.11.0
- Loading branch information
Showing
21 changed files
with
276 additions
and
48 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
10 changes: 10 additions & 0 deletions
10
elastic4s-core/src/main/scala/com/sksamuel/elastic4s/api/PitApi.scala
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,10 @@ | ||
package com.sksamuel.elastic4s.api | ||
|
||
import com.sksamuel.elastic4s.Index | ||
import com.sksamuel.elastic4s.requests.pit.{CreatePitRequest, DeletePitRequest} | ||
|
||
trait PitApi { | ||
def createPointInTime(index: Index): CreatePitRequest = CreatePitRequest(index) | ||
|
||
def deletePointInTime(id: String) : DeletePitRequest = DeletePitRequest(id) | ||
} |
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
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
9 changes: 9 additions & 0 deletions
9
elastic4s-domain/src/main/scala/com/sksamuel/elastic4s/requests/pit/CreatePitRequest.scala
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,9 @@ | ||
package com.sksamuel.elastic4s.requests.pit | ||
|
||
import com.sksamuel.elastic4s.Index | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
|
||
case class CreatePitRequest(index: Index, keepAlive: Option[FiniteDuration] = None) { | ||
def keepAlive(keepAlive: FiniteDuration): CreatePitRequest = copy(keepAlive = Some(keepAlive)) | ||
} |
3 changes: 3 additions & 0 deletions
3
elastic4s-domain/src/main/scala/com/sksamuel/elastic4s/requests/pit/CreatePitResponse.scala
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,3 @@ | ||
package com.sksamuel.elastic4s.requests.pit | ||
|
||
case class CreatePitResponse (id: String) |
3 changes: 3 additions & 0 deletions
3
elastic4s-domain/src/main/scala/com/sksamuel/elastic4s/requests/pit/DeletePitRequest.scala
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,3 @@ | ||
package com.sksamuel.elastic4s.requests.pit | ||
|
||
case class DeletePitRequest(id: String) |
3 changes: 3 additions & 0 deletions
3
elastic4s-domain/src/main/scala/com/sksamuel/elastic4s/requests/pit/DeletePitResponse.scala
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,3 @@ | ||
package com.sksamuel.elastic4s.requests.pit | ||
|
||
case class DeletePitResponse(succeeded: Boolean, num_freed: Int) |
7 changes: 7 additions & 0 deletions
7
elastic4s-domain/src/main/scala/com/sksamuel/elastic4s/requests/searches/Pit.scala
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,7 @@ | ||
package com.sksamuel.elastic4s.requests.searches | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
|
||
case class Pit(id: String, keepAlive: Option[FiniteDuration] = None) { | ||
def keepAlive(keepAlive: FiniteDuration): Pit = copy(keepAlive = Some(keepAlive)) | ||
} |
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
9 changes: 8 additions & 1 deletion
9
...in/src/main/scala/com/sksamuel/elastic4s/requests/searches/queries/ScriptScoreQuery.scala
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 |
---|---|---|
@@ -1,3 +1,10 @@ | ||
package com.sksamuel.elastic4s.requests.searches.queries | ||
|
||
case class ScriptScoreQuery(script: String) extends Query | ||
import com.sksamuel.elastic4s.requests.script.Script | ||
|
||
case class ScriptScoreQuery(query: Option[Query] = None, script: Option[Script] = None, minScore: Option[Double] = None, boost: Option[Double] = None) extends Query { | ||
def boost(boost: Double): ScriptScoreQuery = copy(boost = Option(boost)) | ||
def minScore(min: Double): ScriptScoreQuery = copy(minScore = Option(min)) | ||
def query(query: Query): ScriptScoreQuery = copy(query = Some(query)) | ||
def script(script: Script): ScriptScoreQuery = copy(script = Some(script)) | ||
} |
9 changes: 9 additions & 0 deletions
9
...ic4s-handlers/src/main/scala/com/sksamuel/elastic4s/handlers/pit/DeletePitBuilderFn.scala
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,9 @@ | ||
package com.sksamuel.elastic4s.handlers.pit | ||
|
||
import com.sksamuel.elastic4s.json.{XContentBuilder, XContentFactory} | ||
import com.sksamuel.elastic4s.requests.pit.DeletePitRequest | ||
|
||
object DeletePitBuilderFn { | ||
def apply(request: DeletePitRequest): XContentBuilder = | ||
XContentFactory.jsonBuilder().field("id" , request.id) | ||
} |
32 changes: 32 additions & 0 deletions
32
elastic4s-handlers/src/main/scala/com/sksamuel/elastic4s/handlers/pit/PitHandlers.scala
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,32 @@ | ||
package com.sksamuel.elastic4s.handlers.pit | ||
|
||
import com.sksamuel.elastic4s.{ElasticRequest, Handler, HttpEntity} | ||
import com.sksamuel.elastic4s.requests.pit.{CreatePitRequest, CreatePitResponse, DeletePitRequest, DeletePitResponse} | ||
|
||
trait PitHandlers { | ||
implicit object CreatePitHandler extends Handler[CreatePitRequest, CreatePitResponse] { | ||
|
||
override def build(request: CreatePitRequest): ElasticRequest = | ||
ElasticRequest( | ||
method = "POST", | ||
endpoint = s"/${request.index.name}/_pit", | ||
params = request.keepAlive | ||
.map(keepAlive => Map("keep_alive" -> s"${keepAlive.toSeconds}s")) | ||
.getOrElse(Map.empty[String, String]) | ||
) | ||
} | ||
|
||
implicit object DeletePitHandler extends Handler[DeletePitRequest, DeletePitResponse] { | ||
|
||
override def build(request: DeletePitRequest): ElasticRequest = { | ||
val entity = HttpEntity(DeletePitBuilderFn(request).string, "application/json") | ||
ElasticRequest( | ||
method = "DELETE", | ||
endpoint = s"/_pit", | ||
entity | ||
) | ||
} | ||
} | ||
|
||
|
||
} |
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
12 changes: 9 additions & 3 deletions
12
.../main/scala/com/sksamuel/elastic4s/handlers/searches/queries/ScriptScoreQueryBodyFn.scala
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
46 changes: 46 additions & 0 deletions
46
elastic4s-tests/src/test/scala/com/sksamuel/elastic4s/requests/pit/PitTests.scala
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,46 @@ | ||
package com.sksamuel.elastic4s.requests.pit | ||
|
||
import com.sksamuel.elastic4s.testkit.DockerTests | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import scala.concurrent.duration.DurationInt | ||
import scala.util.Try | ||
|
||
class PitTests extends AnyFlatSpec with Matchers with DockerTests { | ||
|
||
Try { | ||
client.execute { | ||
deleteIndex("pit") | ||
}.await | ||
} | ||
|
||
client.execute { | ||
createIndex("pit").mapping { | ||
mapping( | ||
textField("name").stored(true), | ||
textField("brand").stored(true), | ||
textField("ingredients").stored(true) | ||
) | ||
} | ||
}.await | ||
|
||
"A create pit request" should "create a pit" in { | ||
val resp = client | ||
.execute(createPointInTime("pit").keepAlive(30.seconds)) | ||
.await.result | ||
|
||
resp.id.length should be > 0 | ||
} | ||
|
||
"A delete pit request" should "delete a pit" in { | ||
val pit = client | ||
.execute(createPointInTime("pit").keepAlive(30.seconds)) | ||
.await.result | ||
|
||
val result = client.execute(deletePointInTime(pit.id)).await.result | ||
|
||
result.succeeded should be(true) | ||
result.num_freed should be > 0 | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...c4s-tests/src/test/scala/com/sksamuel/elastic4s/search/queries/ScriptScoreQueryTest.scala
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,69 @@ | ||
package com.sksamuel.elastic4s.search.queries | ||
|
||
import com.sksamuel.elastic4s.requests.script.Script | ||
import com.sksamuel.elastic4s.testkit.DockerTests | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import scala.util.Try | ||
|
||
class ScriptScoreQueryTest extends AnyWordSpec with DockerTests with Matchers { | ||
|
||
Try { | ||
client.execute { | ||
deleteIndex("person") | ||
}.await | ||
} | ||
|
||
client.execute( | ||
bulk( | ||
indexInto("person") fields( | ||
"name" -> "reese", | ||
"age" -> 1.0 | ||
), | ||
indexInto("person") fields( | ||
"name" -> "finch", | ||
"age" -> 1.0 | ||
), | ||
indexInto("person") fields( | ||
"name" -> "finch", | ||
"age" -> 2.0 | ||
), | ||
indexInto("person") fields( | ||
"name" -> "finch", | ||
"age" -> 3.0 | ||
) | ||
).refreshImmediately | ||
).await | ||
|
||
"script score query" should { | ||
"filter by query" in { | ||
client.execute { | ||
search("person") query { | ||
scriptScoreQuery(termQuery("name", "finch")) | ||
.script(Script("doc['age'].value % 3")) | ||
} | ||
}.await.result.totalHits shouldBe 3 | ||
} | ||
"rank by custom score" in { | ||
client.execute { | ||
search("person") query { | ||
scriptScoreQuery(termQuery("name", "finch")) | ||
.script(Script("doc['age'].value % 3")) | ||
} | ||
}.await.result.hits.hits.map(_.sourceAsString) shouldBe Array( | ||
"""{"name":"finch","age":2.0}""", | ||
"""{"name":"finch","age":1.0}""", | ||
"""{"name":"finch","age":3.0}""" | ||
) | ||
} | ||
"return correct custom score" in { | ||
client.execute { | ||
search("person") query { | ||
scriptScoreQuery(termQuery("name", "finch")) | ||
.script(Script("doc['age'].value % 3")) | ||
} | ||
}.await.result.hits.hits.map(_.score) shouldBe Array(2.0, 1.0, 0.0) | ||
} | ||
} | ||
} |
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