Skip to content

Commit

Permalink
Implement meta Projections
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill5k committed Jul 3, 2024
1 parent aae0c55 commit dda149d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ trait Aggregate extends AsJava {
def geoNear(point: Point, distanceField: String, options: GeoNearOptions): Aggregate
def geoNear(point: Point, distanceField: String): Aggregate = geoNear(point, distanceField, GeoNearOptions.geoNearOptions())

/** Creates a \$search pipeline stage supported by MongoDB Atlas. You may use \$meta: "searchScore", e.g., via metaSearchScore ( String )
* in Projection, to extract the relevance score assigned to each found document.
/** Creates a \$search pipeline stage supported by MongoDB Atlas. You may use \$meta: "searchScore", e.g., via
* Projection.metaSearchScore(String), to extract the relevance score assigned to each found document.
* @param operator
* A search operator.
* @param options
Expand All @@ -389,8 +389,8 @@ trait Aggregate extends AsJava {
def search(operator: SearchOperator, options: SearchOptions): Aggregate
def search(operator: SearchOperator): Aggregate = search(operator, SearchOptions.searchOptions())

/** Creates a \$search pipeline stage supported by MongoDB Atlas. You may use \$meta: "searchScore", e.g., via metaSearchScore ( String )
* in Projection, to extract the relevance score assigned to each found document.
/** Creates a \$search pipeline stage supported by MongoDB Atlas. You may use \$meta: "searchScore", e.g., via
* Projection.metaSearchScore(String), to extract the relevance score assigned to each found document.
* @param collector
* A search collector.
* @param options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ trait Projection {
*/
def metaTextScore(fieldName: String): Projection

/** Creates a projection to the given field name of the searchScore, for use with Aggregate.search(SearchOperator,SearchOptions) /
* Aggregate.search(SearchCollector,SearchOptions). Calling this method is equivalent to calling meta(String,String) with "searchScore"
* as the second argument.
*
* @param fieldName
* the field name
* @return
* the projection
* @since 4.7
*/
def metaSearchScore(fieldName: String): Projection

/** Creates a projection to the given field name of the searchHighlights, for use with Aggregate.search(SearchOperator,SearchOptions) /
* Aggregates.search(SearchCollector,SearchOptions). Calling this method is equivalent to calling meta String,String) with
* "searchHighlights" as the second argument.
*
* @param fieldName
* the field name
* @return
* the projection
* @since 4.7
*/
def metaSearchHighlights(fieldName: String): Projection

/** Creates a projection to the given field name of a slice of the array value of that field.
*
* @param fieldName
Expand Down Expand Up @@ -162,31 +186,21 @@ trait Projection {
object Projection {
private val empty: Projection = ProjectionBuilder(Nil)

def computed[T](fieldName: String, expression: T): Projection = empty.computed(fieldName, expression)

def include(fieldName: String): Projection = empty.include(fieldName)

def include(fieldNames: Seq[String]): Projection = empty.include(fieldNames)

def exclude(fieldName: String): Projection = empty.exclude(fieldName)

def exclude(fieldNames: Seq[String]): Projection = empty.exclude(fieldNames)

def excludeId: Projection = empty.excludeId

def elemMatch(fieldName: String): Projection = empty.elemMatch(fieldName)

def elemMatch(fieldName: String, filter: Filter): Projection = empty.elemMatch(fieldName, filter)

def meta(fieldName: String, metaFieldName: String): Projection = empty.meta(fieldName, metaFieldName)

def metaTextScore(fieldName: String): Projection = empty.metaTextScore(fieldName)

def slice(fieldName: String, limit: Int): Projection = empty.slice(fieldName, limit)

def computed[T](fieldName: String, expression: T): Projection = empty.computed(fieldName, expression)
def include(fieldName: String): Projection = empty.include(fieldName)
def include(fieldNames: Seq[String]): Projection = empty.include(fieldNames)
def exclude(fieldName: String): Projection = empty.exclude(fieldName)
def exclude(fieldNames: Seq[String]): Projection = empty.exclude(fieldNames)
def excludeId: Projection = empty.excludeId
def elemMatch(fieldName: String): Projection = empty.elemMatch(fieldName)
def elemMatch(fieldName: String, filter: Filter): Projection = empty.elemMatch(fieldName, filter)
def meta(fieldName: String, metaFieldName: String): Projection = empty.meta(fieldName, metaFieldName)
def metaTextScore(fieldName: String): Projection = empty.metaTextScore(fieldName)
def metaSearchScore(fieldName: String): Projection = empty.metaSearchScore(fieldName)
def metaSearchHighlights(fieldName: String): Projection = empty.metaSearchHighlights(fieldName)
def slice(fieldName: String, limit: Int): Projection = empty.slice(fieldName, limit)
def slice(fieldName: String, skip: Int, limit: Int): Projection = empty.slice(fieldName, skip, limit)

def combinedWith(anotherProjection: Projection): Projection = empty.combinedWith(anotherProjection)
def combinedWith(anotherProjection: Projection): Projection = empty.combinedWith(anotherProjection)
}

final private case class ProjectionBuilder(
Expand Down Expand Up @@ -223,6 +237,12 @@ final private case class ProjectionBuilder(
override def metaTextScore(fieldName: String): Projection =
ProjectionBuilder(Projections.metaTextScore(fieldName) :: projections)

override def metaSearchScore(fieldName: String): Projection =
ProjectionBuilder(Projections.metaSearchScore(fieldName) :: projections)

override def metaSearchHighlights(fieldName: String): Projection =
ProjectionBuilder(Projections.metaSearchHighlights(fieldName) :: projections)

override def slice(fieldName: String, limit: Int): Projection = {
val sliceCommand = Document("$slice" := BsonValue.array(BsonValue.string("$" + fieldName), BsonValue.int(limit)))
val sliceProjection = Document(fieldName := sliceCommand)
Expand Down

0 comments on commit dda149d

Please sign in to comment.