Skip to content

Commit 8c49e38

Browse files
authored
Add phrase operator for Atlas Search (#1586)
Add phrase operator for 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 phrase operator to Atlas Search. JAVA-5724
1 parent d203c44 commit 8c49e38

File tree

7 files changed

+234
-1
lines changed

7 files changed

+234
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.internal.client.model.AbstractConstructibleBsonElement;
19+
20+
import org.bson.conversions.Bson;
21+
22+
import static com.mongodb.assertions.Assertions.notNull;
23+
24+
final class PhraseConstructibleBsonElement extends AbstractConstructibleBsonElement<PhraseConstructibleBsonElement> implements
25+
PhraseSearchOperator {
26+
PhraseConstructibleBsonElement(final String name, final Bson value) {
27+
super(name, value);
28+
}
29+
30+
private PhraseConstructibleBsonElement(final Bson baseElement, final Bson appendedElementValue) {
31+
super(baseElement, appendedElementValue);
32+
}
33+
34+
@Override
35+
protected PhraseConstructibleBsonElement newSelf(final Bson baseElement, final Bson appendedElementValue) {
36+
return new PhraseConstructibleBsonElement(baseElement, appendedElementValue);
37+
}
38+
39+
@Override
40+
public PhraseSearchOperator synonyms(final String name) {
41+
return newWithAppendedValue("synonyms", notNull("name", name));
42+
}
43+
44+
@Override
45+
public PhraseSearchOperator slop(final int slop) {
46+
return newWithAppendedValue("slop", slop);
47+
}
48+
49+
@Override
50+
public PhraseConstructibleBsonElement score(final SearchScore modifier) {
51+
return newWithAppendedValue("score", notNull("modifier", modifier));
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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#phrase(SearchPath, String)
24+
* @see SearchOperator#phrase(Iterable, Iterable)
25+
* @since 5.3
26+
*/
27+
28+
@Sealed
29+
@Beta(Reason.CLIENT)
30+
public interface PhraseSearchOperator extends SearchOperator {
31+
@Override
32+
PhraseSearchOperator score(SearchScore modifier);
33+
34+
/**
35+
* Creates a new {@link PhraseSearchOperator} that uses slop. The default value is 0.
36+
*
37+
* @param slop The allowable distance between words in the query phrase.
38+
* @return A new {@link PhraseSearchOperator}.
39+
*/
40+
PhraseSearchOperator slop(int slop);
41+
42+
/**
43+
* Creates a new {@link PhraseSearchOperator} that uses synonyms.
44+
*
45+
* @param name The name of the synonym mapping.
46+
* @return A new {@link PhraseSearchOperator}.
47+
*
48+
* @mongodb.atlas.manual atlas-search/synonyms/ Synonym mappings
49+
*/
50+
PhraseSearchOperator synonyms(String name);
51+
}

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

+30
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,36 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final
292292
.append("pivot", notNull("pivot", pivot)));
293293
}
294294

295+
/**
296+
* Returns a {@link SearchOperator} that performs a search for documents containing an ordered sequence of terms.
297+
*
298+
* @param path The field to be searched.
299+
* @param query The string to search for.
300+
* @return The requested {@link SearchOperator}.
301+
* @mongodb.atlas.manual atlas-search/phrase/ phrase operator
302+
*/
303+
static PhraseSearchOperator phrase(final SearchPath path, final String query) {
304+
return phrase(singleton(notNull("path", path)), singleton(notNull("query", query)));
305+
}
306+
307+
/**
308+
* Returns a {@link SearchOperator} that performs a search for documents containing an ordered sequence of terms.
309+
*
310+
* @param paths The non-empty fields to be searched.
311+
* @param queries The non-empty strings to search for.
312+
* @return The requested {@link SearchOperator}.
313+
* @mongodb.atlas.manual atlas-search/phrase/ phrase operator
314+
*/
315+
static PhraseSearchOperator phrase(final Iterable<? extends SearchPath> paths, final Iterable<String> queries) {
316+
Iterator<? extends SearchPath> pathIterator = notNull("paths", paths).iterator();
317+
isTrueArgument("paths must not be empty", pathIterator.hasNext());
318+
Iterator<String> queryIterator = notNull("queries", queries).iterator();
319+
isTrueArgument("queries must not be empty", queryIterator.hasNext());
320+
String firstQuery = queryIterator.next();
321+
return new PhraseConstructibleBsonElement("phrase", new Document("path", combineToBsonValue(pathIterator, false))
322+
.append("query", queryIterator.hasNext() ? queries : firstQuery));
323+
}
324+
295325
/**
296326
* Creates a {@link SearchOperator} from a {@link Bson} in situations when there is no builder method that better satisfies your needs.
297327
* 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.phrase;
8586
import static com.mongodb.client.model.search.SearchOperator.text;
8687
import static com.mongodb.client.model.search.SearchOptions.searchOptions;
8788
import static com.mongodb.client.model.search.SearchPath.fieldPath;
@@ -608,7 +609,8 @@ private static Stream<Arguments> searchAndSearchMetaArgs() {
608609
dateRange(fieldPath("fieldName6"))
609610
.lte(Instant.ofEpochMilli(1)),
610611
near(0, 1.5, fieldPath("fieldName7"), fieldPath("fieldName8")),
611-
near(Instant.ofEpochMilli(1), Duration.ofMillis(3), fieldPath("fieldName9"))
612+
near(Instant.ofEpochMilli(1), Duration.ofMillis(3), fieldPath("fieldName9")),
613+
phrase(fieldPath("fieldName10"), "term6")
612614
))
613615
.minimumShouldMatch(1)
614616
.mustNot(singleton(

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

+68
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,74 @@ void near() {
581581
);
582582
}
583583

584+
@Test
585+
void phrase() {
586+
assertAll(
587+
() -> assertThrows(IllegalArgumentException.class, () ->
588+
// queries must not be empty
589+
SearchOperator.phrase(singleton(fieldPath("fieldName")), emptyList())
590+
),
591+
() -> assertThrows(IllegalArgumentException.class, () ->
592+
// paths must not be empty
593+
SearchOperator.phrase(emptyList(), singleton("term"))
594+
),
595+
() -> assertEquals(
596+
new BsonDocument("phrase",
597+
new BsonDocument("path", fieldPath("fieldName").toBsonValue())
598+
.append("query", new BsonString("term"))
599+
),
600+
SearchOperator.phrase(
601+
fieldPath("fieldName"),
602+
"term")
603+
.toBsonDocument()
604+
),
605+
() -> assertEquals(
606+
new BsonDocument("phrase",
607+
new BsonDocument("path", new BsonArray(asList(
608+
fieldPath("fieldName").toBsonValue(),
609+
wildcardPath("wildc*rd").toBsonValue())))
610+
.append("query", new BsonArray(asList(
611+
new BsonString("term1"),
612+
new BsonString("term2"))))
613+
),
614+
SearchOperator.phrase(
615+
asList(
616+
fieldPath("fieldName"),
617+
wildcardPath("wildc*rd")),
618+
asList(
619+
"term1",
620+
"term2"))
621+
.toBsonDocument()
622+
),
623+
() -> assertEquals(
624+
new BsonDocument("phrase",
625+
new BsonDocument("path", fieldPath("fieldName").toBsonValue())
626+
.append("query", new BsonString("term"))
627+
.append("synonyms", new BsonString("synonymMappingName"))
628+
),
629+
SearchOperator.phrase(
630+
singleton(fieldPath("fieldName")),
631+
singleton("term"))
632+
.synonyms("synonymMappingName")
633+
.toBsonDocument()
634+
),
635+
() -> assertEquals(
636+
new BsonDocument("phrase",
637+
new BsonDocument("path", fieldPath("fieldName").toBsonValue())
638+
.append("query", new BsonString("term"))
639+
.append("synonyms", new BsonString("synonymMappingName"))
640+
.append("slop", new BsonInt32(5))
641+
),
642+
SearchOperator.phrase(
643+
singleton(fieldPath("fieldName")),
644+
singleton("term"))
645+
.synonyms("synonymMappingName")
646+
.slop(5)
647+
.toBsonDocument()
648+
)
649+
);
650+
}
651+
584652
private static SearchOperator docExamplePredefined() {
585653
return SearchOperator.exists(
586654
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
@@ -228,6 +228,27 @@ object SearchOperator {
228228
def near(origin: Point, pivot: Number, paths: Iterable[_ <: FieldSearchPath]): GeoNearSearchOperator =
229229
JSearchOperator.near(origin, pivot, paths.asJava)
230230

231+
/**
232+
* Returns a `SearchOperator` that performs a search for documents containing an ordered sequence of terms.
233+
*
234+
* @param path The field to be searched.
235+
* @param query The string to search for.
236+
* @return The requested `SearchOperator`.
237+
* @see [[https://www.mongodb.com/docs/atlas/atlas-search/phrase/ phrase operator]]
238+
*/
239+
def phrase(path: SearchPath, query: String): PhraseSearchOperator = JSearchOperator.phrase(path, query)
240+
241+
/**
242+
* Returns a `SearchOperator` that performs a search for documents containing an ordered sequence of terms.
243+
*
244+
* @param paths The non-empty fields to be searched.
245+
* @param queries The non-empty strings to search for.
246+
* @return The requested `SearchOperator`.
247+
* @see [[https://www.mongodb.com/docs/atlas/atlas-search/phrase/ phrase operator]]
248+
*/
249+
def phrase(paths: Iterable[_ <: SearchPath], queries: Iterable[String]): PhraseSearchOperator =
250+
JSearchOperator.phrase(paths.asJava, queries.asJava)
251+
231252
/**
232253
* Creates a `SearchOperator` from a `Bson` in situations when there is no builder method that better satisfies your needs.
233254
* 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
@@ -119,6 +119,14 @@ package object search {
119119
@Beta(Array(Reason.CLIENT))
120120
type TextSearchOperator = com.mongodb.client.model.search.TextSearchOperator
121121

122+
/**
123+
* @see `SearchOperator.phrase(String, SearchPath)`
124+
* @see `SearchOperator.phrase(Iterable, Iterable)`
125+
*/
126+
@Sealed
127+
@Beta(Array(Reason.CLIENT))
128+
type PhraseSearchOperator = com.mongodb.client.model.search.PhraseSearchOperator
129+
122130
/**
123131
* @see `SearchOperator.autocomplete(String, FieldSearchPath)`
124132
* @see `SearchOperator.autocomplete(Iterable, FieldSearchPath)`

0 commit comments

Comments
 (0)