Skip to content

Add wildcard operator to Atlas Search #1596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 16, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,36 @@ static PhraseSearchOperator phrase(final Iterable<? extends SearchPath> paths, f
.append("query", queryIterator.hasNext() ? queries : firstQuery));
}

/**
* Returns a {@link SearchOperator} that performs a search using a 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,32 @@
/*
* 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import static com.mongodb.client.model.search.SearchOperator.numberRange;
import static com.mongodb.client.model.search.SearchOperator.phrase;
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 @@ -610,7 +611,8 @@ private static Stream<Arguments> searchAndSearchMetaArgs() {
.lte(Instant.ofEpochMilli(1)),
near(0, 1.5, fieldPath("fieldName7"), fieldPath("fieldName8")),
near(Instant.ofEpochMilli(1), Duration.ofMillis(3), fieldPath("fieldName9")),
phrase(fieldPath("fieldName10"), "term6")
phrase(fieldPath("fieldName10"), "term6"),
wildcard(asList("term10", "term11"), asList(wildcardPath("wildc*rd"), fieldPath("fieldName14")))
))
.minimumShouldMatch(1)
.mustNot(singleton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,48 @@ 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()
)
);
}

@Test
void phrase() {
assertAll(
Expand Down
4 changes: 3 additions & 1 deletion driver-kotlin-extensions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,6 @@ tasks.javadocJar.configure {
// ===========================
tasks.sourcesJar { from(project.sourceSets.main.map { it.kotlin }) }

afterEvaluate { tasks.jar { manifest { attributes["Automatic-Module-Name"] = "org.mongodb.driver.kotlin.extensions" } } }
afterEvaluate {
tasks.jar { manifest { attributes["Automatic-Module-Name"] = "org.mongodb.driver.kotlin.extensions" } }
}
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)

/**
* Returns a `SearchOperator` that performs a search for documents containing an ordered sequence of terms.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,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