-
Notifications
You must be signed in to change notification settings - Fork 6
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 Near Search Operator #62
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
029f5d3
feat: add near search operator
username1103 2a5b94c
feat: add near search operator example
username1103 bf0e622
feat: add NearSearchRepository example
username1103 4e1ad44
chore: fix comment in NearOperatorDsl
username1103 a97e0b1
fix: typo
username1103 6234cf7
chore: add link in comment
username1103 aed90c9
refactor: use aggregate extension function
username1103 2f32aab
refactor: use findAnnotation extension function
username1103 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
...in/com/github/inflab/spring/data/mongodb/core/aggregation/search/NearSearchOperatorDsl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,141 @@ | ||||||||||||||||||||||
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.firstOrAll | ||||||||||||||||||||||
import com.github.inflab.spring.data.mongodb.core.extension.toDotPath | ||||||||||||||||||||||
import org.bson.Document | ||||||||||||||||||||||
import org.springframework.data.mongodb.core.geo.GeoJsonPoint | ||||||||||||||||||||||
import java.time.temporal.Temporal | ||||||||||||||||||||||
import kotlin.reflect.KProperty | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* A Kotlin DSL to configure near search operator using idiomatic Kotlin code. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @author username1103 | ||||||||||||||||||||||
* @since 1.0 | ||||||||||||||||||||||
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/near">near</a> | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
@AggregationMarker | ||||||||||||||||||||||
class NearSearchOperatorDsl { | ||||||||||||||||||||||
private val document = Document() | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Indexed field or fields to search. | ||||||||||||||||||||||
* See Path Construction. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @param path The indexed field or fields to search. | ||||||||||||||||||||||
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/path-construction/#std-label-ref-path">Path Construction</a> | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
fun path(vararg path: String) { | ||||||||||||||||||||||
document["path"] = path.toList().firstOrAll() | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Indexed number type field or fields to search. | ||||||||||||||||||||||
* See Path Construction. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @param path The indexed field or fields to search. | ||||||||||||||||||||||
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/path-construction/#std-label-ref-path">Path Construction</a> | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
@JvmName("pathNumber") | ||||||||||||||||||||||
fun path(vararg path: KProperty<Number?>) { | ||||||||||||||||||||||
document["path"] = path.map { it.toDotPath() }.firstOrAll() | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Indexed date type field or fields to search. | ||||||||||||||||||||||
* See Path Construction. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @param path The indexed field or fields to search. | ||||||||||||||||||||||
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/path-construction/#std-label-ref-path">Path Construction</a> | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
@JvmName("pathDate") | ||||||||||||||||||||||
fun path(vararg path: KProperty<Temporal?>) { | ||||||||||||||||||||||
document["path"] = path.map { it.toDotPath() }.firstOrAll() | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Indexed geo type field or fields to search. | ||||||||||||||||||||||
* See Path Construction for more information. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @param path The indexed field or fields to search. | ||||||||||||||||||||||
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/path-construction/#std-label-ref-path">Path Construction</a> | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
@JvmName("pathPoint") | ||||||||||||||||||||||
fun path(vararg path: KProperty<GeoJsonPoint?>) { | ||||||||||||||||||||||
document["path"] = path.map { it.toDotPath() }.firstOrAll() | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Origin to query for Number field. | ||||||||||||||||||||||
* This is the origin from which the proximity of the results is measured. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* For number fields, the value must be of BSON int32, int64, or double data types. | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
fun origin(origin: Number) { | ||||||||||||||||||||||
document["origin"] = origin | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Origin to query for Date field. | ||||||||||||||||||||||
* This is the origin from which the proximity of the results is measured. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* For date fields, the value must be an ISODate formatted date. | ||||||||||||||||||||||
* @see <a href="https://www.mongodb.com/docs/upcoming/reference/glossary/#std-term-ISODate">ISODate</a> | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
fun origin(origin: Temporal) { | ||||||||||||||||||||||
document["origin"] = origin | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Origin to query for Geo field. | ||||||||||||||||||||||
* This is the origin from which the proximity of the results is measured. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* For geo fields. the value must be a GeoJSON point. | ||||||||||||||||||||||
* @see <a href="https://www.mongodb.com/docs/upcoming/reference/geojson/#std-label-geojson-point">GeoJson Point</a> | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
fun origin(origin: GeoJsonPoint) { | ||||||||||||||||||||||
document["origin"] = Document("type", "Point").append("coordinates", origin.coordinates) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Value to use to calculate scores of Atlas Search result documents. Score is calculated using the following formula: | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* ``` | ||||||||||||||||||||||
* pivot | ||||||||||||||||||||||
* score = ------------------ | ||||||||||||||||||||||
* pivot + distance | ||||||||||||||||||||||
Comment on lines
+105
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better if these lines are wrapped in code block.
Suggested change
|
||||||||||||||||||||||
* ``` | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* where distance is the difference between origin and the indexed field value. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* Results have a score equal to 1/2 (or 0.5) when their indexed field value is pivot units away from origin. | ||||||||||||||||||||||
* The value of pivot must be greater than (i.e. >) 0. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* If origin is a: | ||||||||||||||||||||||
* - Number, pivot can be specified as an integer or floating point number. | ||||||||||||||||||||||
* - Date, pivot must be specified in milliseconds and can be specified as a 32 or 64 bit integer. | ||||||||||||||||||||||
* - GeoJSON point, pivot is measured in meters and must be specified as an integer or floating point number. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @param pivot The value to use to calculate scores of Atlas Search result documents. | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
fun pivot(pivot: Number) { | ||||||||||||||||||||||
document["pivot"] = pivot | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* The score assigned to matching search term results. Use one of the following options to modify the score: | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* - 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. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @param scoreConfiguration The configuration block for [ScoreSearchOptionDsl] | ||||||||||||||||||||||
* @see <a href="https://www.mongodb.com/docs/atlas/atlas-search/score/modify-score">Modify the Score</a> | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
fun score(scoreConfiguration: ScoreSearchOptionDsl.() -> Unit) { | ||||||||||||||||||||||
document["score"] = ScoreSearchOptionDsl().apply(scoreConfiguration).get() | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
internal fun build() = Document("near", document) | ||||||||||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is missing
@param
docs for origin and pivot method :)