Skip to content
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

Implement QueryStringSearchOperatorDsl #57

Merged
merged 6 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.github.inflab.spring.data.mongodb.core.aggregation.search

import com.github.inflab.spring.data.mongodb.core.annotation.AggregationMarker

/**
* A Kotlin DSL to configure queryString query option operator using idiomatic Kotlin code.
*
* @author minwoo
* @since 1.0
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/queryString/#options">queryString options</a>
*/
@AggregationMarker
class QueryStringQueryOptionDsl {
var query: Query? = null

class Query(val value: String, val field: String?) {
override fun toString(): String {
return when (field) {
null -> value
else -> "$field:$value"
}
}
}

/**
* Indicates 0 or more characters to match.
*/
val WILDCARD = "*"

/**
* Indicates any single character to match.
*/
val QUESTION = "?"

/**
* Creates a text query.
*
* @param value The value to search
* @param field Indexed field search
*/
fun text(value: String, field: String? = null): Query {
val escaped = value.replace("*", "\\*").replace("?", "\\?")

return Query("\"$escaped\"", field)
}

/**
* Creates a wildcard query.
*
* @param value The value to search
* @param field Indexed field to search
*/
fun wildcard(value: String, field: String? = null): Query {
return Query(value, field)
}

/**
* Creates a regex query.
*
* @param pattern The pattern to search
* @param field Indexed field to search
*/
fun regex(pattern: String, field: String? = null): Query {
return Query("/$pattern/", field)
}

/**
* Creates a range query.
*
* @param left The left value to search
* @param right The right value to search
* @param leftInclusion The left value is included in the range
* @param rightInclusion The right value is included in the range
* @param field Indexed field to search
*/
fun range(left: String, right: String, leftInclusion: Boolean = true, rightInclusion: Boolean = true, field: String? = null): Query {
val leftBracket = if (leftInclusion) "[" else "{"
val rightBracket = if (rightInclusion) "]" else "}"

val leftExp = when (left) {
WILDCARD -> WILDCARD
QUESTION -> QUESTION
else -> "\"$left\""
}
val rightExp = when (right) {
WILDCARD -> WILDCARD
QUESTION -> QUESTION
else -> "\"$right\""
}

return Query("$leftBracket$leftExp TO $rightExp$rightBracket", field)
}

/**
* Creates a fuzzy query.
*
* @param value The value to search
* @param maxEdits Maximum number of single-character edits required to match the specified search term.
* @param field indexed field to search
*/
fun fuzzy(value: String, maxEdits: Int, field: String? = null): Query {
return Query("$value~$maxEdits", field)
}

/**
* Creates a delimiters for subqueries.
*
* @param query The Query for subqueries.
*/
fun sub(query: Query, field: String? = null): Query {
return Query("${field?.let { "$it:" }.orEmpty()}(${query.value})", query.field)
}

/**
* Creates an operator that indicates `NOT` boolean operator.
* Specified search value must be absent for a document to be included in the results.
*
* @param query The Query to apply.
*/
fun not(query: Query): Query {
return Query("NOT (${query.value})", query.field)
}

/**
* Creates an operator that indicates `AND` boolean operator.
* All search values must be present for a document to be included in the results.
*
* @param query The Query to apply.
*/
infix fun Query.and(query: Query): Query {
return Query("$this AND $query", null)
}

/**
* Creates an operator that indicates OR boolean operator.
* At least one of the search value must be present for a document to be included in the results.
*
* @param query The Query to apply.
*/
infix fun Query.or(query: Query): Query {
return Query("$this OR $query", null)
}

internal fun build(): String {
return checkNotNull(query) { "query must not be null" }.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.github.inflab.spring.data.mongodb.core.aggregation.search

import com.github.inflab.spring.data.mongodb.core.annotation.AggregationMarker
import com.github.inflab.spring.data.mongodb.core.extension.toDotPath
import org.bson.Document
import kotlin.reflect.KProperty

/**
* A Kotlin DSL to configure queryString search operator using idiomatic Kotlin code.
*
* @author minwoo
* @since 1.0
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/queryString">queryString</a>
*/
@AggregationMarker
class QueryStringSearchOperatorDsl {
private val document = Document()

/**
* The indexed field to search by default.
* Atlas Search only searches the field in `defaultPath` if you omit the field to search in the query.
*
* @param path The indexed field to search by default.
*/
fun defaultPath(path: String) {
document["defaultPath"] = path
}

/**
* The indexed field to search by default.
* Atlas Search only searches the field in `defaultPath` if you omit the field to search in the query.
*
* @param path The indexed field to search by default.
*/
fun defaultPath(path: KProperty<String?>) {
document["defaultPath"] = path.toDotPath()
}

/**
* The indexed field to search by default.
* Atlas Search only searches the field in `defaultPath` if you omit the field to search in the query.
*
* @param path The indexed field to search by default.
*/
@JvmName("defaultPathIterable")
fun defaultPath(path: KProperty<Iterable<String?>?>) {
document["defaultPath"] = path.toDotPath()
}

/**
* Specifies one or more indexed fields and values to search.
* Fields and values are colon-delimited.
* For example, to search the plot field for the string baseball, use `plot:baseball`.
*
* @param configuration The configuration block for the [QueryStringQueryOptionDsl].
*/
fun query(configuration: QueryStringQueryOptionDsl.() -> Unit) {
document["query"] = QueryStringQueryOptionDsl().apply(configuration).build()
}

/**
* The score assigned to matching search results.
* You can modify the default score using the following options:
*
* - boost: multiply the result score by the given number.
* - constant: replace the result score with the given number.
* - function: replace the result score using the given expression.
*
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/scoring/#std-label-scoring-ref">Score the Documents in the Results</a>
*/
fun score(scoreConfiguration: ScoreSearchOptionDsl.() -> Unit) {
document["score"] = ScoreSearchOptionDsl().apply(scoreConfiguration).get()
}

internal fun build() = Document("queryString", document)
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,13 @@ interface SearchOperator {
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/wildcard">wildcard</a>
*/
fun wildcard(configuration: WildcardSearchOperatorDsl.() -> Unit)

/**
* Supports querying a combination of indexed fields and values.
* You can perform text, wildcard, regular expression, fuzzy, and range searches on string fields using the queryString operator.
*
* @param configuration The configuration block for the [QueryStringSearchOperatorDsl].
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/queryString">queryString</a>
*/
fun queryString(configuration: QueryStringSearchOperatorDsl.() -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ class SearchOperatorDsl : SearchOperator {
override fun wildcard(configuration: WildcardSearchOperatorDsl.() -> Unit) {
operators.add(WildcardSearchOperatorDsl().apply(configuration).build())
}

override fun queryString(configuration: QueryStringSearchOperatorDsl.() -> Unit) {
operators.add(QueryStringSearchOperatorDsl().apply(configuration).build())
}
}
Loading