Atlas Search

Atlas Search operator, compound/filter, score, highlight, sequence token, paging/count와 Driver-native 확장 경로를 1.0.0 기준으로 구성합니다.


기본 Atlas Search

java
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는 aggregation pipeline의 첫 stage여야 하므로 일반 fields(...).end() 흐름과 분리되어 있습니다. index를 생략한 .search()도 사용할 수 있습니다.

Search operator

• root builder: text, phrase, autocomplete, equals, exists, in, range, compound

• reusable clause: SearchOperators.text/phrase/autocomplete/equals/in/exists/range

• Driver-native root operator: operator(SearchOperator)

• named Driver wrapper: SearchOperators.driver(name, operator)

java
.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

java
.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의 차이

java
.search("articles_default")
.text(text -> text.path("title").query("mongodb"))
.fields(
    pair("deleted", false),
    pair("status", "PUBLISHED")
)
.findAll()
.execute();

search().fields(...)는 $search 내부 filter가 아니라 $search 뒤의 일반 $match입니다. Search index 단계에서 후보를 제한하려면 compound.filter(...)를 사용합니다.

text
$search
-> stage(Bson) if configured
-> $match (fields)
-> metadata addFields / score match
-> paging
-> projection

Score와 정렬

java
.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()와 일반 sort는 호출한 순서대로 priority가 정해집니다. 안정적인 pagination을 위해 score와 함께 stable unique field 정렬을 두는 것이 좋습니다.

Highlight / score details / sequence token

java
.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();

기본 alias는 highlights, scoreDetails, searchSequenceToken입니다. addFieldsScoreDetails()는 Search option의 score details도 활성화합니다.

Paging / sequence token / count

java
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(...)와 score threshold까지 반영한 최종 pipeline count입니다.

count().executeSearchMeta()

$searchMeta metadata count입니다. SearchCountType.TOTAL/LOWER_BOUND를 지원하며 post-search $match는 실행하지 않습니다.

existsQuery().execute()

Search pipeline 결과가 한 건 이상 존재하는지 확인합니다.

Driver-native 확장

java
.search("search-index")
.operator(driverSearchOperator)
.driverOptions(options -> options.option("someNewOption", value))
.stage(Aggregates.score("$rating"))
.findAll()
.execute();

stage(Bson)은 $search 직후에 들어가며 executeSearchMeta()의 $searchMeta metadata-count pipeline에는 적용되지 않습니다. 새 Driver 기능이 DSL convenience API보다 먼저 추가되면 operator, driverOptions, stage, SortSpec.driver, customizeAggregation을 우선 사용합니다.

© 2026 Byeolnaerim. All rights reserved.소개개인정보처리방침