模块:Mongoid::SearchIndexable

扩展方式:
ActiveSupport::Concern
包含在:
可组合
定义于:
lib/mongoid/search_indexable.rb

Overview

封装有关管理搜索索引的行为。 仅当连接到Atlas 集群时才支持此功能。

在命名空间下定义

模块: 类方法 类: 状态

实例方法摘要折叠

实例方法详细信息

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

使用自动嵌入,对文本与此document的存储文本字段相似的document执行Atlas Vector Search查询。当前 document 将从结果中排除。

例子:

查找具有类似说明的文章。

article.auto_embed_search(limit: 5, filter: { status: 'published' })

参数:

  • 索引(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) 搜索(默认:false)。

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

    查询时嵌入模型覆盖。

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

    要附加的其他聚合阶段。

返回:

  • ( Array< Mongoid::Document > )

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mongoid/search_indexable.rb', line 123

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

  if text.nil?
    提高 ArgumentError,
          #{resolved_path} document 为零;无法执行自动嵌入搜索
  end

  self_filter = { '_id' => { '$ne' => _id } }
  组合过滤器 = 筛选器 ? { ' $and ' => [ self_filter, 筛选器 ] } : self_filter

  self.class.auto_embed_search(
    text,
    index: 索引(index),
    path: 路径,
    limit: limit,
    num_candidates: num_candidates,
    过滤器: 组合过滤器,
    精确: 精确,
    模型: 模型,
    管道: 管道
  )
end

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

使用此document的存储嵌入作为查询向量,对与此类似的document执行向量搜索。document本身会从结果中排除。

例子:

查找与此类似的文章。

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

参数:

  • 索引(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 属性。



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mongoid/search_indexable.rb', line 79

def vector_search(index: nil, path: nil, limit: 10, num_candidates: nil, 过滤器: nil, 管道: [])
  _index, solved_path = self.class.发送(:resolve_vector_index, 索引(index), 路径)
  query_vector = public_send(solved_path)

  if query_vector.nil?
    提高 ArgumentError,
          #{resolved_path} 在该 document 中为零;无法执行向量搜索}”
  end

  self_filter = { '_id' => { '$ne' => _id } }
  组合过滤器 = 筛选器 ? { ' $and ' => [ self_filter, 筛选器 ] } : self_filter

  self.class.vector_search(
    query_vector,
    index: 索引(index),
    path: 路径,
    limit: limit,
    num_candidates: num_candidates,
    过滤器: 组合过滤器,
    管道: 管道
  )
end