Skip to content

Commit

Permalink
Add queryString operator to Atlas Search (#1588)
Browse files Browse the repository at this point in the history
Add queryString operator to 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 queryString operator to Atlas Search.

JAVA-5727
  • Loading branch information
joykim1005 authored Jan 16, 2025
1 parent e022dbd commit 0d87508
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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#queryString(FieldSearchPath, String)
* @since 5.3
*/
@Sealed
@Beta(Reason.CLIENT)
public interface QueryStringSearchOperator extends SearchOperator {
@Override
QueryStringSearchOperator score(SearchScore modifier);
}
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, RegexSearchOperator,
NumberNearSearchOperator, DateNearSearchOperator, GeoNearSearchOperator, RegexSearchOperator, QueryStringSearchOperator,
ValueBoostSearchScore, PathBoostSearchScore, ConstantSearchScore, FunctionSearchScore,
GaussSearchScoreExpression, PathSearchScoreExpression,
FacetSearchCollector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,23 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final
.append("pivot", notNull("pivot", pivot)));
}

/**
* Returns a {@link SearchOperator} that supports querying a combination of indexed fields and values.
*
* @param defaultPath The field to be searched by default.
* @param query One or more indexed fields and values to search.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/queryString/ queryString operator
*/
static QueryStringSearchOperator queryString(final FieldSearchPath defaultPath, final String query) {
isTrueArgument("path must not be empty", defaultPath != null);
isTrueArgument("query must not be empty", query != null);

return new SearchConstructibleBsonElement("queryString",
new Document("defaultPath", defaultPath.toBsonValue())
.append("query", query));
}

/**
* Returns a {@link 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 @@ -82,6 +82,7 @@
import static com.mongodb.client.model.search.SearchOperator.exists;
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.queryString;
import static com.mongodb.client.model.search.SearchOperator.regex;
import static com.mongodb.client.model.search.SearchOperator.phrase;
import static com.mongodb.client.model.search.SearchOperator.text;
Expand Down Expand Up @@ -612,7 +613,8 @@ private static Stream<Arguments> searchAndSearchMetaArgs() {
near(0, 1.5, fieldPath("fieldName7"), fieldPath("fieldName8")),
near(Instant.ofEpochMilli(1), Duration.ofMillis(3), fieldPath("fieldName9")),
phrase(fieldPath("fieldName10"), "term6"),
regex(fieldPath("fieldName11"), "term7")
regex(fieldPath("fieldName11"), "term7"),
queryString(fieldPath("fieldName12"), "term8")
))
.minimumShouldMatch(1)
.mustNot(singleton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,30 @@ void near() {
);
}

@Test
void queryString() {
assertAll(
() -> assertThrows(IllegalArgumentException.class, () ->
// queries must not be empty
SearchOperator.queryString(fieldPath("fieldName"), null)
),
() -> assertThrows(IllegalArgumentException.class, () ->
// paths must not be empty
SearchOperator.queryString(null, "term1 AND (term2 OR term3)")
),
() -> assertEquals(
new BsonDocument("queryString",
new BsonDocument("defaultPath", fieldPath("fieldName").toBsonValue())
.append("query", new BsonString("term1 AND (term2 OR term3)"))
),
SearchOperator.queryString(
fieldPath("fieldName"),
"term1 AND (term2 OR term3)")
.toBsonDocument()
)
);
}

@Test
void phrase() {
assertAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ object SearchOperator {
def near(origin: Point, pivot: Number, paths: Iterable[_ <: FieldSearchPath]): GeoNearSearchOperator =
JSearchOperator.near(origin, pivot, paths.asJava)

/**
* Returns a `SearchOperator` that supports querying a combination of indexed fields and values.
*
* @param defaultPath The field to be searched by default.
* @param query One or more indexed fields and values to search.
* @return The requested `SearchOperator`.
* @see [[https://www.mongodb.com/docs/atlas/atlas-search/queryString/ queryString operator]]
*/
def queryString(defaultPath: FieldSearchPath, query: String): QueryStringSearchOperator =
JSearchOperator.queryString(defaultPath, query)

/**
* 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 @@ -198,6 +198,13 @@ package object search {
@Beta(Array(Reason.CLIENT))
type GeoNearSearchOperator = com.mongodb.client.model.search.GeoNearSearchOperator

/**
* @see `SearchOperator.queryString`
*/
@Sealed
@Beta(Array(Reason.CLIENT))
type QueryStringSearchOperator = com.mongodb.client.model.search.QueryStringSearchOperator

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

0 comments on commit 0d87508

Please sign in to comment.