Docs Menu
Docs Home
/ / /
루비 드라이버
/

Atlas search 인덱스

이 가이드에서는 Ruby 드라이버를 사용하여 Atlas Search 및 Atlas Vector Search 인덱스를 프로그래밍 방식으로 관리하는 방법을 학습할 수 있습니다.

Atlas Search 기능을 사용하면 MongoDB Atlas에서 호스팅되는 컬렉션에서 전체 텍스트 검색을 수행할 수 있습니다. Atlas Search에 대해 자세히 알아보려면 Atlas Search 개요를 참조하세요.

Atlas Vector Search를 사용하면 MongoDB Atlas에 저장된 벡터 임베딩에 대해 시맨틱 검색을 수행할 수 있습니다. Atlas Vector Search에 대해 자세히 알아보려면 Atlas Vector Search 개요를 참조하세요.

다음 메서드를 호출하여 Atlas Search 인덱스를 관리할 수 있습니다.

  • search_indexes#create_one

  • search_indexes#create_many

  • search_indexes#update_one

  • search_indexes#drop_one

참고

Atlas Search 및 Vector Search 인덱스 관리는 비동기적입니다.

Ruby 드라이버는 Atlas Search 및 Vector Search 검색 인덱스를 비동기적으로 관리합니다. 다음 섹션에서 설명하는 메서드는 서버 응답을 즉시 반환하지만, 검색 인덱스의 변경은 배경에서 이루어지므로 일정 시간이 지나야 완료될 수 있습니다.

다음 섹션에서는 코드 예시를 제공하여 이전의 각 명령을 사용하는 방법을 알아봅니다.

단일 Atlas Search 또는 벡터 검색 인덱스를 생성하려면 search_indexes#create_one 메서드를 사용하세요. 여러 인덱스를 만들려면 search_indexes#create_many 메서드를 사용합니다. 두 메서드는 즉시 반환되며, 인덱스는 배경에서 비동기적으로 생성됩니다.

다음 코드 예시는 인덱스 정의와 선택 사항인 인덱스 이름을 제공하여 Atlas Search 인덱스를 생성하는 방법을 보여줍니다.

# Creates indexes on all dynamically indexable fields with a default index name
collection.search_indexes.create_one(
{ mappings: { dynamic: true } }
)
# Creates an index on the specified field with the specified index name
index_definition = {
mappings: {
dynamic: false,
fields: {
fullplot: { type: 'string' }
}
}
}
collection.search_indexes.create_one(index_definition, name: 'mySearchIndex')

참고

기본적으로 드라이버는 type 매개변수를 전달하지 않으면 Atlas Search 인덱스를 생성합니다. 벡터 검색 인덱스를 생성하려면 create_one을 호출할 때 type 매개변수를 'vectorSearch'로 설정해야 합니다.

search_indexes#create_many를 사용하여 인덱스 사양 배열을 제공함으로써 여러 Atlas Search 또는 Vector Search 인덱스를 생성할 수 있습니다. 각 인덱스 사양에는 다음 구성 요소가 포함되어야 합니다.

  • definition 매개변수: 인덱스를 정의합니다.

  • name 매개변수: 인덱스 이름을 지정합니다.

  • type 매개 변수: 인덱스('search' 또는 'vectorSearch') 유형 지정

다음 코드 예시는 한 번의 호출로 Atlas Search 및 Vector Search 인덱스를 만드는 방법을 보여줍니다.

index_spec_1 = {
name: 'searchIndex_plot',
type: 'search',
definition: {
mappings: {
dynamic: false,
fields: {
plot: { type: 'string' }
}
}
}
}
index_spec_2 = {
name: 'vsIndex_plot_embedding',
type: 'vectorSearch',
definition: {
fields: [
{
type: "vector",
path: "plot_embedding",
numDimensions: 1536,
similarity: "dotProduct"
}
]
}
}
collection.search_indexes.create_many([index_spec_1, index_spec_2])

긴 인덱스 정의의 경우, 메서드 호출 외부에서 인덱스 정의를 설정하는 것이 유용합니다. 인덱스 정의 구문에 대해 자세히 알아보려면 Atlas 매뉴얼의 Atlas Search 인덱스 구문 검토 가이드를 참조하세요.

Atlas Search 또는 벡터 검색 인덱스를 업데이트하려면 search_indexes#update_one 메서드를 사용하세요.

인덱스를 업데이트하려면 새로운 인덱스 정의를 제공해야 합니다. 업데이트할 인덱스를 지정하려면 인덱스의 name 또는 id를 사용해야 합니다. 다음 코드에서는 Atlas Search 인덱스를 업데이트하는 방법을 보여줍니다.

updated_definition = {
mappings: {
dynamic: false,
fields: { fullplot: { type: 'string' } }
}
}
# Specifies the index to update by using the index name
collection.search_indexes.update_one(updated_definition, name: 'searchIndex_plot')
# Specifies the index to update by using the index id
collection.search_indexes.update_one(updated_definition, id: <index id>)

Atlas Search 또는 벡터 검색 인덱스를 삭제하려면 search_indexes#drop_one 메서드를 사용하세요.

인덱스를 삭제하려면 인덱스의 id 또는 name을 제공해야 합니다. 다음 코드에서는 컬렉션에서 검색 인덱스를 삭제하는 방법을 보여줍니다.

# Specifies the index to delete by using the index name
collection.search_indexes.drop_one(name: 'searchIndex_plot')
# Specifies the index to delete by using the index id
collection.search_indexes.drop_one(id: <index id>)

search_indexes 객체를 사용하여 컬렉션의 각 Atlas Search 및 벡터 검색 인덱스의 전체 인덱스 사양을 나열할 수 있습니다.

puts collection.search_indexes.collect(&:to_json)

각 인덱스의 인덱스 사양에서 개별 필드를 나열하려면 search_indexes 객체를 반복하세요.

collection.search_indexes.each do |index_spec|
p index_spec['id']
p index_spec['name']
p index_spec['status']
p index_spec['queryable']
p index_spec['latestDefinition']
end

MongoDB Atlas Search에 대해 자세히 알아보려면 Atlas Search 문서를 참조하세요.

이 가이드에서 설명하는 메서드에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.

돌아가기

멀티키

이 페이지의 내용