Skip to content

Commit

Permalink
Fix code remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
vanjaftn committed Nov 15, 2023
1 parent dbf61fa commit 51eefde
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 29 deletions.
5 changes: 2 additions & 3 deletions docs/overview/queries/elastic_query_disjunction_max.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import zio.elasticsearch.ElasticQuery.disjunctionMax

You can create a `Disjunction max` query using the `disjunctionMax` method this way:
```scala
val query: DisjunctionMaxQuery = disjunctionMax(queries = Chunk(term(field = "stringField", value = "test"), exists( field = "intField")))
val query: DisjunctionMaxQuery = disjunctionMax(term(field = "stringField", value = "test"), exists(field = "intField"))
```

If you want to change the `tieBreaker`, you can use `tieBreaker` method:
```scala
val queryWithPrefixLength: DisjunctionMaxQuery = disjunctionMax(queries = Chunk(exists("existsField"), ids("1", "2", "3"))).tieBreaker(0.5f)
val queryWithTieBreaker: DisjunctionMaxQuery = disjunctionMax(exists(field = "existsField"), ids(values = "1", "2", "3")).tieBreaker(0.5f)
```

You can find more information about `Disjunction max` query [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html).

Original file line number Diff line number Diff line change
Expand Up @@ -1141,15 +1141,13 @@ object HttpExecutorSpec extends IntegrationSpec {
.refreshTrue
)
query = disjunctionMax(
Chunk(
term(
field = TestDocument.stringField,
value = firstDocument.stringField.toLowerCase
),
matchPhrase(
field = TestDocument.stringField,
value = secondDocument.stringField
)
term(
field = TestDocument.stringField,
value = firstDocument.stringField.toLowerCase
),
matchPhrase(
field = TestDocument.stringField,
value = secondDocument.stringField
)
)
res <- Executor.execute(ElasticRequest.search(firstSearchIndex, query)).documentAs[TestDocument]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,32 @@ object ElasticQuery {
/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.DisjunctionMax]] using the specified parameters.
*
* @param query
* a mandatory query to be wrapped inside of disjunction max query
* @param queries
* the type-safe [[ElasticQuery]] objects to be wrapped inside of disjunction max query
* the rest of the queries to be wrapped inside of disjunction max query
* @tparam S
* document for which field query is executed. An implicit `Schema` instance must be in scope
* @return
* an instance of [[zio.elasticsearch.query.DisjunctionMax]] that represents the `disjunction max` query to be
* performed.
*/
final def disjunctionMax[S: Schema](queries: Chunk[ElasticQuery[S]]): DisjunctionMaxQuery[S] =
DisjunctionMax[S](queries = queries, tieBreaker = None)
final def disjunctionMax[S: Schema](query: ElasticQuery[S], queries: ElasticQuery[S]*): DisjunctionMaxQuery[S] =
DisjunctionMax[S](query = query, queries = Chunk.fromIterable(queries), tieBreaker = None)

/**
* Constructs an instance of [[zio.elasticsearch.query.DisjunctionMax]] using the specified parameters.
*
* @param query
* a mandatory query to be wrapped inside of disjunction max query
* @param queries
* the [[ElasticQuery]] objects to be wrapped inside of disjunction max query
* the rest of the queries to be wrapped inside of disjunction max query
* @return
* an instance of [[zio.elasticsearch.query.DisjunctionMax]] that represents the `disjunction max` query to be
* performed.
*/
final def disjunctionMax(queries: Chunk[ElasticQuery[Any]]): DisjunctionMaxQuery[Any] =
DisjunctionMax[Any](queries = queries, tieBreaker = None)
final def disjunctionMax(query: ElasticQuery[Any], queries: ElasticQuery[Any]*): DisjunctionMaxQuery[Any] =
DisjunctionMax[Any](query = query, queries = Chunk.fromIterable(queries), tieBreaker = None)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.ExistsQuery]], that checks existence of the field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ sealed trait DisjunctionMaxQuery[S] extends ElasticQuery[S] {
}

