Skip to content

Commit e022dbd

Browse files
authored
Add regex operator to Atlas Search (#1587)
Add regex operator to Atlas Search There are several Atlas Search query operators that are not implemented with first class support in the Java driver. This PR adds the regex operator to Atlas Search. JAVA-5726
1 parent 0922a24 commit e022dbd

File tree

7 files changed

+148
-2
lines changed

7 files changed

+148
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.client.model.search;
17+
18+
import com.mongodb.annotations.Beta;
19+
import com.mongodb.annotations.Reason;
20+
import com.mongodb.annotations.Sealed;
21+
22+
/**
23+
* @see SearchOperator#regex(SearchPath, String)
24+
* @see SearchOperator#regex(Iterable, Iterable)
25+
* @since 5.3
26+
*/
27+
28+
@Sealed
29+
@Beta(Reason.CLIENT)
30+
public interface RegexSearchOperator extends SearchOperator {
31+
@Override
32+
RegexSearchOperator score(SearchScore modifier);
33+
}

Diff for: driver-core/src/main/com/mongodb/client/model/search/SearchConstructibleBsonElement.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
final class SearchConstructibleBsonElement extends AbstractConstructibleBsonElement<SearchConstructibleBsonElement> implements
3232
MustCompoundSearchOperator, MustNotCompoundSearchOperator, ShouldCompoundSearchOperator, FilterCompoundSearchOperator,
3333
ExistsSearchOperator, TextSearchOperator, AutocompleteSearchOperator,
34-
NumberNearSearchOperator, DateNearSearchOperator, GeoNearSearchOperator,
34+
NumberNearSearchOperator, DateNearSearchOperator, GeoNearSearchOperator, RegexSearchOperator,
3535
ValueBoostSearchScore, PathBoostSearchScore, ConstantSearchScore, FunctionSearchScore,
3636
GaussSearchScoreExpression, PathSearchScoreExpression,
3737
FacetSearchCollector,

Diff for: driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java

+30
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,36 @@ static PhraseSearchOperator phrase(final Iterable<? extends SearchPath> paths, f
322322
.append("query", queryIterator.hasNext() ? queries : firstQuery));
323323
}
324324

