Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
final class SearchConstructibleBsonElement extends AbstractConstructibleBsonElement<SearchConstructibleBsonElement> implements
MustCompoundSearchOperator, MustNotCompoundSearchOperator, ShouldCompoundSearchOperator, FilterCompoundSearchOperator,
ExistsSearchOperator, TextSearchOperator, AutocompleteSearchOperator,
NumberNearSearchOperator, DateNearSearchOperator, GeoNearSearchOperator,
NumberNearSearchOperator, DateNearSearchOperator, GeoNearSearchOperator, WildcardSearchOperator,
ValueBoostSearchScore, PathBoostSearchScore, ConstantSearchScore, FunctionSearchScore,
GaussSearchScoreExpression, PathSearchScoreExpression,
FacetSearchCollector,
Expand Down Expand Up @@ -118,6 +118,11 @@ public FilterCompoundSearchOperator filter(final Iterable<? extends SearchOperat
return newCombined("filter", clauses);
}

@Override
public WildcardSearchOperator allowAnalyzedField(final boolean allowAnalyzedField) {
return newWithAppendedValue("allowAnalyzedField", allowAnalyzedField);
}

private SearchConstructibleBsonElement newCombined(final String ruleName, final Iterable<? extends SearchOperator> clauses) {
notNull("clauses", clauses);
isTrueArgument("clauses must not be empty", sizeAtLeast(clauses, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,36 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final
.append("pivot", notNull("pivot", pivot)));
}

/**
* Returns a {@link SearchOperator} that enables queries which use special characters in the search string that can match any character.
*
* @param query The string to search for.
* @param path The indexed field to be searched.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/wildcard/ wildcard operator
*/
static WildcardSearchOperator wildcard(final String query, final SearchPath path) {
return wildcard(singleton(notNull("query", query)), singleton(notNull("path", path)));
}

/**
* Returns a {@link SearchOperator} that enables queries which use special characters in the search string that can match any character.
*
* @param queries The non-empty strings to search for.
* @param paths The non-empty index fields to be searched.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/wildcard/ wildcard operator
*/
static WildcardSearchOperator wildcard(final Iterable<String> queries, final Iterable<? extends SearchPath> paths) {
Iterator<String> queryIterator = notNull("queries", queries).iterator();
isTrueArgument("queries must not be empty", queryIterator.hasNext());
String firstQuery = queryIterator.next();
Iterator<? extends SearchPath> pathIterator = notNull("paths", paths).iterator();
isTrueArgument("paths must not be empty", pathIterator.hasNext());
return new SearchConstructibleBsonElement("wildcard", new Document("query", queryIterator.hasNext() ? queries : firstQuery)
.append("path", combineToBsonValue(pathIterator, false)));
}

/**
* Creates a {@link SearchOperator} from a {@link Bson} in situations when there is no builder method that better satisfies your needs.
* This method cannot be used to validate the syntax.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.client.model.search;

import com.mongodb.annotations.Beta;
import com.mongodb.annotations.Reason;
import com.mongodb.annotations.Sealed;

/**
* @see SearchOperator#wildcard(String, SearchPath)
* @see SearchOperator#wildcard(Iterable, Iterable)
* @since 4.7
*/
@Sealed
@Beta(Reason.CLIENT)
public interface WildcardSearchOperator extends SearchOperator {
@Override
WildcardSearchOperator score(SearchScore modifier);

/**
* Creates a new {@link WildcardSearchOperator} that runs against an analyzed field. The default value is false.
*
* <p> Must be set to true if the query is run against an analyzed field. </p>
*
* @param allowAnalyzedField The boolean value that sets if the query should run against an analyzed field.
*
* @return A new {@link WildcardSearchOperator}.
*/
WildcardSearchOperator allowAnalyzedField(boolean allowAnalyzedField);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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.SearchOperator.wildcard;
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;
Expand Down Expand Up @@ -608,7 +609,9 @@ private static Stream<Arguments> searchAndSearchMetaArgs() {
dateRange(fieldPath("fieldName6"))
.lte(Instant.ofEpochMilli(1)),
near(0, 1.5, fieldPath("fieldName7"), fieldPath("fieldName8")),
near(Instant.ofEpochMilli(1), Duration.ofMillis(3), fieldPath("fieldName9"))
near(Instant.ofEpochMilli(1), Duration.ofMillis(3), fieldPath("fieldName9")),
wildcard(asList("term10", "term11"), asList(wildcardPath("wildc*rd"), fieldPath("fieldName14")))
.allowAnalyzedField(true)
))
.minimumShouldMatch(1)
.mustNot(singleton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mongodb.client.model.geojson.Point;
import com.mongodb.client.model.geojson.Position;
import org.bson.BsonArray;
import org.bson.BsonBoolean;
import org.bson.BsonDateTime;
import org.bson.BsonDocument;
import org.bson.BsonDouble;
Expand Down Expand Up @@ -581,6 +582,61 @@ void near() {
);
}

@Test
void wildcard() {
assertAll(
() -> assertThrows(IllegalArgumentException.class, () ->
// queries must not be empty
SearchOperator.wildcard(emptyList(), singleton(fieldPath("fieldName")))
),
() -> assertThrows(IllegalArgumentException.class, () ->
// paths must not be empty
SearchOperator.wildcard(singleton("term"), emptyList())
),
() -> assertEquals(
new BsonDocument("wildcard",
new BsonDocument("query", new BsonString("term"))
.append("path", fieldPath("fieldName").toBsonValue())
),
SearchOperator.wildcard(
"term",
fieldPath("fieldName"))
.toBsonDocument()
),
() -> assertEquals(
new BsonDocument("wildcard",
new BsonDocument("query", new BsonArray(asList(
new BsonString("term1"),
new BsonString("term2"))))
.append("path", new BsonArray(asList(
fieldPath("fieldName").toBsonValue(),
wildcardPath("wildc*rd").toBsonValue())))
),
SearchOperator.wildcard(
asList(
"term1",
"term2"),
asList(
fieldPath("fieldName"),
wildcardPath("wildc*rd")))
.toBsonDocument()
),
() -> assertEquals(
new BsonDocument("wildcard",
new BsonDocument("query", new BsonString("term"))
.append("path", fieldPath("fieldName").toBsonValue())
.append("allowAnalyzedField", new BsonBoolean(true))
),
SearchOperator.wildcard(
singleton("term"),
singleton(fieldPath("fieldName")))
.allowAnalyzedField(true)
.toBsonDocument()
)
);
}


private static SearchOperator docExamplePredefined() {
return SearchOperator.exists(
fieldPath("fieldName"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,27 @@ object SearchOperator {
def near(origin: Point, pivot: Number, paths: Iterable[_ <: FieldSearchPath]): GeoNearSearchOperator =
JSearchOperator.near(origin, pivot, paths.asJava)

/**
* Returns a `SearchOperator` that enables queries which use special characters in the search string that can match any character.
*
* @param query The string to search for.
* @param path The indexed field to be searched.
* @return The requested `SearchOperator`.
* @see [[https://www.mongodb.com/docs/atlas/atlas-search/wildcard/ wildcard operator]]
*/
def wildcard(query: String, path: SearchPath): WildcardSearchOperator = JSearchOperator.wildcard(query, path)

/**
* Returns a `SearchOperator` that enables queries which use special characters in the search string that can match any character.
*
* @param queries The non-empty strings to search for.
* @param paths The non-empty indexed fields to be searched.
* @return The requested `SearchOperator`.
* @see [[https://www.mongodb.com/docs/atlas/atlas-search/wildcard/ wildcard operator]]
*/
def wildcard(queries: Iterable[String], paths: Iterable[_ <: SearchPath]): WildcardSearchOperator =
JSearchOperator.wildcard(queries.asJava, paths.asJava)

/**
* Creates a `SearchOperator` from a `Bson` in situations when there is no builder method that better satisfies your needs.
* This method cannot be used to validate the syntax.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ package object search {
@Beta(Array(Reason.CLIENT))
type GeoNearSearchOperator = com.mongodb.client.model.search.GeoNearSearchOperator

/**
* @see `SearchOperator.wildcard(String, SearchPath)`
* @see `SearchOperator.wildcard(Iterable, Iterable)`
*/
@Sealed
@Beta(Array(Reason.CLIENT))
type WildcardSearchOperator = com.mongodb.client.model.search.WildcardSearchOperator

/**
* Fuzzy search options that may be used with some [[SearchOperator]]s.
*
Expand Down