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
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
PR fixes
  • Loading branch information
katcharov committed Jan 24, 2025
commit 65840ebcafc785839823775eda11a8d11dd2ea1c
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,29 @@
import com.mongodb.annotations.Beta;
import com.mongodb.annotations.Reason;
import com.mongodb.annotations.Sealed;
import com.mongodb.assertions.Assertions;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.geojson.Point;

import java.util.UUID;

import org.bson.BsonArray;
import org.bson.BsonBoolean;
import org.bson.BsonType;
import org.bson.BsonValue;
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.ArrayList;
import java.util.Iterator;

import org.bson.types.ObjectId;
import java.util.List;
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 @@ -298,22 +301,29 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final
}

/**
* Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* 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) {
return in(notNull("path", path), concat(notNull("value", value), values));
static InSearchOperator in(final FieldSearchPath path, final boolean value, final boolean... values) {
Assertions.notNull("values", values);
List<BsonValue> list = new ArrayList<>();
list.add(new BsonBoolean(value));
for (boolean n : values) {
list.add(new BsonBoolean(n));
}
BsonArray bsonArray = new BsonArray(list);
return in(notNull("path", path), bsonArray);
}

/**
* Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* 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.
Expand All @@ -326,8 +336,8 @@ static InSearchOperator in(final FieldSearchPath path, final ObjectId value, fin
}

/**
* Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* 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.
Expand All @@ -340,8 +350,8 @@ static InSearchOperator in(final FieldSearchPath path, final Number value, final
}

/**
* Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* 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.
Expand All @@ -354,8 +364,8 @@ static InSearchOperator in(final FieldSearchPath path, final Instant value, fina
}

/**
* Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* 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.
Expand All @@ -368,8 +378,8 @@ static InSearchOperator in(final FieldSearchPath path, final UUID value, final U
}

/**
* Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* 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.
Expand All @@ -382,27 +392,23 @@ static InSearchOperator in(final FieldSearchPath path, final String value, final
}

/**
* Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* 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.
* @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<T> valueIterator = (Iterator<T>) notNull("values", values).iterator();
isTrueArgument("values must not be empty", valueIterator.hasNext());
T firstValue = valueIterator.next();

Iterator<T> typeIterator = (Iterator<T>) notNull("values", values).iterator();
while (typeIterator.hasNext()) {
Object element = typeIterator.next();
isTrueArgument("values must be of same type", firstValue.getClass().isInstance(element));
}

return new SearchConstructibleBsonElement("in", new Document("path", notNull("path", path).toValue())
.append("value", valueIterator.hasNext() ? values : firstValue));
boolean hasMore = valueIterator.hasNext();
return new SearchConstructibleBsonElement("in", new Document()
.append("path", path.toValue())
.append("value", hasMore ? values : firstValue));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ object SearchOperator {
JSearchOperator.near(origin, pivot, paths.asJava)

/**
* Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* Returns a `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.
Expand All @@ -250,8 +250,8 @@ object SearchOperator {
JSearchOperator.in(path, value, values: _*)

/**
* Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* Returns a `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.
Expand All @@ -263,8 +263,8 @@ object SearchOperator {
JSearchOperator.in(path, value, values: _*)

/**
* Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* Returns a `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.
Expand All @@ -276,8 +276,8 @@ object SearchOperator {
JSearchOperator.in(path, value, values: _*)

/**
* Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* Returns a `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.
Expand All @@ -289,8 +289,8 @@ object SearchOperator {
JSearchOperator.in(path, value, values: _*)

/**
* Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* Returns a `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.
Expand All @@ -302,8 +302,8 @@ object SearchOperator {
JSearchOperator.in(path, value, values: _*)

/**
* Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* Returns a `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.
Expand All @@ -315,8 +315,8 @@ object SearchOperator {
JSearchOperator.in(path, value, values: _*)

/**
* Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of
* the field equals any value in the specified array.
* Returns a `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.
Expand Down