325+
/**
326+
* Returns a {@link SearchOperator} that performs a search using a regular expression.
327+
*
328+
* @param path The field to be searched.
329+
* @param query The string to search for.
330+
* @return The requested {@link SearchOperator}.
331+
* @mongodb.atlas.manual atlas-search/regex/ regex operator
332+
*/
333+
static RegexSearchOperator regex(final SearchPath path, final String query) {
334+
return regex(singleton(notNull("path", path)), singleton(notNull("query", query)));
335+
}
336+
337+
/**
338+
* Returns a {@link SearchOperator} that performs a search using a regular expression.
339+
*
340+
* @param paths The non-empty fields to be searched.
341+
* @param queries The non-empty strings to search for.
342+
* @return The requested {@link SearchOperator}.
343+
* @mongodb.atlas.manual atlas-search/regex/ regex operator
344+
*/
345+
static RegexSearchOperator regex(final Iterable<? extends SearchPath> paths, final Iterable<String> queries) {
346+
Iterator<? extends SearchPath> pathIterator = notNull("paths", paths).iterator();
347+
isTrueArgument("paths must not be empty", pathIterator.hasNext());
348+
Iterator<String> queryIterator = notNull("queries", queries).iterator();
349+
isTrueArgument("queries must not be empty", queryIterator.hasNext());
350+
String firstQuery = queryIterator.next();
351+
return new SearchConstructibleBsonElement("regex", new Document("path", combineToBsonValue(pathIterator, false))
352+
.append("query", queryIterator.hasNext() ? queries : firstQuery));
353+
}
354+
325355
/**
326356
* Creates a {@link SearchOperator} from a {@link Bson} in situations when there is no builder method that better satisfies your needs.
327357
* This method cannot be used to validate the syntax.

Diff for: driver-core/src/test/functional/com/mongodb/client/model/search/AggregatesSearchIntegrationTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import static com.mongodb.client.model.search.SearchOperator.exists;
8383
import static com.mongodb.client.model.search.SearchOperator.near;
8484
import static com.mongodb.client.model.search.SearchOperator.numberRange;
85+
import static com.mongodb.client.model.search.SearchOperator.regex;
8586
import static com.mongodb.client.model.search.SearchOperator.phrase;
8687
import static com.mongodb.client.model.search.SearchOperator.text;
8788
import static com.mongodb.client.model.search.SearchOptions.searchOptions;
@@ -610,7 +611,8 @@ private static Stream<Arguments> searchAndSearchMetaArgs() {
610611
.lte(Instant.ofEpochMilli(1)),
611612
near(0, 1.5, fieldPath("fieldName7"), fieldPath("fieldName8")),
612613
near(Instant.ofEpochMilli(1), Duration.ofMillis(3), fieldPath("fieldName9")),
613-
phrase(fieldPath("fieldName10"), "term6")
614+
phrase(fieldPath("fieldName10"), "term6"),
615+
regex(fieldPath("fieldName11"), "term7")
614616
))
615617
.minimumShouldMatch(1)
616618
.mustNot(singleton(

Diff for: driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java

+52
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,58 @@ void phrase() {
649649
);
650650
}
651651

652+
@Test
653+
void regex() {
654+
assertAll(
655+
() -> assertThrows(IllegalArgumentException.class, () ->
656+
// queries must not be empty
657+
SearchOperator.regex(singleton(fieldPath("fieldName")), emptyList())
658+
),
659+
() -> assertThrows(IllegalArgumentException.class, () ->
660+
// paths must not be empty
661+
SearchOperator.regex(emptyList(), singleton("term"))
662+
),
663+
() -> assertEquals(
664+
new BsonDocument("regex",
665+
new BsonDocument("path", fieldPath("fieldName").toBsonValue())
666+
.append("query", new BsonString("term"))
667+
),
668+
SearchOperator.regex(
669+
fieldPath("fieldName"),
670+
"term")
671+
.toBsonDocument()
672+
),
673+
() -> assertEquals(
674+
new BsonDocument("regex",
675+
new BsonDocument("path", fieldPath("fieldName").toBsonValue())
676+
.append("query", new BsonString("term"))
677+
),
678+
SearchOperator.regex(
679+
singleton(fieldPath("fieldName")),
680+
singleton("term"))
681+
.toBsonDocument()
682+
),
683+
() -> assertEquals(
684+
new BsonDocument("regex",
685+
new BsonDocument("path", new BsonArray(asList(
686+
fieldPath("fieldName").toBsonValue(),
687+
wildcardPath("wildc*rd").toBsonValue())))
688+
.append("query", new BsonArray(asList(
689+
new BsonString("term1"),
690+
new BsonString("term2"))))
691+
),
692+
SearchOperator.regex(
693+
asList(
694+
fieldPath("fieldName"),
695+
wildcardPath("wildc*rd")),
696+
asList(
697+
"term1",
698+
"term2"))
699+
.toBsonDocument()
700+
)
701+
);
702+
}
703+
652704
private static SearchOperator docExamplePredefined() {
653705
return SearchOperator.exists(
654706
fieldPath("fieldName"));

Diff for: driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala

+21
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,27 @@ object SearchOperator {
249249
def phrase(paths: Iterable[_ <: SearchPath], queries: Iterable[String]): PhraseSearchOperator =
250250
JSearchOperator.phrase(paths.asJava, queries.asJava)
251251

252+
/**
253+
* Returns a `SearchOperator` that performs a search using a regular expression.
254+
*
255+
* @param path The field to be searched.
256+
* @param query The string to search for.
257+
* @return The requested `SearchOperator`.
258+
* @see [[https://www.mongodb.com/docs/atlas/atlas-search/regex/ regex operator]]
259+
*/
260+
def regex(path: SearchPath, query: String): RegexSearchOperator = JSearchOperator.regex(path, query)
261+
262+
/**
263+
* Returns a `SearchOperator` that performs a search using a regular expression.
264+
*
265+
* @param paths The non-empty fields to be searched.
266+
* @param queries The non-empty strings to search for.
267+
* @return The requested `SearchOperator`.
268+
* @see [[https://www.mongodb.com/docs/atlas/atlas-search/regex/ regex operator]]
269+
*/
270+
def regex(paths: Iterable[_ <: SearchPath], queries: Iterable[String]): RegexSearchOperator =
271+
JSearchOperator.regex(paths.asJava, queries.asJava)
272+
252273
/**
253274
* Creates a `SearchOperator` from a `Bson` in situations when there is no builder method that better satisfies your needs.
254275
* This method cannot be used to validate the syntax.

Diff for: driver-scala/src/main/scala/org/mongodb/scala/model/search/package.scala

+8
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ package object search {
135135
@Beta(Array(Reason.CLIENT))
136136
type AutocompleteSearchOperator = com.mongodb.client.model.search.AutocompleteSearchOperator
137137

138+
/**
139+
* @see `SearchOperator.regex(String, SearchPath)`
140+
* @see `SearchOperator.regex(Iterable, Iterable)`
141+
*/
142+
@Sealed
143+
@Beta(Array(Reason.CLIENT))
144+
type RegexSearchOperator = com.mongodb.client.model.search.RegexSearchOperator
145+
138146
/**
139147
* A base for a [[NumberRangeSearchOperatorBase]] which allows creating instances of this operator.
140148
* This interface is a technicality and does not represent a meaningful element of the full-text search query syntax.

0 commit comments

Comments
 (0)