Skip to content

Commit

Permalink
Do misc $search improvements (mongodb#973)
Browse files Browse the repository at this point in the history
* Rename `SearchOptions.defaultSearchOptions` to `searchOptions`
* Stop allowing `null` `SearchOptions` in `Aggregates.search`/`searchMeta`
  • Loading branch information
stIncMale authored Jun 29, 2022
1 parent a14bb7e commit 091b41d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 64 deletions.
37 changes: 17 additions & 20 deletions driver-core/src/main/com/mongodb/client/model/Aggregates.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Objects;

import static com.mongodb.assertions.Assertions.assertTrue;
import static com.mongodb.client.model.search.SearchOptions.searchOptions;
import static com.mongodb.internal.Iterables.concat;
import static java.util.Arrays.asList;
import static org.bson.assertions.Assertions.notNull;
Expand Down Expand Up @@ -671,7 +672,7 @@ public static <TExpression> Bson setWindowFields(@Nullable final TExpression par
* @since 4.7
*/
public static Bson search(final SearchOperator operator) {
return search(operator, null);
return search(operator, searchOptions());
}

/**
Expand All @@ -683,17 +684,16 @@ public static Bson search(final SearchOperator operator) {
*
* @param operator A search operator.
* @param options Optional {@code $search} pipeline stage fields.
* Specifying {@code null} is equivalent to either specifying {@link SearchOptions#defaultSearchOptions()}
* or calling {@link #search(SearchOperator)}.
* Specifying {@link SearchOptions#searchOptions()} is equivalent to calling {@link #search(SearchOperator)}.
* @return The {@code $search} pipeline stage.
*
* @mongodb.atlas.manual atlas-search/query-syntax/#-search $search
* @mongodb.atlas.manual atlas-search/operators-and-collectors/#operators Search operators
* @mongodb.atlas.manual atlas-search/scoring/ Scoring
* @since 4.7
*/
public static Bson search(final SearchOperator operator, @Nullable final SearchOptions options) {
return new SearchStage("$search", notNull("operator", operator), options);
public static Bson search(final SearchOperator operator, final SearchOptions options) {
return new SearchStage("$search", notNull("operator", operator), notNull("options", options));
}

/**
Expand All @@ -710,7 +710,7 @@ public static Bson search(final SearchOperator operator, @Nullable final SearchO
* @since 4.7
*/
public static Bson search(final SearchCollector collector) {
return search(collector, null);
return search(collector, searchOptions());
}

/**
Expand All @@ -720,17 +720,16 @@ public static Bson search(final SearchCollector collector) {
*
* @param collector A search collector.
* @param options Optional {@code $search} pipeline stage fields.
* Specifying {@code null} is equivalent to either specifying {@link SearchOptions#defaultSearchOptions()}
* or calling {@link #search(SearchCollector)}.
* Specifying {@link SearchOptions#searchOptions()} is equivalent to calling {@link #search(SearchCollector)}.
* @return The {@code $search} pipeline stage.
*
* @mongodb.atlas.manual atlas-search/query-syntax/#-search $search
* @mongodb.atlas.manual atlas-search/operators-and-collectors/#collectors Search collectors
* @mongodb.atlas.manual atlas-search/scoring/ Scoring
* @since 4.7
*/
public static Bson search(final SearchCollector collector, @Nullable final SearchOptions options) {
return new SearchStage("$search", notNull("collector", collector), options);
public static Bson search(final SearchCollector collector, final SearchOptions options) {
return new SearchStage("$search", notNull("collector", collector), notNull("options", options));
}

/**
Expand All @@ -747,7 +746,7 @@ public static Bson search(final SearchCollector collector, @Nullable final Searc
* @since 4.7
*/
public static Bson searchMeta(final SearchOperator operator) {
return searchMeta(operator, null);
return searchMeta(operator, searchOptions());
}

/**
Expand All @@ -758,16 +757,15 @@ public static Bson searchMeta(final SearchOperator operator) {
*
* @param operator A search operator.
* @param options Optional {@code $search} pipeline stage fields.
* Specifying {@code null} is equivalent to either specifying {@link SearchOptions#defaultSearchOptions()}
* or calling {@link #searchMeta(SearchOperator)}.
* Specifying {@link SearchOptions#searchOptions()} is equivalent to calling {@link #searchMeta(SearchOperator)}.
* @return The {@code $searchMeta} pipeline stage.
*
* @mongodb.atlas.manual atlas-search/query-syntax/#-searchmeta $searchMeta
* @mongodb.atlas.manual atlas-search/operators-and-collectors/#operators Search operators
* @since 4.7
*/
public static Bson searchMeta(final SearchOperator operator, @Nullable final SearchOptions options) {
return new SearchStage("$searchMeta", notNull("operator", operator), options);
public static Bson searchMeta(final SearchOperator operator, final SearchOptions options) {
return new SearchStage("$searchMeta", notNull("operator", operator), notNull("options", options));
}

/**
Expand All @@ -784,7 +782,7 @@ public static Bson searchMeta(final SearchOperator operator, @Nullable final Sea
* @since 4.7
*/
public static Bson searchMeta(final SearchCollector collector) {
return searchMeta(collector, null);
return searchMeta(collector, searchOptions());
}

/**
Expand All @@ -795,16 +793,15 @@ public static Bson searchMeta(final SearchCollector collector) {
*
* @param collector A search collector.
* @param options Optional {@code $search} pipeline stage fields.
* Specifying {@code null} is equivalent to either specifying {@link SearchOptions#defaultSearchOptions()}
* or calling {@link #searchMeta(SearchCollector)}.
* Specifying {@link SearchOptions#searchOptions()} is equivalent to calling {@link #searchMeta(SearchCollector)}.
* @return The {@code $searchMeta} pipeline stage.
*
* @mongodb.atlas.manual atlas-search/query-syntax/#-searchmeta $searchMeta
* @mongodb.atlas.manual atlas-search/operators-and-collectors/#collectors Search collectors
* @since 4.7
*/
public static Bson searchMeta(final SearchCollector collector, @Nullable final SearchOptions options) {
return new SearchStage("$searchMeta", notNull("collector", collector), options);
public static Bson searchMeta(final SearchCollector collector, final SearchOptions options) {
return new SearchStage("$searchMeta", notNull("collector", collector), notNull("options", options));
}

static void writeBucketOutput(final CodecRegistry codecRegistry, final BsonDocumentWriter writer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public interface SearchOptions extends Bson {
* The following code creates two functionally equivalent {@link SearchOptions} objects,
* though they may not be {@linkplain Object#equals(Object) equal}.
* <pre>{@code
* SearchOptions options1 = SearchOptions.defaultSearchOptions().index("indexName");
* SearchOptions options2 = SearchOptions.defaultSearchOptions().option("index", "indexName");
* SearchOptions options1 = SearchOptions.searchOptions().index("indexName");
* SearchOptions options2 = SearchOptions.searchOptions().option("index", "indexName");
* }</pre>
*
* @param name The option name.
Expand All @@ -90,7 +90,7 @@ public interface SearchOptions extends Bson {
*
* @return {@link SearchOptions} that represents server defaults.
*/
static SearchOptions defaultSearchOptions() {
static SearchOptions searchOptions() {
return SearchConstructibleBson.EMPTY_IMMUTABLE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
import static com.mongodb.client.model.search.SearchOperator.near;
import static com.mongodb.client.model.search.SearchOperator.numberRange;
import static com.mongodb.client.model.search.SearchOperator.text;
import static com.mongodb.client.model.search.SearchOptions.defaultSearchOptions;
import static com.mongodb.client.model.search.SearchOptions.searchOptions;
import static com.mongodb.client.model.search.SearchPath.fieldPath;
import static com.mongodb.client.model.search.SearchPath.wildcardPath;
import static com.mongodb.client.model.search.SearchScore.boost;
Expand Down Expand Up @@ -294,7 +294,7 @@ private static Stream<Arguments> args() {
stageCreator(
// `multi` is used here only to verify that it is tolerated
exists(fieldPath("title").multi("keyword")),
defaultSearchOptions()
searchOptions()
.option("index", "default")
.count(lowerBound().threshold(2_000))
),
Expand All @@ -314,7 +314,7 @@ private static Stream<Arguments> args() {
"`highlight` option",
stageCreator(
text(singleton(fieldPath("plot")), asList("factory", "century")),
defaultSearchOptions()
searchOptions()
.highlight(paths(
fieldPath("title").multi("keyword"),
wildcardPath("pl*t"))
Expand All @@ -336,7 +336,7 @@ private static Stream<Arguments> args() {
"`returnStoredSource` option",
stageCreator(
exists(fieldPath("plot")),
defaultSearchOptions()
searchOptions()
.returnStoredSource(true)
),
MFLIX_MOVIES_NS,
Expand All @@ -360,7 +360,7 @@ private static Stream<Arguments> args() {
"alternate analyzer (`multi` field path)",
stageCreator(
text(singleton(fieldPath("title").multi("keyword")), singleton("The Cheat")),
defaultSearchOptions().count(total())
searchOptions().count(total())
),
MFLIX_MOVIES_NS,
asList(
Expand Down Expand Up @@ -398,7 +398,7 @@ private static Stream<Arguments> args() {
Instant.from(Year.of(1985)
.atMonth(Month.JANUARY).atDay(1).atStartOfDay().atOffset(UTC)),
Instant.now())))),
defaultSearchOptions()
searchOptions()
),
MFLIX_MOVIES_NS,
asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import static com.mongodb.client.model.search.SearchCount.total
import static com.mongodb.client.model.search.SearchFacet.stringFacet
import static com.mongodb.client.model.search.SearchHighlight.paths
import static com.mongodb.client.model.search.SearchOperator.exists
import static com.mongodb.client.model.search.SearchOptions.defaultSearchOptions
import static com.mongodb.client.model.search.SearchOptions.searchOptions
import static com.mongodb.client.model.search.SearchPath.fieldPath
import static com.mongodb.client.model.search.SearchPath.wildcardPath
import static java.util.Arrays.asList
Expand Down Expand Up @@ -624,7 +624,7 @@ class AggregatesSpecification extends Specification {
BsonDocument searchDoc = toBson(
search(
(SearchOperator) exists(fieldPath('fieldName')),
defaultSearchOptions()
searchOptions()
)
)

Expand All @@ -641,7 +641,7 @@ class AggregatesSpecification extends Specification {
(SearchCollector) facet(
exists(fieldPath('fieldName')),
[stringFacet('stringFacetName', fieldPath('fieldName1'))]),
defaultSearchOptions()
searchOptions()
.index('indexName')
.count(total())
.highlight(paths(
Expand Down Expand Up @@ -715,7 +715,7 @@ class AggregatesSpecification extends Specification {
BsonDocument searchDoc = toBson(
searchMeta(
(SearchOperator) exists(fieldPath('fieldName')),
defaultSearchOptions()
searchOptions()
)
)

Expand All @@ -732,7 +732,7 @@ class AggregatesSpecification extends Specification {
(SearchCollector) facet(
exists(fieldPath('fieldName')),
[stringFacet('stringFacetName', fieldPath('fieldName1'))]),
defaultSearchOptions()
searchOptions()
.index('indexName')
.count(total())
.highlight(paths(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@

final class SearchOptionsTest {
@Test
void defaultSearchOptions() {
void searchOptions() {
assertEquals(
new BsonDocument(),
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.toBsonDocument()
);
}
Expand All @@ -44,18 +44,18 @@ void defaultSearchOptions() {
void option() {
assertAll(
() -> assertEquals(
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.index("indexName")
.toBsonDocument(),
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.option("index", new BsonString("indexName"))
.toBsonDocument()
),
() -> assertEquals(
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.option("index", "indexName")
.toBsonDocument(),
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.option("index", new BsonString("indexName"))
.toBsonDocument()
)
Expand All @@ -67,7 +67,7 @@ void index() {
assertEquals(
new BsonDocument()
.append("index", new BsonString("indexName")),
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.index("indexName")
.toBsonDocument()
);
Expand All @@ -80,7 +80,7 @@ void highlight() {
new BsonDocument()
.append("highlight", new BsonDocument()
.append("path", wildcardPath("wildc*rd").toBsonValue())),
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.highlight(
paths(wildcardPath("wildc*rd")))
.toBsonDocument()
Expand All @@ -91,7 +91,7 @@ void highlight() {
.append("path", new BsonArray(asList(
wildcardPath("wildc*rd").toBsonValue(),
fieldPath("fieldName").toBsonValue())))),
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.highlight(
paths(
wildcardPath("wildc*rd"),
Expand All @@ -106,7 +106,7 @@ void count() {
assertEquals(
new BsonDocument()
.append("count", total().toBsonDocument()),
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.count(total())
.toBsonDocument()
);
Expand All @@ -117,7 +117,7 @@ void returnStoredSource() {
assertEquals(
new BsonDocument()
.append("returnStoredSource", new BsonBoolean(true)),
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.returnStoredSource(true)
.toBsonDocument()
);
Expand All @@ -133,7 +133,7 @@ void options() {
.append("path", fieldPath("fieldName").toBsonValue()))
.append("count", total().toBsonDocument())
.append("returnStoredSource", new BsonBoolean(true)),
SearchOptions.defaultSearchOptions()
SearchOptions.searchOptions()
.index("indexName")
.option("name", new BsonArray(singletonList(new BsonString("value"))))
.highlight(
Expand All @@ -145,16 +145,16 @@ void options() {
}

@Test
void defaultSearchOptionsIsUnmodifiable() {
String expected = SearchOptions.defaultSearchOptions().toBsonDocument().toJson();
SearchOptions.defaultSearchOptions().option("name", "value");
assertEquals(expected, SearchOptions.defaultSearchOptions().toBsonDocument().toJson());
void searchOptionsIsUnmodifiable() {
String expected = SearchOptions.searchOptions().toBsonDocument().toJson();
SearchOptions.searchOptions().option("name", "value");
assertEquals(expected, SearchOptions.searchOptions().toBsonDocument().toJson());
}

@Test
void defaultSearchOptionsIsImmutable() {
String expected = SearchOptions.defaultSearchOptions().toBsonDocument().toJson();
SearchOptions.defaultSearchOptions().toBsonDocument().append("name", new BsonString("value"));
assertEquals(expected, SearchOptions.defaultSearchOptions().toBsonDocument().toJson());
void searchOptionsIsImmutable() {
String expected = SearchOptions.searchOptions().toBsonDocument().toJson();
SearchOptions.searchOptions().toBsonDocument().append("name", new BsonString("value"));
assertEquals(expected, SearchOptions.searchOptions().toBsonDocument().toJson());
}
}
Loading

0 comments on commit 091b41d

Please sign in to comment.