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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix merge conflict and remove allowAnalyzedField
  • Loading branch information
joykim1005 committed Jan 14, 2025
commit 1aef14c847377d0b2453463869c7d0ef6b3d021f
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ 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 performs a search for documents containing an ordered sequence of terms.
*
* @param path The field to be searched.
* @param query The string to search for.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/phrase/ phrase operator
*/
static PhraseSearchOperator phrase(final SearchPath path, final String query) {
return phrase(singleton(notNull("path", path)), singleton(notNull("query", query)));
}

/**
* Returns a {@link SearchOperator} that performs a search for documents containing an ordered sequence of terms.
*
* @param paths The non-empty fields to be searched.
* @param queries The non-empty strings to search for.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/phrase/ phrase operator
*/
static PhraseSearchOperator phrase(final Iterable<? extends SearchPath> paths, final Iterable<String> queries) {
Iterator<? extends SearchPath> pathIterator = notNull("paths", paths).iterator();
isTrueArgument("paths must not be empty", pathIterator.hasNext());
Iterator<String> queryIterator = notNull("queries", queries).iterator();
isTrueArgument("queries must not be empty", queryIterator.hasNext());
String firstQuery = queryIterator.next();
return new PhraseConstructibleBsonElement("phrase", new Document("path", combineToBsonValue(pathIterator, false))
.append("query", queryIterator.hasNext() ? queries : firstQuery));
}

/**
* Returns a {@link SearchOperator} that enables queries which use special characters in the search string that can match any character.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,4 @@
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 @@ -611,8 +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"),
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,7 +19,6 @@
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 @@ -620,23 +619,78 @@ void wildcard() {
fieldPath("fieldName"),
wildcardPath("wildc*rd")))
.toBsonDocument()
)
);
}

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


private static SearchOperator docExamplePredefined() {
return SearchOperator.exists(
fieldPath("fieldName"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,27 @@ object SearchOperator {
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.
*
* @param path The field to be searched.
* @param query The string to search for.
* @return The requested `SearchOperator`.
* @see [[https://www.mongodb.com/docs/atlas/atlas-search/phrase/ phrase operator]]
*/
def phrase(path: SearchPath, query: String): PhraseSearchOperator = JSearchOperator.phrase(path, query)

/**
* Returns a `SearchOperator` that performs a search for documents containing an ordered sequence of terms.
*
* @param paths The non-empty fields to be searched.
* @param queries The non-empty strings to search for.
* @return The requested `SearchOperator`.
* @see [[https://www.mongodb.com/docs/atlas/atlas-search/phrase/ phrase operator]]
*/
def phrase(paths: Iterable[_ <: SearchPath], queries: Iterable[String]): PhraseSearchOperator =
JSearchOperator.phrase(paths.asJava, queries.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
You are viewing a condensed version of this merge commit. You can view the full changes here.