diff --git a/docs/overview/queries/elastic_query_disjunction_max.md b/docs/overview/queries/elastic_query_disjunction_max.md index 17fe13f9c..c74b81748 100644 --- a/docs/overview/queries/elastic_query_disjunction_max.md +++ b/docs/overview/queries/elastic_query_disjunction_max.md @@ -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). - diff --git a/modules/integration/src/test/scala/zio/elasticsearch/HttpExecutorSpec.scala b/modules/integration/src/test/scala/zio/elasticsearch/HttpExecutorSpec.scala index a6bfa0ff5..37a8eb5a5 100644 --- a/modules/integration/src/test/scala/zio/elasticsearch/HttpExecutorSpec.scala +++ b/modules/integration/src/test/scala/zio/elasticsearch/HttpExecutorSpec.scala @@ -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] diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala index d6cbb0635..89e495372 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala @@ -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, diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala b/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala index 6c1ae64fa..9fb0334d0 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala @@ -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 => @@ -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)) - } } diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala index 373198dc7..c04888633 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala @@ -417,14 +417,24 @@ 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 ) ) @@ -432,8 +442,8 @@ object ElasticQuerySpec extends ZIOSpecDefault { 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) ) ) @@ -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 = """ @@ -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 = """ |{ @@ -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") { diff --git a/website/sidebars.js b/website/sidebars.js index 3a89fe705..b9dd44199 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -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', ], }, {