모듈: Mongoid::SearchIndexable::ClassMethods

다음에 정의됨:
lib/mongoid/search_indexable.rb

개요

기능의 클래스 수준 메서드에 대한 구현입니다.

인스턴스 메서드 요약 접기

인스턴스 메서드 세부 정보

#auto_embed_search(text, 인덱스: nil, 경로: nil, limit: 10, num_candidates: nil, 필터: nil, exact: false, model: nil, 파이프라인: []) ⇒ 배열<Mongoid::document>

자동 임베딩을 사용하여 Atlas Vector Search 쿼리 수행합니다. Atlas 쿼리 시 제공된 텍스트에서 쿼리 벡터를 생성합니다. 미리 계산된 임베딩이 필요하지 않습니다.

반환된 각 document 에는 관련성 점수로 채워진 vector_search_score 속성이 있습니다. vector_search와(과) 달리, 인덱싱된 텍스트 필드 반환된 문서에 유지됩니다.

예시:

텍스트로 검색합니다.

Article.auto_embed_search('machine learning', limit: 5)

등가 최근접 이웃 검색 (numCandidates 없음).

Article.auto_embed_search('deep learning', exact: true, limit: 5)

매개변수:

  • text (string)

    쿼리 text입니다.

  • index (string | 기호 | nil) (기본값: nil)

    사용할 자동 임베딩 인덱스 의 이름입니다(모델에 하나만 선언된 경우 선택 사항).

  • 경로 (string | 기호 | nil) (기본값: nil)

    인덱싱된 텍스트 필드 경로 ( 인덱스 정의에서 모호하지 않은 경우 선택 사항).

  • limit (정수) (기본값: 10)

    최대 결과 수(기본값: 10).

  • num_candidates (정수 | nil) (기본값: nil)

    근사 최근접 이웃 검색 후보자 ; 기본값은 limit * 10입니다. 정확하면 무시됩니다: true.

  • 필터 (해시 | nil) (기본값: nil)

    사전 필터링을 위한 선택 사항 MongoDB 필터.

  • 정확한 (true | false) (기본값은 false)

    ANN(기본값: false) 대신 등가 최근접 이웃(ENN) 검색을 사용합니다. true이면 numCandidates가 생략됩니다.

  • 모델 (string | nil) (기본값: nil)

    쿼리 시간 embedding model 재정의.

  • 파이프라인 (배열) (기본값: [])

    벡터 검색 및 점수 프로젝션 이후에 추가 집계 단계가 추가되었습니다.

반환합니다:

  • (Array<Mongoid::Document>)

    각각 vector_search_score 속성이 채워진 일치하는 document.



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# 파일 'lib/mongoid/search_indexable.rb', 줄 352

def auto_embed_search(text, index: nil, 경로: nil, limit: 10, num_candidates: nil, 필터: nil, 정확: 거짓, model: nil, 파이프라인: []) # Rubocop:disable Metrics/ParameterLists
  resolve_index, resolve_path = resolve_auto_embed_index(index, 경로)

  vs_options = {
    '인덱스' => resolve_index,
    '경로' => resolve_path,
    '쿼리' => { 'text' => text },
    'limit' => limit
  }
  vs_options['numCandidates'] = num_candidates || (limit * 10) 하지 않는 한 정확한
  vs_options['정확한'] = true 만약 정확한
  vs_options['필터하다'] = 필터 만약 필터
  vs_options['model'] = 모델 만약 모델

  agg_pipeline = [
    { '$vectorSearch' => vs_options },
    { '$addFields'    => { 'vector_search_score' => { '$meta' => 'vectorSearchScore' } } }
  ]
  agg_pipeline.concat(배열(파이프라인))

  컬렉션.집계(agg_pipeline).map { |doc| 인스턴스화(doc) }
end

#create_search_indexesArray<String>

등록된 모든 검색 인덱스의 생성을 요청합니다. 검색 인덱스는 비동기적으로 생성되며 완전히 사용하는 데 몇 분 정도 걸릴 수 있습니다.

반환합니다:

  • (Array<String>)

    검색 인덱스의 이름입니다.



155
156
157
158
159
# 파일 'lib/mongoid/search_indexable.rb', 줄 155

def create_search_indexes
  반환 만약 search_index_specs.비어 있나요?

  컬렉션.search_indexes.create_many(search_index_specs)
end

#remove_search_index(name: nil, ID: nil) ⇒ 객체

