From f04af379316039a055041e6872be67a95542770e Mon Sep 17 00:00:00 2001 From: vanjaftn Date: Mon, 13 Nov 2023 10:48:35 +0100 Subject: [PATCH] Fix code remarks --- .../queries/elastic_query_disjunction_max.md | 6 ++-- .../zio/elasticsearch/HttpExecutorSpec.scala | 6 ++-- .../zio/elasticsearch/ElasticQuery.scala | 22 ++++++++++--- .../zio/elasticsearch/query/Queries.scala | 32 +------------------ 4 files changed, 24 insertions(+), 42 deletions(-) diff --git a/docs/overview/queries/elastic_query_disjunction_max.md b/docs/overview/queries/elastic_query_disjunction_max.md index 144f8e620..17fe13f9c 100644 --- a/docs/overview/queries/elastic_query_disjunction_max.md +++ b/docs/overview/queries/elastic_query_disjunction_max.md @@ -3,7 +3,7 @@ id: elastic_query_disjunction_max title: "Disjunction max Query" --- -The `Disjunction max` query returns documents that match one or more query clauses. For documents that match multiple query clauses, the relevance score is set to the highest relevance score from all matching query clauses. When the relevance scores of the returned documents are identical, tie breaker parameter gives more weight to documents that match multiple query clauses. +The `Disjunction max` query returns documents that match one or more query clauses. For documents that match multiple query clauses, the relevance score is set to the highest relevance score from all matching query clauses. When the relevance scores of the returned documents are identical, tie breaker parameter can be used for giving more weight to documents that match multiple query clauses. In order to use the `Disjunction max` query import the following: ```scala @@ -13,12 +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(queries = Chunk(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( queries = Chunk( exists("existsField"), ids("1", "2", "3"))).tieBreaker(0.5f)) +val queryWithPrefixLength: DisjunctionMaxQuery = disjunctionMax(queries = Chunk(exists("existsField"), ids("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 8b8c9756b..adc323840 100644 --- a/modules/integration/src/test/scala/zio/elasticsearch/HttpExecutorSpec.scala +++ b/modules/integration/src/test/scala/zio/elasticsearch/HttpExecutorSpec.scala @@ -1080,16 +1080,16 @@ object HttpExecutorSpec extends IntegrationSpec { Executor.execute(ElasticRequest.createIndex(firstSearchIndex)), Executor.execute(ElasticRequest.deleteIndex(firstSearchIndex)).orDie ), - test("search for a document using a disjunction max query ttt") { + test("search for a document using a disjunction max query") { checkOnce(genDocumentId, genTestDocument, genDocumentId, genTestDocument) { (firstDocumentId, firstDocument, secondDocumentId, secondDocument) => for { _ <- Executor.execute(ElasticRequest.deleteByQuery(firstSearchIndex, matchAll)) firstDocumentUpdated = - firstDocument.copy(stringField = s"this is a ${firstDocument.stringField} test.") + firstDocument.copy(stringField = s"This is a ${firstDocument.stringField} test.") secondDocumentUpdated = secondDocument.copy(stringField = - s"this is not a ${firstDocument.stringField} test. It is a ${secondDocument.stringField} test, but not ${firstDocument.stringField}" + s"This is a ${secondDocument.stringField} test. It should be in the list before ${firstDocument.stringField}, because it has higher relevance score than ${firstDocument.stringField}" ) _ <- Executor.execute( ElasticRequest diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala index b56309355..320208c44 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala @@ -88,18 +88,30 @@ object ElasticQuery { Wildcard(field = field, value = s"*$value*", boost = None, caseInsensitive = None) /** - * Constructs an instance of [[zio.elasticsearch.query.DisjunctionMax]] using the specified parameters. + * Constructs a type-safe instance of [[zio.elasticsearch.query.DisjunctionMax]] using the specified parameters. * * @param queries - * the type-safe [[ElasticQuery]] object to be wrapped inside of disjunction max query + * the type-safe [[ElasticQuery]] objects to be wrapped inside of disjunction max query * @tparam S - * document for which field query is executed + * 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) + + /** + * Constructs an instance of [[zio.elasticsearch.query.DisjunctionMax]] using the specified parameters. + * + * @param queries + * the [[ElasticQuery]] objects 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[S](queries: Chunk[ElasticQuery[S]]): DisjunctionMaxQuery[S] = - DisjunctionMax(queries = queries, tieBreaker = None) + final def disjunctionMax(queries: Chunk[ElasticQuery[Any]]): DisjunctionMaxQuery[Any] = + DisjunctionMax[Any](queries = 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 2655f0602..eecce44aa 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/Queries.scala @@ -202,30 +202,6 @@ private[elasticsearch] final case class ConstantScore[S](query: ElasticQuery[S], sealed trait DisjunctionMaxQuery[S] extends ElasticQuery[S] { - /** - * Adds specified queries to the [[zio.elasticsearch.query.DisjunctionMaxQuery]]. Returned documents must match one or - * more of these queries. - * - * @param allQueries - * the queries to be added - * @tparam S1 - * the type of the sub-queries, for which an implicit [[zio.schema.Schema]] is required - * @return - * an instance of the [[zio.elasticsearch.query.DisjunctionMaxQuery]] with queries added. - */ - def queries[S1 <: S: Schema](allQueries: ElasticQuery[S1]*): DisjunctionMaxQuery[S1] - - /** - * Adds specified queries to the [[zio.elasticsearch.query.DisjunctionMaxQuery]]. Returned documents must match one or - * more of these queries. - * - * @param allQueries - * the queries to be added - * @return - * an instance of the [[zio.elasticsearch.query.DisjunctionMaxQuery]] with queries added. - */ - def queries(allQueries: ElasticQuery[Any]*): DisjunctionMaxQuery[S] - /** * Sets the `tieBreaker` parameter for the [[zio.elasticsearch.query.DisjunctionMaxQuery]]. The `tieBreaker` value is * a floating-point factor between 0 and 1.0 that is used to give more weight to documents that match multiple query @@ -234,7 +210,7 @@ sealed trait DisjunctionMaxQuery[S] extends ElasticQuery[S] { * @param value * a number to set `tieBreaker` parameter to * @return - * a new instance of the [[zio.elasticsearch.query.DisjunctionMaxQuery]] with the `tieBreaker` value set. + * an instance of the [[zio.elasticsearch.query.DisjunctionMaxQuery]] enriched with the `tieBreaker` parameter. */ def tieBreaker(value: Float): DisjunctionMaxQuery[S] } @@ -247,12 +223,6 @@ private[elasticsearch] final case class DisjunctionMax[S]( def tieBreaker(value: Float): DisjunctionMaxQuery[S] = self.copy(tieBreaker = Some(value)) - def queries[S1 <: S: Schema](allQueries: ElasticQuery[S1]*): DisjunctionMaxQuery[S1] = - self.copy(queries = queries ++ allQueries) - - def queries(allQueries: ElasticQuery[Any]*): DisjunctionMaxQuery[S] = - self.copy(queries = queries ++ allQueries) - private[elasticsearch] def toJson(fieldPath: Option[String]): Json = { val disMaxFields = Chunk(