Skip to content

Commit

Permalink
feat: implement QueryStringSearchRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
masonJS committed Sep 30, 2023
1 parent 9886b6f commit 73b16a7
Showing 1 changed file with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package com.github.inflab.example.spring.data.mongodb.repository.atlas

import com.github.inflab.example.spring.data.mongodb.entity.mflix.Movies
import com.github.inflab.spring.data.mongodb.core.aggregation.aggregation
import com.mongodb.client.model.Projections.excludeId
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.aggregate
import org.springframework.data.mongodb.core.aggregation.AggregationResults
import org.springframework.stereotype.Repository

@Repository
class QueryStringSearchRepository(
private val mongoTemplate: MongoTemplate,
) {

data class PlotAndFullplotDto(
val title: String,
val plot: String,
val fullplot: String,
val score: Double,
)

/**
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/queryString/#boolean-operator-queries">Boolean Operator Queries (OR AND)</a>
*/
fun findFullplotWithPlot(): AggregationResults<PlotAndFullplotDto> {
val aggregation = aggregation {
search {
queryString {
defaultPath(Movies::fullplot)
query {
query = sub(text("captain") or text("kirk"), "plot") and text("enterprise")
}
}
}

// TODO: add $limit stage

project {
excludeId()
+Movies::title
+Movies::plot
+Movies::fullplot
searchScore()
}
}

return mongoTemplate.aggregate<Movies, PlotAndFullplotDto>(aggregation)
}

data class TitleDto(val title: String)

/**
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/queryString/#range-queries">Range Queries (TO)</a>
*/
fun findPoltWithTitleRange(): AggregationResults<TitleDto> {
val aggregation = aggregation {
search {
queryString {
defaultPath(Movies::plot)
query {
query = range(left = "count", right = WILDCARD, leftInclusion = true, rightInclusion = true, field = "title")
}
}
}

// TODO: add $limit stage

project {
excludeId()
+Movies::title
}
}

return mongoTemplate.aggregate<Movies, TitleDto>(aggregation)
}

/**
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/queryString/#wildcard-queries">Wildcard Queries (Fuzzy)</a>
*/
fun findTitleByFuzzy(): AggregationResults<TitleDto> {
val aggregation = aggregation {
search {
queryString {
defaultPath(Movies::title)
query {
query = fuzzy("catch", 2)
}
}
}

// TODO: add $limit stage

project {
excludeId()
+Movies::title
}
}

return mongoTemplate.aggregate<Movies, TitleDto>(aggregation)
}

/**
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/queryString/#wildcard-queries">Wildcard Queries (Wildcard)</a>
*/
fun findTitleByWildcard(): AggregationResults<TitleDto> {
val aggregation = aggregation {
search {
queryString {
defaultPath(Movies::title)
query {
query = wildcard("cou*t?*")
}
}
}

// TODO: add $limit stage

project {
excludeId()
+Movies::title
}
}

return mongoTemplate.aggregate<Movies, TitleDto>(aggregation)
}

/**
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/queryString/#wildcard-queries">Wildcard Queries (Regex)</a>
*/
fun findTitleByRegex(): AggregationResults<TitleDto> {
val aggregation = aggregation {
search {
queryString {
defaultPath(Movies::title)
query {
query = regex(".tal(y|ian)")
}
}
}

// TODO: add $limit stage

project {
excludeId()
+Movies::title
}
}

return mongoTemplate.aggregate<Movies, TitleDto>(aggregation)
}
}

0 comments on commit 73b16a7

Please sign in to comment.