Atlas Search

Atlas Searchオペレーター、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は集約パイプラインの最初のステージである必要があるため、一般的なfields(...).end()フローとは分離されています。インデックスを省略した.search()も使用できます。

Searchオペレーター

• ルートビルダー: text, phrase, autocomplete, equals, exists, in, range, compound

• 再利用可能なクローズ: SearchOperators.text/phrase/autocomplete/equals/in/exists/range

• Driver-nativeルートオペレーター: operator(SearchOperator)

• 名前付きDriverラッパー: 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クローズとポスト検索フィールドの違い

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

search().fields(...)は$search内部のフィルターではなく、$searchの後の一般的な$matchです。Searchインデックス段階で候補を制限するには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()と一般的なソートは呼び出した順序で優先順位が決まります。安定したページネーションのために、スコアとともに安定したユニークフィールドのソートを行うことをお勧めします。

ハイライト / スコアの詳細 / シーケンストークン

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

デフォルトのエイリアスは highlights, scoreDetails, searchSequenceToken です。 addFieldsScoreDetails() は検索オプションのスコア詳細も有効にします。

ページング / シーケンストークン / カウント

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(...) とスコア閾値を反映した最終パイプラインカウントです。

count().executeSearchMeta()

$searchMeta メタデータカウントです。 SearchCountType.TOTAL/LOWER_BOUND をサポートし、post-search $match は実行しません。

existsQuery().execute()

検索パイプラインの結果が1件以上存在するか確認します。

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 メタデータカウントパイプラインには適用されません。新しいドライバ機能がDSL便利APIより先に追加されると、 operator, driverOptions, stage, SortSpec.driver, customizeAggregation を優先して使用します。

© 2026 Byeolnaerim. 無断転載を禁じます。紹介プライバシーポリシー