private[elasticsearch] final case class DisjunctionMax[S](
query: ElasticQuery[S],
queries: Chunk[ElasticQuery[S]],
tieBreaker: Option[Float]
) extends DisjunctionMaxQuery[S] { self =>
Expand All @@ -224,14 +225,13 @@ private[elasticsearch] final case class DisjunctionMax[S](
self.copy(tieBreaker = Some(value))

private[elasticsearch] def toJson(fieldPath: Option[String]): Json = {
val allQueries = query +: queries
val disMaxFields =
Chunk(
Some("queries" -> Arr(queries.map(_.toJson(fieldPath)))),
Some("queries" -> Arr(allQueries.map(_.toJson(fieldPath)))),
tieBreaker.map("tie_breaker" -> _.toJson)
).collect { case Some(obj) => obj }

Obj("dis_max" -> Obj(disMaxFields))

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,23 +417,33 @@ object ElasticQuerySpec extends ZIOSpecDefault {
)
},
test("disjunctionMax") {
val query = disjunctionMax(Chunk(exists("existsField"), ids("1", "2", "3")))
val queryWithTieBreaker = disjunctionMax(Chunk(exists("existsField"), ids("1", "2", "3"))).tieBreaker(0.5f)
val query = disjunctionMax(exists("existsField"), ids("1", "2", "3"))
val queryTs = disjunctionMax(exists(TestDocument.stringField), ids("1", "2", "3"))
val queryWithTieBreaker = disjunctionMax(exists("existsField"), ids("1", "2", "3")).tieBreaker(0.5f)

assert(query)(
equalTo(
DisjunctionMax[Any](
queries =
Chunk(Exists[Any](field = "existsField", boost = None), Ids[Any](values = Chunk("1", "2", "3"))),
query = Exists[Any](field = "existsField", boost = None),
queries = Chunk(Ids[Any](values = Chunk("1", "2", "3"))),
tieBreaker = None
)
)
) &&
assert(queryTs)(
equalTo(
DisjunctionMax[TestDocument](
query = Exists[Any](field = "stringField", boost = None),
queries = Chunk(Ids[Any](values = Chunk("1", "2", "3"))),
tieBreaker = None
)
)
) &&
assert(queryWithTieBreaker)(
equalTo(
DisjunctionMax[Any](
queries =
Chunk(Exists[Any](field = "existsField", boost = None), Ids[Any](values = Chunk("1", "2", "3"))),
query = Exists[Any](field = "existsField", boost = None),
queries = Chunk(Ids[Any](values = Chunk("1", "2", "3"))),
tieBreaker = Some(0.5f)
)
)
Expand Down Expand Up @@ -2531,9 +2541,10 @@ object ElasticQuerySpec extends ZIOSpecDefault {
assert(queryWithAllParams.toJson(fieldPath = None))(equalTo(expectedWithAllParams.toJson))
},
test("disjunctionMax") {
val query = disjunctionMax(queries = Chunk(exists("existsField"), ids("1", "2", "3")))
val query = disjunctionMax(exists("existsField"), ids("1", "2", "3"))
val queryTs = disjunctionMax(exists(TestDocument.stringField), ids("1", "2", "3"))
val queryWithTieBreaker =
disjunctionMax(queries = Chunk(exists("existsField"), ids("1", "2", "3"))).tieBreaker(0.5f)
disjunctionMax(exists("existsField"), ids("1", "2", "3")).tieBreaker(0.5f)

val expected =
"""
Expand All @@ -2547,6 +2558,18 @@ object ElasticQuerySpec extends ZIOSpecDefault {
|}
|""".stripMargin

val expectedTs =
"""
|{
| "dis_max": {
| "queries": [
| { "exists": { "field": "stringField" } },
| { "ids": { "values": ["1", "2", "3"] } }
| ]
| }
|}
|""".stripMargin

val expectedWithTieBreaker =
"""
|{
Expand All @@ -2561,6 +2584,7 @@ object ElasticQuerySpec extends ZIOSpecDefault {
|""".stripMargin

assert(query.toJson(fieldPath = None))(equalTo(expected.toJson)) &&
assert(queryTs.toJson(fieldPath = None))(equalTo(expectedTs.toJson)) &&
assert(queryWithTieBreaker.toJson(fieldPath = None))(equalTo(expectedWithTieBreaker.toJson))
},
test("exists") {
Expand Down
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module.exports = {
'overview/aggregations/elastic_aggregation_sum',
'overview/aggregations/elastic_aggregation_terms',
'overview/aggregations/elastic_aggregation_value_count',
'overview/aggregations/elastic_aggregation_weighted_avg',
],
},
{
Expand Down

0 comments on commit 51eefde

Please sign in to comment.