Skip to content

Add in operator to Atlas Search #1605

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 16 commits into from
Feb 4, 2025
12 changes: 3 additions & 9 deletions bson/src/main/org/bson/codecs/BsonArrayCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
import org.bson.BsonWriter;
import org.bson.codecs.configuration.CodecRegistry;

import java.util.ArrayList;
import java.util.List;

import static org.bson.assertions.Assertions.notNull;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;

Expand Down Expand Up @@ -60,16 +57,13 @@ public BsonArrayCodec(final CodecRegistry codecRegistry) {

@Override
public BsonArray decode(final BsonReader reader, final DecoderContext decoderContext) {
BsonArray bsonArray = new BsonArray();
reader.readStartArray();

List<BsonValue> list = new ArrayList<>();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
list.add(readValue(reader, decoderContext));
bsonArray.add(readValue(reader, decoderContext));
}

reader.readEndArray();

return new BsonArray(list);
return bsonArray;
}

@Override
Expand Down
30 changes: 15 additions & 15 deletions driver-core/src/main/com/mongodb/client/model/mql/MqlValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public static MqlBoolean of(final boolean of) {
*/
public static MqlArray<MqlBoolean> ofBooleanArray(final boolean... array) {
Assertions.notNull("array", array);
List<BsonValue> list = new ArrayList<>();
BsonArray bsonArray = new BsonArray();
for (boolean b : array) {
list.add(new BsonBoolean(b));
bsonArray.add(new BsonBoolean(b));
}
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list)));
return new MqlExpression<>((cr) -> new AstPlaceholder(bsonArray));
}

/**
Expand All @@ -102,11 +102,11 @@ public static MqlInteger of(final int of) {
*/
public static MqlArray<MqlInteger> ofIntegerArray(final int... array) {
Assertions.notNull("array", array);
List<BsonValue> list = new ArrayList<>();
BsonArray bsonArray = new BsonArray();
for (int i : array) {
list.add(new BsonInt32(i));
bsonArray.add(new BsonInt32(i));
}
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list)));
return new MqlExpression<>((cr) -> new AstPlaceholder(bsonArray));
}

/**
Expand All @@ -130,11 +130,11 @@ public static MqlInteger of(final long of) {
*/
public static MqlArray<MqlInteger> ofIntegerArray(final long... array) {
Assertions.notNull("array", array);
List<BsonValue> list = new ArrayList<>();
BsonArray bsonArray = new BsonArray();
for (long i : array) {
list.add(new BsonInt64(i));
bsonArray.add(new BsonInt64(i));
}
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list)));
return new MqlExpression<>((cr) -> new AstPlaceholder(bsonArray));
}

/**
Expand All @@ -158,11 +158,11 @@ public static MqlNumber of(final double of) {
*/
public static MqlArray<MqlNumber> ofNumberArray(final double... array) {
Assertions.notNull("array", array);
List<BsonValue> list = new ArrayList<>();
BsonArray bsonArray = new BsonArray();
for (double n : array) {
list.add(new BsonDouble(n));
bsonArray.add(new BsonDouble(n));
}
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list)));
return new MqlExpression<>((cr) -> new AstPlaceholder(bsonArray));
}

