Atlas Search
กำหนดค่า Atlas Search operator, compound/filter, score, highlight, sequence token, paging/count ตามมาตรฐาน 1.0.0
Atlas Search พื้นฐาน
Flux<Article> results = dsl
.executeEntity(Article.class, MongoTemplateName.FRONT)
.search("articles_default")
.text(text -> text
.path("title")
.query("reactive mongo dsl")
)
.findAll()
.execute();$search และ $searchMeta ต้องเป็น stage แรกของ aggregation pipeline ดังนั้นจึงแยกออกจากการไหลทั่วไปของ fields(...).end() สามารถใช้ .search() โดยไม่ต้องระบุ index ได้
Search operator
• root builder: text, phrase, autocomplete, equals, exists, in, range, compound
• clause ที่นำกลับมาใช้ใหม่: SearchOperators.text/phrase/autocomplete/equals/in/exists/range
• Driver-native root operator: operator(SearchOperator)
• named Driver wrapper: SearchOperators.driver(name, operator)
.search("articles_default")
.compound(compound -> compound
.mustText("title", text -> text.query("mongodb"))
.shouldText("summary", text -> text.query("mongodb"))
.filterEquals("status", eq -> eq.value("PUBLISHED"))
.mustNotExists("deletedAt")
.minimumShouldMatch(0)
)
.findAll()
.execute();compound รองรับ must, mustNot, should, filter, minimumShouldMatch, score และสามารถส่ง AtlasSearchOperator ทั่วไปได้โดยตรง
Text / Phrase / Autocomplete
.text(text -> text
.paths("title", "summary")
.query("reactive mongo dsl")
.matchCriteria(SearchMatchCriteria.ALL)
.fuzzy(1, 1, 50)
.score(SearchScoreSpec.boost(3.0))
)• text: path(s), query(ies), fuzzy, matchCriteria, synonyms, score
• phrase: path(s), query(ies), slop, synonyms, score
• autocomplete: single path, query(ies), tokenOrder, fuzzy, score
• ไม่ใช้ fuzzy(...) และ synonyms(...) พร้อมกัน
ความแตกต่างระหว่าง Search clause และ post-search fields
.search("articles_default")
.text(text -> text.path("title").query("mongodb"))
.fields(
pair("deleted", false),
pair("status", "PUBLISHED")
)
.findAll()
.execute();search().fields(...) ไม่ใช่ filter ภายในของ $search แต่เป็น $match ทั่วไปหลังจาก $search หากต้องการจำกัดผู้สมัครในขั้นตอน Search index ให้ใช้ compound.filter(...)
$search
-> stage(Bson) if configured
-> $match (fields)
-> metadata addFields / score match
-> paging
-> projectionScore และการจัดเรียง
.search("articles_default")
.text(text -> text.path("title").query("mongodb"))
.addFieldsScore("searchScore")
.matchScoreGte(1.5)
.scoreDesc()
.sorts(sort -> sort.desc("publishedAt").desc("id"))
.findAll()
.execute();scoreDesc()/scoreAsc() และการจัดเรียงทั่วไปจะมีลำดับความสำคัญตามลำดับที่เรียกใช้ เพื่อให้การแบ่งหน้าเสถียร ควรจัดเรียงด้วย score และฟิลด์ที่ไม่ซ้ำกันที่เสถียร
Highlight / รายละเอียด score / sequence token
.search("articles_default")
.text(text -> text.path("title").query("mongodb"))
.highlight(highlight -> highlight
.path("title")
.maxCharsToExamine(100_000)
.maxNumPassages(3)
)
.addFieldsHighlights()
.addFieldsScoreDetails()
.addFieldsSequenceToken()
.findAll()
.execute();อัลลิอาสพื้นฐานคือ highlights, scoreDetails, searchSequenceToken ฟังก์ชัน addFieldsScoreDetails() จะเปิดใช้งานรายละเอียดคะแนนในตัวเลือกการค้นหา
การแบ่งหน้า / โทเค็นลำดับ / จำนวน
Mono<PageResult<Article>> page = dsl
.executeEntity(Article.class, key)
.search("articles_default")
.text(text -> text.path("title").query("mongodb"))
.paging(0, 20)
.findAll()
.executePage();
// sequence token
.searchAfter(token)
// or
.searchBefore(token)count().execute()
จำนวนพายป์ไลน์สุดท้ายที่สะท้อนถึง post-search fields(...) และเกณฑ์คะแนน
count().executeSearchMeta()
จำนวนเมตาดาต้า $searchMeta รองรับ SearchCountType.TOTAL/LOWER_BOUND และ $match หลังการค้นหาไม่ถูกดำเนินการ
existsQuery().execute()
ตรวจสอบว่าผลลัพธ์ของพายป์ไลน์การค้นหามีมากกว่าหนึ่งรายการหรือไม่
การขยาย Driver-native
.search("search-index")
.operator(driverSearchOperator)
.driverOptions(options -> options.option("someNewOption", value))
.stage(Aggregates.score("$rating"))
.findAll()
.execute();stage(Bson) จะถูกเพิ่มหลังจาก $search และจะไม่ถูกนำไปใช้ในพายป์ไลน์จำนวนเมตาดาต้า $searchMeta ของ executeSearchMeta() หากฟีเจอร์ใหม่ของ Driver ถูกเพิ่มก่อน API ความสะดวก DSL จะใช้ operator, driverOptions, stage, SortSpec.driver, customizeAggregation ก่อน