Atlas Search
개요
In this guide, you can learn how to use the Java driver to run Atlas Search queries on a collection. Atlas Search enables you to perform full-text searches on collections hosted on MongoDB Atlas. Atlas Search indexes specify the behavior of the search and which fields to index.
샘플 데이터
The example in this guide uses the movies
collection in the sample_mflix
database from the Atlas sample datasets. To learn how to
create a free MongoDB Atlas cluster and load the sample datasets, see the
Get Started with Atlas guide.
Atlas Search 쿼리 실행
This section shows how to create an aggregation pipeline to run an
Atlas Search query on a collection. You can use the Aggregates.search()
builder
method to create a $search
pipeline stage, which specifies the search
criteria. Then, call the aggregate()
method and pass your pipeline as a parameter.
팁
To learn more about aggregation operations and builders, see the 집계 guide.
Before running an Atlas Search query, you must create an Atlas Search index on your collection. To learn how to programmatically create an Atlas Search index, see the Atlas Search 및 Vector Search 인덱스 section in the Indexes guide.
Atlas Search Example
This example runs an Atlas Search query by performing the following actions:
Constructs a
$search
stage by using theAggregates.search()
builder method, instructing the driver to query for documents in which thetitle
field contains the word"Alabama"
Constructs a
$project
stage by using theAggregates.project()
builder method, instructing the driver to include thetitle
field in the query resultsPasses the pipeline stages to the
aggregate()
method and prints the results
collection.aggregate( Arrays.asList( Aggregates.search(SearchOperator.text( SearchPath.fieldPath("title"), "Alabama")), Aggregates.project(Projections.include("title")) ) ).forEach(doc -> System.out.println(doc.toJson()));
{"_id": {"$oid": "..."}, "title": "Alabama Moon"} {"_id": {"$oid": "..."}, "title": "Crazy in Alabama"} {"_id": {"$oid": "..."}, "title": "Sweet Home Alabama"}
팁
Java Driver Atlas Search Examples
To view more examples that use the Java driver to perform Atlas Search queries, see Atlas Search Tutorials in the Atlas documentation.
추가 정보
Atlas Search 에 대해 자세히 학습 Atlas 설명서에서 Atlas Search 참조하세요.
API 문서
To learn more about the methods mentioned in this guide, see the following API documentation: