模块:Mongoid::SearchIndexable::ClassMethods

定义于:
lib/mongoid/search_indexable.rb

Overview

该功能的类级方法的实现。

实例方法摘要折叠

实例方法详细信息

# auto_embed_search (text, 索引:nil,path:nil,limit:10,num_candidates:nil, 过滤器:nil,exact:false,model:nil, 管道:[]) ⇒ Array<Mongoid::Document>

使用自动嵌入执行Atlas Vector Search查询。Atlas在查询时根据提供的文本生成查询向量;无需预先计算的嵌入。

每个返回的 document 都有一个用其相关性分数填充的 vector_search_score 属性。与 vector_search 不同,索引文本字段会保留在返回的 document 中。

例子:

按文本搜索。

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

精确最近邻 (ENN) 搜索(无 numCandidates)。

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

参数:

  • text ( string )

    查询文本。

  • 索引(index) (String | Symbol | nil) (默认为: nil

    要使用的自动嵌入索引的名称(当model上仅声明一个时可选)。

  • 路径 (String | Symbol | nil) (默认为: nil

    索引文本字段路径(Field Path)(如果索引定义明确无歧义,则可选)。

  • limit 整数 (默认为: 10

    最大结果数(默认:10)。

  • num_candidates ( Integer | nil ) (默认为: nil

    近似最近邻 (ANN)搜索候选;默认为限制 * 10。精确时忽略:true。

  • 筛选器 哈希 | nil (默认为: nil

    用于预过滤的可选MongoDB过滤器。

  • 精确 ( true | false ) (默认为: false

    使用精确最近邻 (ENN)搜索而不是 ANN(默认:false)。如果为 true,则省略 numCandidates。

  • 模型 ( string | nil ) (默认为: nil

    查询时嵌入模型覆盖。

  • 管道 数组 (默认为: []

    在向量搜索和分数投影之后附加的其他聚合阶段。

返回:

  • ( Array< Mongoid::Document > )

    匹配的 document,每个 document 都有一个填充的 vector_search_score 属性。



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

def auto_embed_search(text, index: nil, path: nil, limit: 10, num_candidates: nil, 过滤器: nil, 精确: false, 模型: nil, 管道: []) # rubocop:disable Metrics/ParameterLists
  solved_index, solved_path = resolve_auto_embed_index(索引(index), 路径)

  vs_options = {
    ' index ' => solved_index,
    'path' => solved_path,
    '查询' => { 'text' => text },
    'limit' => limit
  }
  vs_options['numCandidates'] = num_candidates || (limit * 10) 除非 精确
  vs_options['exact'] = true if 精确
  vs_options[' 过滤 '] = 筛选器 if 筛选器
  vs_options['model'] = 模型 if 模型

  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
# File 'lib/mongoid/search_indexable.rb', line 155

def create_search_indexes
  return if 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
# File 'lib/mongoid/search_indexable.rb', line 197

def remove_search_index(名称: nil, ID : nil)
  记录器.信息(
    " 搜索索引: 正在 删除 ID' #{ collection . name } ' 上的 搜索 集合 ' #{ { name || ID } ' " \ " 。 ")集合 search_indexes  drop_one ( name: name , ID : ID ) end

# remove_search_indexes对象

注意:

如果这样可以仅删除搜索索引,那就太好了

请求删除所有已注册的搜索索引。 请注意,搜索索引是异步删除的,可能需要几分钟才能完全删除。

已在model上声明,但由于model可能未命名索引,因此我们不能保证知道相应索引的名称或 ID。不过,可以合理地假设,model 的意图是一对一地声明所有所需的搜索索引,因此删除所有搜索索引就足够了。如果需要删除特定索引或索引设立,请考虑将 search_indexes.each 与 remove_search_index 一起使用。



218
219
220
221
222
# File 'lib/mongoid/search_indexable.rb', line 218

def remove_search_indexes
  search_indexes. do |spec|
    remove_search_index ID : spec[' 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
# File 'lib/mongoid/search_indexable.rb', line 237

def search_index(name_or_defn, defn = nil)
  名称 = name_or_defn
  名称, defn = nil, 名称 if 名称.is_a?(哈希)

  spec = { 定义: defn }.点击 { |s| s[:name] = 名称.to_s if 名称 }
  search_index_specs.推动(spec)
end

# search_indexes (options = {}) ⇒对象

查询当前model集合上可用搜索索引的便捷方法。

参数:

  • 选项 哈希 (默认为: {}

    要传递给搜索索引查询的选项。

选项哈希 ( options ):

  • :id string

    要查询的特定索引的 ID(可选)

  • :name string

    要查询的特定索引的名称(可选)

  • :aggregate 哈希

    要传递给聚合命令的选项哈希(可选)



188
189
190
# File 'lib/mongoid/search_indexable.rb', line 188

def search_indexes(选项 = {})
  集合.search_indexes(选项)
end

# vector_search (vector, 索引: nil, path: nil, limit: 10, num_candidates: nil, 过滤器: nil, 管道: []) ⇒ 数组<document>

执行Atlas Vector Search查询并返回匹配的document。每个返回的 document 都有一个用其相关性分数填充的 vector_search_score 属性。

默认下,从返回的 document 中排除向量字段(由 path: 给出),因为向量很大,检索后几乎没有用处。

例子:

按显式查询向量搜索。

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

参数:

  • 向量 ( Array<Numeric> )

    查询向量。

  • 索引(index) (String | Symbol | nil) (默认为: nil

    要使用的向量搜索索引的名称(如果model上只声明了一个,则为可选)。

  • 路径 (String | Symbol | nil) (默认为: nil

    包含存储的向量的字段(如果索引定义不含糊,则可选)。

  • limit 整数 (默认为: 10

    结果的最大数量(默认:10)。

  • num_candidates ( Integer | nil ) (默认为: nil

    近似最近邻 (ANN)搜索期间要考虑的候选数量;默认为限制 * 10。

  • 筛选器 哈希 | nil (默认为: nil

    可选的MongoDB过滤器,用于在评分之前对候选对象进行预筛选。

  • 管道 数组 (默认为: []

    在向量搜索和分数投影之后附加的其他聚合阶段。

返回:

  • ( Array< Mongoid::Document > )

    匹配的 document,每个 document 都有一个填充的 vector_search_score 属性。



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

def vector_search(向量, index: nil, path: nil, limit: 10, num_candidates: nil, 过滤器: nil, 管道: []) # rubocop:disable Metrics/ParameterLists
  solved_index, solved_path = resolve_vector_index(索引(index), 路径)
  num_candidates ||= limit * 10

  vs_options = {
    ' index ' => solved_index,
    'path' => solved_path,
    'queryVector' => 向量,
    'numCandidates' => num_candidates,
    'limit' => limit
  }
  vs_options[' 过滤 '] = 筛选器 if 筛选器

  agg_pipeline = [
    { ' $vectorSearch ' => vs_options },
    { ' $addFields '    => { 'vector_search_score' => { '$meta' => 'vectorSearchScore' } } },
    { '$ 项目'      => { solved_path => 0 } }
  ]
  agg_pipeline.concat(阵列(管道))

  集合.聚合(agg_pipeline).map { |doc| 实例化(doc) }
end

#vector_search_index(name_or_defn, defn = nil) ⇒ 对象

添加向量搜索索引定义。还可以在首次调用model时在model上定义一个只读 vector_search_score字段,该字段将填充 vector_search 返回的document。

例子:

创建 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
# File 'lib/mongoid/search_indexable.rb', line 259

def vector_search_index(name_or_defn, defn = nil)
  名称 = name_or_defn
  名称, defn = nil, 名称 if 名称.is_a?(哈希)

  spec = { 类型: 'vectorSearch', 定义: defn }.点击 { |s| s[:name] = 名称.to_s if 名称 }
  search_index_specs.推动(spec)

  return if 字段.键?('vector_search_score')

  字段 :vector_search_score, 类型: Float
  attr_readonly :vector_search_score
end

# wait_for_search_indexes (names, Interval: 5 ) {|SearchIndexable::Status| ... } ⇒对象

等待创建命名搜索索引。

参数:

  • 名称 ( Array<String> )

    要等待的索引名称列表

  • interval 整数 (默认为: 5

    再次轮询之前等待的秒数(仅在给出进度回调时使用)。

产量:



168
169
170
171
172
173
174
175
176
# File 'lib/mongoid/search_indexable.rb', line 168

def wait_for_search_indexes(名称, interval: 5)
  循环 do
    状态 = 状态.new(get_indexes(名称))
    产量 状态 if block_given?
    中断 if 状态.准备好了吗?

    睡眠 interval
  end
end