지정된 이름 또는 ID로 지정된 검색 인덱스 를 제거합니다. 이름 또는 ID 중 하나를 입력해야 하지만 둘 다 입력할 수는 없습니다.

매개변수:

  • 이름 (string | nil) (기본값: nil)

    제거 인덱스 의 이름

  • id (string | nil) (기본값: nil)

    제거 인덱스 의 ID



197
198
199
200
201
202
203
204
# 파일 'lib/mongoid/search_indexable.rb', 줄 197

def remove_search_index(이름: nil, ID: nil)
  로거.정보(
    "검색 인덱스: 컬렉션  ID' #{collection. 컬렉션}'에서 검색 인덱스 '#{ name ||ID } '
  )

  컬렉션.search_indexes.drop_one(이름: name, ID: ID)

#remove_search_indexes객체

참고:

검색 인덱스만 제거 할 수 있다면 좋을 것입니다.

등록된 모든 검색 인덱스의 제거를 요청합니다. 검색 인덱스는 비동기적으로 제거되며 완전히 삭제되는 데 몇 분 정도 걸릴 수 있습니다.

즉, 모델에서 선언된 인덱스이지만 모델이 인덱스 이름을 지정하지 못할 수 있기 때문에 해당 인덱스의 이름이나 ID를 알 수 있다고 보장할 수 없습니다. 그러나 모델이 원하는 모든 검색 인덱스를 일대일로 선언하는 것이 의도이므로 모든 검색 인덱스를 제거해도 충분하다고 가정하는 것은 무리가 아닙니다. 특정 인덱스 또는 인덱스 설정하다 을 대신 제거해야 하는 경우 search_indexes.each를 remove_search_index와 함께 사용하는 것이 좋습니다.



218
219
220
221
222
# 파일 'lib/mongoid/search_indexable.rb', 줄 218

def remove_search_indexes
  search_indexes. do |사양|
    remove_search_index ID: 사양['ID']
  end
end

#search_index(name_or_defn, defn = nil) ⇒ 객체

제공된 단일 또는 복합 키에 대한 인덱스 정의를 추가합니다.

예시:

기본 인덱스 를 만듭니다.

class Person
  include Mongoid::Document
  field :name, type: String
  search_index({ ... })
  search_index :name_of_index, { ... }
end

매개변수:

  • name_or_defn (기호 | string | 해시)

    정의할 인덱스 의 이름이거나 인덱스 정의입니다.

  • defn (해시) (기본값: nil)

    검색 인덱스 정의입니다.



237
238
239
240
241
242
243
# 파일 'lib/mongoid/search_indexable.rb', 줄 237

def search_index(name_or_defn, defn = nil)
  이름 = name_or_defn
  이름, defn = nil, 이름 만약 이름.is_a?(해시)

  사양 = { 정의: defn }. { |s| s[:name] = 이름.to_s 만약 이름 }
  search_index_specs.push(사양)
end

#search_indexes(options = {}) ⇒ 객체

현재 모델의 컬렉션 에서 사용할 수 있는 검색 인덱스를 쿼리하기 위한 편리한 메서드입니다.

매개변수:

  • 옵션 (해시) (기본값: {})

    검색 인덱스 쿼리 에 전달할 옵션입니다.

옵션 해시(options):

  • :id (string)

    쿼리 할 특정 인덱스 의 ID(선택 사항)

  • :name (string)

    쿼리 할 특정 인덱스 의 이름(선택 사항)

  • :aggregate (해시)

    집계 명령에 전달할 옵션 해시(선택 사항)



188
189
190
# 파일 'lib/mongoid/search_indexable.rb', 줄 188

def search_indexes(옵션 = {})
  컬렉션.search_indexes(옵션)
end

#vector_search(vector, 인덱스: nil, 경로: nil, limit: 10, num_candidates: nil, 필터: nil, 파이프라인: []) ⇒ 배열<Mongoid::Document>

Atlas Vector Search 쿼리 수행하고 일치하는 문서를 반환합니다. 반환된 각 document 에는 관련성 점수로 채워진 vector_search_score 속성이 있습니다.

벡터 필드 (path: 에서 제공)는 벡터가 크고 검색 후 거의 유용하지 않기 때문에 기본값 으로 반환된 문서에서 제외됩니다.

예시:

명시적 쿼리 벡터로 검색합니다.

Article.vector_search(embedding, limit: 5, filter: { status: 'published' })