/**
Expand Down Expand Up @@ -310,12 +310,12 @@ public static MqlDocument current() {
public static <T extends MqlValue> MqlArray<T> ofArray(final T... array) {
Assertions.notNull("array", array);
return new MqlExpression<>((cr) -> {
List<BsonValue> list = new ArrayList<>();
BsonArray bsonArray = new BsonArray();
for (T v : array) {
Assertions.notNull("elements of array", v);
list.add(((MqlExpression<?>) v).toBsonValue(cr));
bsonArray.add(((MqlExpression<?>) v).toBsonValue(cr));
}
return new AstPlaceholder(new BsonArray(list));
return new AstPlaceholder(bsonArray);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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;
import org.bson.types.ObjectId;

import java.time.Instant;
import java.util.UUID;

/**
* @see SearchOperator#in(FieldSearchPath, boolean, boolean...)
* @see SearchOperator#in(FieldSearchPath, ObjectId, ObjectId...)
* @see SearchOperator#in(FieldSearchPath, Number, Number...)
* @see SearchOperator#in(FieldSearchPath, Instant, Instant...)
* @see SearchOperator#in(FieldSearchPath, UUID, UUID...)
* @see SearchOperator#in(FieldSearchPath, String, String...)
* @see SearchOperator#in(FieldSearchPath, Iterable)
* @since 5.3
*/
@Sealed
@Beta(Reason.CLIENT)
public interface InSearchOperator extends SearchOperator {
@Override
InSearchOperator score(SearchScore modifier);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class SearchConstructibleBsonElement extends AbstractConstructibleBsonElem
MustCompoundSearchOperator, MustNotCompoundSearchOperator, ShouldCompoundSearchOperator, FilterCompoundSearchOperator,
ExistsSearchOperator, TextSearchOperator, AutocompleteSearchOperator,
NumberNearSearchOperator, DateNearSearchOperator, GeoNearSearchOperator,
EqualsSearchOperator, MoreLikeThisSearchOperator,
EqualsSearchOperator, InSearchOperator, MoreLikeThisSearchOperator,
RegexSearchOperator, QueryStringSearchOperator, WildcardSearchOperator,
ValueBoostSearchScore, PathBoostSearchScore, ConstantSearchScore, FunctionSearchScore,
GaussSearchScoreExpression, PathSearchScoreExpression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,26 @@
import com.mongodb.annotations.Sealed;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.geojson.Point;

import java.util.UUID;

import org.bson.BsonArray;
import org.bson.BsonBinary;
import org.bson.BsonNull;
import org.bson.BsonBoolean;
import org.bson.BsonDocument;
import org.bson.BsonNull;
import org.bson.BsonType;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

import java.time.Duration;
import java.time.Instant;
import java.util.Iterator;

import org.bson.types.ObjectId;
import java.util.UUID;

import static com.mongodb.assertions.Assertions.isTrueArgument;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.Iterables.concat;
import static com.mongodb.internal.client.model.Util.combineToBsonValue;
import static java.util.Collections.singleton;
import static com.mongodb.assertions.Assertions.notNull;

/**
* The core part of the {@link Aggregates#search(SearchOperator, SearchOptions) $search} pipeline stage of an aggregation pipeline.
Expand Down Expand Up @@ -300,6 +299,117 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final
.append("pivot", notNull("pivot", pivot)));
}

/**
* Returns a {@link SearchOperator} that searches for documents where the
* value or array of values at a given path contains any of the specified values
*
* @param path The indexed field to be searched.
* @param value The boolean value to search for.
* @param values More fields to be searched.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/in/ in operator
*/
static InSearchOperator in(final FieldSearchPath path, final boolean value, final boolean... values) {
notNull("values", values);
BsonArray bsonArray = new BsonArray();
bsonArray.add(new BsonBoolean(value));
for (boolean v : values) {
bsonArray.add(new BsonBoolean(v));
}
return in(notNull("path", path), bsonArray);
}

/**
* Returns a {@link SearchOperator} that searches for documents where the
* value or array of values at a given path contains any of the specified values
*
* @param path The indexed field to be searched.
* @param value The objectId value to search for.
* @param values More fields to be searched.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/in/ in operator
*/
static InSearchOperator in(final FieldSearchPath path, final ObjectId value, final ObjectId... values) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is based on

static DateRangeSearchOperatorBase dateRange(final FieldSearchPath path, final FieldSearchPath... paths)

Having two params ensures that the user is unable to provide zero values. I am not sure we should do this even there, but I think in this case, we should not have both of these params, but just the values, since the docs did not seem to exclude empty lists. (Note that the docs on values seem to be incorrect)

@stIncMale cc

Copy link
Member

@stIncMale stIncMale Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check if the server actually allows passing value: [].

