Skip to content

Commit f044d78

Browse files
committed
SCBC-428: FTS BooleanQuery fails unless all 3 of "must", "should", and "mustNot" are specified
Allow the user to not specify must, should, mustNot. Change-Id: I20ebf725066d1dda5586ade58627cd2a70a21599 Reviewed-on: https://review.couchbase.org/c/couchbase-jvm-clients/+/210965 Tested-by: Build Bot <[email protected]> Reviewed-by: David Nault <[email protected]>
1 parent 9eca7d1 commit f044d78

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

scala-client/src/main/scala/com/couchbase/client/scala/search/queries/BooleanQuery.scala

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import com.couchbase.client.core.api.search.queries.CoreBooleanQuery
2222
*
2323
* @since 1.0.0
2424
*/
25-
case class BooleanQuery(
26-
private[scala] val must: ConjunctionQuery = ConjunctionQuery(),
27-
private[scala] val should: DisjunctionQuery = DisjunctionQuery(),
28-
private[scala] val mustNot: DisjunctionQuery = DisjunctionQuery(),
25+
case class BooleanQuery private (
26+
private[scala] val must: Option[ConjunctionQuery] = None,
27+
private[scala] val should: Option[DisjunctionQuery] = None,
28+
private[scala] val mustNot: Option[DisjunctionQuery] = None,
2929
private[scala] val field: Option[String] = None,
3030
private[scala] val boost: Option[Double] = None
3131
) extends SearchQuery {
@@ -36,31 +36,43 @@ case class BooleanQuery(
3636
* @return a copy of this, for chaining
3737
*/
3838
def shouldMin(minForShould: Int): BooleanQuery = {
39-
copy(should = should.min(minForShould))
39+
copy(should = Some((this.should match {
40+
case Some(existing) => existing
41+
case None => DisjunctionQuery()
42+
}).min(minForShould)))
4043
}
4144

4245
/** Results must satisfy all of these queries.
4346
*
4447
* @return a copy of this, for chaining
4548
*/
4649
def must(mustQueries: SearchQuery*): BooleanQuery = {
47-
copy(must = must.and(mustQueries: _*))
50+
copy(must = Some((must match {
51+
case Some(existing) => existing
52+
case _ => ConjunctionQuery()
53+
}).and(mustQueries: _*)))
4854
}
4955

5056
/** Results must not satisfy any of these queries.
5157
*
5258
* @return a copy of this, for chaining
5359
*/
5460
def mustNot(mustNotQueries: SearchQuery*): BooleanQuery = {
55-
copy(mustNot = mustNot.or(mustNotQueries: _*))
61+
copy(mustNot = Some((mustNot match {
62+
case Some(existing) => existing
63+
case _ => DisjunctionQuery()
64+
}).or(mustNotQueries: _*)))
5665
}
5766

5867
/** Results should satisfy all of these queries.
5968
*
6069
* @return a copy of this, for chaining
6170
*/
6271
def should(shouldQueries: SearchQuery*): BooleanQuery = {
63-
copy(should = should.or(shouldQueries: _*))
72+
copy(should = Some((should match {
73+
case Some(existing) => existing
74+
case _ => DisjunctionQuery()
75+
}).or(shouldQueries: _*)))
6476
}
6577

6678
/** The boost parameter is used to increase the relative weight of a clause (with a boost greater than 1) or decrease
@@ -76,9 +88,9 @@ case class BooleanQuery(
7688

7789
override private[scala] def toCore =
7890
new CoreBooleanQuery(
79-
must.toCore,
80-
mustNot.toCore,
81-
should.toCore,
91+
must.map(_.toCore).orNull,
92+
mustNot.map(_.toCore).orNull,
93+
should.map(_.toCore).orNull,
8294
boost.map(_.asInstanceOf[java.lang.Double]).orNull
8395
)
8496
}

0 commit comments

Comments
 (0)