매개변수:

  • 벡터 (Array<Numeric>)

    쿼리 벡터입니다.

  • index (string | 기호 | nil) (기본값: nil)

    사용할 벡터 검색 인덱스 의 이름입니다(모델에 하나만 선언된 경우 선택 사항).

  • 경로 (string | 기호 | nil) (기본값: nil)

    저장된 벡터가 포함된 필드 ( 인덱스 정의에서 모호하지 않은 경우 선택 사항).

  • limit (정수) (기본값: 10)

    최대 결과 수입니다(기본값: 10).

  • num_candidates (정수 | nil) (기본값: nil)

    근사 최근접 이웃 검색 시 고려해야 할 후보의 수입니다. 기본값은 limit * 10입니다.

  • 필터 (해시 | nil) (기본값: nil)

    점수를 매기기 전에 후보를 사전 필터링하는 선택 사항인 MongoDB 필터하다 입니다.

  • 파이프라인 (배열) (기본값: [])

    벡터 검색 및 점수 프로젝션 후에 추가할 추가 집계 단계입니다.

반환합니다:

  • (Array<Mongoid::Document>)

    각각 vector_search_score 속성이 채워진 일치하는 document.



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# 파일 'lib/mongoid/search_indexable.rb', 줄 298

def vector_search(벡터, index: nil, 경로: nil, limit: 10, num_candidates: nil, 필터: nil, 파이프라인: []) # Rubocop:disable Metrics/ParameterLists
  resolve_index, resolve_path = resolve_vector_index(index, 경로)
  num_candidates ||= limit * 10

  vs_options = {
    '인덱스' => resolve_index,
    '경로' => resolve_path,
    'queryVector' => 벡터,
    'numCandidates' => num_candidates,
    'limit' => limit
  }
  vs_options['필터하다'] = 필터 만약 필터

  agg_pipeline = [
    { '$vectorSearch' => vs_options },
    { '$addFields'    => { 'vector_search_score' => { '$meta' => 'vectorSearchScore' } } },
    { '$ 프로젝트'      => { resolve_path => 0 } }
  ]
  agg_pipeline.concat(배열(파이프라인))

  컬렉션.집계(agg_pipeline).map { |doc| 인스턴스화(doc) }
end

#vector_search_index(name_or_defn, defn = nil) ⇒ 객체

벡터 검색 인덱스 정의를 추가합니다. 또한 모델이 처음 호출될 때 읽기 전용 vector_search_score 필드 정의하며, 이 필드는 vector_search에서 반환된 문서에 채워집니다.

예시:

벡터 검색 인덱스를 생성합니다.

class Person
  include Mongoid::Document
  vector_search_index({ fields: [...] })
  vector_search_index :my_vector_index, { fields: [...] }
end

매개변수:

  • name_or_defn (기호 | string | 해시)

    정의할 인덱스 의 이름이거나 인덱스 정의입니다.

  • defn (해시) (기본값: nil)

    벡터 검색 인덱스 정의입니다.



259
260
261
262
263
264
265
266
267
268
269
270
# 파일 'lib/mongoid/search_indexable.rb', 줄 259

def vector_search_index(name_or_defn, defn = nil)
  이름 = name_or_defn
  이름, defn = nil, 이름 만약 이름.is_a?(해시)

  사양 = { 유형: 'vectorSearch', 정의: defn }. { |s| s[:name] = 이름.to_s 만약 이름 }
  search_index_specs.push(사양)

  반환 만약 필드.키?('vector_search_score')

  필드 :vector_search_score, 유형: Float
  attr_readonly :vector_search_score
end

#wait_for_search_indexes(names, 간격: 5) {|SearchIndexable::Status| ... } ⇒ 객체

명명된 검색 인덱스가 생성될 때까지 기다립니다.

매개변수:

  • 이름 (Array<String>)

    대기할 인덱스 이름 목록

  • interval (정수) (기본값: 5)

    다시 폴링하기 전에 대기할 시간(초)입니다(진행률 콜백 이 제공될 때만 사용됨).

수율:



168
169
170
171
172
173
174
175
176
# 파일 'lib/mongoid/search_indexable.rb', 줄 168

def wait_for_search_indexes(이름, interval: 5)
  루프 do
    상태 = 상태.신규(get_indexes(이름))
    yield 상태 만약 block_given?
    휴식 만약 상태.준비됐나요?

    sleep interval
  end
end