  • If it does not, then following the approach taken in the rest of the search building API, we should introduce in(final FieldSearchPath path, final ObjectId value, final ObjectId... values). We may follow that with adding another thing to be re-reviewed to JAVA-5752: Re-visit some aspects of the Atlas search API design before moving it out of beta
  • If the server indeed allows passing value: [], then we should rather introduce in(final FieldSearchPath path, final ObjectId... values).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return in(notNull("path", path), concat(notNull("value", value), values));
}

/**
* Returns a {@link SearchOperator} that searches for documents where the
* value or array of values at a given path contains any of the specified values
*
* @param path The indexed field to be searched.
* @param value The number value to search for.
* @param values More fields to be searched.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/in/ in operator
*/
static InSearchOperator in(final FieldSearchPath path, final Number value, final Number... values) {
return in(notNull("path", path), concat(notNull("value", value), values));
}

/**
* Returns a {@link SearchOperator} that searches for documents where the
* value or array of values at a given path contains any of the specified values
*
* @param path The indexed field to be searched.
* @param value The instant date value to search for.
* @param values More fields to be searched.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/in/ in operator
*/
static InSearchOperator in(final FieldSearchPath path, final Instant value, final Instant... values) {
return in(notNull("path", path), concat(notNull("value", value), values));
}

/**
* Returns a {@link SearchOperator} that searches for documents where the
* value or array of values at a given path contains any of the specified values
*
* @param path The indexed field to be searched.
* @param value The uuid value to search for.
* @param values More fields to be searched.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/in/ in operator
*/
static InSearchOperator in(final FieldSearchPath path, final UUID value, final UUID... values) {
return in(notNull("path", path), concat(notNull("value", value), values));
}

/**
* Returns a {@link SearchOperator} that searches for documents where the
* value or array of values at a given path contains any of the specified values
*
* @param path The indexed field to be searched.
* @param value The string value to search for.
* @param values More fields to be searched.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/in/ in operator
*/
static InSearchOperator in(final FieldSearchPath path, final String value, final String... values) {
return in(notNull("path", path), concat(notNull("value", value), values));
}

/**
* Returns a {@link SearchOperator} that searches for documents where the
* value or array of values at a given path contains any of the specified values
*
* @param path The indexed field to be searched.
* @param values The non-empty values to search for. Value can be either a single value or an array of values of only one of the supported BSON types and can't be a mix of different types.
* @param <T> the type of elements in {@code values}.
* @return The requested {@link SearchOperator}.
* @mongodb.atlas.manual atlas-search/in/ in operator
*/
static <T> InSearchOperator in(final FieldSearchPath path, final Iterable<? extends T> values) {
notNull("path", path);
Iterator<? extends T> valueIterator = notNull("values", values).iterator();
isTrueArgument("values must not be empty", valueIterator.hasNext());
T firstValue = valueIterator.next();
boolean hasMore = valueIterator.hasNext();
return new SearchConstructibleBsonElement("in", new Document()
.append("path", path.toValue())
.append("value", hasMore ? values : firstValue));
}

/**
* Returns a {@link SearchOperator} that searches for documents where a field matches the specified value.
*
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.dateRange;
import static com.mongodb.client.model.search.SearchOperator.equalsNull;
import static com.mongodb.client.model.search.SearchOperator.exists;
import static com.mongodb.client.model.search.SearchOperator.in;
import static com.mongodb.client.model.search.SearchOperator.moreLikeThis;
import static com.mongodb.client.model.search.SearchOperator.near;
import static com.mongodb.client.model.search.SearchOperator.numberRange;
Expand Down Expand Up @@ -617,6 +618,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"),
in(fieldPath("fieldName10"), true),
in(fieldPath("fieldName11"), "term4", "term5"),
regex(fieldPath("title").multi("keyword"), "term7"),
queryString(fieldPath("fieldName12"), "term8"),
moreLikeThis(new BsonDocument("like", new BsonDocument("fieldName10",
Expand Down
Loading