Docs Menu
Docs Home
/ /

MongoDB Search 및 MongoDB Vector Search 인덱스

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

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

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

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

  • search_indexes#create_one

  • search_indexes#create_many

  • search_indexes#update_one

  • search_indexes#drop_one

참고

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

Ruby 운전자 MongoDB Search 및 MongoDB Vector Search 인덱스를 비동기적으로 관리합니다. 다음 섹션에서 설명하는 메서드는 서버 응답을 즉시 반환하지만 검색 인덱스에 대한 변경 사항은 배경 에서 수행되며 잠시 후까지 완료되지 않을 수 있습니다.

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

단일 MongoDB Search 또는 MongoDB Vector Search 인덱스 만들려면 search_indexes#create_one 메서드를 사용합니다. 여러 인덱스를 만들려면 search_indexes#create_many 메서드를 사용합니다. 두 메서드 모두 즉시 반환되지만 인덱스는 배경 에서 비동기적으로 생성됩니다.

다음 코드 예시 인덱스 정의와 인덱스 이름(선택 사항)을 제공하여 MongoDB 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 매개 변수를 전달하지 않으면 MongoDB Search 인덱스 생성합니다. MongoDB Vector Search 인덱스 만들려면 create_one를 호출할 때 type 매개 변수를 'vectorSearch' 로 설정하다 해야 합니다.

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

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

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

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

다음 코드 예시 한 번의 호출로 MongoDB Search 인덱스와 MongoDB 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 매뉴얼의 MongoDB Search 인덱스 구문 검토 또는 MongoDB Vector Search용 필드 인덱싱 방법 가이드를 참조하세요.

MongoDB Search 또는 MongoDB Vector Search 인덱스 업데이트 하려면 search_indexes#update_one 메서드를 사용합니다.

인덱스 업데이트 하려면 새 인덱스 정의를 제공해야 합니다. 인덱스 의 name 또는 id 을 사용하여 업데이트 하려는 인덱스 지정해야 합니다. 다음 코드는 MongoDB 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>)

MongoDB Search 또는 MongoDB Vector 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 객체 사용하여 컬렉션 에 있는 각 MongoDB Search 및 MongoDB Vector 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 Search에 대해 자세히 학습 MongoDB Search 설명서를 참조하세요.

MongoDB Vector Search에 대해 자세히 학습 Ruby 드라이버의 MongoDB Vector Search 쿼리 실행 가이드 또는 MongoDB Vector Search 문서를 참조하세요.

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

돌아가기

멀티키

이 페이지의 내용